Skip to content

Commit 2fc1460

Browse files
authored
fix client scope non-default, add support too large access tokens on authorization flow (#660)
1 parent 4f08b98 commit 2fc1460

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

configurator/src/Configurator/Vendor/Keycloak/KeycloakConfigurator.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ private function configureClients(): void
103103
'openid',
104104
'groups',
105105
] as $scope) {
106-
$this->keycloakManager->createScope($scope);
106+
$this->keycloakManager->createScope($scope, [
107+
'type' => 'default',
108+
]);
107109
}
108110

109111
foreach ($this->getAppScopes() as $app => $appScopes) {
@@ -140,7 +142,7 @@ private function configureClients(): void
140142

141143
if (isset($appScopes[$app])) {
142144
foreach ($appScopes[$app] as $scope) {
143-
$this->keycloakManager->addScopeToClient($scope, $clientData['id']);
145+
$this->keycloakManager->addScopeToClient($scope, $clientData['id'], false);
144146
}
145147
}
146148
}
@@ -170,7 +172,7 @@ private function configureClients(): void
170172
);
171173

172174
foreach ($this->getAppScopes()['databox'] as $scope) {
173-
$this->keycloakManager->addScopeToClient($scope, $clientData['id']);
175+
$this->keycloakManager->addScopeToClient($scope, $clientData['id'], false);
174176
}
175177
}
176178
}
@@ -240,7 +242,7 @@ private function configureClient(
240242
'openid',
241243
'profile',
242244
] as $scope) {
243-
$this->keycloakManager->addScopeToClient($scope, $clientData['id']);
245+
$this->keycloakManager->addScopeToClient($scope, $clientData['id'], true);
244246
}
245247

246248
return $clientData;
@@ -318,9 +320,9 @@ private function getBooleanEnv(string $name, bool $defaultValue = false): bool
318320

319321
private function configureDefaultClientScopes(): void
320322
{
321-
$rolesScope = $this->keycloakManager->getDefaultClientScopesByName('roles');
323+
$rolesScope = $this->keycloakManager->getDefaultClientScopeByName('roles');
322324
if (null === $rolesScope) {
323-
throw new \InvalidArgumentException(sprintf('Scope named "roles" not found in client scopes'));
325+
throw new \InvalidArgumentException('Scope named "roles" not found in client scopes');
324326
}
325327
$scopeId = $rolesScope['id'];
326328

configurator/src/Configurator/Vendor/Keycloak/KeycloakManager.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ public function getDefaultClientScopes(): array
109109
]))->toArray();
110110
}
111111

112-
public function getDefaultClientScopesByName(string $name): ?array
112+
public function getDefaultClientScopeByName(string $name): ?array
113113
{
114114
$scopes = $this->getDefaultClientScopes();
115115
foreach ($scopes as $scope) {
116-
if ('roles' === $scope['name']) {
116+
if ($name === $scope['name']) {
117117
return $scope;
118118
}
119119
}
@@ -232,12 +232,19 @@ private function getScopeByName(string $name): ?array
232232
return null;
233233
}
234234

235-
public function addScopeToClient(string $scope, string $clientId): void
235+
public function addScopeToClient(string $scope, string $clientId, bool $isDefault): void
236236
{
237237
$scopeData = $this->getScopeByName($scope);
238238

239239
HttpClientUtil::debugError(fn () => $this->getAuthenticatedClient()
240-
->request('PUT', UriTemplate::resolve('{realm}/clients/{clientId}/default-client-scopes/{scopeId}', [
240+
->request('DELETE', UriTemplate::resolve('{realm}/clients/{clientId}/'.(!$isDefault ? 'default' : 'optional').'-client-scopes/{scopeId}', [
241+
'realm' => $this->keycloakRealm,
242+
'clientId' => $clientId,
243+
'scopeId' => $scopeData['id'],
244+
])), 404, []);
245+
246+
HttpClientUtil::debugError(fn () => $this->getAuthenticatedClient()
247+
->request('PUT', UriTemplate::resolve('{realm}/clients/{clientId}/'.($isDefault ? 'default' : 'optional').'-client-scopes/{scopeId}', [
241248
'realm' => $this->keycloakRealm,
242249
'clientId' => $clientId,
243250
'scopeId' => $scopeData['id'],

lib/php/auth-bundle/Security/OAuthAuthorizationAuthenticator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
private readonly UrlGeneratorInterface $urlGenerator,
3535
private readonly KeycloakUrlGenerator $keycloakUrlGenerator,
3636
private readonly AuthStateEncoder $authStateEncoder,
37+
private readonly JwtExtractor $jwtExtractor,
3738
private readonly string $clientId,
3839
) {
3940
}
@@ -61,7 +62,12 @@ public function authenticate(Request $request): Passport
6162
$accessTokenBadge = new AccessTokenBadge($accessToken);
6263
$refreshTokenBadge = new RefreshTokenBadge($refreshToken);
6364

64-
return new SelfValidatingPassport(new UserBadge($accessToken), [
65+
$token = $this->jwtExtractor->parseJwt($accessToken);
66+
$user = $this->jwtExtractor->getUserFromToken($token);
67+
68+
return new SelfValidatingPassport(new UserBadge($user->getUserIdentifier(), function () use ($user): JwtUser|JwtOauthClient {
69+
return $user;
70+
}), [
6571
$accessTokenBadge,
6672
$refreshTokenBadge,
6773
]);

0 commit comments

Comments
 (0)