Skip to content

Commit

Permalink
[TASK] Add base64url encode/decode functionality
Browse files Browse the repository at this point in the history
Add static library functions that allow using base64url-compliant
values (according to https://tools.ietf.org/html/rfc4648#section-5).

The difference to classic base64 is, that the result alphabet is
adjusted like shown below, padding (`=`) is stripped completely:

 + position #62: `+` -> `-` (minus)
 + position #63: `/` -> `_` (underscore)

Resolves: #92509
Releases: main, 11.5
Change-Id: I8804824f3a7fb4e08e18ef6dee6826744dbd41bc
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75868
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
  • Loading branch information
ohader committed Sep 26, 2022
1 parent ac1703a commit 04b04f4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
36 changes: 36 additions & 0 deletions typo3/sysext/core/Classes/Utility/StringUtility.php
Expand Up @@ -209,4 +209,40 @@ public static function multibyteStringPad(string $string, int $length, string $p
}
return $string;
}

/**
* Returns base64 encoded value with a URL and filename safe alphabet
* according to https://tools.ietf.org/html/rfc4648#section-5
*
* The difference to classic base64 is, that the result
* alphabet is adjusted like shown below, padding (`=`)
* is stripped completely:
* + position #62: `+` -> `-` (minus)
* + position #63: `/` -> `_` (underscore)
*
* @param string $value raw value
* @return string base64url encoded string
*/
public static function base64urlEncode(string $value): string
{
return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']);
}

/**
* Returns base64 decoded value with a URL and filename safe alphabet
* according to https://tools.ietf.org/html/rfc4648#section-5
*
* The difference to classic base64 is, that the result
* alphabet is adjusted like shown below, padding (`=`)
* is stripped completely:
* + position #62: `-` (minus) -> `+`
* + position #63: `_` (underscore) -> `/`
*
* @param string $value base64url decoded string
* @return string raw value
*/
public static function base64urlDecode(string $value): string
{
return base64_decode(strtr($value, ['-' => '+', '_' => '/']));
}
}
51 changes: 51 additions & 0 deletions typo3/sysext/core/Tests/Unit/Utility/StringUtilityTest.php
Expand Up @@ -258,4 +258,55 @@ public function multibyteStringPadReturnsCorrectResultsMultibyte(string $expecte
StringUtility::multibyteStringPad($string, $length, $pad_string, $pad_type)
);
}

public static function base64urlRoundTripWorksDataProvider(): \Generator
{
yield ['a'];
yield ['aa'];
yield ['aaa'];
yield ['aaaa'];
yield [random_bytes(31)];
yield [random_bytes(32)];
yield [random_bytes(33)];
}

/**
* @test
* @dataProvider base64urlRoundTripWorksDataProvider
*/
public function base64urlRoundTripWorks(string $rawValue): void
{
$encoded = StringUtility::base64urlEncode($rawValue);
$decoded = StringUtility::base64urlDecode($encoded);
self::assertSame($rawValue, $decoded);
}

public static function base64urlDataProvider(): \Generator
{
yield ['', ''];
yield ['a', 'YQ'];
yield ['aa', 'YWE'];
yield ['aa>', 'YWE-'];
yield ['aa?', 'YWE_'];
yield ['aaa', 'YWFh'];
yield ['aaaa', 'YWFhYQ'];
}

/**
* @test
* @dataProvider base64urlDataProvider
*/
public function base64urlEncodeWorks(string $rawValue, string $encodedValue): void
{
self::assertSame($encodedValue, StringUtility::base64urlEncode($rawValue));
}

/**
* @test
* @dataProvider base64urlDataProvider
*/
public function base64urlDecodeWorks(string $rawValue, string $encodedValue): void
{
self::assertSame($rawValue, StringUtility::base64urlDecode($encodedValue));
}
}

0 comments on commit 04b04f4

Please sign in to comment.