-
Notifications
You must be signed in to change notification settings - Fork 22
/
shibboleth.php
515 lines (424 loc) · 16.1 KB
/
shibboleth.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
<?php
/*
Plugin Name: Shibboleth
Plugin URI: http://wordpress.org/extend/plugins/shibboleth
Description: Easily externalize user authentication to a <a href="http://shibboleth.internet2.edu">Shibboleth</a> Service Provider
Author: Will Norris, mitcho (Michael 芳貴 Erlewine)
Version: 1.7
License: Apache 2 (http://www.apache.org/licenses/LICENSE-2.0.html)
*/
define ( 'SHIBBOLETH_PLUGIN_REVISION', preg_replace( '/\$Rev: (.+) \$/', '\\1',
'$Rev$') ); // this needs to be on a separate line so that svn:keywords can work its magic
// run activation function if new revision of plugin
$shibboleth_plugin_revision = shibboleth_get_option('shibboleth_plugin_revision');
if ($shibboleth_plugin_revision === false || SHIBBOLETH_PLUGIN_REVISION != $shibboleth_plugin_revision) {
add_action('admin_init', 'shibboleth_activate_plugin');
}
/**
* Perform automatic login. This is based on the user not being logged in,
* an active session and the option being set to true.
*/
function shibboleth_auto_login() {
$shibboleth_auto_login = shibboleth_get_option('shibboleth_auto_login');
if ( !is_user_logged_in() && shibboleth_session_active() && $shibboleth_auto_login ) {
do_action('login_form_shibboleth');
$userobj = wp_signon('', true);
if ( is_wp_error($userobj) ) {
// TODO: Proper error return.
} else {
wp_safe_redirect($_SERVER['REQUEST_URI']);
exit();
}
}
}
add_action('init', 'shibboleth_auto_login');
/**
* Activate the plugin. This registers default values for all of the
* Shibboleth options and attempts to add the appropriate mod_rewrite rules to
* WordPress's .htaccess file.
*/
function shibboleth_activate_plugin() {
if ( function_exists('switch_to_blog') ) switch_to_blog($GLOBALS['current_site']->blog_id);
shibboleth_add_option('shibboleth_login_url', get_option('home') . '/Shibboleth.sso/Login');
shibboleth_add_option('shibboleth_default_login', false);
shibboleth_add_option('shibboleth_auto_login', false);
shibboleth_add_option('shibboleth_logout_url', get_option('home') . '/Shibboleth.sso/Logout');
$headers = array(
'username' => array( 'name' => 'eppn', 'managed' => false),
'first_name' => array( 'name' => 'givenName', 'managed' => true),
'last_name' => array( 'name' => 'sn', 'managed' => true),
'nickname' => array( 'name' => 'eppn', 'managed' => true),
'display_name' => array( 'name' => 'displayName', 'managed' => true),
'email' => array( 'name' => 'mail', 'managed' => true),
);
shibboleth_add_option('shibboleth_headers', $headers);
$roles = array(
'administrator' => array(
'header' => 'entitlement',
'value' => 'urn:mace:example.edu:entitlement:wordpress:admin',
),
'author' => array(
'header' => 'affiliation',
'value' => 'faculty',
),
// TODO: this could likely do strange things if WordPress has an actual role named 'default'
'default' => 'subscriber',
);
shibboleth_add_option('shibboleth_roles', $roles);
shibboleth_add_option('shibboleth_update_roles', true);
shibboleth_insert_htaccess();
shibboleth_migrate_old_data();
shibboleth_update_option('shibboleth_plugin_revision', SHIBBOLETH_PLUGIN_REVISION);
if ( function_exists('restore_current_blog') ) restore_current_blog();
}
register_activation_hook('shibboleth/shibboleth.php', 'shibboleth_activate_plugin');
/**
* Cleanup certain plugins options on deactivation.
*/
function shibboleth_deactivate_plugin() {
shibboleth_remove_htaccess();
}
register_deactivation_hook('shibboleth/shibboleth.php', 'shibboleth_deactivate_plugin');
/**
* Migrate old data to newer formats.
*/
function shibboleth_migrate_old_data() {
// new header format, allowing each header to be marked as 'managed' individually
$managed = shibboleth_get_option('shibboleth_update_users');
$headers = shibboleth_get_option('shibboleth_headers');
$updated = false;
foreach ($headers as $key => $value) {
if ( is_string($value) ) {
$headers[$key] = array(
'name' => $value,
'managed' => $managed,
);
$updated = true;
}
}
if ( $updated ) {
shibboleth_update_option('shibboleth_headers', $headers);
}
shibboleth_delete_option('shibboleth_update_users');
}
/**
* Load Shibboleth admin hooks only on admin page loads.
*
* 'admin_init' is actually called *after* 'admin_menu', so we have to hook in
* to the 'init' action for this.
*/
function shibboleth_admin_hooks() {
if ( defined('WP_ADMIN') && WP_ADMIN === true ) {
require_once dirname(__FILE__) . '/options-admin.php';
require_once dirname(__FILE__) . '/options-user.php';
}
}
add_action('init', 'shibboleth_admin_hooks');
/**
* Check if a Shibboleth session is active.
*
* @return boolean if session is active
* @uses apply_filters calls 'shibboleth_session_active' before returning final result
*/
function shibboleth_session_active() {
$active = false;
$session_headers = array('Shib-Session-ID', 'Shib_Session_ID', 'HTTP_SHIB_IDENTITY_PROVIDER');
foreach ($session_headers as $header) {
if ( array_key_exists($header, $_SERVER) && !empty($_SERVER[$header]) ) {
$active = true;
break;
}
}
$active = apply_filters('shibboleth_session_active', $active);
return $active;
}
/**
* Authenticate the user using Shibboleth. If a Shibboleth session is active,
* use the data provided by Shibboleth to log the user in. If a Shibboleth
* session is not active, redirect the user to the Shibboleth Session Initiator
* URL to initiate the session.
*/
function shibboleth_authenticate($user, $username, $password) {
if ( shibboleth_session_active() ) {
return shibboleth_authenticate_user();
} else {
$initiator_url = shibboleth_session_initiator_url( $_REQUEST['redirect_to'] );
wp_redirect($initiator_url);
exit;
}
}
/**
* When wp-login.php is loaded with 'action=shibboleth', hook Shibboleth
* into the WordPress authentication flow.
*/
function shibboleth_login_form_shibboleth() {
add_filter('authenticate', 'shibboleth_authenticate', 10, 3);
}
add_action('login_form_shibboleth', 'shibboleth_login_form_shibboleth');
/**
* If a Shibboleth user requests a password reset, and the Shibboleth password
* reset URL is set, redirect the user there.
*/
function shibboleth_retrieve_password( $user_login ) {
$password_reset_url = shibboleth_get_option('shibboleth_password_reset_url');
if ( !empty($password_reset_url) ) {
$user = get_userdatabylogin($user_login);
if ( $user && get_usermeta($user->ID, 'shibboleth_account') ) {
wp_redirect($password_reset_url);
exit;
}
}
}
add_action('retrieve_password', 'shibboleth_retrieve_password');
/**
* If Shibboleth is the default login method, add 'action=shibboleth' to the
* WordPress login URL.
*/
function shibboleth_login_url($login_url) {
if ( shibboleth_get_option('shibboleth_default_login') ) {
$login_url = add_query_arg('action', 'shibboleth', $login_url);
}
return $login_url;
}
add_filter('login_url', 'shibboleth_login_url');
/**
* If the Shibboleth logout URL is set and the user has an active Shibboleth
* session, log the user out of Shibboleth after logging them out of WordPress.
*/
function shibboleth_logout() {
$logout_url = shibboleth_get_option('shibboleth_logout_url');
if ( !empty($logout_url) && shibboleth_session_active() ) {
wp_redirect($logout_url);
exit;
}
}
add_action('wp_logout', 'shibboleth_logout', 20);
/**
* Generate the URL to initiate Shibboleth login.
*
* @param string $redirect the final URL to redirect the user to after all login is complete
* @return the URL to direct the user to in order to initiate Shibboleth login
* @uses apply_filters() Calls 'shibboleth_session_initiator_url' before returning session intiator URL
*/
function shibboleth_session_initiator_url($redirect = null) {
// first build the target URL. This is the WordPress URL the user will be returned to after Shibboleth
// is done, and will handle actually logging the user into WordPress using the data provdied by Shibboleth
if ( function_exists('switch_to_blog') ) switch_to_blog($GLOBALS['current_site']->blog_id);
$target = site_url('wp-login.php');
if ( function_exists('restore_current_blog') ) restore_current_blog();
$target = add_query_arg('action', 'shibboleth', $target);
if ( !empty($redirect) ) {
$target = add_query_arg('redirect_to', urlencode($redirect), $target);
}
// now build the Shibboleth session initiator URL
$initiator_url = shibboleth_get_option('shibboleth_login_url');
$initiator_url = add_query_arg('target', urlencode($target), $initiator_url);
$initiator_url = apply_filters('shibboleth_session_initiator_url', $initiator_url);
return $initiator_url;
}
/**
* Authenticate the user based on the current Shibboleth headers.
*
* If the data available does not map to a WordPress role (based on the
* configured role-mapping), the user will not be allowed to login.
*
* If this is the first time we've seen this user (based on the username
* attribute), a new account will be created.
*
* Known users will have their profile data updated based on the Shibboleth
* data present if the plugin is configured to do so.
*
* @return WP_User|WP_Error authenticated user or error if unable to authenticate
*/
function shibboleth_authenticate_user() {
$shib_headers = shibboleth_get_option('shibboleth_headers');
// ensure user is authorized to login
$user_role = shibboleth_get_user_role();
if ( empty($user_role) ) {
return new WP_Error('no_access', __('You do not have sufficient access.'));
}
$username = $_SERVER[$shib_headers['username']['name']];
$user = new WP_User($username);
if ( $user->ID ) {
if ( !get_usermeta($user->ID, 'shibboleth_account') ) {
// TODO: what happens if non-shibboleth account by this name already exists?
//return new WP_Error('invalid_username', __('Account already exists by this name.'));
}
}
// create account if new user
if ( !$user->ID ) {
$user = shibboleth_create_new_user($username);
}
if ( !$user->ID ) {
$error_message = 'Unable to create account based on data provided.';
if (defined('WP_DEBUG') && WP_DEBUG) {
$error_message .= '<!-- ' . print_r($_SERVER, true) . ' -->';
}
return new WP_Error('missing_data', $error_message);
}
// update user data
update_usermeta($user->ID, 'shibboleth_account', true);
shibboleth_update_user_data($user->ID);
if ( shibboleth_get_option('shibboleth_update_roles') ) {
$user->set_role($user_role);
do_action( 'shibboleth_set_user_roles', $user );
}
return $user;
}
/**
* Create a new WordPress user account, and mark it as a Shibboleth account.
*
* @param string $user_login login name for the new user
* @return object WP_User object for newly created user
*/
function shibboleth_create_new_user($user_login) {
if ( empty($user_login) ) return null;
// create account and flag as a shibboleth acount
require_once( ABSPATH . WPINC . '/registration.php' );
$user_id = wp_insert_user(array('user_login'=>$user_login));
$user = new WP_User($user_id);
update_usermeta($user->ID, 'shibboleth_account', true);
// always update user data and role on account creation
shibboleth_update_user_data($user->ID, true);
$user_role = shibboleth_get_user_role();
$user->set_role($user_role);
do_action( 'shibboleth_set_user_roles', $user );
return $user;
}
/**
* Get the role the current user should have. This is determined by the role
* mapping configured for the plugin, and the Shibboleth headers present at the
* time of login.
*
* @return string the role the current user should have
* @uses apply_filters() Calls 'shibboleth_roles' after retrieving shibboleth_roles array
* @uses apply_filters() Calls 'shibboleth_user_role' before returning final user role
*/
function shibboleth_get_user_role() {
global $wp_roles;
if ( !$wp_roles ) $wp_roles = new WP_Roles();
$shib_roles = apply_filters('shibboleth_roles', shibboleth_get_option('shibboleth_roles'));
$user_role = $shib_roles['default'];
foreach ( $wp_roles->role_names as $key => $name ) {
$role_header = $shib_roles[$key]['header'];
$role_value = $shib_roles[$key]['value'];
if ( empty($role_header) || empty($role_value) ) continue;
$values = split(';', $_SERVER[$role_header]);
if ( in_array($role_value, $values) ) {
$user_role = $key;
break;
}
}
$user_role = apply_filters('shibboleth_user_role', $user_role);
return $user_role;
}
/**
* Get the user fields that are managed by Shibboleth.
*
* @return Array user fields managed by Shibboleth
*/
function shibboleth_get_managed_user_fields() {
$headers = shibboleth_get_option('shibboleth_headers');
$managed = array();
foreach ($headers as $name => $value) {
if ( $value['managed'] ) {
$managed[] = $name;
}
}
return $managed;
}
/**
* Update the user data for the specified user based on the current Shibboleth headers. Unless
* the 'force_update' parameter is true, only the user fields marked as 'managed' fields will be
* updated.
*
* @param int $user_id ID of the user to update
* @param boolean $force_update force update of user data, regardless of 'managed' flag on fields
* @uses apply_filters() Calls 'shibboleth_user_*' before setting user attributes,
* where '*' is one of: login, nicename, first_name, last_name,
* nickname, display_name, email
*/
function shibboleth_update_user_data($user_id, $force_update = false) {
require_once( ABSPATH . WPINC . '/registration.php' );
$shib_headers = shibboleth_get_option('shibboleth_headers');
$user_fields = array(
'user_login' => 'username',
'user_nicename' => 'username',
'first_name' => 'first_name',
'last_name' => 'last_name',
'nickname' => 'nickname',
'display_name' => 'display_name',
'user_email' => 'email'
);
$user_data = array(
'ID' => $user_id,
);
foreach ($user_fields as $field => $header) {
if ( $force_update || $shib_headers[$header]['managed'] ) {
$filter = 'shibboleth_' . ( strpos($field, 'user_') === 0 ? '' : 'user_' ) . $field;
$user_data[$field] = apply_filters($filter, $_SERVER[$shib_headers[$header]['name']]);
}
}
wp_update_user($user_data);
}
/**
* Sanitize the nicename using sanitize_user
* See discussion: http://wordpress.org/support/topic/377030
*
* @since 1.4
*/
add_filter( 'shibboleth_user_nicename', 'sanitize_user' );
/**
* Add a "Login with Shibboleth" link to the WordPress login form. This link
* will be wrapped in a <p> with an id value of "shibboleth_login" so that
* deployers can style this however they choose.
*/
function shibboleth_login_form() {
$login_url = add_query_arg('action', 'shibboleth');
$login_url = remove_query_arg('reauth', $login_url);
echo '<p id="shibboleth_login"><a href="' . esc_url($login_url) . '">' . __('Login with Shibboleth', 'shibboleth') . '</a></p>';
}
add_action('login_form', 'shibboleth_login_form');
/**
* Insert directives into .htaccess file to enable Shibboleth Lazy Sessions.
*/
function shibboleth_insert_htaccess() {
if ( got_mod_rewrite() ) {
$htaccess = get_home_path() . '.htaccess';
$rules = array('AuthType shibboleth', 'Require shibboleth');
insert_with_markers($htaccess, 'Shibboleth', $rules);
}
}
/**
* Remove directives from .htaccess file to enable Shibboleth Lazy Sessions.
*/
function shibboleth_remove_htaccess() {
if ( got_mod_rewrite() ) {
$htaccess = get_home_path() . '.htaccess';
insert_with_markers($htaccess, 'Shibboleth', array());
}
}
/* Custom option functions to correctly use WPMU *_site_option functions when available. */
function shibboleth_get_option($key, $default = false ) {
return function_exists('get_site_option') ? get_site_option($key, $default) : get_option($key, $default);
}
function shibboleth_add_option($key, $value, $autoload = 'yes') {
if (function_exists('add_site_option')) {
return add_site_option($key, $value);
} else {
return add_option($key, $value, '', $autoload);
}
}
function shibboleth_update_option($key, $value) {
return function_exists('update_site_option') ? update_site_option($key, $value) : update_option($key, $value);
}
function shibboleth_delete_option($key) {
return function_exists('delete_site_option') ? delete_site_option($key) : delete_option($key);
}
/**
* Load localization files.
*/
function shibboleth_load_textdomain() {
load_plugin_textdomain('shibboleth', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/');
}
add_action('plugins_loaded', 'shibboleth_load_textdomain');