Created
November 30, 2020 09:09
-
-
Save yigitkerem/2c6a060db7cb2361ecd9613341f5c553 to your computer and use it in GitHub Desktop.
This file is an implementation of oauth for SkyMake4
This file contains hidden or 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 | |
session_name("SkyMakeSessionStorage"); | |
session_start(); | |
include_once "../SkyMakeDatabaseConnector/SkyMakeDBconfig.php"; | |
include_once "../SkyMakeConfiguration.php"; | |
include_once "../SkyMakeFunctionSet/Operation-Requirements/MainFunctions.php"; | |
include "../classes/user.php"; | |
$client_id = ''; | |
$client_secret = ''; | |
$redirect_uri = ''; | |
$idp_uri = ''; | |
if(!isset($_SESSION["oauth_authcode"]) and !isset($_GET["code"])){ | |
header("location: ".$idp_uri."oauth/authorize?client_id=".$client_id."&redirect_uri=".$redirect_uri."&response_type=code"); | |
} | |
if(isset($_GET["code"])){ | |
$_SESSION["oauth_authcode"] = $_GET["code"]; | |
//echo "Requesting Authorization Code"; | |
$url = $idp_uri."oauth/token"; | |
$data = array( | |
'grant_type' => 'authorization_code', | |
'code' => $_SESSION["oauth_authcode"], | |
'client_id' => $client_id, | |
'client_secret' => $client_secret, | |
'redirect_uri' => $redirect_uri | |
); | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data) | |
) | |
); | |
//var_dump($options); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if ($result === FALSE) { | |
session_destroy(); | |
die("Token has expired. Please try again. If authorization takes longr than a few seconds try using a better connection"); | |
} | |
echo(json_decode($result,true)["access_token"]); | |
$_SESSION["oauth_bearer"] = json_decode($result,true)["access_token"]; | |
} | |
if(isset($_SESSION["oauth_bearer"])){ | |
//echo "Requesting Credidentals"; | |
$result = file_get_contents($idp_uri."?oauth=me&access_token=".$_SESSION["oauth_bearer"], false); | |
$username = json_decode($result,true)["user_email"]; | |
$sql = "SELECT id FROM skymake_users WHERE username = ?"; | |
if ($stmt = mysqli_prepare($link, $sql)) { | |
// Bind variables to the prepared statement as parameters | |
mysqli_stmt_bind_param($stmt, "s", $param_username); | |
// Set parameters | |
$param_username = trim($username); | |
// Attempt to execute the prepared statement | |
if (mysqli_stmt_execute($stmt)) { | |
// store result | |
mysqli_stmt_store_result($stmt); | |
if (mysqli_stmt_num_rows($stmt) != 1) { | |
//Register user | |
$param_password = md5(uniqid(rand(), true)); | |
$sql = "INSERT INTO skymake_users (username, password) VALUES (?, ?)"; | |
if ($stmt = mysqli_prepare($link, $sql)) { | |
// Bind variables to the prepared statement as parameters | |
mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password); | |
// Set parameters | |
$param_username = trim($username); | |
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash | |
// Attempt to execute the prepared statement | |
if (mysqli_stmt_execute($stmt)) { | |
// Redirect to login page | |
header("location: /oauth/"); | |
} else { | |
die("Something went wrong. Please try again later.".mysqli_stmt_error($stmt)); | |
} | |
// Close statement | |
mysqli_stmt_close($stmt); | |
} | |
} else { | |
//Login user | |
$sql = "SELECT id, username, password FROM skymake_users WHERE username = ?"; | |
if ($stmt = mysqli_prepare($link, $sql)) { | |
// Bind variables to the prepared statement as parameters | |
mysqli_stmt_bind_param($stmt, "s", $param_username); | |
// Set parameters | |
$param_username = $username; | |
// Attempt to execute the prepared statement | |
if (mysqli_stmt_execute($stmt)) { | |
// Store result | |
mysqli_stmt_store_result($stmt); | |
// Check if username exists, if yes then verify password | |
if (mysqli_stmt_num_rows($stmt) == 1) { | |
// Bind result variables | |
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password); | |
if (mysqli_stmt_fetch($stmt)) { | |
session_start(); | |
//prevent unathorized updates | |
$_SESSION["UPDATE_AUTHORIZED"] = false; | |
// Store data in session variables | |
$_SESSION["loggedin"] = true; | |
$_SESSION["id"] = $id; | |
$_SESSION["username"] = $username; | |
//get assigned class | |
$_SESSION["classid"] = SMUser::getStudentClassID($link,$_SESSION["username"]); | |
$_SESSION["dm"] = "off"; | |
// Redirect user to welcome page | |
// Logged in successfully. | |
header("Location: /home"); | |
} | |
} else { | |
// Display an error message if username doesn't exist | |
$username_err = "No account found with that username."; | |
} | |
} else { | |
// ANY OTHER ERROR - Will need an update in a future build. | |
die("Oops! Something went wrong. Please try again later."); | |
} | |
// Close statement | |
mysqli_stmt_close($stmt); | |
} | |
} | |
} else { | |
die(_("Oops! Something went wrong. Please try again later."). mysqli_stmt_error($stmt)); | |
} | |
// Close statement | |
mysqli_stmt_close($stmt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment