function verify_user($username, $password, $return_token = false) { #Creates an object for the return of the json object. $json_verified = array(); $json_verified['Verified'] = false; #The entire statement is enclosed in a try in case of a PDO exeption. try { #Includes the database connection file in this script require '/var/www/dbconnection/Get_db_connection.php'; #Runs the get_db_connection function in the above file which returns #the PDO connection to the database $db_connection = DbConnection::get_instance()->get_db_connection(); #Creates a prepared statement to select the username and password associated with the account $stmt_handle = $db_connection->prepare('Select UserID, Password, Verified From Biometrix.dbo.LoginTable WHERE Username = :name'); #bands the value of :name in the above statement to the first value #passed in on the commandline $stmt_handle->bindValue(':name', $username, PDO::PARAM_STR); #Executes the prepared statement $stmt_handle->execute(); $pass_correct = false; $userid = 0; #Fetches the first row, if null the username and password were wrong if ($row = $stmt_handle->fetch()) { if ($row[2] == 0) { $json_verified['Verified'] = false; $json_verified['Error'] = "Please verify your email account"; } else { if (password_verify($password, $row[1])) { $pass_correct = true; $json_verified['Verified'] = true; #creates a return token for the user if one was #requested if ($return_token = true) { $userid = intval($row[0]); #$userid = 1; require '/var/www/dbconnection/Sign_jwt.php'; $json_verified['Token'] = JWTSign::sign_token($userid); } } } } } catch (PDOException $except) { echo $except->getMessage(); $db_connection = null; } #Returns the json object for success or failure of login return $json_verified; }
#Binds the username and email to the statement $stmt_handle->bindValue(':name', $username, PDO::PARAM_STR); $stmt_handle->bindValue(':email', $email, PDO::PARAM_STR); #Executes the prepared statement $stmt_handle->execute(); #Creates another prepared statement to retrieve the newly added #user's ID $stmt_handle = $db_connection->prepare('Select UserID From Biometrix.dbo.LoginTable WHERE Username = :name AND Verified = 1'); #Binds and executes the statement $stmt_handle->bindValue(':name', $username, PDO::PARAM_STR); $stmt_handle->execute(); if ($row = $stmt_handle->fetch()) { $userid = $row[0]; } } if ($userid != 0) { require '/var/www/dbconnection/Sign_jwt.php'; $json_verified['Verified'] = true; $json_verified['Google'] = true; $json_verified['Operation'] = "GoogleLogin"; $json_verified['Token'] = JWTSign::sign_token($userid); echo json_encode($json_verified); } else { $json_verified['Error'] = "Unexpected error"; } $db_connection = null; } catch (PDOException $except) { echo $except->getMessage() . "\n"; } catch (InvalidArgumentException $arg_except) { echo $arg_except->getMessage() . "\n"; }
public static function decode_token($token) { return JWT::decode($token, JWTSign::get_key())->userid; }
<?php require '/var/www/dbconnection/Sign_jwt.php'; try { echo JWTSign::decode_token("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyc2VyaWQiOiIxIn0.fYhJXsbcaSfyc-h5l49utWTvD2TpRbffwOCOzyZdfo4"); } catch (Exception $except) { echo "Invalid Token"; } exit; date_default_timezone_set("UTC"); $cur_date = getdate(); echo var_dump($cur_date); exit; require '/var/www/dbconnection/Sign_jwt.php'; $userid = 1; echo var_dump($userid); $token = JWTSign::sign_token($userid); var_dump($token); #echo $token; echo "\n"; echo JWTSign::decode_token($token); echo "\n"; echo JWTSign::decode_token("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiIxIn0.fYhJXsbcaSfyc-h5l49utWTvD2TpRbffwOCOzyZdfo4");
case "Delete": $userid = 0; require '/var/www/dbconnection/Sign_jwt.php'; try { $userid = JWTSign::decode_token($http_post["Token"]); $params = json_decode($http_post["Params"], true); $table = $http_post["Table"]; require '/var/www/dboperations/delete_values.php'; $return_json = delete_values($userid, $params, $table); $return_json["Operation"] = $operation; echo json_encode($return_json); } catch (Exception $except) { echo "Invalid Token. Try logging out and in again."; } break; case "Sync": $userid = 0; require '/var/www/dbconnection/Sign_jwt.php'; try { $userid = JWTSign::decode_token($http_post["Token"]); $params = json_decode($http_post["Params"], true); $table = $http_post["Table"]; require '/var/www/dboperations/sync.php'; } catch (Exception $except) { echo "Invalid Token. Try logging out and in again."; } break; default: echo "Welcome to Biometrix!\nEither your chosen operation is not setup,\nor you are accessing this page directly from the web"; break; }