|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Security; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
12
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
13
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @see https://symfony.com/doc/current/_images/authentication-guard-methods.png |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractAuthenticator extends AbstractGuardAuthenticator |
|
19
|
|
|
{ |
|
20
|
|
|
private $extractor; |
|
21
|
|
|
private $response; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Extractor $extractor, ResponseFactory $response) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->extractor = $extractor; |
|
26
|
|
|
$this->response = $response; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function supports(Request $request): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return null !== $this->extractor->extractKey($request); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function start(Request $request, AuthenticationException $exception = null): Response |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->response->fromError(Response::HTTP_UNAUTHORIZED, $exception); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getCredentials(Request $request): string |
|
40
|
|
|
{ |
|
41
|
|
|
return (string) $this->extractor->extractKey($request); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function checkCredentials($credentials, UserInterface $user): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response |
|
50
|
|
|
{ |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->response->fromError(Response::HTTP_FORBIDDEN, $exception); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function supportsRememberMe(): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|