Created
October 23, 2017 06:42
-
-
Save kinlane/00db3d871b615c8b1c43dbc60ae41f86 to your computer and use it in GitHub Desktop.
Github Oauth Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('OAUTH2_CLIENT_ID', '[your client id]'); | |
define('OAUTH2_CLIENT_SECRET', '[your client secret]'); | |
$authorizeURL = 'https://github.com/login/oauth/authorize'; | |
$tokenURL = 'https://github.com/login/oauth/access_token'; | |
$apiURLBase = 'https://api.github.com/'; | |
session_start(); | |
// Start the login process by sending the user to Github's authorization page | |
if(get('action') == 'login') { | |
// Generate a random hash and store in the session for security | |
$_SESSION['state'] = hash('sha256', microtime(TRUE).rand().$_SERVER['REMOTE_ADDR']); | |
unset($_SESSION['access_token']); | |
$params = array( | |
'client_id' => OAUTH2_CLIENT_ID, | |
'redirect_uri' => 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'], | |
'scope' => 'user', | |
'state' => $_SESSION['state'] | |
); | |
// Redirect the user to Github's authorization page | |
header('Location: ' . $authorizeURL . '?' . http_build_query($params)); | |
die(); | |
} | |
// When Github redirects the user back here, there will be a "code" and "state" parameter in the query string | |
if(get('code')) { | |
// Verify the state matches our stored state | |
if(!get('state') || $_SESSION['state'] != get('state')) { | |
header('Location: ' . $_SERVER['PHP_SELF']); | |
die(); | |
} | |
// Exchange the auth code for a token | |
$token = apiRequest($tokenURL, array( | |
'client_id' => OAUTH2_CLIENT_ID, | |
'client_secret' => OAUTH2_CLIENT_SECRET, | |
'redirect_uri' => 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'], | |
'state' => $_SESSION['state'], | |
'code' => get('code') | |
)); | |
$_SESSION['access_token'] = $token->access_token; | |
header('Location: ' . $_SERVER['PHP_SELF']); | |
} | |
if(session('access_token')) | |
{ | |
$user = apiRequest($apiURLBase . 'user'); | |
// We have a valid user. | |
// 1) We need to add or validate against an account | |
// 2) We need to add application for user w/ key | |
// If already have key stored, use that, not new. | |
// 3) If we use AWS API Gateway we need to add key, and to plan. | |
// Send Back to Portal - Update with your URL | |
header('Location: https://github.oauth.apievangelist.com/?login=' . $user->login . '&code=' . session('access_token') . '#Authentication'); | |
} | |
else | |
{ | |
//echo '<h3>Not logged in</h3>'; | |
//echo '<p><a href="?action=login">Log In</a></p>'; | |
} | |
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/json'; | |
$headers[] = 'User-Agent: ANS'; | |
if(session('access_token')) | |
$headers[] = 'Authorization: Bearer ' . session('access_token'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$response = curl_exec($ch); | |
return json_decode($response); | |
} | |
function get($key, $default=NULL) { | |
return array_key_exists($key, $_GET) ? $_GET[$key] : $default; | |
} | |
function session($key, $default=NULL) { | |
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment