Skip to content

Commit a3d28a9

Browse files
committed
Respect redirect_to param to wp-login.php with Azure logins
1 parent 7de6357 commit a3d28a9

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/authorizer/class-authentication.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,18 @@ function ( $entry ) {
404404
// See: https://github.com/thenetworg/oauth2-azure.
405405
session_start();
406406
try {
407+
// Save the redirect URL for WordPress so we can restore it after a
408+
// successful login (note: we can't add the redirect_to querystring
409+
// param to the redirectUri param below because it won't match the
410+
// approved URI set in the Azure portal).
411+
$login_querystring = array();
412+
if ( isset( $_SERVER['QUERY_STRING'] ) ) {
413+
parse_str( $_SERVER['QUERY_STRING'], $login_querystring ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
414+
}
415+
if ( isset( $login_querystring['redirect_to'] ) ) {
416+
$_SESSION['azure_redirect_to'] = $login_querystring['redirect_to'];
417+
}
418+
407419
$provider = new \TheNetworg\OAuth2\Client\Provider\Azure( array(
408420
'clientId' => $auth_settings['oauth2_clientid'],
409421
'clientSecret' => $auth_settings['oauth2_clientsecret'],

src/authorizer/class-wp-plugin-authorizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function __construct() {
6060
add_filter( 'login_errors', array( Login_Form::get_instance(), 'show_advanced_login_error' ) );
6161
}
6262

63+
// Redirect to wp-login.php?redirect_to=? destination after an Azure login.
64+
add_filter( 'login_redirect', array( Options\External\OAuth2::get_instance(), 'maybe_redirect_after_azure_login' ), 10, 2 );
65+
6366
// Enable localization. Translation files stored in /languages.
6467
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
6568

src/authorizer/options/external/class-oauth2.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,21 @@ public function print_text_oauth2_url_resource( $args = '' ) {
263263
<?php
264264
}
265265

266+
267+
/**
268+
* Restore any redirect_to value saved during an Azure login (in the
269+
* `authenticate` hook). This is needed since the Azure portal needs an
270+
* approved URI to visit after logging in, and cannot have a variable
271+
* redirect_to param in it like the normal WordPress redirect flow.
272+
*
273+
* @hook login_redirect
274+
*/
275+
public function maybe_redirect_after_azure_login( $redirect_to ) {
276+
if ( ! empty( $_SESSION['azure_redirect_to'] ) ) {
277+
$redirect_to = $_SESSION['azure_redirect_to'];
278+
}
279+
280+
return $redirect_to;
281+
}
282+
266283
}

0 commit comments

Comments
 (0)