@@ -11,11 +11,11 @@ class Utils
1111 * @param string $jwt
1212 * @return array
1313 */
14- public static function parseJwt ($ jwt )
14+ public static function parseJwt ($ jwt ): array
1515 {
1616 $ tokenParts = explode ('. ' , $ jwt );
17- $ header = json_decode (self ::base64url_decode ($ tokenParts [0 ]), true );
18- $ payload = json_decode (self ::base64url_decode ($ tokenParts [1 ]), true );
17+ $ header = json_decode (self ::base64urlDecode ($ tokenParts [0 ]), true );
18+ $ payload = json_decode (self ::base64urlDecode ($ tokenParts [1 ]), true );
1919
2020 return ['header ' => $ header , 'payload ' => $ payload ];
2121 }
@@ -25,13 +25,13 @@ public static function parseJwt($jwt)
2525 * @param array $payload
2626 * @return string
2727 */
28- public static function generateJwt ($ headers , $ payload , $ key )
28+ public static function generateJwt ($ headers , $ payload , $ key ): string
2929 {
30- $ encodedHeaders = self ::base64url_encode (json_encode ($ headers ));
31- $ encodedPayload = self ::base64url_encode (json_encode ($ payload ));
30+ $ encodedHeaders = self ::base64urlEncode (json_encode ($ headers ));
31+ $ encodedPayload = self ::base64urlEncode (json_encode ($ payload ));
3232
3333 $ signature = hash_hmac ('SHA256 ' , "$ encodedHeaders. $ encodedPayload " , $ key , true );
34- $ encodedSignature = self ::base64url_encode ($ signature );
34+ $ encodedSignature = self ::base64urlEncode ($ signature );
3535
3636 return "$ encodedHeaders. $ encodedPayload. $ encodedSignature " ;
3737 }
@@ -41,7 +41,7 @@ public static function generateJwt($headers, $payload, $key)
4141 * @param mixed $timeFn
4242 * @return bool
4343 */
44- public static function isJwtValid ($ jwt , $ timeFn , $ key )
44+ public static function isJwtValid ($ jwt , $ timeFn , $ key ): bool
4545 {
4646 // split the jwt
4747 $ tokenParts = explode ('. ' , $ jwt );
@@ -50,28 +50,29 @@ public static function isJwtValid($jwt, $timeFn, $key)
5050 $ tokenSignature = $ tokenParts [2 ];
5151
5252 // check the expiration time - note this will cause an error if there is no 'exp' claim in the jwt
53- $ expiration = json_decode (self ::base64url_decode ($ payload ))->exp ;
53+ $ expiration = json_decode (self ::base64urlDecode ($ payload ))->exp ;
5454 $ isTokenExpired = $ expiration <= $ timeFn ();
5555
5656 // build a signature based on the header and payload using the secret
5757 $ signature = hash_hmac ('SHA256 ' , $ header .'. ' .$ payload , $ key , true );
58- $ isSignatureValid = self ::base64url_encode ($ signature ) === $ tokenSignature ;
58+ $ isSignatureValid = self ::base64urlEncode ($ signature ) === $ tokenSignature ;
5959
6060 return $ isSignatureValid && ! $ isTokenExpired ;
6161 }
6262
6363 /**
6464 * https://www.php.net/manual/en/function.base64-encode.php#127544
6565 */
66- public static function base64url_encode ($ str ): string
66+ public static function base64urlEncode ($ str ): string
6767 {
6868 return rtrim (strtr (base64_encode ($ str ), '+/ ' , '-_ ' ), '= ' );
6969 }
7070
7171 /**
7272 * https://www.php.net/manual/en/function.base64-encode.php#127544
7373 */
74- public static function base64url_decode ($ data ) {
74+ public static function base64urlDecode ($ data ): string
75+ {
7576 return base64_decode (strtr ($ data , '-_ ' , '+/ ' ), true );
7677 }
7778
@@ -86,7 +87,7 @@ public static function decodeSocketId($socketId): ?object
8687 {
8788 $ socketIdObject = null ;
8889 if ($ socketId ) {
89- $ socketIdJsonString = self ::base64url_decode ($ socketId );
90+ $ socketIdJsonString = self ::base64urlDecode ($ socketId );
9091 if (!$ socketIdJsonString ) {
9192 throw new AblyException ("Base64 decoding failed, " .self ::SOCKET_ID_ERROR );
9293 }
0 commit comments