Completed
Pull Request — 0.3.x (#4)
by Alexandru-Daniel
04:53
created

AuthenticationFailureHandler::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Krtv\Bundle\SingleSignOnServiceProviderBundle\Authentication\Handler;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\HttpKernelInterface;
7
use Symfony\Component\HttpKernel\UriSigner;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Symfony\Component\Security\Core\Security;
10
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
11
12
/**
13
 * Class AuthenticationFailureHandler
14
 * @package Krtv\Bundle\SingleSignOnServiceProviderBundle\Authentication\Handler
15
 */
16
class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
17
{
18
    /**
19
     * @var UriSigner
20
     */
21
    private $uriSigner;
22
23
    /**
24
     * @param UriSigner $signer
25
     */
26 1
    public function setUriSigner(UriSigner $signer)
27
    {
28 1
        $this->uriSigner = $signer;
29 1
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
35
    {
36 1
        if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
37
            $this->options['failure_path'] = $failureUrl;
38
        }
39
40 1
        if (null === $this->options['failure_path']) {
41
            $this->options['failure_path'] = $this->options['login_path'];
42
        }
43
44 1
        if ($this->options['failure_forward']) {
45
            $this->log(sprintf('Forwarding to %s', $this->options['failure_path']));
46
47
            $subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
48
            $subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
49
50
            return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
51
        }
52
53 1
        $this->log(sprintf('Redirecting to %s', $this->options['failure_path']));
54
55 1
        $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
56
57 1
        $failureUrl = $this->options['failure_path'];
58 1
        $failureUrl .= strpos($failureUrl, '?') !== false ? '&' : '?';
59 1
        $failureUrl .= sprintf('_otp_failure=1&');
60 1
        $failureUrl .= sprintf('_otp_failure_time=%s', microtime(true));
61
62 1
        $failureUrl = $this->uriSigner->sign($failureUrl);
63
64 1
        return $this->httpUtils->createRedirectResponse($request, $failureUrl);
65
    }
66
67
    /**
68
     * @param string $message
69
     */
70 1
    private function log($message)
71
    {
72 1
        if (null !== $this->logger) {
73 1
            $this->logger->debug($message);
74 1
        }
75 1
    }
76
}
77