Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust credential storage to use base64url instead of plain base64 #81

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/BinaryString.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function __construct(
) {
}

/**
* This accepts strings encoded in either standard base64 or base64url,
* since it converts the latter to the former while decoding.
*/
public static function fromBase64Url(string $base64Url): BinaryString
{
$base64 = strtr($base64Url, ['-' => '+', '_' => '/']);
Expand Down
13 changes: 5 additions & 8 deletions src/Codecs/Credential.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* an opaque string without inspection or modification. This library makes
* the following promises:
*
* - The opaque strings will not be outside of the base64 character range
* (A-Za-z0-9/+=)
* - The opaque strings will not be outside of the base64(+url) character range
* (A-Za-z0-9-_/+=)
* - The opaque strings are versioned, and if a new version is introduced,
* there will be an upgrade/conversion path
* - The opaque strings will not exceed 64KiB (65535 bytes)
Expand All @@ -46,7 +46,7 @@
* Format spec:
*
* A CredentialObj shall be encoded to a string.
* That string shall be a base64-encoded representation of:
* That string shall be a base64url-encoded representation of:
*
* [ version ] [ version-specific data ]
*
Expand Down Expand Up @@ -220,7 +220,7 @@ private function encodeV2(CredentialInterface $credential): string

$binary = pack(self::PACK_UINT8, $version) . $versionSpecificFormat;

return base64_encode($binary);
return (new BinaryString($binary))->toBase64Url();
}

/**
Expand Down Expand Up @@ -253,10 +253,7 @@ private static function parseTransportFlags(int $flags): array

public function decode(string $encoded): CredentialInterface
{
$binary = base64_decode($encoded, true);
assert($binary !== false);

$bytes = new BinaryString($binary);
$bytes = BinaryString::fromBase64Url($encoded);

$version = $bytes->readUint8();
assert(($version & 0x80) === 0, 'High bit in version must not be set');
Expand Down
32 changes: 32 additions & 0 deletions tests/Codecs/CredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public static function v2Credentials(): array
'v+hUUYF9qszFOeJYoCfBEY2BoiJYIBuUBOTsbnswM3PD9Lj61GTyVQBalOm2' .
'8aW5GWVNe7kOMQ==',
],
'no att cert base64url' => [
'AgsACv69Y58M4y3CsWkAADEXAAAATaUBAgMmIAEhWCBOknC_s6jMNgiYeThI' .
'v-hUUYF9qszFOeJYoCfBEY2BoiJYIBuUBOTsbnswM3PD9Lj61GTyVQBalOm2' .
'8aW5GWVNe7kOMQ',
],
'saved att cert' => [
'AhgACr/Sj9YstWchvM4AADBlAAAATaUBAgMmIAEhWCBOknC/s6jMNgiYeThI' .
'v+hUUYF9qszFOeJYoCfBEY2BoiJYIBuUBOTsbnswM3PD9Lj61GTyVQBalOm2' .
Expand Down Expand Up @@ -173,6 +178,33 @@ public static function v2Credentials(): array
'N0J5YmFvQSIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6Nzc3NyIsInR5' .
'cGUiOiJ3ZWJhdXRobi5jcmVhdGUifQ=='
],
'saved att cert base64url' => [
'AhgACr_Sj9YstWchvM4AADBlAAAATaUBAgMmIAEhWCBOknC_s6jMNgiYeThI' .
'v-hUUYF9qszFOeJYoCfBEY2BoiJYIBuUBOTsbnswM3PD9Lj61GTyVQBalOm2' .
'8aW5GWVNe7kODAAAA20AAAB1o2NmbXRoZmlkby11MmZnYXR0U3RtdKJjeDVj' .
'gVkCMTCCAi0wggEXoAMCAQICBAW2BXkwCwYJKoZIhvcNAQELMC4xLDAqBgNV' .
'BAMTI1l1YmljbyBVMkYgUm9vdCBDQSBTZXJpYWwgNDU3MjAwNjMxMCAXDTE0' .
'MDgwMTAwMDAwMFoYDzIwNTAwOTA0MDAwMDAwWjAoMSYwJAYDVQQDDB1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