-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWT.php
More file actions
194 lines (161 loc) · 6.94 KB
/
JWT.php
File metadata and controls
194 lines (161 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace PhpJWT\Token;
use PhpJWT\Exceptions\JWTException;
use PhpJWT\Exceptions\TokenExpiredException;
use PhpJWT\Exceptions\TokenInvalidException;
/**
* Core JWT implementation.
* Supports HS256, HS384, HS512, RS256, RS384, RS512, ES256, ES384, ES512.
*/
class JWT
{
private const ALGORITHMS = [
'HS256' => ['family' => 'hmac', 'algo' => 'sha256'],
'HS384' => ['family' => 'hmac', 'algo' => 'sha384'],
'HS512' => ['family' => 'hmac', 'algo' => 'sha512'],
'RS256' => ['family' => 'rsa', 'algo' => OPENSSL_ALGO_SHA256],
'RS384' => ['family' => 'rsa', 'algo' => OPENSSL_ALGO_SHA384],
'RS512' => ['family' => 'rsa', 'algo' => OPENSSL_ALGO_SHA512],
'ES256' => ['family' => 'ec', 'algo' => OPENSSL_ALGO_SHA256],
'ES384' => ['family' => 'ec', 'algo' => OPENSSL_ALGO_SHA384],
'ES512' => ['family' => 'ec', 'algo' => OPENSSL_ALGO_SHA512],
];
private string $algorithm;
private int $leeway; // seconds of clock skew tolerance
public function __construct(string $algorithm = 'HS256', int $leeway = 0)
{
if (!isset(self::ALGORITHMS[$algorithm])) {
throw new JWTException("Unsupported algorithm: {$algorithm}");
}
$this->algorithm = $algorithm;
$this->leeway = $leeway;
}
/**
* Encode a payload into a signed JWT string.
*/
public function encode(array $payload, string $key): string
{
$header = $this->base64UrlEncode(json_encode([
'typ' => 'JWT',
'alg' => $this->algorithm,
]));
$body = $this->base64UrlEncode(json_encode($payload));
$signingInput = "{$header}.{$body}";
$signature = $this->sign($signingInput, $key);
return "{$signingInput}.{$this->base64UrlEncode($signature)}";
}
/**
* Decode and validate a JWT string, returning the payload.
*/
public function decode(string $token, string $key): array
{
$parts = explode('.', $token);
if (count($parts) !== 3) {
throw new TokenInvalidException('JWT must have exactly 3 segments.');
}
[$headerB64, $payloadB64, $signatureB64] = $parts;
$header = json_decode($this->base64UrlDecode($headerB64), true);
if (!$header || !isset($header['alg'])) {
throw new TokenInvalidException('Invalid JWT header.');
}
if ($header['alg'] !== $this->algorithm) {
throw new TokenInvalidException(
"Algorithm mismatch: expected {$this->algorithm}, got {$header['alg']}."
);
}
$payload = json_decode($this->base64UrlDecode($payloadB64), true);
if (!is_array($payload)) {
throw new TokenInvalidException('Invalid JWT payload.');
}
$signature = $this->base64UrlDecode($signatureB64);
$signingInput = "{$headerB64}.{$payloadB64}";
if (!$this->verify($signingInput, $signature, $key)) {
throw new TokenInvalidException('JWT signature verification failed.');
}
$now = time();
// Check expiry
if (isset($payload['exp']) && ($payload['exp'] + $this->leeway) < $now) {
throw new TokenExpiredException('JWT has expired.', $payload);
}
// Check not-before
if (isset($payload['nbf']) && ($payload['nbf'] - $this->leeway) > $now) {
throw new TokenInvalidException('JWT is not yet valid (nbf).');
}
// Check issued-at sanity
if (isset($payload['iat']) && ($payload['iat'] - $this->leeway) > $now) {
throw new TokenInvalidException('JWT issued-at is in the future (iat).');
}
return $payload;
}
// ──────────────────────────────────────────────
// Signing & verification
// ──────────────────────────────────────────────
private function sign(string $data, string $key): string
{
$meta = self::ALGORITHMS[$this->algorithm];
return match ($meta['family']) {
'hmac' => hash_hmac($meta['algo'], $data, $key, true),
'rsa' => $this->signRsa($data, $key, $meta['algo']),
'ec' => $this->signEc($data, $key, $meta['algo']),
default => throw new JWTException("Unknown algorithm family: {$meta['family']}"),
};
}
private function verify(string $data, string $signature, string $key): bool
{
$meta = self::ALGORITHMS[$this->algorithm];
return match ($meta['family']) {
'hmac' => hash_equals(hash_hmac($meta['algo'], $data, $key, true), $signature),
'rsa' => $this->verifyRsa($data, $signature, $key, $meta['algo']),
'ec' => $this->verifyEc($data, $signature, $key, $meta['algo']),
default => throw new JWTException("Unknown algorithm family: {$meta['family']}"),
};
}
private function signRsa(string $data, string $privateKey, int $algo): string
{
$key = openssl_pkey_get_private($privateKey);
if (!$key) {
throw new JWTException('Invalid RSA private key.');
}
$success = openssl_sign($data, $signature, $key, $algo);
if (!$success) {
throw new JWTException('RSA signing failed.');
}
return $signature;
}
private function verifyRsa(string $data, string $signature, string $publicKey, int $algo): bool
{
$key = openssl_pkey_get_public($publicKey);
if (!$key) {
throw new JWTException('Invalid RSA public key.');
}
return openssl_verify($data, $signature, $key, $algo) === 1;
}
private function signEc(string $data, string $privateKey, int $algo): string
{
return $this->signRsa($data, $privateKey, $algo); // OpenSSL handles EC the same way
}
private function verifyEc(string $data, string $signature, string $publicKey, int $algo): bool
{
return $this->verifyRsa($data, $signature, $publicKey, $algo);
}
// ──────────────────────────────────────────────
// Helpers
// ──────────────────────────────────────────────
public function base64UrlEncode(string $data): string
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
public function base64UrlDecode(string $data): string
{
$remainder = strlen($data) % 4;
if ($remainder) {
$data .= str_repeat('=', 4 - $remainder);
}
$decoded = base64_decode(strtr($data, '-_', '+/'), true);
if ($decoded === false) {
throw new TokenInvalidException('Base64 decoding failed.');
}
return $decoded;
}
}