Skip to content

Commit 9914472

Browse files
committed
refactor: Extract JWT::urlsafeToStandardB64 method
1 parent 39d2e18 commit 9914472

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/JWK.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public static function parseKey(array $jwk, string $defaultAlg = null): ?Key
173173
throw new UnexpectedValueException('x not set');
174174
}
175175

176-
$publicKey = \base64_encode(JWT::urlsafeB64Decode($jwk['x']));
176+
$publicKey = JWT::urlsafeToStandardB64($jwk['x']);
177177
$alg = self::OKP_CURVES[$jwk['crv']];
178178
return new Key($publicKey, $alg);
179179
default:

src/JWT.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,26 @@ public static function jsonEncode(array $input): string
385385
* @throws InvalidArgumentException invalid base64 characters
386386
*/
387387
public static function urlsafeB64Decode(string $input): string
388+
{
389+
return \base64_decode(self::urlsafeToStandardB64($input));
390+
}
391+
392+
/**
393+
* Convert a string from URL-safe Base64 to standard Base64.
394+
*
395+
* @param string $input A Base64 encoded string with URL-safe characters (-_ and no padding)
396+
*
397+
* @return string A Base64 encoded string with standard characters (+/) and padding (=), when
398+
* needed.
399+
*/
400+
public static function urlsafeToStandardB64(string $input): string
388401
{
389402
$remainder = \strlen($input) % 4;
390403
if ($remainder) {
391404
$padlen = 4 - $remainder;
392405
$input .= \str_repeat('=', $padlen);
393406
}
394-
return \base64_decode(\strtr($input, '-_', '+/'));
407+
return \strtr($input, '-_', '+/');
395408
}
396409

397410
/**

0 commit comments

Comments
 (0)