|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Biurad\Security\Authenticator; |
|
4
|
|
|
|
|
5
|
|
|
use Biurad\Security\Interfaces\AuthenticatorInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; |
|
12
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
|
13
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Authenticates whether the given CSRF token is valid. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class CsrfTokenAuthenticator implements AuthenticatorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private CsrfTokenManagerInterface $csrfTokenManager; |
|
23
|
|
|
private string $csrfTokenId; |
|
24
|
|
|
private string $csrfParameter; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(CsrfTokenManagerInterface $csrfTokenManager, string $csrfTokenId = 'authenticate', string $csrfParameter = '_csrf_token') |
|
27
|
|
|
{ |
|
28
|
|
|
$this->csrfTokenManager = $csrfTokenManager; |
|
29
|
|
|
$this->csrfTokenId = $csrfTokenId; |
|
30
|
|
|
$this->csrfParameter = $csrfParameter; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setToken(?TokenInterface $token): void |
|
37
|
|
|
{ |
|
38
|
|
|
// This authenticator does not use a token. |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function supports(ServerRequestInterface $request): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return 'POST' === $request->getMethod(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function authenticate(ServerRequestInterface $request, array $credentials): ?TokenInterface |
|
53
|
|
|
{ |
|
54
|
|
|
if (empty($csrfToken = $credentials[$this->csrfParameter] ?? null)) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!\is_string($csrfToken)) { |
|
59
|
|
|
throw new BadRequestException(\sprintf('The key "%s" must be a string, "%s" given.', $this->csrfParameter, \gettype($csrfToken))); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$csrfToken = new CsrfToken($this->csrfTokenId, $csrfToken); |
|
63
|
|
|
|
|
64
|
|
|
if (false === $this->csrfTokenManager->isTokenValid($csrfToken)) { |
|
65
|
|
|
throw new InvalidCsrfTokenException('Invalid CSRF token.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function failure(ServerRequestInterface $request, AuthenticationException $exception): ?ResponseInterface |
|
75
|
|
|
{ |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|