Passed
Push — 4.4 ( 40bda2...4fe51e )
by Pol
02:52
created

CasGuardAuthenticator::supportsRememberMe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\CasBundle\Security;
6
7
use drupol\CasBundle\Security\Core\User\CasUserProviderInterface;
8
use drupol\psrcas\CasInterface;
9
use drupol\psrcas\Introspection\Contract\ServiceValidate;
10
use drupol\psrcas\Introspection\Introspector;
11
use drupol\psrcas\Utils\Uri;
12
use InvalidArgumentException;
13
use Psr\Http\Message\ServerRequestFactoryInterface;
14
use Psr\Http\Message\UriFactoryInterface;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
use Symfony\Component\Security\Core\User\UserProviderInterface;
22
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
23
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
24
25
/**
26
 * Class CasGuardAuthenticator.
27
 */
28
class CasGuardAuthenticator extends AbstractGuardAuthenticator implements LogoutSuccessHandlerInterface
29
{
30
    /**
31
     * The PSR CAS library.
32
     *
33
     * @var \drupol\psrcas\CasInterface
34
     */
35
    private $cas;
36
37
    /**
38
     * @var \Psr\Http\Message\ServerRequestFactoryInterface
39
     */
40
    private $serverRequestFactory;
41
42
    /**
43
     * @var \Psr\Http\Message\UriFactoryInterface
44
     */
45
    private $uriFactory;
46
47
    /**
48
     * CasGuardAuthenticator constructor.
49
     *
50
     * @param \drupol\psrcas\CasInterface $cas
51
     * @param \Psr\Http\Message\UriFactoryInterface $uriFactory
52
     * @param \Psr\Http\Message\ServerRequestFactoryInterface $serverRequestFactory
53
     */
54 10
    public function __construct(
55
        CasInterface $cas,
56
        UriFactoryInterface $uriFactory,
57
        ServerRequestFactoryInterface $serverRequestFactory
58
    ) {
59 10
        $this->cas = $cas;
60 10
        $this->uriFactory = $uriFactory;
61 10
        $this->serverRequestFactory = $serverRequestFactory;
62 10
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function checkCredentials($credentials, UserInterface $user)
68
    {
69
        try {
70 1
            $introspect = Introspector::detect($credentials);
71 1
        } catch (InvalidArgumentException $exception) {
72 1
            throw new AuthenticationException($exception->getMessage());
73
        }
74
75 1
        if (false === ($introspect instanceof ServiceValidate)) {
76 1
            throw new AuthenticationException(
77 1
                'Failure in the returned response'
78
            );
79
        }
80
81 1
        return true;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getCredentials(Request $request)
88
    {
89
        $response = $this
90
            ->cas
91
            ->requestTicketValidation();
92
93
        if (null === $response) {
94
            throw new AuthenticationException('Unable to authenticate the user with such service ticket.');
95
        }
96
97
        return $response;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function getUser($credentials, UserProviderInterface $userProvider)
104
    {
105 1
        if (false === ($userProvider instanceof CasUserProviderInterface)) {
106 1
            throw new AuthenticationException('Unable to load the user through the given User Provider.');
107
        }
108
109
        try {
110 1
            $user = $userProvider->loadUserByResponse($credentials);
111 1
        } catch (AuthenticationException $exception) {
112 1
            throw $exception;
113
        }
114
115 1
        return $user;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
122
    {
123 1
        if (true === $request->query->has('ticket')) {
124
            // Remove the ticket parameter.
125 1
            $uri = Uri::removeParams(
126 1
                $this->uriFactory->createUri(
127 1
                    $request->getUri()
128
                ),
129 1
                'ticket'
130
            );
131
132
            // Add the renew parameter to force login again.
133 1
            $uri = Uri::withParam($uri, 'renew', 'true');
134
135 1
            return new RedirectResponse((string) $uri);
136
        }
137 1
    }
138
139
    /**
140
     * @param Request $request
141
     * @param TokenInterface $token
142
     * @param string $providerKey
143
     *
144
     * @return Response|null
145
     */
146 1
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
147
    {
148 1
        return new RedirectResponse(
149 1
            (string) Uri::removeParams(
150 1
                $this->uriFactory->createUri(
151 1
                    $request->getUri()
152
                ),
153 1
                'ticket',
154 1
                'renew'
155
            )
156
        );
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 1
    public function onLogoutSuccess(Request $request)
163
    {
164 1
        return new RedirectResponse(
165
            $this
166 1
                ->cas
167 1
                ->logout()
168 1
                ->getHeaderLine('location')
169
        );
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function start(Request $request, ?AuthenticationException $authException = null)
176
    {
177 1
        return new RedirectResponse(
178
            $this
179 1
                ->cas
180 1
                ->login()
181 1
                ->getHeaderLine('location')
182
        );
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 2
    public function supports(Request $request)
189
    {
190
        return $this
191 2
            ->cas
192 2
            ->withServerRequest(
193
                $this
194 2
                    ->serverRequestFactory
195 2
                    ->createServerRequest(
196 2
                        $request->getMethod(),
197 2
                        $request->getUri()
198
                    )
199
            )
200 2
            ->supportAuthentication();
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206 1
    public function supportsRememberMe()
207
    {
208 1
        return false;
209
    }
210
}
211