You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

<?php
use \Firebase\JWT\JWT;
class AuthenticationToken {
private $auth;
private $token;
private $response;
/*
public function getAuthorizationHeader(){
$headers = null;
if (isset($_SERVER['Authorization'])) {
$headers = trim($_SERVER["Authorization"]);
}
else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
} elseif (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
// Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
//print_r($requestHeaders);
if (isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
}
return $headers;
}*/
public function tokenGet() {
$this->auth = null;
$requestHeaders = apache_request_headers();
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
if(isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
if(!empty($headers)) {
if(preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
$this->auth = $matches[1];
}
}
return $this->auth;
}
public function tokenCheck($secret) {
$this->token = $this->tokenGet();
$this->response = array();
try {
$decoded = JWT::decode($this->token, $secret, array('HS256'));
$this->response = array(
"status" => 200,
"access" => "GRANTED",
"token" => $this->token//$decoded
);
} catch(Exception $e) {
$this->response = array(
"status" => 401,
"access" => "DENIED",
"error" => $e->getMessage()
);
}
return $this->response;
}
}
?>