Skip to content

Commit

Permalink
[TASK] Add freshness to user (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott authored Apr 21, 2021
1 parent 7fffa5a commit 36f6f44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Security/KeyCloakAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public function getUser($credentials, UserProviderInterface $userProvider): ?Key
$roles,
$scopes,
$this->getEmailFromToken($credentials->headers->get('X-Auth-Token')),
$this->getFullNameFromToken($credentials->headers->get('X-Auth-Token'))
$this->getFullNameFromToken($credentials->headers->get('X-Auth-Token')),
true
);
}

Expand Down
16 changes: 15 additions & 1 deletion src/Security/KeyCloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class KeyCloakUser implements UserInterface
private array $roles;
private ?string $fullName = null;
private ?string $email = null;
private bool $fresh = false;

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

public function getRoles(): array
Expand Down Expand Up @@ -67,4 +69,16 @@ public function getDisplayName(): string
{
return $this->fullName ?? $this->username;
}

public function isFresh(): bool
{
return $this->fresh;
}

public function setFresh(bool $fresh): self
{
$this->fresh = $fresh;

return $this;
}
}
8 changes: 5 additions & 3 deletions src/Security/KeyCloakUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@ public function __construct(array $roleMapping, array $defaultRoles = ['ROLE_USE
* @param array $scopes
* @param string|null $email
* @param string|null $fullName
* @param bool $fresh
* @return KeyCloakUser
*/
public function loadUserByUsername(
$username,
array $keycloakGroups = [],
array $scopes = [],
?string $email = null,
?string $fullName = null
?string $fullName = null,
bool $fresh = false
): 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), $email, $fullName);
return new KeyCloakUser($username, array_values($roles), $email, $fullName, $fresh);
}

/**
Expand All @@ -58,7 +60,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(), $user->getEmail(), $user->getFullName());
return new KeyCloakUser($user->getUsername(), $user->getRoles(), $user->getEmail(), $user->getFullName(), false);
}

/**
Expand Down

0 comments on commit 36f6f44

Please sign in to comment.