@@ -36,13 +36,15 @@ class JWT
36
36
* When checking nbf, iat or expiration times,
37
37
* we want to provide some extra leeway time to
38
38
* account for clock skew.
39
+ *
40
+ * @var int
39
41
*/
40
- public static int $ leeway = 0 ;
42
+ public static $ leeway = 0 ;
41
43
42
44
/**
43
45
* @var array<string, string[]>
44
46
*/
45
- public static array $ supported_algs = [
47
+ public static $ supported_algs = [
46
48
'ES384 ' => ['openssl ' , 'SHA384 ' ],
47
49
'ES256 ' => ['openssl ' , 'SHA256 ' ],
48
50
'HS256 ' => ['hash_hmac ' , 'SHA256 ' ],
@@ -77,8 +79,10 @@ class JWT
77
79
* @uses jsonDecode
78
80
* @uses urlsafeB64Decode
79
81
*/
80
- public static function decode (string $ jwt , Key |array |ArrayAccess $ keyOrKeyArray ): stdClass
81
- {
82
+ public static function decode (
83
+ string $ jwt ,
84
+ $ keyOrKeyArray
85
+ ): stdClass {
82
86
// Validate JWT
83
87
$ timestamp = \time ();
84
88
@@ -90,24 +94,18 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
90
94
throw new UnexpectedValueException ('Wrong number of segments ' );
91
95
}
92
96
list ($ headb64 , $ bodyb64 , $ cryptob64 ) = $ tks ;
93
- if (false === ($ headerRaw = static ::urlsafeB64Decode ($ headb64 ))) {
94
- throw new UnexpectedValueException ('Invalid header encoding ' );
95
- }
97
+ $ headerRaw = static ::urlsafeB64Decode ($ headb64 );
96
98
if (null === ($ header = static ::jsonDecode ($ headerRaw ))) {
97
99
throw new UnexpectedValueException ('Invalid header encoding ' );
98
100
}
99
- if (false === ($ payloadRaw = static ::urlsafeB64Decode ($ bodyb64 ))) {
100
- throw new UnexpectedValueException ('Invalid claims encoding ' );
101
- }
101
+ $ payloadRaw = static ::urlsafeB64Decode ($ bodyb64 );
102
102
if (null === ($ payload = static ::jsonDecode ($ payloadRaw ))) {
103
103
throw new UnexpectedValueException ('Invalid claims encoding ' );
104
104
}
105
105
if (!$ payload instanceof stdClass) {
106
106
throw new UnexpectedValueException ('Payload must be a JSON object ' );
107
107
}
108
- if (false === ($ sig = static ::urlsafeB64Decode ($ cryptob64 ))) {
109
- throw new UnexpectedValueException ('Invalid signature encoding ' );
110
- }
108
+ $ sig = static ::urlsafeB64Decode ($ cryptob64 );
111
109
if (empty ($ header ->alg )) {
112
110
throw new UnexpectedValueException ('Empty algorithm ' );
113
111
}
@@ -159,7 +157,7 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
159
157
* Converts and signs a PHP object or array into a JWT string.
160
158
*
161
159
* @param array<mixed> $payload PHP array
162
- * @param string|OpenSSLAsymmetricKey|OpenSSLCertificate|array<mixed> $key The secret key.
160
+ * @param string|resource| OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
163
161
* @param string $keyId
164
162
* @param array<string, string> $head An array with header elements to attach
165
163
*
@@ -170,7 +168,7 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
170
168
*/
171
169
public static function encode (
172
170
array $ payload ,
173
- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ key ,
171
+ $ key ,
174
172
string $ alg ,
175
173
string $ keyId = null ,
176
174
array $ head = null
@@ -197,7 +195,7 @@ public static function encode(
197
195
* Sign a string with a given key and algorithm.
198
196
*
199
197
* @param string $msg The message to sign
200
- * @param string|OpenSSLAsymmetricKey|OpenSSLCertificate|array<mixed> $key The secret key.
198
+ * @param string|resource| OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
201
199
* @param string $alg Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
202
200
* 'HS512', 'RS256', 'RS384', and 'RS512'
203
201
*
@@ -207,7 +205,7 @@ public static function encode(
207
205
*/
208
206
public static function sign (
209
207
string $ msg ,
210
- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ key ,
208
+ $ key ,
211
209
string $ alg
212
210
): string {
213
211
if (empty (static ::$ supported_algs [$ alg ])) {
@@ -222,7 +220,7 @@ public static function sign(
222
220
return \hash_hmac ($ algorithm , $ msg , $ key , true );
223
221
case 'openssl ' :
224
222
$ signature = '' ;
225
- $ success = \openssl_sign ($ msg , $ signature , $ key , $ algorithm );
223
+ $ success = \openssl_sign ($ msg , $ signature , $ key , $ algorithm ); // @phpstan-ignore-line
226
224
if (!$ success ) {
227
225
throw new DomainException ("OpenSSL unable to sign data " );
228
226
}
@@ -258,7 +256,7 @@ public static function sign(
258
256
*
259
257
* @param string $msg The original message (header and body)
260
258
* @param string $signature The original signature
261
- * @param string|OpenSSLAsymmetricKey|OpenSSLCertificate|array<mixed> $keyMaterial For HS*, a string key works. for RS*, must be an instance of OpenSSLAsymmetricKey
259
+ * @param string|resource| OpenSSLAsymmetricKey|OpenSSLCertificate $keyMaterial For HS*, a string key works. for RS*, must be an instance of OpenSSLAsymmetricKey
262
260
* @param string $alg The algorithm
263
261
*
264
262
* @return bool
@@ -268,7 +266,7 @@ public static function sign(
268
266
private static function verify (
269
267
string $ msg ,
270
268
string $ signature ,
271
- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ keyMaterial ,
269
+ $ keyMaterial ,
272
270
string $ alg
273
271
): bool {
274
272
if (empty (static ::$ supported_algs [$ alg ])) {
@@ -278,7 +276,7 @@ private static function verify(
278
276
list ($ function , $ algorithm ) = static ::$ supported_algs [$ alg ];
279
277
switch ($ function ) {
280
278
case 'openssl ' :
281
- $ success = \openssl_verify ($ msg , $ signature , $ keyMaterial , $ algorithm );
279
+ $ success = \openssl_verify ($ msg , $ signature , $ keyMaterial , $ algorithm ); // @phpstan-ignore-line
282
280
if ($ success === 1 ) {
283
281
return true ;
284
282
} elseif ($ success === 0 ) {
@@ -322,7 +320,7 @@ private static function verify(
322
320
*
323
321
* @throws DomainException Provided string was invalid JSON
324
322
*/
325
- public static function jsonDecode (string $ input ): mixed
323
+ public static function jsonDecode (string $ input )
326
324
{
327
325
$ obj = \json_decode ($ input , false , 512 , JSON_BIGINT_AS_STRING );
328
326
@@ -339,11 +337,11 @@ public static function jsonDecode(string $input): mixed
339
337
*
340
338
* @param array<mixed> $input A PHP array
341
339
*
342
- * @return string|false JSON representation of the PHP array
340
+ * @return string JSON representation of the PHP array
343
341
*
344
342
* @throws DomainException Provided object could not be encoded to valid JSON
345
343
*/
346
- public static function jsonEncode (array $ input ): string | false
344
+ public static function jsonEncode (array $ input ): string
347
345
{
348
346
if (PHP_VERSION_ID >= 50400 ) {
349
347
$ json = \json_encode ($ input , \JSON_UNESCAPED_SLASHES );
@@ -356,6 +354,9 @@ public static function jsonEncode(array $input): string|false
356
354
} elseif ($ json === 'null ' && $ input !== null ) {
357
355
throw new DomainException ('Null result with non-null input ' );
358
356
}
357
+ if ($ json === false ) {
358
+ throw new DomainException ('Provided object could not be encoded to valid JSON ' );
359
+ }
359
360
return $ json ;
360
361
}
361
362
@@ -365,8 +366,10 @@ public static function jsonEncode(array $input): string|false
365
366
* @param string $input A Base64 encoded string
366
367
*
367
368
* @return string A decoded string
369
+ *
370
+ * @throws InvalidArgumentException invalid base64 characters
368
371
*/
369
- public static function urlsafeB64Decode (string $ input ): string | false
372
+ public static function urlsafeB64Decode (string $ input ): string
370
373
{
371
374
$ remainder = \strlen ($ input ) % 4 ;
372
375
if ($ remainder ) {
@@ -399,8 +402,10 @@ public static function urlsafeB64Encode(string $input): string
399
402
*
400
403
* @return Key
401
404
*/
402
- private static function getKey (Key |array |ArrayAccess $ keyOrKeyArray , ?string $ kid ): Key
403
- {
405
+ private static function getKey (
406
+ $ keyOrKeyArray ,
407
+ ?string $ kid
408
+ ): Key {
404
409
if ($ keyOrKeyArray instanceof Key) {
405
410
return $ keyOrKeyArray ;
406
411
}
0 commit comments