Skip to content
Extraits de code Groupes Projets
Valider 012e49c7 rédigé par Steel's avatar Steel
Parcourir les fichiers

use AIDC

parent fc7166b9
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<?php <?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 // Fill these out with the values you got from Github
$githubClientID = ''; $githubClientID = '6a028017-3100-44c6-a837-9752c3b9b8c4';
$githubClientSecret = ''; $githubClientSecret = 'kazgNEQ26MstoIELYeFLftkTK_';
// This is the URL we'll send the user to first to get their authorization // 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 // 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 // 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 // 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 // Start a session so we have a place to store things between redirects
session_start(); 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 // Start the login process by sending the user
// to Github's authorization page // to Github's authorization page
...@@ -31,7 +37,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { ...@@ -31,7 +37,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') {
'response_type' => 'code', 'response_type' => 'code',
'client_id' => $githubClientID, 'client_id' => $githubClientID,
'redirect_uri' => $baseURL, 'redirect_uri' => $baseURL,
'scope' => 'user public_repo', 'scope' => 'profile',
'state' => $_SESSION['state'] 'state' => $_SESSION['state']
); );
...@@ -40,7 +46,6 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') { ...@@ -40,7 +46,6 @@ if(isset($_GET['action']) && $_GET['action'] == 'login') {
die(); die();
} }
if(isset($_GET['action']) && $_GET['action'] == 'logout') { if(isset($_GET['action']) && $_GET['action'] == 'logout') {
unset($_SESSION['access_token']); unset($_SESSION['access_token']);
header('Location: '.$baseURL); header('Location: '.$baseURL);
...@@ -59,33 +64,40 @@ if(isset($_GET['code'])) { ...@@ -59,33 +64,40 @@ if(isset($_GET['code'])) {
} }
// Exchange the auth code for an access token // Exchange the auth code for an access token
$token = apiRequest($tokenURL, array( $query_data = array(
'grant_type' => 'authorization_code', 'grant_type' => 'authorization_code',
'client_id' => $githubClientID, 'client_id' => $githubClientID,
'client_secret' => $githubClientSecret, 'client_secret' => $githubClientSecret,
'redirect_uri' => $baseURL, 'redirect_uri' => $baseURL,
'code' => $_GET['code'] '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']; $_SESSION['access_token'] = $token['access_token'];
header('Location: ' . $baseURL); header('Location: ' . $baseURL);
die(); die();
} }
var_dump($_SESSION['access_token']);
if(isset($_GET['action']) && $_GET['action'] == 'repos') { if(isset($_GET['action']) && $_GET['action'] == 'profile') {
// Find all repos created by the authenticated user if(!empty($_SESSION['access_token'])) {
$repos = apiRequest($apiURLBase.'user/repos?'.http_build_query([ $query = '{"query":"query { profile { id name givenName familyName nickname birthdate email schoolLogin promotion groups { role group { name } } } }"}';
'sort' => 'created', $profile = apiRequest($query);
'direction' => 'desc' echo '<pre>';
])); json_encode($profile);
echo '</pre>';
echo '<ul>'; } else {
foreach($repos as $repo) { echo '<h3>Not logged in</h3>';
echo '<li><a href="' . $repo['html_url'] . '">' echo '<p><a href="?action=login">Log In</a></p>';
. $repo['name'] . '</a></li>';
} }
echo '</ul>';
} }
// If there is an access token in the session // If there is an access token in the session
...@@ -93,7 +105,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') { ...@@ -93,7 +105,7 @@ if(isset($_GET['action']) && $_GET['action'] == 'repos') {
if(!isset($_GET['action'])) { if(!isset($_GET['action'])) {
if(!empty($_SESSION['access_token'])) { if(!empty($_SESSION['access_token'])) {
echo '<h3>Logged In</h3>'; 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>'; echo '<p><a href="?action=logout">Log Out</a></p>';
} else { } else {
echo '<h3>Not logged in</h3>'; echo '<h3>Not logged in</h3>';
...@@ -105,23 +117,27 @@ if(!isset($_GET['action'])) { ...@@ -105,23 +117,27 @@ if(!isset($_GET['action'])) {
// 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
function apiRequest($url, $post=FALSE, $headers=array()) { function apiRequest($query) {
$ch = curl_init($url); global $apiURLBase;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $endpoint = $apiURLBase . 'graphql/v0';
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers = [ $headers = array();
'Accept: application/vnd.github.v3+json, application/json', $headers[] = 'Content-Type: application/json';
'User-Agent: https://example-app.com/'
];
if(isset($_SESSION['access_token']))
$headers[] = 'Authorization: Bearer '. $_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); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch); $response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
return json_decode($response, true); return json_decode($response, true);
} }
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