3 namespace Wallabag\ApiBundle\Security\Firewall
;
5 use Symfony\Component\HttpFoundation\Response
;
6 use Symfony\Component\HttpKernel\Event\GetResponseEvent
;
7 use Symfony\Component\Security\Http\Firewall\ListenerInterface
;
8 use Symfony\Component\Security\Core\Exception\AuthenticationException
;
9 use Symfony\Component\Security\Core\SecurityContextInterface
;
10 use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface
;
11 use Wallabag\ApiBundle\Security\Authentication\Token\WsseUserToken
;
12 use Psr\Log\LoggerInterface
;
14 class WsseListener
implements ListenerInterface
16 protected $securityContext;
17 protected $authenticationManager;
20 public function __construct(SecurityContextInterface
$securityContext, AuthenticationManagerInterface
$authenticationManager, LoggerInterface
$logger)
22 $this->securityContext
= $securityContext;
23 $this->authenticationManager
= $authenticationManager;
24 $this->logger
= $logger;
27 public function handle(GetResponseEvent
$event)
29 $request = $event->getRequest();
31 $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
32 if (!$request->headers
->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers
->get('x-wsse'), $matches)) {
36 $token = new WsseUserToken();
37 $token->setUser($matches[1]);
39 $token->digest
= $matches[2];
40 $token->nonce
= $matches[3];
41 $token->created
= $matches[4];
44 $authToken = $this->authenticationManager
->authenticate($token);
46 $this->securityContext
->setToken($authToken);
49 } catch (AuthenticationException
$failed) {
50 $failedMessage = 'WSSE Login failed for '.$token->getUsername().'. Why ? '.$failed->getMessage();
51 $this->logger
->err($failedMessage);
53 // Deny authentication with a '403 Forbidden' HTTP response
54 $response = new Response();
55 $response->setStatusCode(403);
56 $response->setContent($failedMessage);
57 $event->setResponse($response);