-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-8356: Reworked Ibexa\Core\MVC\Symfony\Security\Authentication\Aut…
…henticatorInterface usages to comply with Symfony-based authentication
- Loading branch information
1 parent
4fb3e4a
commit 0bb638e
Showing
8 changed files
with
206 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\GraphQL\Security; | ||
|
||
use Exception; | ||
use GraphQL\Language\AST\ArgumentNode; | ||
use GraphQL\Language\AST\NodeKind; | ||
use GraphQL\Language\Parser; | ||
use GraphQL\Language\Visitor; | ||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Core\MVC\Symfony\Security\User\APIUserProviderInterface; | ||
use Ibexa\Core\MVC\Symfony\Security\UserInterface as IbexaUser; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; | ||
use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
|
||
final class JWTAuthenticator extends AbstractAuthenticator implements InteractiveAuthenticatorInterface | ||
{ | ||
private string $username; | ||
|
||
private string $password; | ||
|
||
public function __construct( | ||
private readonly JWTTokenManagerInterface $tokenManager, | ||
private readonly APIUserProviderInterface $userProvider, | ||
private readonly PermissionResolver $permissionResolver, | ||
) { | ||
} | ||
|
||
public function supports(Request $request): ?bool | ||
{ | ||
$payload = json_decode($request->getContent(), true); | ||
if (!isset($payload['query'])) { | ||
return false; | ||
} | ||
|
||
try { | ||
$credentials = $this->extractCredentials($payload['query']); | ||
} catch (Exception) { | ||
return false; | ||
} | ||
|
||
if (isset($credentials['username'], $credentials['password'])) { | ||
$this->username = $credentials['username']; | ||
$this->password = $credentials['password']; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function authenticate(Request $request): Passport | ||
{ | ||
$passport = new Passport( | ||
new UserBadge($this->username, [$this->userProvider, 'loadUserByUsername']), | ||
new PasswordCredentials($this->password) | ||
); | ||
|
||
$user = $passport->getUser(); | ||
if ($user instanceof IbexaUser) { | ||
$this->permissionResolver->setCurrentUserReference($user->getAPIUser()); | ||
} | ||
|
||
$passport->setAttribute('token', $this->tokenManager->create($user)); | ||
|
||
return $passport; | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
return new Response( | ||
$this->formatMutationResponseData([ | ||
'token' => $this->tokenManager->create($token->getUser()), | ||
'message' => null, | ||
]) | ||
); | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
return new Response( | ||
$this->formatMutationResponseData([ | ||
'token' => null, | ||
'message' => $exception->getMessageKey(), | ||
]), | ||
Response::HTTP_FORBIDDEN | ||
); | ||
} | ||
|
||
public function isInteractive(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
* | ||
* @throws \Exception | ||
*/ | ||
private function extractCredentials(string $graphqlQuery): array | ||
{ | ||
$parsed = Parser::parse($graphqlQuery); | ||
$credentials = []; | ||
Visitor::visit( | ||
$parsed, | ||
[ | ||
NodeKind::ARGUMENT => static function (ArgumentNode $node) use (&$credentials): void { | ||
$credentials[$node->name->value] = (string)$node->value->value; | ||
}, | ||
] | ||
); | ||
|
||
return $credentials; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $data | ||
*/ | ||
private function formatMutationResponseData(array $data): string | ||
{ | ||
return json_encode([ | ||
'data' => [ | ||
'CreateToken' => $data, | ||
], | ||
]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.