1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Hmac\Symfony; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
6
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
9
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Handles an authentication event. |
13
|
|
|
*/ |
14
|
|
|
class HmacAuthenticationListener |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface |
18
|
|
|
* Stores a security token for authentication. |
19
|
|
|
*/ |
20
|
|
|
protected $tokenStorage; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface |
24
|
|
|
* Manages the available authentication providers. |
25
|
|
|
*/ |
26
|
|
|
protected $authManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface |
30
|
|
|
* Response handling for a client making an unauthenticated request. |
31
|
|
|
*/ |
32
|
|
|
protected $entryPoint; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes the authentication listener. |
36
|
|
|
* |
37
|
|
|
* @param \Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface $tokenStorage |
38
|
|
|
* Storage for a security token during authentication. |
39
|
|
|
* @param \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface $authManager |
40
|
|
|
* An authentication provider manager. |
41
|
|
|
* @param \Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface $entryPoint |
42
|
|
|
* An entry point for unauthenticated client requests. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authManager, AuthenticationEntryPointInterface $entryPoint) |
45
|
3 |
|
{ |
46
|
|
|
$this->tokenStorage = $tokenStorage; |
47
|
3 |
|
$this->authManager = $authManager; |
48
|
3 |
|
$this->entryPoint = $entryPoint; |
49
|
3 |
|
} |
50
|
3 |
|
|
51
|
|
|
/** |
52
|
|
|
* Handles the incoming request. |
53
|
|
|
* |
54
|
|
|
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event |
55
|
3 |
|
* The event corresponding to the request. |
56
|
|
|
*/ |
57
|
3 |
|
public function __invoke(RequestEvent $event) |
58
|
|
|
{ |
59
|
|
|
$request = $event->getRequest(); |
60
|
3 |
|
|
61
|
1 |
|
// Requests require an Authorization header. |
62
|
1 |
|
if (!$request->headers->has('Authorization')) { |
63
|
|
|
$event->setResponse($this->entryPoint->start($request)); |
64
|
|
|
return; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
$token = new HmacToken($request); |
68
|
2 |
|
|
69
|
1 |
|
try { |
70
|
1 |
|
$authToken = $this->authManager->authenticate($token); |
71
|
1 |
|
$this->tokenStorage->setToken($authToken); |
72
|
1 |
|
$request->attributes->set('hmac.key', $authToken->getCredentials()); |
73
|
|
|
} catch (AuthenticationException $e) { |
74
|
2 |
|
$event->setResponse($this->entryPoint->start($request, $e)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|