Completed
Push — master ( ec565a...6e1366 )
by Marcel
03:52
created

HandleSamlRequestSubscriber::onRequest()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 12.8936

Importance

Changes 0
Metric Value
cc 9
eloc 10
nc 4
nop 1
dl 0
loc 20
ccs 7
cts 11
cp 0.6364
crap 12.8936
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorToken;
6
use SchulIT\LightSamlIdpBundle\RequestStorage\RequestStorageInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10
use Symfony\Component\HttpKernel\Event\RequestEvent;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
17
/**
18
 * Listener which checks whether there is a SAMLRequest pending from before login. If so, the listener redirects to the
19
 * SSO controller in order to send the SAMLResponse (and redirect the user to the requested service).
20
 */
21
class HandleSamlRequestSubscriber implements EventSubscriberInterface {
22
23
    private $urlGenerator;
24
    private $tokenStorage;
25
    private $samlRequestStorage;
26
27 12
    public function __construct(TokenStorageInterface $tokenStorage, RequestStorageInterface $requestStorage, UrlGeneratorInterface $urlGenerator) {
28 12
        $this->tokenStorage = $tokenStorage;
29 12
        $this->samlRequestStorage = $requestStorage;
30 12
        $this->urlGenerator = $urlGenerator;
31 12
    }
32
33 12
    public function onRequest(RequestEvent $event) {
34 12
        $request = $event->getRequest();
35 12
        $route = $request->get('_route');
36
37 12
        if(!$event->isMasterRequest()) {
38
            // prevent loops
39
            return;
40
        }
41
42
        /** @var TokenInterface|null $token */
43 12
        $token = $this->tokenStorage->getToken();
44
45 12
        if($token === null || !$token->isAuthenticated() || $token instanceof AnonymousToken || $token instanceof TwoFactorToken || $route === 'idp_saml') {
46
            // prevent loops
47 12
            return;
48
        }
49
50
        if($this->samlRequestStorage->has() && $event->hasResponse() === false) {
51
            $response = new RedirectResponse($this->urlGenerator->generate('idp_saml'));
52
            $event->setResponse($response);
53
        }
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public static function getSubscribedEvents() {
60
        return [
61
            RequestEvent::class => ['onRequest', 10]
62
        ];
63
    }
64
}