Making WordPress.org


Ignore:
Timestamp:
11/08/2022 03:52:54 AM (3 years ago)
Author:
dd32
Message:

Login: SSO: When performing a remote-login (ie. wordpress.org => buddypress.org) use the redirect_to as the SSO login destination instead of relying upon /wp-login.php being accessible at the root.

This opens up support for Multisite Subdirectory installs, such as WordCamp.org.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/common/includes/wporg-sso/wp-plugin.php

    r12222 r12223  
    258258            add_filter( 'register_url', array( $this, 'register_url' ), 20 );
    259259
     260            // Maybe do a Remote SSO login
     261            $this->_maybe_perform_remote_login();
     262
    260263            if ( preg_match( '!/wp-signup\.php$!', $_SERVER['REQUEST_URI'] ) ) {
    261264                // Note: wp-signup.php is not a physical file, and so it's matched on it's request uri.
     
    273276                    // Allow logout on non-dotorg hosts.
    274277                    if ( isset( $_GET['action'] ) && empty( $_POST ) && 'logout' == $_GET['action'] ) {
    275                         if ( ! preg_match( '!wordpress\.org$!', $_SERVER['HTTP_HOST'] ) ) {
     278                        if ( ! preg_match( '!wordpress\.org$!', $this->host ) ) {
    276279                            return;
    277280                        }
    278                     }
    279 
    280                     // Remote SSO login?
    281                     if ( isset( $_GET['action'] ) && 'remote-login' === $_GET['action'] && ! empty( $_GET['sso_token'] ) ) {
    282                         $this->_maybe_perform_remote_login();
    283281                    }
    284282
     
    479477                $redirect = wp_get_referer();
    480478                if (
    481                     str_starts_with( $redirect, wp_login_url() ) &&
    482                     ! str_contains( $redirect, '/wp-admin/' )
     479                    str_starts_with( $redirect, wp_login_url() ) ||
     480                    str_contains( $redirect, '/wp-admin/' )
    483481                ) {
    484482                    $redirect = home_url('/');
     
    524522         */
    525523        protected function _maybe_perform_remote_login() {
     524            if ( empty( $_GET['sso_token'] ) ) {
     525                return;
     526            }
     527
    526528            $remote_token = wp_unslash( $_GET['sso_token'] );
    527529            if ( ! is_string( $remote_token ) || 3 !== substr_count( $remote_token, '|' ) ) {
     
    552554                if ( isset( $_GET['redirect_to'] ) ) {
    553555                    $this->_safe_redirect( wp_unslash( $_GET['redirect_to'] ) );
     556
     557                } elseif ( ! str_contains( $this->script, '/wp-login.php' ) ) {
     558                    // SSO login, no redirect_url, and on a not-a-login page. Remove sso arg and redirect to self.
     559                    $this->_safe_redirect( remove_query_arg( 'sso_token' ) );
     560
    554561                } else {
    555562                    $this->_safe_redirect( home_url( '/' ) );
     563
    556564                }
     565                exit;
     566            } else {
     567                // Invalid auth, remove the query var.
     568                $this->_safe_redirect( remove_query_arg( 'sso_token' ) );
    557569                exit;
    558570            }
     
    589601                $sso_token   = $user->ID . '|' . $hash . '|' . $valid_until . '|' . $remember_me;
    590602
    591                 $redirect = add_query_arg(
    592                     array(
    593                         'action'      => 'remote-login',
    594                         'sso_token'   => urlencode( $sso_token ),
    595                         'redirect_to' => urlencode( $redirect ),
    596                     ),
    597                     'https://' . $redirect_host . '/wp-login.php' // Assume that wp-login exists and is accessible
    598                 );
     603                $redirect = add_query_arg( 'sso_token', urlencode( $sso_token ), $redirect );
    599604            }
    600605
Note: See TracChangeset for help on using the changeset viewer.