1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Hmac\Symfony; |
4
|
|
|
|
5
|
|
|
use Acquia\Hmac\RequestAuthenticatorInterface; |
6
|
|
|
use Laminas\Diactoros\ResponseFactory; |
7
|
|
|
use Laminas\Diactoros\ServerRequestFactory; |
8
|
|
|
use Laminas\Diactoros\StreamFactory; |
9
|
|
|
use Laminas\Diactoros\UploadedFileFactory; |
10
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Provides the means to authenticate an HTTP HMAC request. |
17
|
|
|
*/ |
18
|
|
|
class HmacAuthenticationProvider implements AuthenticationProviderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Acquia\Hmac\RequestAuthenticatorInterface |
22
|
|
|
* A HMAC request authenticator service. |
23
|
|
|
*/ |
24
|
|
|
protected $authenticator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializes the authentication provider. |
28
|
3 |
|
* |
29
|
|
|
* @param \Acquia\Hmac\RequestAuthenticatorInterface $authenticator |
30
|
3 |
|
* The HMAC request authenticator service. |
31
|
3 |
|
*/ |
32
|
|
|
public function __construct(RequestAuthenticatorInterface $authenticator) |
33
|
|
|
{ |
34
|
|
|
$this->authenticator = $authenticator; |
35
|
|
|
} |
36
|
2 |
|
|
37
|
|
|
/** |
38
|
2 |
|
* {@inheritDoc} |
39
|
2 |
|
*/ |
40
|
|
|
public function authenticate(TokenInterface $token) |
41
|
|
|
{ |
42
|
2 |
|
$httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory()); |
43
|
|
|
$psr7Request = $httpMessageFactory->createRequest($token->getRequest()); |
|
|
|
|
44
|
1 |
|
|
45
|
1 |
|
try { |
46
|
1 |
|
$key = $this->authenticator->authenticate($psr7Request); |
47
|
|
|
|
48
|
|
|
return new HmacToken($token->getRequest(), $key); |
49
|
|
|
} catch (\Exception $e) { |
50
|
|
|
throw new AuthenticationException($e->getMessage()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
1 |
|
|
54
|
|
|
/** |
55
|
1 |
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function supports(TokenInterface $token) |
58
|
|
|
{ |
59
|
|
|
return $token instanceof HmacToken; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|