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

BC support of Custom Claims #1222

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 14 additions & 1 deletion src/AuthorizationValidators/BearerTokenValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function validateAuthorization(ServerRequestInterface $request)
->withAttribute('oauth_access_token_id', $claims->get('jti'))
->withAttribute('oauth_client_id', $this->convertSingleRecordAudToString($claims->get('aud')))
->withAttribute('oauth_user_id', $claims->get('sub'))
->withAttribute('oauth_scopes', $claims->get('scopes'));
->withAttribute('oauth_scopes', $claims->get('scopes'))
->withAttribute('oauth_custom_claims', $this->extractCustomClaims($claims->all()));
}

/**
Expand All @@ -132,4 +133,16 @@ private function convertSingleRecordAudToString($aud)
{
return \is_array($aud) && \count($aud) === 1 ? $aud[0] : $aud;
}

/**
* Extract custom claims
*
* @param array $claims
*
* @return array
*/
private function extractCustomClaims($claims)
{
return \array_diff_key($claims, \array_flip(['jti', 'aud', 'sub', 'scopes', 'iat', 'nbf', 'exp']));
}
}
8 changes: 8 additions & 0 deletions tests/AuthorizationValidators/BearerTokenValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ public function testBearerTokenValidatorAcceptsValidToken()
->expiresAt((new DateTimeImmutable())->add(new DateInterval('PT1H')))
->relatedTo('user-id')
->withClaim('scopes', 'scope1 scope2 scope3 scope4')
->withClaim('attr1', 'value')
->withClaim('attr2', 42)
->getToken(new Sha256(), LocalFileReference::file(__DIR__ . '/../Stubs/private.key'));

$request = (new ServerRequest())->withHeader('authorization', \sprintf('Bearer %s', $validJwt->toString()));

$validRequest = $bearerTokenValidator->validateAuthorization($request);

$this->assertArrayHasKey('authorization', $validRequest->getHeaders());

$this->assertEquals('token-id', $validRequest->getAttribute('oauth_access_token_id'));
$this->assertEquals('client-id', $validRequest->getAttribute('oauth_client_id'));
$this->assertEquals('user-id', $validRequest->getAttribute('oauth_user_id'));
$this->assertEquals('scope1 scope2 scope3 scope4', $validRequest->getAttribute('oauth_scopes'));
$this->assertEquals(['attr1' => 'value', 'attr2' => 42], $validRequest->getAttribute('oauth_custom_claims'));
}

public function testBearerTokenValidatorRejectsExpiredToken()
Expand Down