oauth/index.php
2024-07-08 12:10:05 +03:00

59 lines
1.7 KiB
PHP

function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers = [
'Accept: application/vnd.github.v3+json, application/json',
'User-Agent: http://example-app.com/'
];
if(isset($_SESSION['access_token']))
$headers[] = 'Authorization: Bearer '.$SESSION['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return json_decode($response, true);
}
$githubClientID = 'Ov23li271OczQKR6ISyL'
$githubClientSecret = '9df1c6e9fa98690dbcc9ed12676dc2adcc1cf8cf';
$authorizeURL = 'https://github.com/login/oauth/authorize';
$tokenURL = 'https://github.com/login/oauth/access_token';
$apiURLBase = 'https://api.github.com/';
$baseURL = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
session_start();
if(!isset($_GET['action'])) {
if(!empty($_SESSION['access_token'])) {
echo '<h3> Logged In </h3>'
echo '<p><a href="?action=repos">View Repos</a></p>'
echo '<p><a href="?action=logout">Log Out</a></p>'
} else {
echo '<h3> Not Logged In </h3>'
echo '<p><a href="?action=login">Log In</a></p>'
}
die();
}
if(!isset($_GET['action']) && $_GET['action'] == 'login') {
unset($_SESSION['access_token']);
$_SESSION['state'] = bin2hex(random_bytes(16));
$params = array(
'response_type' => 'code',
'client_id' => '$githubClientID',
'redirect_uri' => '$baseURL',
'scope' => 'user_public_repo',
'state' => '$_SESSION['state']'
);
header('Location: '.$authorizeURL.'?'.http_build_query($params));
die();
}