@@ -36,13 +36,15 @@ class JWT
3636 * When checking nbf, iat or expiration times,
3737 * we want to provide some extra leeway time to
3838 * account for clock skew.
39+ *
40+ * @var int
3941 */
40- public static int $ leeway = 0 ;
42+ public static $ leeway = 0 ;
4143
4244 /**
4345 * @var array<string, string[]>
4446 */
45- public static array $ supported_algs = [
47+ public static $ supported_algs = [
4648 'ES384 ' => ['openssl ' , 'SHA384 ' ],
4749 'ES256 ' => ['openssl ' , 'SHA256 ' ],
4850 'HS256 ' => ['hash_hmac ' , 'SHA256 ' ],
@@ -77,8 +79,10 @@ class JWT
7779 * @uses jsonDecode
7880 * @uses urlsafeB64Decode
7981 */
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 {
8286 // Validate JWT
8387 $ timestamp = \time ();
8488
@@ -90,24 +94,18 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
9094 throw new UnexpectedValueException ('Wrong number of segments ' );
9195 }
9296 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 );
9698 if (null === ($ header = static ::jsonDecode ($ headerRaw ))) {
9799 throw new UnexpectedValueException ('Invalid header encoding ' );
98100 }
99- if (false === ($ payloadRaw = static ::urlsafeB64Decode ($ bodyb64 ))) {
100- throw new UnexpectedValueException ('Invalid claims encoding ' );
101- }
101+ $ payloadRaw = static ::urlsafeB64Decode ($ bodyb64 );
102102 if (null === ($ payload = static ::jsonDecode ($ payloadRaw ))) {
103103 throw new UnexpectedValueException ('Invalid claims encoding ' );
104104 }
105105 if (!$ payload instanceof stdClass) {
106106 throw new UnexpectedValueException ('Payload must be a JSON object ' );
107107 }
108- if (false === ($ sig = static ::urlsafeB64Decode ($ cryptob64 ))) {
109- throw new UnexpectedValueException ('Invalid signature encoding ' );
110- }
108+ $ sig = static ::urlsafeB64Decode ($ cryptob64 );
111109 if (empty ($ header ->alg )) {
112110 throw new UnexpectedValueException ('Empty algorithm ' );
113111 }
@@ -159,7 +157,7 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
159157 * Converts and signs a PHP object or array into a JWT string.
160158 *
161159 * @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.
163161 * @param string $keyId
164162 * @param array<string, string> $head An array with header elements to attach
165163 *
@@ -170,7 +168,7 @@ public static function decode(string $jwt, Key|array|ArrayAccess $keyOrKeyArray)
170168 */
171169 public static function encode (
172170 array $ payload ,
173- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ key ,
171+ $ key ,
174172 string $ alg ,
175173 string $ keyId = null ,
176174 array $ head = null
@@ -197,7 +195,7 @@ public static function encode(
197195 * Sign a string with a given key and algorithm.
198196 *
199197 * @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.
201199 * @param string $alg Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
202200 * 'HS512', 'RS256', 'RS384', and 'RS512'
203201 *
@@ -207,7 +205,7 @@ public static function encode(
207205 */
208206 public static function sign (
209207 string $ msg ,
210- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ key ,
208+ $ key ,
211209 string $ alg
212210 ): string {
213211 if (empty (static ::$ supported_algs [$ alg ])) {
@@ -222,7 +220,7 @@ public static function sign(
222220 return \hash_hmac ($ algorithm , $ msg , $ key , true );
223221 case 'openssl ' :
224222 $ signature = '' ;
225- $ success = \openssl_sign ($ msg , $ signature , $ key , $ algorithm );
223+ $ success = \openssl_sign ($ msg , $ signature , $ key , $ algorithm ); // @phpstan-ignore-line
226224 if (!$ success ) {
227225 throw new DomainException ("OpenSSL unable to sign data " );
228226 }
@@ -258,7 +256,7 @@ public static function sign(
258256 *
259257 * @param string $msg The original message (header and body)
260258 * @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
262260 * @param string $alg The algorithm
263261 *
264262 * @return bool
@@ -268,7 +266,7 @@ public static function sign(
268266 private static function verify (
269267 string $ msg ,
270268 string $ signature ,
271- string | OpenSSLAsymmetricKey | OpenSSLCertificate | array $ keyMaterial ,
269+ $ keyMaterial ,
272270 string $ alg
273271 ): bool {
274272 if (empty (static ::$ supported_algs [$ alg ])) {
@@ -278,7 +276,7 @@ private static function verify(
278276 list ($ function , $ algorithm ) = static ::$ supported_algs [$ alg ];
279277 switch ($ function ) {
280278 case 'openssl ' :
281- $ success = \openssl_verify ($ msg , $ signature , $ keyMaterial , $ algorithm );
279+ $ success = \openssl_verify ($ msg , $ signature , $ keyMaterial , $ algorithm ); // @phpstan-ignore-line
282280 if ($ success === 1 ) {
283281 return true ;
284282 } elseif ($ success === 0 ) {
@@ -322,7 +320,7 @@ private static function verify(
322320 *
323321 * @throws DomainException Provided string was invalid JSON
324322 */
325- public static function jsonDecode (string $ input ): mixed
323+ public static function jsonDecode (string $ input )
326324 {
327325 $ obj = \json_decode ($ input , false , 512 , JSON_BIGINT_AS_STRING );
328326
@@ -339,11 +337,11 @@ public static function jsonDecode(string $input): mixed
339337 *
340338 * @param array<mixed> $input A PHP array
341339 *
342- * @return string|false JSON representation of the PHP array
340+ * @return string JSON representation of the PHP array
343341 *
344342 * @throws DomainException Provided object could not be encoded to valid JSON
345343 */
346- public static function jsonEncode (array $ input ): string | false
344+ public static function jsonEncode (array $ input ): string
347345 {
348346 if (PHP_VERSION_ID >= 50400 ) {
349347 $ json = \json_encode ($ input , \JSON_UNESCAPED_SLASHES );
@@ -356,6 +354,9 @@ public static function jsonEncode(array $input): string|false
356354 } elseif ($ json === 'null ' && $ input !== null ) {
357355 throw new DomainException ('Null result with non-null input ' );
358356 }
357+ if ($ json === false ) {
358+ throw new DomainException ('Provided object could not be encoded to valid JSON ' );
359+ }
359360 return $ json ;
360361 }
361362
@@ -365,8 +366,10 @@ public static function jsonEncode(array $input): string|false
365366 * @param string $input A Base64 encoded string
366367 *
367368 * @return string A decoded string
369+ *
370+ * @throws InvalidArgumentException invalid base64 characters
368371 */
369- public static function urlsafeB64Decode (string $ input ): string | false
372+ public static function urlsafeB64Decode (string $ input ): string
370373 {
371374 $ remainder = \strlen ($ input ) % 4 ;
372375 if ($ remainder ) {
@@ -399,8 +402,10 @@ public static function urlsafeB64Encode(string $input): string
399402 *
400403 * @return Key
401404 */
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 {
404409 if ($ keyOrKeyArray instanceof Key) {
405410 return $ keyOrKeyArray ;
406411 }
0 commit comments