Skip to content

Commit

Permalink
feature #60 Add OAuth Client ID to OAuth2Token (robotjoosen)
Browse files Browse the repository at this point in the history
This PR was merged into the 0.1-dev branch.

Discussion
----------

Add OAuth Client ID to OAuth2Token

This PR should help with issue #50. It adds Client ID to the Passport and Token.

This way the client can be retrieved like this:
```
$clientId = $this->security->getToken()->getOAuthClientId();
$userRepository->findOneByClientId($clientId);
```

Commits
-------

8c0e70f Add OAuth Client ID to OAuth2Token
  • Loading branch information
chalasr committed Nov 6, 2021
2 parents 699c99c + 8c0e70f commit c8c657e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Security/Authentication/Token/OAuth2Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ final class OAuth2Token extends AbstractToken
public function __construct(
?UserInterface $user,
string $accessTokenId,
string $oauthClientId,
array $scopes,
string $rolePrefix
) {
$this->setAttribute('access_token_id', $accessTokenId);
$this->setAttribute('oauth_client_id', $oauthClientId);
$this->setAttribute('scopes', $scopes);

// Build roles from scope
Expand Down Expand Up @@ -54,4 +56,10 @@ public function getCredentials(): string
/** @var string */
return $this->getAttribute('access_token_id');
}

public function getOAuthClientId(): string
{
/** @var string */
return $this->getAttribute('oauth_client_id');
}
}
10 changes: 9 additions & 1 deletion src/Security/Authenticator/OAuth2Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function authenticate(Request $request): PassportInterface
/** @var list<string> $scopes */
$scopes = $psr7Request->getAttribute('oauth_scopes', []);

/** @var string $oauthClientId */
$oauthClientId = $psr7Request->getAttribute('oauth_client_id', '');

$userLoader = function (string $userIdentifier): UserInterface {
if ('' === $userIdentifier) {
return new NullUser();
Expand All @@ -109,6 +112,8 @@ public function authenticate(Request $request): PassportInterface

$passport->setAttribute('accessTokenId', $accessTokenId);

$passport->setAttribute('oauthClientId', $oauthClientId);

return $passport;
}

Expand All @@ -127,7 +132,10 @@ public function createAuthenticatedToken(PassportInterface $passport, string $fi
/** @var ScopeBadge $scopeBadge */
$scopeBadge = $passport->getBadge(ScopeBadge::class);

$token = new OAuth2Token($passport->getUser(), $accessTokenId, $scopeBadge->getScopes(), $this->rolePrefix);
/** @var string $oauthClientId */
$oauthClientId = $passport->getAttribute('oauthClientId');

$token = new OAuth2Token($passport->getUser(), $accessTokenId, $oauthClientId, $scopeBadge->getScopes(), $this->rolePrefix);
$token->setAuthenticated(true);

return $token;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/OAuth2AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function testCreateAuthenticatedToken(): void
new ScopeBadge(['scope_one', 'scope_two']),
]);
$passport->setAttribute('accessTokenId', 'accessTokenId');
$passport->setAttribute('oauthClientId', 'oauthClientId');

$authenticator = new OAuth2Authenticator(
$this->createMock(HttpMessageFactoryInterface::class),
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/OAuth2TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ public function testTokenSerialization(): void
{
$user = new User();
$accessTokenId = 'accessTokenId';
$oauthClientId = 'oauthClientId';
$scopes = [FixtureFactory::FIXTURE_SCOPE_FIRST];
$rolePrefix = 'ROLE_OAUTH2_';

$token = new OAuth2Token($user, $accessTokenId, $scopes, $rolePrefix);
$token = new OAuth2Token($user, $accessTokenId, $oauthClientId, $scopes, $rolePrefix);

/** @var OAuth2Token $unserializedToken */
$unserializedToken = unserialize(serialize($token));

$this->assertSame($user->getUsername(), $unserializedToken->getUser()->getUsername());
$this->assertSame($accessTokenId, $token->getCredentials());
$this->assertSame($oauthClientId, $token->getOAuthClientId());
$this->assertSame($scopes, $token->getScopes());
$this->assertSame([sprintf('%s%s', $rolePrefix, strtoupper($scopes[0]))], $token->getRoleNames());

Expand Down

0 comments on commit c8c657e

Please sign in to comment.