Skip to content

Commit f4e2361

Browse files
committed
Renamed base64encode and decode util helpers to camelCase, added return types for all helper methods
1 parent baf6820 commit f4e2361

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/Utils.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/AblyBroadcasterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function testPublishPayloadShouldNotIncludeSocketKey()
333333
$socketIdObject->clientId = 'sacOO7';
334334
$payload = [
335335
'foo' => 'bar',
336-
'socket' => Utils::base64url_encode(json_encode($socketIdObject))
336+
'socket' => Utils::base64urlEncode(json_encode($socketIdObject))
337337
];
338338

339339
$broadcaster->broadcast(["channel1", "channel2"], 'testEvent', $payload);

tests/UtilsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public function testDecodeSocketId() {
3838
$originalSocketIdObj = new \stdClass();
3939
$originalSocketIdObj->connectionKey = 'key';
4040
$originalSocketIdObj->clientId = null;
41-
$socketIdObject = Utils::decodeSocketId(Utils::base64url_encode(json_encode($originalSocketIdObj)));
41+
$socketIdObject = Utils::decodeSocketId(Utils::base64urlEncode(json_encode($originalSocketIdObj)));
4242
self::assertEquals('key', $socketIdObject->connectionKey);
4343
self::assertNull($socketIdObject->clientId);
4444

4545
$originalSocketIdObj = new \stdClass();
4646
$originalSocketIdObj->connectionKey = 'key';
4747
$originalSocketIdObj->clientId = 'id';
48-
$socketIdObject = Utils::decodeSocketId(Utils::base64url_encode(json_encode($originalSocketIdObj)));
48+
$socketIdObject = Utils::decodeSocketId(Utils::base64urlEncode(json_encode($originalSocketIdObj)));
4949
self::assertEquals('key', $socketIdObject->connectionKey);
5050
self::assertEquals('id', $socketIdObject->clientId);
5151
}
@@ -64,7 +64,7 @@ public function testExceptionOnMissingClientIdInSocketId()
6464

6565
self::expectException(AblyException::class);
6666
self::expectExceptionMessage("ClientId is missing, ".Utils::SOCKET_ID_ERROR);
67-
Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketIdObject)));
67+
Utils::decodeSocketId(Utils::base64urlEncode(json_encode($socketIdObject)));
6868
}
6969

7070
public function testExceptionOnMissingConnectionKeyInSocketId()
@@ -74,6 +74,6 @@ public function testExceptionOnMissingConnectionKeyInSocketId()
7474

7575
self::expectException(AblyException::class);
7676
self::expectExceptionMessage("ConnectionKey is not set, ".Utils::SOCKET_ID_ERROR);
77-
Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketIdObject)));
77+
Utils::decodeSocketId(Utils::base64urlEncode(json_encode($socketIdObject)));
7878
}
7979
}

0 commit comments

Comments
 (0)