-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthentication.php
78 lines (63 loc) · 2.51 KB
/
Authentication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?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\Mutation;
use Ibexa\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface;
use Ibexa\GraphQL\Security\JWTUser;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
final class Authentication
{
/** @var \Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface */
private $tokenManager;
/** @var \Symfony\Component\HttpFoundation\RequestStack */
private $requestStack;
/** @var \Ibexa\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface|null */
private $authenticator;
public function __construct(
JWTTokenManagerInterface $tokenManager,
RequestStack $requestStack,
?AuthenticatorInterface $authenticator = null
) {
$this->tokenManager = $tokenManager;
$this->requestStack = $requestStack;
$this->authenticator = $authenticator;
}
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function createToken($args): array
{
$username = $args['username'];
$password = $args['password'];
$request = $this->requestStack->getCurrentRequest();
$request->attributes->set('username', $username);
$request->attributes->set('password', (string) $password);
try {
$user = $this->getAuthenticator()->authenticate($request)->getUser();
$token = $this->tokenManager->create(
new JWTUser($user, $username)
);
return ['token' => $token];
} catch (AuthenticationException $e) {
return ['message' => 'Wrong username or password', 'token' => null];
}
}
private function getAuthenticator(): AuthenticatorInterface
{
if (null === $this->authenticator) {
throw new \RuntimeException(
sprintf(
"No %s instance injected. Ensure 'ezpublish_rest_session' is configured under your firewall",
AuthenticatorInterface::class
)
);
}
return $this->authenticator;
}
}
class_alias(Authentication::class, 'EzSystems\EzPlatformGraphQL\GraphQL\Mutation\Authentication');