whallysson /
jwt
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CodeBlog\JWT; |
||
| 4 | |||
| 5 | use CodeBlog\JWT\Helpers; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class CodeBlog JWT |
||
| 9 | * |
||
| 10 | * @author Whallysson Avelino <https://github.com/whallysson> |
||
| 11 | * @package CodeBlog\JWT |
||
| 12 | */ |
||
| 13 | |||
| 14 | class JWT extends JWTAuth |
||
| 15 | { |
||
| 16 | |||
| 17 | // Converte e assina um objeto ou matriz PHP em uma cadeia JWT. |
||
| 18 | /** |
||
| 19 | * @param array $payload |
||
| 20 | * @param string $secret |
||
| 21 | * @param string $hash |
||
| 22 | * |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public function encode(array $payload, string $secret, string $hash = 'HS256'): string |
||
| 26 | { |
||
| 27 | $header = array('typ' => 'JWT', 'alg' => $hash); |
||
| 28 | |||
| 29 | $sing_header = Helpers::encode(json_encode($header)); |
||
| 30 | $sing_payload = Helpers::encode(json_encode($payload)); |
||
| 31 | |||
| 32 | $signature = $this->signature($sing_header, $sing_payload, $secret, $hash); |
||
| 33 | |||
| 34 | return "{$sing_header}.{$sing_payload}.{$signature}"; |
||
| 35 | } |
||
| 36 | |||
| 37 | // Decodifica uma string JWT em um objeto PHP. |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $jwt |
||
| 41 | * @param string $secret |
||
| 42 | * |
||
| 43 | * @return null|\stdClass |
||
| 44 | */ |
||
| 45 | public function decode(string $jwt, string $secret): ?\stdClass |
||
| 46 | { |
||
| 47 | if (empty($secret)) { |
||
| 48 | Helpers::throwError(401, 'A Key não pode ser vazia'); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (self::validate($jwt, $secret)) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 52 | return $this->getPayloadDecodeJson(); |
||
| 53 | } |
||
| 54 | |||
| 55 | return null; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function authHeader(): string |
||
| 62 | { |
||
| 63 | $allHeaders = array_change_key_case(getallheaders(), CASE_LOWER); |
||
| 64 | $authorization = !empty($allHeaders['authorization']) ? $allHeaders['authorization'] : null; |
||
| 65 | |||
| 66 | // Verifica se o Token foi informado |
||
| 67 | if (empty($authorization)) { |
||
| 68 | return Helpers::throwError(401); |
||
|
0 ignored issues
–
show
|
|||
| 69 | } |
||
| 70 | |||
| 71 | $parts = explode(' ', $authorization); |
||
| 72 | |||
| 73 | // Verifica o formato do Token |
||
| 74 | if (count($parts) !== 2) { |
||
| 75 | return Helpers::throwError(401, 'Token error'); |
||
|
0 ignored issues
–
show
|
|||
| 76 | } |
||
| 77 | |||
| 78 | list($scheme, $token) = $parts; |
||
| 79 | |||
| 80 | // Verifica se existe a palavra "Bearer" no Token |
||
| 81 | if (!preg_match('/^Bearer$/i', $scheme)) { |
||
| 82 | return Helpers::throwError(401, 'Token mal formado'); |
||
|
0 ignored issues
–
show
|
|||
| 83 | } |
||
| 84 | |||
| 85 | return $token; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Validate a JSON Web Token's expiration and signature |
||
| 90 | * |
||
| 91 | * @param string $token |
||
| 92 | * @param string $secret |
||
| 93 | * |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function validate(string $token, string $secret): bool |
||
| 97 | { |
||
| 98 | return $this->splitToken($token) |
||
| 99 | ->validateHeader() |
||
| 100 | ->validatePayload() |
||
| 101 | ->validateExpiration() |
||
| 102 | ->validateSignature($secret); |
||
| 103 | } |
||
| 104 | |||
| 105 | } |
||
| 106 |