-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleCAS.php
338 lines (308 loc) · 8.88 KB
/
SimpleCAS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace Bundle\SimpleCASBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session;
use Bundle\SimpleCASBundle\Adapter\Adapter;
use Bundle\SimpleCASBundle\Exception\NoUserForPrincipalException;
/**
* Client class for authenticating users against a CAS server using SimpleCAS.
*
* This is a replacement for the \SimpleCAS class that ships with the SimpleCAS
* library. It implements a friendly interface for controller actions and
* integrates with Symfony's dependency injection container.
*
* @author Jeremy Mikola <jmikola@gmail.com>
*/
class SimpleCAS
{
/**
* Session attribute for the CAS principal identifier.
*/
const UID = '__SIMPLECAS_UID';
/**
* Session attribute for login action redirection.
*/
const LOGIN_REDIRECT_URL = '__SIMPLECAS_LOGIN_REDIRECT_URL';
/**
* CAS service protocol.
*
* @var \SimpleCAS_Protocol
*/
protected $protocol;
/**
* HTTP request object.
*
* @var Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* User session service.
*
* @var Symfony\Bundle\FrameworkBundle\User
*/
protected $session;
/**
* Database adapter.
*
* @var Bundle\SimpleCASBundle\Adapter\Adapter
*/
protected $adapter;
/**
* Is user authenticated?
*
* @var boolean
*/
protected $authenticated = false;
/**
* Construct a CAS client object.
*
* If the session contains a CAS principal identifier, the current session
* will be considered authenticated.
*
* @param \SimpleCAS_Protocol $protocol
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\HttpFoundation\Session $session
* @param Bundle\SimpleCASBundle\Adapter\Adapter $adapter
* @return SimpleCAS
*/
public function __construct(\SimpleCAS_Protocol $protocol, Request $request, Session $session, Adapter $adapter = null)
{
$this->protocol = $protocol;
$this->request = $request;
$this->session = $session;
$this->adapter = $adapter;
if ($this->session->get(static::UID)) {
$this->authenticated = true;
}
}
/**
* Validate a CAS sign-on ticket.
*
* Attempt to authenticate the current session by verifying the ticket
* against the CAS server and return whether the user is now authenticated.
*
* @param string $ticket
* @return boolean
*/
public function validateTicket($ticket)
{
if ($uid = $this->protocol->validateTicket($ticket, $this->getCurrentUrl())) {
$this->authenticate($uid);
return true;
} else {
$this->unauthenticate();
return false;
}
}
/**
* Check if the current session is authenticated.
*
* @return boolean
*/
public function isAuthenticated()
{
return $this->authenticated;
}
/**
* Return the authenticated user's principal identifier.
*
* @return string
*/
public function getUid()
{
return $this->session->get(static::UID);
}
/**
* Marks the current session as authenticated for a principal identifier.
*
* This method may be used to force the authentication state of the user
* without requiring validation against the CAS server.
*
* @param string $uid Principal identifier for the authenticated user
* @return SimpleCAS
*/
public function authenticate($uid)
{
$this->session->set(static::UID, $uid);
$this->authenticated = true;
return $this;
}
/**
* Marks the current session as unauthenticated.
*
* @return SimpleCAS
*/
public function unauthenticate()
{
$this->session->remove(static::UID);
$this->authenticated = false;
return $this;
}
/**
* Return the database object for the authenticated user or null if the
* current user is not authenticated.
*
* This method will throw a BadMethodCallException if no database adapter is
* available. A NoUserForPrincipalException will be thrown if no user
* object can be found for the principal.
*
* @return object
* @throws \BadMethodCallException
* @throws Bundle\SimpleCASBundle\Exception\NoUserForPrincipalException
*/
public function getUser()
{
if (!$this->adapter) {
throw new \BadMethodCallException('SimpleCAS database adapter is not configured');
}
if ($this->authenticated) {
if ($user = $this->adapter->getUserByPrincipal($this->getUid())) {
return $user;
} else {
throw new NoUserForPrincipalException(sprintf('No user object found for principal identifier "%s"', $this->getUid()));
}
} else {
return null;
}
}
/**
* Return the database object for the authenticated user or redirect to the
* CAS server's login URL if the current user is not authenticated.
*
* A convenience method that wraps successive calls to requireLogin() and
* getUser().
*
* @return object
*/
public function requireUser()
{
return $this->requireLogin()->getUser();
}
/**
* Return the authenticated user's principal identifier or redirect to the
* CAS server's login URL if the current user is not authenticated.
*
* A convenience method that wraps successive calls to requireLogin() and
* getUid().
*
* @return string
*/
public function requireUid()
{
return $this->requireLogin()->getUid();
}
/**
* Require authentication for the current user.
*
* Redirect to the CAS server's login URL if the current user is not
* authenticated. Otherwise, return this CAS client object.
*
* @return SimpleCAS
*/
public function requireLogin()
{
if (!$this->authenticated) {
$this->redirect($this->getLoginUrl());
}
return $this;
}
/**
* Force the current user to logout if currently authenticated.
*
* Redirect to the CAS server's logout URL if the current user is
* authenticated. Otherwise, return this CAS client object.
*
* @return SimpleCAS
*/
public function requireLogout()
{
if ($this->authenticated) {
$this->unauthenticate()->redirect($this->getLogoutUrl());
}
return $this;
}
/**
* Return the CAS server's login URL.
*
* The service URL is optional and will default to the current URL.
*
* @param string $url
* @return string
*/
public function getLoginUrl($url = null)
{
return $this->protocol->getLoginURL($url ?: $this->getCurrentUrl());
}
/**
* Return the CAS server's logout URL.
*
* The service URL is optional and will default to the current URL.
*
* @param string $url
* @return string
*/
public function getLogoutUrl($url = null)
{
return $this->protocol->getLogoutURL($url ?: $this->getCurrentUrl());
}
/**
* Returns the current URL without CAS-affecting parameters.
*
* @return string
*/
public function getCurrentUrl()
{
$replacements = array(
'/\?logout/' => '',
'/&ticket=[^&]*/' => '',
'/\?ticket=[^&;]*/' => '?',
'/\?%26/' => '?',
'/\?&/' => '?',
'/\?$/' => '',
);
$uri = $this->request->getScheme() . '://' . $this->request->getHost() . $this->request->getRequestUri();
return preg_replace(array_keys($replacements), array_values($replacements), $uri);
}
/**
* Gets the login redirect URL from the session.
*
* @param mixed $default
* @return string
*/
public function getLoginRedirectUrl($default = null)
{
return $this->session->get(static::LOGIN_REDIRECT_URL, $default);
}
/**
* Sets the login redirect URL in the session.
*
* @param string $loginRedirectUrl
* @return SimpleCAS
*/
public function setLoginRedirectUrl($loginRedirectUrl)
{
$this->session->set(static::LOGIN_REDIRECT_URL, $loginRedirectUrl);
return $this;
}
/**
* Remove the login redirect URL from the session.
*
* @return SimpleCAS
*/
public function removeLoginRedirectUrl()
{
$this->session->remove(static::LOGIN_REDIRECT_URL);
return $this;
}
/**
* Redirect the client to another URL.
*
* @param string $url
*/
protected function redirect($url)
{
// TODO: Refactor once Symfony supports redirect exceptions
header('Location: ' . $url);
exit();
}
}