From 670a76807cfdc402c92669e206221b39b1a21556 Mon Sep 17 00:00:00 2001 From: "El. Abdellah" Date: Thu, 11 Nov 2021 14:42:02 +0100 Subject: [PATCH] refactor deprecated methodes --- Security/Authenticator/KeycloakBearerAuthenticator.php | 4 ++-- Security/User/KeycloakBearerUser.php | 10 +++++++++- Security/User/KeycloakBearerUserProvider.php | 7 ++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Security/Authenticator/KeycloakBearerAuthenticator.php b/Security/Authenticator/KeycloakBearerAuthenticator.php index ab09105..e75d96a 100644 --- a/Security/Authenticator/KeycloakBearerAuthenticator.php +++ b/Security/Authenticator/KeycloakBearerAuthenticator.php @@ -28,13 +28,13 @@ class KeycloakBearerAuthenticator extends AbstractAuthenticator public function supports(Request $request): ?bool { - return $request->headers->has('Authorization'); + return true; } public function authenticate(Request $request): PassportInterface { $token = $request->headers->get('Authorization'); - if (null === $token) { + if (null === $token || empty($token)) { // The token header was empty, authentication fails with HTTP Status // Code 401 "Unauthorized" throw new CustomUserMessageAuthenticationException('Token is not present in the request headers'); diff --git a/Security/User/KeycloakBearerUser.php b/Security/User/KeycloakBearerUser.php index e8089ed..84eca85 100644 --- a/Security/User/KeycloakBearerUser.php +++ b/Security/User/KeycloakBearerUser.php @@ -245,6 +245,14 @@ class KeycloakBearerUser implements UserInterface, \Serializable return $this->preferred_username; } + /** + * @return string + */ + public function getUserIdentifier(): string + { + return $this->preferred_username; + } + /** * Removes sensitive data from the user. * @@ -298,4 +306,4 @@ class KeycloakBearerUser implements UserInterface, \Serializable $this->accessToken ) = unserialize($serialized, ['allowed_classes' => false]); } -} \ No newline at end of file +} diff --git a/Security/User/KeycloakBearerUserProvider.php b/Security/User/KeycloakBearerUserProvider.php index 4ce7c17..700b888 100644 --- a/Security/User/KeycloakBearerUserProvider.php +++ b/Security/User/KeycloakBearerUserProvider.php @@ -5,6 +5,7 @@ namespace ABEL\Bundle\keycloakBearerOnlyAdapterBundle\Security\User; use GuzzleHttp\Client; +use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\User\UserInterface; @@ -115,11 +116,11 @@ class KeycloakBearerUserProvider implements UserProviderInterface{ $jwt = json_decode($response->getBody(), true); if (!$jwt['active']) { - throw new \UnexpectedValueException('The token does not exist or is not valid anymore'); + throw new CustomUserMessageAuthenticationException('The token does not exist or is not valid anymore'); } if (!isset($jwt['resource_access'][$this->client_id])) { - throw new \UnexpectedValueException('The token does not have the necessary permissions!'); + throw new CustomUserMessageAuthenticationException('The token does not have the necessary permissions!'); } return new KeycloakBearerUser( @@ -142,4 +143,4 @@ class KeycloakBearerUserProvider implements UserProviderInterface{ { return $this->loadUserByIdentifier($username); } -} \ No newline at end of file +}