Skip to content

Commit

Permalink
[TASK] Get roles from jwt token instead of header (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Woeler authored Jun 26, 2020
1 parent 1a643be commit e1a3fdf
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Security/KeyCloakAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function supports(Request $request): bool
{
return $request->headers->has('X-Auth-Token')
&& $request->headers->has('X-Auth-Username')
&& $request->headers->has('X-Auth-Userid')
&& $request->headers->has('X-Auth-Roles');
&& $request->headers->has('X-Auth-Userid');
}

/**
Expand All @@ -67,8 +66,8 @@ public function getCredentials(Request $request): Request
public function getUser($credentials, UserProviderInterface $userProvider): ?KeyCloakUser
{
$this->session->set('JWT_TOKEN', $credentials->headers->get('X-Auth-Token'));
$roles = explode(',', $credentials->headers->get('X-Auth-Roles')) ?? [];
$scopes = $this->getRolesFromToken($credentials->headers->get('X-Auth-Token'));
$roles = $this->getRolesFromToken($credentials->headers->get('X-Auth-Token'));
$scopes = $this->getScopesFromToken($credentials->headers->get('X-Auth-Token'));

return $userProvider->loadUserByUsername(
$credentials->headers->get('X-Auth-Username'),
Expand Down Expand Up @@ -128,7 +127,7 @@ private function decodeJwtToken(string $token): array
return json_decode($this->JWTService->getPayload(), true, 512, JSON_THROW_ON_ERROR);
}

private function getRolesFromToken(string $token): array
private function getScopesFromToken(string $token): array
{
$roles= [];
$scopes = explode(' ', $this->decodeJwtToken($token)['scope']);
Expand All @@ -140,6 +139,11 @@ private function getRolesFromToken(string $token): array
return $roles;
}

private function getRolesFromToken(string $token): array
{
return $this->decodeJwtToken($token)['realm_access']['roles'] ?? [];
}

public function getFullNameFromToken(string $token): ?string
{
$data = $this->decodeJwtToken($token);
Expand Down

0 comments on commit e1a3fdf

Please sign in to comment.