Completed
Pull Request — develop (#110)
by Daan van
10:17 queued 07:23
created

SamlListener::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Security\Firewall;
20
21
use Exception;
22
use Psr\Log\LoggerInterface;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Handler\AuthenticationHandler;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\SamlInteractionProvider;
25
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26
use Symfony\Component\Security\Core\Exception\AuthenticationException;
27
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
28
29
class SamlListener implements ListenerInterface
30
{
31
    /**
32
     * @var LoggerInterface
33
     */
34
    private $logger;
35
36
    /**
37
     * @var SamlInteractionProvider
38
     */
39
    private $samlInteractionProvider;
40
41
    /**
42
     * @var AuthenticationHandler
43
     */
44
    private $authenticationHandler;
45
46
    public function __construct(
47
        AuthenticationHandler $authenticationHandler,
48
        SamlInteractionProvider $samlInteractionProvider,
49
        LoggerInterface $logger
50
    ) {
51
        $this->authenticationHandler   = $authenticationHandler;
52
        $this->samlInteractionProvider = $samlInteractionProvider;
53
        $this->logger                  = $logger;
54
    }
55
56
    public function handle(GetResponseEvent $event)
57
    {
58
        try {
59
            $this->authenticationHandler->process($event);
60
        } catch (AuthenticationException $exception) {
61
            $this->samlInteractionProvider->reset();
62
63
            $this->logger->warning(sprintf(
64
                'Could not authenticate, AuthenticationException encountered: "%s"',
65
                $exception->getMessage()
66
            ));
67
68
            throw $exception;
69
        } catch (Exception $exception) {
70
            $this->samlInteractionProvider->reset();
71
72
            $this->logger->error(sprintf(
73
                'Could not authenticate, Exception of type "%s" encountered: "%s"',
74
                get_class($exception),
75
                $exception->getMessage()
76
            ));
77
78
            throw $exception;
79
        }
80
    }
81
}
82