| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace Models {
- require_once 'Config/Settings.class.php';
- require_once 'External/PHP-JWT/src/JWT.php';
- class User {
- //
- public $ID;
- public $firstname;
- public $lastname;
- public $email;
- public $type;
- /**
- *
- */
- public function __construct() {
- $this->reset();
- }
- /**
- *
- */
- protected function reset() {
- $this->ID = 0;
- $this->firstname = '';
- $this->lastname = '';
- $this->email = '';
- $this->type = '';
- }
- /**
- *
- */
- public function logout() {
- $this->reset();
- }
- /**
- * Check provided JWT token.
- */
- public function checkToken() {
- $authHeader = $_SERVER['HTTP_AUTHORIZATION'];
- $arr = explode(" ", $authHeader);
- if(count($arr) == 2) {
- $jwt = $arr[1];
- if($jwt) {
- try {
- $decoded = \Firebase\JWT\JWT::decode($jwt, \Config\Settings::getTokenPrivateKey(), array('HS256'));
- $this->ID = $decoded->data->ID;
- $this->firstname = $decoded->data->firstname;
- $this->lastname = $decoded->data->lastname;
- $this->email = $decoded->data->email;
- $this->type = $decoded->data->type;
- // Update token
- $issuedat_claim = time(); // issued at
- $expire_claim = $issuedat_claim + \Config\Settings::getTokenExpiration();
- $decoded->iat = $issuedat_claim;
- $decoded->exp = $expire_claim;
- return \Firebase\JWT\JWT::encode($decoded, \Config\Settings::getTokenPrivateKey());
- }
- catch ( \Firebase\JWT\ExpiredException $e ) {
- throw new \Exception('Access denied.');
- }
- catch (\Exception $e){
- throw new \Exception('Access denied.');
- /*return json_encode(Array(
- "result" => "ERROR",
- "message" => "Access denied",
- "data" => $e->getMessage()
- ));*/
- }
- }
- else {
- throw new \Exception('Access denied.');
- }
- }
- else {
- throw new \Exception('Access denied.');
- }
- return false;
- }
- }
- }
- ?>
|