1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\ApiBundle\Security\Firewall; |
4
|
|
|
|
5
|
|
|
use Happyr\ApiBundle\Service\ResponseFactory; |
6
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
10
|
|
|
use Symfony\Component\Security\Http\Firewall\ListenerInterface; |
11
|
|
|
use Happyr\ApiBundle\Security\Authentication\Token\WsseUserToken; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Listens for incoming events and checks if they have x-wsse in the header. If not ignore, otherwise, sets up a |
15
|
|
|
* token and sends it of to validation. If validation passes, stores the token in the cache. If it fails, throw |
16
|
|
|
* an exception. |
17
|
|
|
* |
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class WsseListener implements ListenerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var TokenStorageInterface |
24
|
|
|
*/ |
25
|
|
|
protected $tokenStorage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AuthenticationManagerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $authenticationManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ResponseFactory |
34
|
|
|
*/ |
35
|
|
|
private $responseFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TokenStorageInterface $tokenStorage |
39
|
|
|
* @param AuthenticationManagerInterface $authenticationManager |
40
|
|
|
*/ |
41
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager) |
42
|
|
|
{ |
43
|
|
|
$this->tokenStorage = $tokenStorage; |
44
|
|
|
$this->authenticationManager = $authenticationManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ResponseFactory $responseFactory |
49
|
|
|
**/ |
50
|
|
|
public function setResponseFactory(ResponseFactory $responseFactory) |
51
|
|
|
{ |
52
|
|
|
$this->responseFactory = $responseFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param GetResponseEvent $event |
57
|
|
|
*/ |
58
|
|
|
public function handle(GetResponseEvent $event) |
59
|
|
|
{ |
60
|
|
|
$request = $event->getRequest(); |
61
|
|
|
|
62
|
|
|
$wsseRegex = '|UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([a-zA-Z0-9+/]+={0,2})", Created="([^"]+)"|'; |
63
|
|
|
if (!$request->headers->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) { |
64
|
|
|
// If we do not have any WSSE headers... |
65
|
|
|
$event->setResponse($this->responseFactory->createForbidden()); |
66
|
|
|
|
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$token = new WsseUserToken(); |
71
|
|
|
$token->setDigest($matches[2]) |
72
|
|
|
->setNonce($matches[3]) |
73
|
|
|
->setCreated($matches[4]) |
74
|
|
|
->setUser($matches[1]); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$authToken = $this->authenticationManager->authenticate($token); |
78
|
|
|
$this->tokenStorage->setToken($authToken); |
79
|
|
|
} catch (AuthenticationException $e) { |
80
|
|
|
$event->setResponse($this->responseFactory->createUnauthorized()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|