Skip to content

Commit 8c0e70f

Browse files
committed
Add OAuth Client ID to OAuth2Token
1 parent 699c99c commit 8c0e70f

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/Security/Authentication/Token/OAuth2Token.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ final class OAuth2Token extends AbstractToken
1818
public function __construct(
1919
?UserInterface $user,
2020
string $accessTokenId,
21+
string $oauthClientId,
2122
array $scopes,
2223
string $rolePrefix
2324
) {
2425
$this->setAttribute('access_token_id', $accessTokenId);
26+
$this->setAttribute('oauth_client_id', $oauthClientId);
2527
$this->setAttribute('scopes', $scopes);
2628

2729
// Build roles from scope
@@ -54,4 +56,10 @@ public function getCredentials(): string
5456
/** @var string */
5557
return $this->getAttribute('access_token_id');
5658
}
59+
60+
public function getOAuthClientId(): string
61+
{
62+
/** @var string */
63+
return $this->getAttribute('oauth_client_id');
64+
}
5765
}

src/Security/Authenticator/OAuth2Authenticator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function authenticate(Request $request): PassportInterface
9292
/** @var list<string> $scopes */
9393
$scopes = $psr7Request->getAttribute('oauth_scopes', []);
9494

95+
/** @var string $oauthClientId */
96+
$oauthClientId = $psr7Request->getAttribute('oauth_client_id', '');
97+
9598
$userLoader = function (string $userIdentifier): UserInterface {
9699
if ('' === $userIdentifier) {
97100
return new NullUser();
@@ -109,6 +112,8 @@ public function authenticate(Request $request): PassportInterface
109112

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

115+
$passport->setAttribute('oauthClientId', $oauthClientId);
116+
112117
return $passport;
113118
}
114119

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

130-
$token = new OAuth2Token($passport->getUser(), $accessTokenId, $scopeBadge->getScopes(), $this->rolePrefix);
135+
/** @var string $oauthClientId */
136+
$oauthClientId = $passport->getAttribute('oauthClientId');
137+
138+
$token = new OAuth2Token($passport->getUser(), $accessTokenId, $oauthClientId, $scopeBadge->getScopes(), $this->rolePrefix);
131139
$token->setAuthenticated(true);
132140

133141
return $token;

tests/Unit/OAuth2AuthenticatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function testCreateAuthenticatedToken(): void
143143
new ScopeBadge(['scope_one', 'scope_two']),
144144
]);
145145
$passport->setAttribute('accessTokenId', 'accessTokenId');
146+
$passport->setAttribute('oauthClientId', 'oauthClientId');
146147

147148
$authenticator = new OAuth2Authenticator(
148149
$this->createMock(HttpMessageFactoryInterface::class),

tests/Unit/OAuth2TokenTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ public function testTokenSerialization(): void
1515
{
1616
$user = new User();
1717
$accessTokenId = 'accessTokenId';
18+
$oauthClientId = 'oauthClientId';
1819
$scopes = [FixtureFactory::FIXTURE_SCOPE_FIRST];
1920
$rolePrefix = 'ROLE_OAUTH2_';
2021

21-
$token = new OAuth2Token($user, $accessTokenId, $scopes, $rolePrefix);
22+
$token = new OAuth2Token($user, $accessTokenId, $oauthClientId, $scopes, $rolePrefix);
2223

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

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

0 commit comments

Comments
 (0)