-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSimpleSamlAuth.class.php
539 lines (491 loc) · 16.7 KB
/
SimpleSamlAuth.class.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
* SimpleSamlAuth - LGPL 3.0 licensed
* Copyright (C) 2015 Jørn Åne
*
* SAML authentication class using SimpleSAMLphp.
*
* This class will log in users from SAML assertions received by SimpleSAMLphp.
* It does so by settings hooks in MediaWiki which override the session handling system
* and disable functionality that is redundant for federated logins.
*
* @file
* @ingroup Extensions
* @defgroup SimpleSamlAuth
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL (GNU Lesser General Public License)
* @copyright (C) 2015, Jørn Åne
* @author Jørn Åne
*/
if ( !defined( 'MEDIAWIKI' ) ) {
die( "This is a MediaWiki extension, and must be run from within MediaWiki.\n" );
}
class SimpleSamlAuth {
/** SAML Assertion Service */
protected static $as;
/** Whether $as is initialised */
private static $initialised;
/** Semaphore that will prevent any actions when set to false */
private static $armed = true;
/**
* Construct a new object and register it in $wgHooks.
* See README.md for possible values in $config.
*
* @param $config mixed[] Configuration settings for the SimpleSamlAuth extension.
*
* @return boolean
*/
private static function init() {
global $wgSamlSspRoot;
global $wgSamlAuthSource;
global $wgSessionName;
global $wgSessionsInMemcached;
global $wgSessionsInObjectCache;
if ( !self::$armed ) {
return false;
}
if ( self::$initialised ) {
return true;
}
if ( ( !isset( $wgSessionName ) || !$wgSessionName )
&& ( !isset( $wgSessionsInObjectCache ) || !$wgSessionsInObjectCache )
&& ( !isset( $wgSessionsInMemcached ) || !$wgSessionsInMemcached )
) {
ini_restore( 'session.name' );
$wgSessionName = ini_get( 'session.name' );
}
// Load the SimpleSAMLphp service
require_once rtrim( $wgSamlSspRoot, DIRECTORY_SEPARATOR ) .
DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . '_autoload.php';
self::$as = new SimpleSAML\Auth\Simple( $wgSamlAuthSource );
self::$initialised = is_object( self::$as );
return self::$initialised;
}
/**
* Will prevent any further action from this extension in the current request.
*/
private static function disarm() {
self::$armed = false;
}
/**
* Disables preferences which are redundant while using an external authentication source.
* Password change and e-mail settings are always disabled,
* Real name is only disabled if it is obtained from SAML.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
*
* @param $user User User whose preferences are being modified.
* ignored by this method because it checks the SAML assertion instead.
* @param &$preferences Preferences description array, to be fed to an HTMLForm object.
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookGetPreferences( $user, &$preferences ) {
if ( !self::init() ) {
return true;
}
global $wgSamlRequirement;
global $wgSamlRealnameAttr;
if ( $wgSamlRequirement >= SAML_LOGIN_ONLY || self::$as->isAuthenticated() ) {
unset( $preferences['password'] );
unset( $preferences['rememberpassword'] );
if ( isset( $wgSamlRealnameAttr ) ) {
unset( $preferences['realname'] );
}
unset( $preferences['emailaddress'] );
}
return true;
}
/**
* Disables special pages which are redundant while using an external authentication source.
* Password change is always disabled,
* e-mail confirmation is disabled when autoconfirm is disabled.
*
* Note: When autoMailConfirm is true, but mailAttr is invalid,
* users will have no way to confirm their e-mail address.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/SpecialPage_initList
*
* @param $pages string[] List of special pages in MediaWiki
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookSpecialPage_initList( &$pages ) {
if ( !self::init() ) {
return true;
}
global $wgSamlRequirement;
if ( $wgSamlRequirement >= SAML_LOGIN_ONLY || self::$as->isAuthenticated() ) {
unset( $pages['ChangePassword'] );
unset( $pages['PasswordReset'] );
unset( $pages['ConfirmEmail'] );
unset( $pages['ChangeEmail'] );
}
return true;
}
/**
* Hooked function, executed when the user visits the UserLogin page.
*
* If SimpleSamlAuth is configured not to allow local logons,
* a SAML assertion is required, which will most likely redirect the user.
* Otherwise, an error message is displayed explaining that the page is disabled.
*
* If SimpleSamlAuth is configured to allow local logons,
* an extra "field" is added to the logon form,
* which is a link/button which will redirect the user to SimpleSAMLphp to logon through SAML.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/UserLoginForm
*
* @param $template UserloginTemplate
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookLoginForm( &$template ) {
if ( !self::init() ) {
return true;
}
global $wgSamlRequirement;
$url = self::$as->getLoginURL( Title::newMainPage()->getFullUrl() );
if ( $wgSamlRequirement >= SAML_LOGIN_ONLY ) {
self::$as->requireAuth( array(
'ReturnTo' => Title::newMainPage()->getFullUrl(),
'KeepPost' => false,
) );
$err = wfMessage( 'simplesamlauth-pagedisabled' )->parse();
return $err;
}
if ( !self::$as->isAuthenticated() ) {
$template->set(
'extrafields',
'<a class="mw-ui-button mw-ui-constructive" href="'
. htmlentities( $url )
. '">'
. wfMessage( 'simplesamlauth-login' )->escaped()
. '</a>'
);
}
return true;
}
/**
* Hooked function, executed when the user visits the UserLogout page.
* This hook will execute the SimpleSAMLphp Single Sign Out feature,
* so that the logout is propagated to the IdP.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/UserLogout
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookUserLogout() {
if ( !self::init() ) {
return true;
}
global $wgSamlPostLogoutRedirect;
global $wgRequest;
if ( self::$as->isAuthenticated() ) {
$returnTo = $wgRequest->getVal( 'returnto' );
if ( isset( $wgSamlPostLogoutRedirect ) ) {
self::$as->logout( $wgSamlPostLogoutRedirect );
} elseif ( $returnTo ) {
$page = Title::newFromText( $returnTo );
if ( isset( $page ) ) {
self::$as->logout( $page->getFullUrl() );
}
} else {
self::$as->logout();
}
}
return true;
}
/**
* Hooked function, if a SAML assertion exist,
* log in the corresponding MediaWiki user or logout from SAML.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/UserLoadFromSession
*
* @param $user User MediaWiki User object
* @param $result boolean a user is logged in
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookLoadSession( $user, &$result ) {
if ( !self::init() ) {
return true;
}
global $wgSamlRequirement;
global $wgSamlUsernameAttr;
global $wgBlockDisablesLogin;
global $wgContLang;
if ( $result ) {
// Another hook already logged in
self::disarm();
return true;
}
if ( $wgSamlRequirement >= SAML_REQUIRED ) {
self::$as->requireAuth();
}
if ( self::$as->isAuthenticated() ) {
$attr = self::$as->getAttributes();
if ( !User::isUsableName( $wgContLang->ucfirst( reset( $attr[$wgSamlUsernameAttr] ) ) ) ) {
return 'Illegal username: ' . reset( $attr[$wgSamlUsernameAttr] );
}
$loadError = self::loadUser( $user, $attr );
if ( $loadError ) return $loadError;
if ( $wgBlockDisablesLogin && $user->isBlocked() ) {
$block = $user->getBlock();
throw new UserBlockedError( $block );
} else {
// Set that we authenticated a user
$result = true;
// Some MediaWiki internals need a session
// to function. Since we authenticated
// from the outside, the MediaWiki session
// might not have been initialized.
if ( session_id() == '' ) {
wfSetupSession();
}
// Apparently, nothing went wrong, and we
// have a fancy user from a SAML assertion.
// Success! Return true for no errors.
return true;
}
}
// Not authenticated, but no errors either
// Return means success, $result is still false
return true;
}
/**
* Replace the MediaWiki login/logout links with direct links to SimpleSAMLphp.
* This takes away the need to set up a redirect on the special UserLogin and UserLogout pages,
* and as a side effect makes redirects after login/logout more predictable.
*
* @link http://www.mediawiki.org/wiki/Manual:Hooks/PersonalUrls
*
* @param &$personal_urls array the array of URLs set up so far
* @param Title $title the Title object of the current article
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookPersonalUrls( array &$personal_urls, Title $title ) {
if ( !self::init() ) {
return true;
}
global $wgSamlRequirement;
global $wgSamlPostLogoutRedirect;
global $wgRequest;
if ( $wgSamlRequirement >= SAML_LOGIN_ONLY || self::$as->isAuthenticated() ) {
if ( isset( $personal_urls['logout'] ) ) {
if ( isset( $wgSamlPostLogoutRedirect ) ) {
$personal_urls['logout']['href'] =
self::$as->getLogoutURL( $wgSamlPostLogoutRedirect );
} elseif ( $wgSamlRequirement >= SAML_REQUIRED ) {
$personal_urls['logout']['href'] =
self::$as->getLogoutURL(
self::$as->getLoginURL( Title::newMainPage()->getFullUrl() )
);
} else {
$personal_urls['logout']['href'] =
self::$as->getLogoutURL( $personal_urls['logout']['href'] );
}
}
if ( !self::$as->isAuthenticated() ) {
$returnTo = $wgRequest->getVal( 'returnto' );
foreach ( array( 'login', 'anonlogin' ) as $link ) {
if ( isset( $personal_urls[$link] ) ) {
if ( $returnTo && Title::newFromText( $returnTo ) ) {
$page = Title::newFromTextThrow( $returnTo );
$url = $page->getFullUrl();
$personal_urls[$link]['href'] = self::$as->getLoginURL( $url );
} elseif ( $title->isSpecial( 'Userlogout' ) ) {
$personal_urls[$link]['href'] = self::$as->getLoginURL(
Title::newMainPage()->getFullUrl()
);
} else {
$personal_urls[$link]['href'] = self::$as->getLoginURL();
}
}
}
}
}
return true;
}
/**
* Use this to do something completely different, after the basic globals have been set up,
* but before ordinary actions take place.
*
* Takes control of the session before a stray SubmitAction calls wfSetupSession() for us.
* This is a bug in MediaWiki which has not been fixed yet.
*
* @link https://bugzilla.wikimedia.org/show_bug.cgi?id=65493
* @link http://www.mediawiki.org/wiki/Manual:Hooks/MediaWikiPerformAction
*
* @param object $output $wgOut
* @param object $article $wgArticle
* @param object $title $wgTitle
* @param object $user $wgUser
* @param object $request $wgRequest
* @param object $wiki MediaWiki object, added in 1.13
*
* @return boolean|string true on success, false on silent error, string on verbose error
*/
public static function hookMediaWikiPerformAction( $output, $article, $title, $user, $req, $w ) {
// Just running init will set the correct session cookie name.
// This will prevent the session being initiated
// with the wrong cookie name.
self::init();
return true;
}
/**
* Called to determine the class to handle the article rendering, based on title
*
* Reads the requested title. If the title matches any title mentioned in $wgWhitelistRead,
* the value of $wgSamlRequirement will be lowered to be SAML_LOGIN_ONLY at most.
*
* The effect of this, is that the site admin can use SAML_REQUIRED but still open some
* pages to be queried by anonymous users. This may be useful for allowing bots to read
* pages, for example.
*
* This hook is only called for articles, so it is not possible to whitelist special pages
* this way.
*
* @link https://www.mediawiki.org/wiki/Manual:Hooks/ArticleFromTitle
* @link https://www.mediawiki.org/wiki/Manual:$wgWhitelistRead
*/
public static function hookArticleFromTitle( &$title, &$article, $context ) {
if ( !self::init() ) {
return true;
}
global $wgWhitelistRead;
global $wgSamlRequirement;
if ( is_array( $wgWhitelistRead ) && in_array( $title, $wgWhitelistRead ) ) {
$wgSamlRequirement = min( $wgSamlRequirement, SAML_LOGIN_ONLY );
}
return true;
}
/**
* Return a user object that corresponds to the current SAML assertion.
* If no SAML assertion is set, the function returns null.
* If the user doesn't exist, and auto create has been turned on in the config,
* the user is created.
*
* If realnameAttr and/or mailAttr and/or groupMap are set in the config,
* these attributes are synchronised to the MediaWiki user.
* This also happens if the user already exists.
*
* @param $user MediaWiki user that will be made to correspond to the SAML assertion
* @param $attr string[][] SAML attributes
*
* @return void $user is modified upon return
*/
protected static function loadUser( User $user, $attr ) {
global $wgSamlCreateUser;
global $wgSamlUsernameAttr;
global $wgContLang;
$username = $wgContLang->ucfirst( reset( $attr[$wgSamlUsernameAttr] ) );
$id = User::idFromName( $username );
if ( $id || $wgSamlCreateUser ) {
if ( $id ) {
$user->setId( $id );
$user->loadFromId();
} else {
$user->setName( $username );
}
self::updateUser( $user, $attr );
self::setGroups( $user );
} else {
return 'User "'
. htmlentities( reset( $attr[$wgSamlUsernameAttr] ) )
. "\" does not exist and \"\$wgSamlCreateUser\" flag is false.\n";
}
}
/**
* Set users' fields from SAML attributes.
* If the user does not exist in the MediaWiki database,
* it is created. wgSamlCreateUser is not respected.
*
* @param User $user the user
* @param string[][] $attr SAML attributes
*/
protected static function updateUser( User $user, $attr ) {
global $wgSamlRealnameAttr;
global $wgSamlUsernameAttr;
global $wgSamlMailAttr;
global $wgContLang;
global $wgVersion;
$changed = false;
if ( isset( $wgSamlRealnameAttr )
&& isset( $attr[$wgSamlRealnameAttr] )
&& $attr[$wgSamlRealnameAttr]
&& $user->getRealName() !== reset( $attr[$wgSamlRealnameAttr] )
) {
$changed = true;
$user->setRealName( reset( $attr[$wgSamlRealnameAttr] ) );
}
if ( isset( $wgSamlMailAttr )
&& isset( $attr[$wgSamlMailAttr] )
&& $attr[$wgSamlMailAttr]
&& (
!$user->isEmailConfirmed()
|| $user->getEmail() !== reset( $attr[$wgSamlMailAttr] )
)
) {
$changed = true;
$user->setEmail( reset( $attr[$wgSamlMailAttr] ) );
$user->confirmEmail();
}
if ( !$user->getId() ) {
$user->setName( $wgContLang->ucfirst( reset( $attr[$wgSamlUsernameAttr] ) ) );
if ( version_compare( $wgVersion, '1.26', '<=' ) ) {
// MW 1.26 and below uses AuthPlugin, which wants setPassword first
$user->setInternalPassword( null ); // prevent manual login until reset
$user->addToDatabase();
} else {
// MW 1.27 and up uses AuthManager, which wants addToDatabase first
$user->addToDatabase();
$user->setInternalPassword( null ); // prevent manual login until reset
}
} elseif ( $changed ) {
$user->saveSettings();
}
}
/**
* Add groups based on the existence of attributes in the SAML assertion.
*
* @param User $user add MediaWiki permissions to this user from the current SAML assertion
*
* @return void $user is modified on return
*
* @note $wgSamlGroupMap is in the form:
* $wgSamlGroupMap = [
* 'mediawikigroup' => [
* 'samlAttrName' => ['acceptable','saml','values'],
* ]
* ]
*/
protected static function setGroups( User $user ) {
global $wgSamlGroupMap;
$allSamlAttrs = self::$as->getAttributes();
foreach ( $wgSamlGroupMap as $mediawikiGroup => $rules ) {
foreach ( $rules as $samlAttrName => $okValues ) {
if ( ! isset( $allSamlAttrs[ $samlAttrName ] ) ) {
continue;
}
// A SAML attribute could contain a list of values. Likewise,
// we may want to specify a list of values that are acceptable
// for that attribute. Thus, we have two lists and if there is
// any intersection between them then the group should be
// applied. If there is no intersection the group should be
// removed.
$intersections = array_intersect( $okValues, $allSamlAttrs[ $samlAttrName ] );
if ( count( $intersections ) > 0 ) {
$user->addGroup( $mediawikiGroup );
// User allowed into group. Break out of this foreach and
// proceed to the next mediawikiGroup
break;
}
else {
$user->removeGroup( $mediawikiGroup );
}
}
}
}
}