diff --git a/src/http/GemToken.php b/src/http/GemToken.php index 1b8acb4..008674e 100644 --- a/src/http/GemToken.php +++ b/src/http/GemToken.php @@ -4,6 +4,13 @@ use Firebase\JWT\JWT; use Firebase\JWT\Key; +/** + * @public function setToken(string $token):void + * @function create(int $userId, int $timeToLiveSecond): string + * @function verify(): bool + * @function renew(int $extensionTime_sec): false|string + * @function GetType():string|null + */ class GemToken { public int $exp; @@ -20,6 +27,7 @@ class GemToken public ?string $userAgent; public ?string $ip; private string $_secret; + private ?string $_token; public function __construct(string $secret , string $issuer = null) { @@ -36,6 +44,15 @@ public function __construct(string $secret , string $issuer = null) $this->_secret = $secret; } + /** + * @param string $token + * @return void + */ + public function setToken(string $token):void + { + $this->_token = $token; + } + /** * @param int $userId @@ -63,14 +80,20 @@ public function create(int $userId, int $timeToLiveSecond): string } return JWT::encode($payloadArray, $this->_generate_key(), 'HS256'); } + /** - * @param string $token + * @return bool * @description pure token without Bearer you can use WebHelper::BearerTokenPurify() got get pure token */ - public function verify(string $token): bool + public function verify(): bool { + if(!$this->_token) + { + $this->error = 'please set token first with setToken(string $token)'; + return false; + } try { - $decodedToken = JWT::decode($token, new Key($this->_generate_key(), 'HS256')); + $decodedToken = JWT::decode($this->_token, new Key($this->_generate_key(), 'HS256')); if (isset($decodedToken->user_id) && $decodedToken->exp > time() && $decodedToken->user_id>0) { $this->token_id = $decodedToken->token_id; $this->user_id = (int)$decodedToken->user_id; @@ -97,22 +120,30 @@ public function verify(string $token): bool return false; } - public function renew(string $token, int $extensionTime_sec): false|string + /** + * @param int $extensionTime_sec + * @return false|string + */ + public function renew(int $extensionTime_sec): false|string { - if ($this->verify($token)) { + if ($this->verify()) { return $this->create($this->user_id, $extensionTime_sec); } return false; } /** - * @param string $token * @return string|null * @description Returns type without validation token */ - public function GetType(string $token):string|null + public function GetType():string|null { - $tokenParts = explode('.', $token); + if(!$this->_token) + { + $this->error = 'please set token first with setToken(string $token)'; + return null; + } + $tokenParts = explode('.', $this->_token); // The payload is the second part of the token $payloadBase64 = $tokenParts[1];