diff --git a/Model/Client.php b/Model/Client.php index 0c7c2449..c8ed4c69 100644 --- a/Model/Client.php +++ b/Model/Client.php @@ -135,4 +135,34 @@ public function getAllowedGrantTypes() { return $this->allowedGrantTypes; } + + public function getRoles() + { + return [ 'ROLE_USER' ]; + } + + public function getPassword() + { + return $this->getSecret(); + } + + public function getSalt() + { + // Will use auto salt system + } + + public function eraseCredentials() + { + // nothind to erase + } + + public function getUsername() + { + return $this->getRandomId(); + } + + public function getUserIdentifier(): string + { + return $this->getRandomId(); + } } diff --git a/Security/Authenticator/OAuth2Passport.php b/Security/Authenticator/OAuth2Passport.php new file mode 100644 index 00000000..845b9c32 --- /dev/null +++ b/Security/Authenticator/OAuth2Passport.php @@ -0,0 +1,13 @@ +serverService = $serverService; $this->userChecker = $userChecker; @@ -51,20 +51,14 @@ public function supports(Request $request): ?bool */ public function authenticate(Request $request): PassportInterface { - return new SelfValidatingPassport( - new UserBadge('admin-workflow') - ); - -// die( 'test public function authenticate(Request $request): PassportInterface' ); try { $tokenString = str_replace('Bearer ', '', $request->headers->get('Authorization')); - // TODO: this is nasty, create a proper interface here - /** @var OAuthToken&TokenInterface&\OAuth2\Model\IOAuth2AccessToken $accessToken */ + /** @var AccessToken $accessToken */ $accessToken = $this->serverService->verifyAccessToken($tokenString); - $scope = $accessToken->getScope(); $user = $accessToken->getUser(); + $client = $accessToken->getClient(); if (null !== $user) { try { @@ -81,6 +75,7 @@ public function authenticate(Request $request): PassportInterface } $roles = (null !== $user) ? $user->getRoles() : []; + $scope = $accessToken->getScope(); if (! empty($scope)) { foreach (explode(' ', $scope) as $role) { @@ -90,31 +85,23 @@ public function authenticate(Request $request): PassportInterface $roles = array_unique($roles, SORT_REGULAR); - if (null !== $user) { - try { - $this->userChecker->checkPostAuth($user); - return new SelfValidatingPassport(new UserBadge($user->getUserIdentifier())); - } catch (AccountStatusException $e) { - throw new OAuth2AuthenticateException( - Response::HTTP_UNAUTHORIZED, - OAuth2::TOKEN_TYPE_BEARER, - $this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), - 'access_denied', - $e->getMessage() - ); - } - } - return new SelfValidatingPassport(new UserBadge($tokenString)); + $accessTokenBadge = new AccessTokenBadge( $accessToken, $roles ); + + return new SelfValidatingPassport( new UserBadge( $client->getUserIdentifier() ), [ $accessTokenBadge ] ); } catch (OAuth2ServerException $e) { throw new AuthenticationException('OAuth2 authentication failed', 0, $e); } - - throw new AuthenticationException('OAuth2 authentication failed'); } public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface { - return parent::createAuthenticatedToken($passport, $firewallName); // TODO: Change the autogenerated stub + /** @var AccessTokenBadge $accessTokenBadge */ + $accessTokenBadge = $passport->getBadge( AccessTokenBadge::class ); + $token = new OAuthToken( $accessTokenBadge->getRoles() ); + $token->setAuthenticated(true); + $token->setToken( $accessTokenBadge->getAccessToken()->getToken() ); + + return $token; } /** @@ -122,7 +109,6 @@ public function createAuthenticatedToken(PassportInterface $passport, string $fi */ public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response { - die('test'); return null; } @@ -131,8 +117,6 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token, */ public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response { - dump($exception); - die(); $data = [ // you may want to customize or obfuscate the message first 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()) diff --git a/Security/Authenticator/Passport/Badge/AccessTokenBadge.php b/Security/Authenticator/Passport/Badge/AccessTokenBadge.php new file mode 100644 index 00000000..b0dc1a0f --- /dev/null +++ b/Security/Authenticator/Passport/Badge/AccessTokenBadge.php @@ -0,0 +1,56 @@ +AccessToken = $AccessToken; + $this->roles = $roles; + } + + /** + * @inheritDoc + */ + public function isResolved(): bool + { + return ! empty ( $this->roles ); + } + + /** + * @return AccessToken + */ + public function getAccessToken(): AccessToken + { + return $this->AccessToken; + } + + /** + * @return array + */ + public function getRoles(): array + { + return $this->roles; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 3562d994..04d1e9db 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ ], "require": { "php": "^7.2.5|^8.0", - "friendsofsymfony/oauth2-php": "~1.1", + "klapaudius/oauth2-php": "~1.4", "symfony/dependency-injection": "~5.3", "symfony/framework-bundle": "~5.3", "symfony/security-bundle": "~5.3",