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