Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
| TokenPayload | |
100.00% |
15 / 15 |
|
100.00% |
15 / 15 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRaw | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSubject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIssuer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAudience | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getJti | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSessionId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIssuedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNotBefore | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExpiration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isExpired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getClaim | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasClaim | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace DevToolbelt\JwtTokenManager; |
| 6 | |
| 7 | use stdClass; |
| 8 | |
| 9 | final class TokenPayload |
| 10 | { |
| 11 | private stdClass $raw; |
| 12 | |
| 13 | public function __construct(stdClass $payload) |
| 14 | { |
| 15 | $this->raw = $payload; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Get the raw payload object. |
| 20 | */ |
| 21 | public function getRaw(): stdClass |
| 22 | { |
| 23 | return $this->raw; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the subject (sub) claim - typically user ID. |
| 28 | */ |
| 29 | public function getSubject(): string |
| 30 | { |
| 31 | return $this->raw->sub; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the issuer (iss) claim. |
| 36 | */ |
| 37 | public function getIssuer(): string |
| 38 | { |
| 39 | return $this->raw->iss; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the audience (aud) claim. |
| 44 | * |
| 45 | * @return array<string> |
| 46 | */ |
| 47 | public function getAudience(): array |
| 48 | { |
| 49 | return is_array($this->raw->aud) ? $this->raw->aud : [$this->raw->aud]; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the JWT ID (jti) claim. |
| 54 | */ |
| 55 | public function getJti(): string |
| 56 | { |
| 57 | return $this->raw->jti; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the session ID (sid) claim if present. |
| 62 | */ |
| 63 | public function getSessionId(): ?string |
| 64 | { |
| 65 | return $this->raw->sid ?? null; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the token type (typ) claim. |
| 70 | */ |
| 71 | public function getType(): string |
| 72 | { |
| 73 | return $this->raw->typ ?? 'access'; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the issued at (iat) timestamp. |
| 78 | */ |
| 79 | public function getIssuedAt(): int |
| 80 | { |
| 81 | return $this->raw->iat; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the not before (nbf) timestamp. |
| 86 | */ |
| 87 | public function getNotBefore(): int |
| 88 | { |
| 89 | return $this->raw->nbf; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the expiration (exp) timestamp. |
| 94 | */ |
| 95 | public function getExpiration(): int |
| 96 | { |
| 97 | return $this->raw->exp; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Check if token is expired. |
| 102 | */ |
| 103 | public function isExpired(): bool |
| 104 | { |
| 105 | return time() > $this->raw->exp; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get a custom claim by name. |
| 110 | */ |
| 111 | public function getClaim(string $name): mixed |
| 112 | { |
| 113 | return $this->raw->{$name} ?? null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check if a claim exists. |
| 118 | */ |
| 119 | public function hasClaim(string $name): bool |
| 120 | { |
| 121 | return isset($this->raw->{$name}); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get all claims as an associative array. |
| 126 | * |
| 127 | * @return array<string, mixed> |
| 128 | */ |
| 129 | public function toArray(): array |
| 130 | { |
| 131 | return (array) $this->raw; |
| 132 | } |
| 133 | } |