Skip to content

Commit

Permalink
[TASK][DEV-455] Add email and fullname to users (#5)
Browse files Browse the repository at this point in the history
* [TASK] Get name and email from header

* [TASK] Get name and email from header
  • Loading branch information
Woeler authored Jun 9, 2020
1 parent cf391cd commit 86dd0ab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
31 changes: 26 additions & 5 deletions src/Security/KeyCloakAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getCredentials(Request $request): Request

/**
* @param Request $credentials
* @param UserProviderInterface $userProvider
* @param UserProviderInterface|KeyCloakUserProvider $userProvider
* @return KeyCloakUser|null
*/
public function getUser($credentials, UserProviderInterface $userProvider): ?KeyCloakUser
Expand All @@ -73,7 +73,9 @@ public function getUser($credentials, UserProviderInterface $userProvider): ?Key
return $userProvider->loadUserByUsername(
$credentials->headers->get('X-Auth-Username'),
$roles,
$scopes
$scopes,
$this->getEmailFromToken($credentials->headers->get('X-Auth-Token')),
$this->getFullNameFromToken($credentials->headers->get('X-Auth-Token'))
);
}

Expand Down Expand Up @@ -119,17 +121,36 @@ public function supportsRememberMe(): bool
return false;
}

private function decodeJwtToken(string $token): array
{
$this->JWTService->verify($token);

return json_decode($this->JWTService->getPayload(), true, 512, JSON_THROW_ON_ERROR);
}

private function getRolesFromToken(string $token): array
{
$roles= [];
$this->JWTService->verify($token);
$payload = json_decode($this->JWTService->getPayload(), true, 512, JSON_THROW_ON_ERROR);
$scopes = explode(' ', $payload['scope']);
$scopes = explode(' ', $this->decodeJwtToken($token)['scope']);

foreach ($scopes as $scope) {
$roles[] = 'ROLE_SCOPE_' . strtoupper(str_replace('.', '_', $scope));
}

return $roles;
}

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

return $data['name'] ?? null;
}

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

return $data['email'] ?? null;
}
}
24 changes: 23 additions & 1 deletion src/Security/KeyCloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class KeyCloakUser implements UserInterface
{
private string $username;
private array $roles;
private ?string $fullName = null;
private ?string $email = null;

public function __construct(string $username, array $roles)
public function __construct(string $username, array $roles, ?string $email, ?string $fullName = null)
{
$this->username = $username;
$this->roles = $roles;
$this->email = $email;
$this->fullName = $fullName;
}

public function getRoles(): array
Expand All @@ -45,4 +49,22 @@ public function eraseCredentials(): void
{
// Do nothing.
}

public function getFullName(): ?string
{
return $this->fullName;
}

public function getEmail(): ?string
{
return $this->email;
}

/**
* @return string The full name of the user. When not present, the username
*/
public function getDisplayName(): string
{
return $this->fullName ?? $this->username;
}
}
19 changes: 11 additions & 8 deletions src/Security/KeyCloakUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

namespace T3G\Bundle\Keycloak\Security;

use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand All @@ -33,16 +29,23 @@ public function __construct(array $roleMapping, array $defaultRoles = ['ROLE_USE
* @param string $username
* @param array $keycloakGroups
* @param array $scopes
* @param string|null $email
* @param string|null $fullName
* @return KeyCloakUser
*/
public function loadUserByUsername($username, array $keycloakGroups = [], array $scopes = []): KeyCloakUser
{
public function loadUserByUsername(
$username,
array $keycloakGroups = [],
array $scopes = [],
?string $email = null,
?string $fullName = null
): KeyCloakUser {
$roles = array_intersect_key($this->roleMapping, array_flip(array_map(static function ($v) {
return str_replace('-', '_', $v);
}, $keycloakGroups)));
$roles = array_merge($roles, $scopes, $this->defaultRoles);

return new KeyCloakUser($username, array_values($roles));
return new KeyCloakUser($username, array_values($roles), $email, $fullName);
}

/**
Expand All @@ -55,7 +58,7 @@ public function refreshUser(UserInterface $user): KeyCloakUser
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
}

return new KeyCloakUser($user->getUsername(), $user->getRoles());
return new KeyCloakUser($user->getUsername(), $user->getRoles(), $user->getEmail(), $user->getFullName());
}

/**
Expand Down

0 comments on commit 86dd0ab

Please sign in to comment.