Skip to content
Extraits de code Groupes Projets
Valider 5cca01da rédigé par Aaron Parecki's avatar Aaron Parecki
Parcourir les fichiers

updates for sample github app

parent c89f1e6b
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -13,24 +13,11 @@ $tokenURL = 'https://github.com/login/oauth/access_token'; ...@@ -13,24 +13,11 @@ $tokenURL = 'https://github.com/login/oauth/access_token';
$apiURLBase = 'https://api.github.com/'; $apiURLBase = 'https://api.github.com/';
// The URL for this script, used as the redirect URL // The URL for this script, used as the redirect URL
$baseURL = 'https://' . $_SERVER['SERVER_NAME'] $baseURL = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
. $_SERVER['PHP_SELF'];
// Start a session so we have a place to store things between redirects // Start a session so we have a place to store things between redirects
session_start(); 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 // Start the login process by sending the user to Github's authorization page
if(isset($_GET['action']) && $_GET['action'] == 'login') { if(isset($_GET['action']) && $_GET['action'] == 'login') {
...@@ -40,6 +27,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { ...@@ -40,6 +27,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') {
$_SESSION['state'] = bin2hex(random_bytes(16)); $_SESSION['state'] = bin2hex(random_bytes(16));
$params = array( $params = array(
'response_type' => 'code',
'client_id' => $githubClientID, 'client_id' => $githubClientID,
'redirect_uri' => $baseURL, 'redirect_uri' => $baseURL,
'scope' => 'user public_repo', 'scope' => 'user public_repo',
...@@ -65,7 +53,7 @@ if(isset($_GET['code'])) { ...@@ -65,7 +53,7 @@ if(isset($_GET['code'])) {
die(); die();
} }
// Exchange the auth code for a token // Exchange the auth code for an access token
$token = apiRequest($tokenURL, array( $token = apiRequest($tokenURL, array(
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
'client_id' => $githubClientID, 'client_id' => $githubClientID,
...@@ -96,6 +84,19 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') { ...@@ -96,6 +84,19 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') {
die(); 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 // This helper function will make API requests to GitHub, setting
// the appropriate headers GitHub expects, and decoding the JSON response // the appropriate headers GitHub expects, and decoding the JSON response
...@@ -107,7 +108,7 @@ function apiRequest($url, $post=FALSE, $headers=array()) { ...@@ -107,7 +108,7 @@ function apiRequest($url, $post=FALSE, $headers=array()) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers = [ $headers = [
'Accept: application/vnd.github.v3+json', 'Accept: application/vnd.github.v3+json, application/json',
'User-Agent: https://example-app.com/' 'User-Agent: https://example-app.com/'
]; ];
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter