Skip to content

Commit

Permalink
Change AbstractTestCase::assertJwtEquals() to compare array data inst…
Browse files Browse the repository at this point in the history
…ead of string contents.
  • Loading branch information
Potherca committed Sep 23, 2022
1 parent a1fa9be commit 0f45dd7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
13 changes: 12 additions & 1 deletion tests/unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ public function assertJwtEquals(array $expected, string $actual): void
{
$encoded = explode('.', $actual);

$decoded = array_map([Base64Url::class, 'decode'], $encoded);
$decoded = array_map(function ($encoded) {
$decoded = Base64Url::decode($encoded);

try {
$decoded = json_decode($decoded, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
// Not (valid) JSON, return Base64Url decoded value
}

return $decoded;
}, $encoded);


// We can not easily compare the signatures in PHP, as the numeric
// representation of the binary string is INF (infinity). So unless
Expand Down
31 changes: 15 additions & 16 deletions tests/unit/TokenGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ final public function testRegistrationAccessTokenGeneration(): void

$actual = $tokenGenerator->generateRegistrationAccessToken('mock client ID', $privateKey);

$expected = [
'{"typ":"JWT","alg":"RS256"}',
'{"iss":"mock issuer","aud":"mock client ID","sub":"mock client ID"}',
];

$this->assertJwtEquals($expected, $actual);
$this->assertJwtEquals([[
"alg" => "RS256",
"typ" => "JWT",
], [
"iss" => "mock issuer",
"aud" => "mock client ID",
"sub" => "mock client ID",
]], $actual);
}

/**
Expand Down Expand Up @@ -298,7 +300,7 @@ final public function testIdTokenGeneration(): void

$now = new \DateTimeImmutable('1234-01-01 12:34:56.789');

$idToken = $tokenGenerator->generateIdToken(
$actual = $tokenGenerator->generateIdToken(
'mock access token',
'mock clientId',
'mock subject',
Expand All @@ -308,14 +310,11 @@ final public function testIdTokenGeneration(): void
$now
);

[$header, $body,] = explode('.', $idToken);

$header = Base64Url::decode($header);
$body = json_decode(Base64Url::decode($body), true);

$this->assertEquals('{"typ":"JWT","alg":"RS256","kid":"0c3932ca20f3a00ad2eb72035f6cc9cb"}', $header);

$this->assertEquals([
$this->assertJwtEquals([[
"alg"=>"RS256",
"kid"=>"0c3932ca20f3a00ad2eb72035f6cc9cb",
"typ"=>"JWT",
],[
'aud' => 'mock clientId',
'azp' => 'mock clientId',
'c_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
Expand All @@ -328,6 +327,6 @@ final public function testIdTokenGeneration(): void
'nbf' => -23225829905.789,
'nonce' => 'mock nonce',
'sub' => 'mock subject',
], $body);
]], $actual);
}
}

0 comments on commit 0f45dd7

Please sign in to comment.