diff --git a/github.php b/github.php index 89bd60d2e21fbc017295a0f1dbe194951e86938f..b248f8cee6cba8c8ee59dfa4f477fe477d95141a 100644 --- a/github.php +++ b/github.php @@ -1,23 +1,29 @@ <?php +error_reporting(E_ALL); +ini_set('display_errors', '1'); +if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb')); + // Fill these out with the values you got from Github -$githubClientID = ''; -$githubClientSecret = ''; +$githubClientID = '6a028017-3100-44c6-a837-9752c3b9b8c4'; +$githubClientSecret = 'kazgNEQ26MstoIELYeFLftkTK_'; // This is the URL we'll send the user to first to get their authorization -$authorizeURL = 'https://github.com/login/oauth/authorize'; +$authorizeURL = 'https://oidc.iiens.net/oauth2/auth'; // This is the endpoint our server will request an access token from -$tokenURL = 'https://github.com/login/oauth/access_token'; +$tokenURL = 'https://oidc.iiens.net/oauth2/token'; // This is the Github base URL we can use to make authenticated API requests -$apiURLBase = 'https://api.github.com/'; +$apiURLBase = 'https://api.iiens.net/'; // The URL for this script, used as the redirect URL -$baseURL = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; +// $baseURL = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; +$baseURL = 'http://localhost:8000/'; // Start a session so we have a place to store things between redirects session_start(); +echo '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" >'; // Start the login process by sending the user // to Github's authorization page @@ -31,7 +37,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { 'response_type' => 'code', 'client_id' => $githubClientID, 'redirect_uri' => $baseURL, - 'scope' => 'user public_repo', + 'scope' => 'profile', 'state' => $_SESSION['state'] ); @@ -40,7 +46,6 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { die(); } - if(isset($_GET['action']) && $_GET['action'] == 'logout') { unset($_SESSION['access_token']); header('Location: '.$baseURL); @@ -59,33 +64,40 @@ if(isset($_GET['code'])) { } // Exchange the auth code for an access token - $token = apiRequest($tokenURL, array( + $query_data = array( 'grant_type' => 'authorization_code', 'client_id' => $githubClientID, 'client_secret' => $githubClientSecret, 'redirect_uri' => $baseURL, 'code' => $_GET['code'] - )); + ); + + $ch = curl_init($tokenURL); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query_data)); + $response = curl_exec($ch); + + $token = json_decode($response, true); + $_SESSION['access_token'] = $token['access_token']; header('Location: ' . $baseURL); die(); } +var_dump($_SESSION['access_token']); -if(isset($_GET['action']) && $_GET['action'] == 'repos') { - // Find all repos created by the authenticated user - $repos = apiRequest($apiURLBase.'user/repos?'.http_build_query([ - 'sort' => 'created', - 'direction' => 'desc' - ])); - - echo '<ul>'; - foreach($repos as $repo) { - echo '<li><a href="' . $repo['html_url'] . '">' - . $repo['name'] . '</a></li>'; +if(isset($_GET['action']) && $_GET['action'] == 'profile') { + if(!empty($_SESSION['access_token'])) { + $query = '{"query":"query { profile { id name givenName familyName nickname birthdate email schoolLogin promotion groups { role group { name } } } }"}'; + $profile = apiRequest($query); + echo '<pre>'; + json_encode($profile); + echo '</pre>'; + } else { + echo '<h3>Not logged in</h3>'; + echo '<p><a href="?action=login">Log In</a></p>'; } - echo '</ul>'; } // If there is an access token in the session @@ -93,7 +105,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') { 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=profile">View Profile</a></p>'; echo '<p><a href="?action=logout">Log Out</a></p>'; } else { echo '<h3>Not logged in</h3>'; @@ -105,23 +117,27 @@ if(!isset($_GET['action'])) { // This helper function will make API requests to GitHub, setting // the appropriate headers GitHub expects, and decoding the JSON response -function apiRequest($url, $post=FALSE, $headers=array()) { - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); +function apiRequest($query) { + global $apiURLBase; + $endpoint = $apiURLBase . 'graphql/v0'; - if($post) - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); + $headers = array(); + $headers[] = 'Content-Type: application/json'; + $headers[] = 'Authorization: Bearer '. $_SESSION['access_token']; - $headers = [ - 'Accept: application/vnd.github.v3+json, application/json', - 'User-Agent: https://example-app.com/' - ]; - - if(isset($_SESSION['access_token'])) - $headers[] = 'Authorization: Bearer ' . $_SESSION['access_token']; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $endpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); + + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + return json_decode($response, true); }