diff --git a/github.php b/github.php index 3e50a12f4d54fb725f2ce5b7a21160e68571cbdb..bc6edeebb0a6d8b1340d6decfc9b267a1e3465eb 100644 --- a/github.php +++ b/github.php @@ -13,24 +13,11 @@ $tokenURL = 'https://github.com/login/oauth/access_token'; $apiURLBase = 'https://api.github.com/'; // 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']; // Start a session so we have a place to store things between redirects session_start(); -// If there is an access token in the session, the user is logged in -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(); -} // Start the login process by sending the user to Github's authorization page if(isset($_GET['action']) && $_GET['action'] == 'login') { @@ -40,6 +27,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { $_SESSION['state'] = bin2hex(random_bytes(16)); $params = array( + 'response_type' => 'code', 'client_id' => $githubClientID, 'redirect_uri' => $baseURL, 'scope' => 'user public_repo', @@ -65,7 +53,7 @@ if(isset($_GET['code'])) { die(); } - // Exchange the auth code for a token + // Exchange the auth code for an access token $token = apiRequest($tokenURL, array( 'grant_type' => 'authorization_code', 'client_id' => $githubClientID, @@ -96,6 +84,19 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') { die(); } +// If there is an access token in the session, the user is logged in +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(); +} + // This helper function will make API requests to GitHub, setting // the appropriate headers GitHub expects, and decoding the JSON response @@ -107,7 +108,7 @@ function apiRequest($url, $post=FALSE, $headers=array()) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); $headers = [ - 'Accept: application/vnd.github.v3+json', + 'Accept: application/vnd.github.v3+json, application/json', 'User-Agent: https://example-app.com/' ];