forked from humanmade/wp-simple-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-simple-saml.php
More file actions
440 lines (368 loc) · 11.5 KB
/
Copy pathwp-simple-saml.php
File metadata and controls
440 lines (368 loc) · 11.5 KB
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
<?php
/**
* Plugin Name: WP Simple SAML
* Description: Integrate SAML 2.0 IDP without the hassle
* Author: Shady Sharaf, Human Made
* Version: 0.1
* Author URI: http://hmn.md
* Text Domain: wp-simple-saml
* Domain Path: /language/
*
* Copyright 2017 Shady Sharaf, Human Made
*
* GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package wp-simple-saml
*/
namespace HumanMade\SimpleSaml;
use OneLogin\Saml2\Auth;
use OneLogin\Saml2\Error;
use OneLogin\Saml2\Utils;
use OneLogin\Saml2\ValidationError;
use WP_Error;
use WP_User;
require_once __DIR__ . '/vendor/autoload.php';
/**
* Get an instance of SAML2 Auth object
*
* @return False|Auth
* @throws Error False.
*/
function instance() {
static $instance;
if ( ! empty( $instance ) ) {
return $instance;
}
$config = apply_filters( 'wpsimplesaml_config', array() );
if ( empty( $config ) ) {
return false;
}
$instance = new Auth( $config );
return $instance;
}
/**
* Intercept login requests
*
* @action login_init
*
* @param string $action The action being intercepted.
* @param string $redirect Redirect URL.
*
* @throws Error|ValidationError SSO Settings, User error.
*/
function intercept( string $action = 'login', string $redirect = '' ) {
// If we have no valid instance, bail completely.
if ( ! instance() ) {
wp_die( 'Invalid SSO settings. Contact your administrator.' );
}
// WPCS: input var okay.
if ( isset( $_POST['RelayState'] ) && Utils::getSelfURL() !== $_POST['RelayState'] ) {
// WPCS: input var okay.
$redirect = $_POST['RelayState'];
}
if ( $redirect ) {
$redirect = urldecode( $redirect );
}
if ( $redirect && ! filter_var( $redirect, FILTER_VALIDATE_URL ) ) {
wp_die( 'Invalid SSO Redirection URL. Contact your administrator.' );
}
if ( empty( $redirect ) ) {
$redirect = admin_url();
}
$is_subdirectory_install = is_multisite() && ! is_subdomain_install();
if ( is_user_logged_in()
// Valid action ?.
&& in_array( $action, array( 'login', 'verify' ), true )
// Subdirectory installs share cookie domains, so user is already logged in, but we still need to verify SSO token.
&& ! ( 'verify' === $action && $is_subdirectory_install )
// If we have not been passed a valid URL, there is nothing to be done.
&& filter_var( $redirect, FILTER_VALIDATE_URL )
) {
// Are we going to an internal link ?.
if ( get_current_blog_id() === get_blog_id( $redirect ) ) {
// Home sweet home!.
redirect( $redirect );
} else { // Else we're trying to login to another environment.
cross_site_sso_redirect( $redirect );
}
} elseif ( 'login' === $action ) {
login( $redirect );
} elseif ( 'verify' === $action && ! empty( $_POST['SAMLResponse'] ) ) {
// We'veo got a SAML response to parse already.
// WPCS: input var okay.
$user = verify();
if ( is_a( $user, WP_User::class ) ) {
// Set authentication cookie.
signon( $user );
// If we're authenticating to another site.
if ( get_current_blog_id() === get_blog_id( $redirect ) ) {
redirect( $redirect );
} else {
cross_site_sso_redirect( $redirect );
}
} elseif ( is_wp_error( $user ) ) {
wp_die( 'Error creating a new user. Contact your administrator. Message: ' . $user->get_error_message() );
} else {
wp_die( 'Invalid SSO response. Contact your administrator, or stop playing around, we can see you!' );
}
} else {
wp_safe_redirect( home_url(), 404 );
exit;
}
}
/**
* Initiate IDP login.
*
* @param string $redirect Redirect to after login.
*
* @throws Error Insance error.
*/
function login( string $redirect = '' ) {
instance()->login( $redirect );
}
/**
* Handle authentication responses.
*
* @return WP_User|WP_Error
* @throws Error|ValidationError WordPress error invalid email or failed parse the authentication response.
*/
function verify() {
$saml = instance();
$saml->processResponse();
if ( ! empty( $saml->getErrors() ) ) {
$errors = implode( ', ', $saml->getErrors() );
wp_die(
sprintf(
'Error: Could not parse the authentication response, '
. 'please forward this error to your administrator: "%s", last error reason: "%s"',
esc_html( $errors ),
esc_html( $saml->getLastErrorReason() )
)
);
}
if ( ! $saml->isAuthenticated() ) {
wp_die( "Error: Authentication wasn't completed successfully." );
}
// Assumes the email is the unique identifier set in SAML IDP.
$email = filter_var( $saml->getNameId(), FILTER_VALIDATE_EMAIL );
if ( ! $email ) {
return new WP_Error( 'invalid_email', 'Error: Invalid email passed. Contact your administrator.' );
}
$attrs = array(
'email' => $email,
'attributes' => $saml->getAttributes(),
);
return settle( null, $attrs );
}
/**
* Create a user and/or update his role based on SAML response.
*
* @param string $user_id UserID of user logging in.
* @param array $attributes Array of User attribites from IDP.
*
* @return WP_User|WP_Error
*/
function settle( $user_id, array $attributes = array() ): mixed {
// Does this user_id exist ?.
if ( ! empty( $user_id ) ) {
$user = get_user_by( 'ID', $user_id );
}
// Is this email registered by an existing user ?.
if ( empty( $user ) ) {
$user = get_user_by( 'email', $attributes['email'] );
}
// No user yet ? lets create a new one.
if ( empty( $user ) ) {
$saml_attrs = $attributes['attributes'];
$first_name = isset( $saml_attrs['fname'] ) && is_array( $saml_attrs['fname'] )
? reset( $saml_attrs['fname'] )
: '';
$last_name = isset( $saml_attrs['lname'] ) && is_array( $saml_attrs['lname'] )
? reset( $saml_attrs['lname'] )
: '';
$user_data = array(
'ID' => $user_id,
'user_login' => $attributes['email'],
'user_pass' => wp_generate_password(),
'user_nicename' => implode( ' ', array_filter( array( $first_name, $last_name ) ) ),
'first_name' => $first_name,
'last_name' => $last_name,
'user_email' => $attributes['email'],
);
$user_id = wp_insert_user( $user_data );
if ( is_wp_error( $user_id ) ) {
return $user_id;
}
$user = get_user_by( 'ID', $user_id );
}
/**
* Filter the role to apply for the new user.
*/
$role = apply_filters( 'wpsimplesaml_map_role', get_option( 'default_role' ), $attributes, $user_id );
// For some reason get_current_blog_id doesn't return the proper value at this stage.
$url = ( $_SERVER['REQUEST_SCHEME'] ?? 'https' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$blog_id = get_blog_id( trailingslashit( $url ) );
if ( $blog_id && get_current_blog_id() !== $blog_id ) {
$user->for_site( $blog_id );
}
// Assign the selected role, if not already assigned.
if ( ! in_array( $role, $user->roles, true ) ) {
$user->set_role( $role );
}
return $user;
}
/**
* Sign in the user, and store the auth cookie.
*
* @param WP_User $user User Object.
*/
function signon( WP_User $user ) {
wp_set_auth_cookie( $user->ID, true, is_ssl() );
}
/**
* Pass SSO token to a subsite to authenticate a user.
*
* @param string $url The url to redirect to after login.
*/
function cross_site_sso_redirect( string $url ) {
if ( ! apply_filters( 'wpsimplesaml_allowed_hosts', false, wp_parse_url( $url, PHP_URL_HOST ), $url ) ) {
wp_die( 'This is not an allowed Cross-network SSO site.' );
}
// Workaround for sub-directory installs, as we usually redirect to admin urls.
if ( false !== strpos( $url, '/wp-admin' ) ) {
$sso_url = preg_replace( '#/wp-admin/?.*#', '/sso/verify', $url );
} else {
$sso_url = str_replace( wp_parse_url( $url, PHP_URL_PATH ), '/sso/verify', $url );
}
?>
<form action="<?php echo esc_url( $sso_url ); ?>" method="post" id="sso_form">
<input type="hidden" name="SAMLResponse" value="<?php echo esc_attr( $_POST['SAMLResponse'] ); ?>">
<input type="hidden" name="RelayState" value="<?php echo esc_attr( $_POST['RelayState'] ); ?>">
</form>
<script>
setTimeout(function () {
document.getElementById('sso_form').submit();
}, 100);
</script>
<?php
}
/**
* Get blog ID of the passed URL, if it is on the same network.
*
* @param string $url Url Site to return blogID.
*
* @return int Blog ID if found, 0 if not.
*/
function get_blog_id( $url ): int {
$fragments = wp_parse_url( $url );
if ( empty( $fragments ) || empty( $fragments['host'] ) ) {
return 0;
}
$site = get_site_by_path( $fragments['host'], $fragments['path'] );
$blog_id = 0;
if ( $site ) {
$blog_id = $site->blog_id;
}
return absint( $blog_id );
}
/**
* Redirect to a URL OR Admin dashboard.
*
* @param string $url URL to redirect to.
*/
function redirect( string $url = '' ) {
wp_safe_redirect( $url );
exit;
}
/**
* Register SSO endpoint.
*
* @action init
*/
function rewrites() {
add_rewrite_endpoint( 'sso', EP_ROOT, true );
}
add_action( 'init', __NAMESPACE__ . '\\rewrites' );
/**
* SSO Endpoint handler.
*
* @action template_redirect.
*/
function endpoint() {
// Bail if not needed.
if ( apply_filters( 'wpsimplesaml_ignore', false ) ) {
return;
}
global $wp_query;
if ( isset( $wp_query->query_vars['sso'] ) ) {
$req = ! empty( $wp_query->query_vars['sso'] ) ? $wp_query->query_vars['sso'] : 'login';
call_user_func_array( __NAMESPACE__ . '\\intercept', explode( '/', $req ) );
exit;
}
// Do not block access to SSO endpoint if blog is not public.
if ( class_exists( 'ds_more_privacy_options' ) ) {
global $ds_more_privacy_options;
remove_action( 'template_redirect', array( $ds_more_privacy_options, 'ds_users_authenticator' ) );
}
}
add_action( 'template_redirect', __NAMESPACE__ . '\\endpoint', 9 );
/**
* Go to homepage after logging out, so we don't trigger the login flow again.
*/
function go_home() {
wp_safe_redirect( home_url() );
exit;
}
add_action( 'wp_logout', __NAMESPACE__ . '\\go_home' );
/**
* Replace WordPress Login.
*
* @throws Error Login error.
*/
function authenticate_with_sso() {
// Bail if not needed.
if ( apply_filters( 'wpsimplesaml_ignore', false ) || ! apply_filters( 'wpsimplesaml_force', true ) ) {
return;
}
// WPCS: input var okay.
if ( isset( $_REQUEST['redirect_to'] ) ) {
// WPCS: input var okay.
$redirect_to = urldecode( wp_unslash( $_REQUEST['redirect_to'] ) );
} else {
$redirect_to = admin_url();
}
login( $redirect_to );
}
add_action( 'wp_authenticate', __NAMESPACE__ . '\\authenticate_with_sso' );
/**
* Show SSO login link in login form.
*
* @action login_form
*/
function login_via_sso_link() {
if ( apply_filters( 'wpsimplesaml_ignore', false ) || ! apply_filters( 'wpsimplesaml_login_link', true ) ) {
return;
}
$redirect = isset( $_GET['redirect_to'] ) ? urldecode( wp_unslash( $_GET['redirect_to'] ) ) : '';
$output = sprintf(
'<p><a href="%s" id="login-via-sso">%s</a></p>',
esc_url( site_url( 'sso/login/' ) . urlencode( $redirect ) ),
esc_html( apply_filters( 'wpsimplesaml_login_text', __( 'Login via SSO', 'wp-simple-saml' ) ) )
);
echo $output;
}
add_action( 'login_form', __NAMESPACE__ . '\\login_via_sso_link' );