<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12239] sites/trunk/common/includes/wporg-sso: Login: SSO: When you logout from a WordPress.org network, logout from all networks.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12239">12239</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12239","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-11-11 05:10:24 +0000 (Fri, 11 Nov 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Login: SSO: When you logout from a WordPress.org network, logout from all networks.

This causes a logout on wordcamp.org to log you out of wordpress.org and vice-versa.

This is needed as WordPress.org doesn't use shared Sessions with WordCamp.org, BuddyPress.org, and bbPress.org, but shares credentials and a central login screen.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkcommonincludeswporgssoclasswporgssophp">sites/trunk/common/includes/wporg-sso/class-wporg-sso.php</a></li>
<li><a href="#sitestrunkcommonincludeswporgssowppluginphp">sites/trunk/common/includes/wporg-sso/wp-plugin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkcommonincludeswporgssoclasswporgssophp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/common/includes/wporg-sso/class-wporg-sso.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/common/includes/wporg-sso/class-wporg-sso.php 2022-11-11 05:02:59 UTC (rev 12238)
+++ sites/trunk/common/includes/wporg-sso/class-wporg-sso.php   2022-11-11 05:10:24 UTC (rev 12239)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                const LOGIN_TOS_COOKIE  = 'wporg_tos_login';
</span><span class="cx" style="display: block; padding: 0 10px">                const TOS_USER_META_KEY = 'tos_revision';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * The time SSO tokens are valid. These are used for remote login/logout on
+                * non-wordpress.org domains.
+                *
+                * @var int
+                */
+               const REMOTE_TOKEN_TIMEOUT = 5;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 const VALID_HOSTS = [
</span><span class="cx" style="display: block; padding: 0 10px">                        'wordpress.org',
</span><span class="cx" style="display: block; padding: 0 10px">                        'bbpress.org',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -185,7 +193,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Tests if the passed host/domain, or URL, is part of the WordPress.org network.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @param unknown $host A domain, hostname, or URL
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param string $host A domain, hostname, or URL
</ins><span class="cx" style="display: block; padding: 0 10px">                  * @return boolean True is ok, false if not
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                protected function _is_valid_targeted_domain( $host ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,14 +209,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                return true;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $host = $this->_get_targetted_host( $host );
+
+                       return in_array( $host, self::VALID_HOSTS, true );
+               }
+
+               /**
+                * Determine the targetted hostname for a given hostname.
+                *
+                * This returns 'wordpress.org' in the case of 'login.wordpress.org'.
+                * This does NOT validate the hostname is valid for a redirect.
+                *
+                * @param string $host The hostname to process.
+                * @return string The hostname, maybe top-level, maybe not.
+                */
+               protected function _get_targetted_host( $host ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( in_array( $host, self::VALID_HOSTS, true ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                return true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         return $host;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // If not a top-level domain, shrink it down and try again.
</span><span class="cx" style="display: block; padding: 0 10px">                        $top_level_host = implode( '.', array_slice( explode( '.', $host ), -2 ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( in_array( $top_level_host, self::VALID_HOSTS, true ) ) {
+                               $host = $top_level_host;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return in_array( $top_level_host, self::VALID_HOSTS, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return $host;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span></span></pre></div>
<a id="sitestrunkcommonincludeswporgssowppluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/common/includes/wporg-sso/wp-plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/common/includes/wporg-sso/wp-plugin.php       2022-11-11 05:02:59 UTC (rev 12238)
+++ sites/trunk/common/includes/wporg-sso/wp-plugin.php 2022-11-11 05:10:24 UTC (rev 12239)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -77,6 +77,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                add_action( 'wp_login', array( $this, 'record_last_logged_in' ), 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                add_action( 'login_form_logout', array( $this, 'login_form_logout' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 add_filter( 'salt', array( $this, 'salt' ), 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! $this->is_sso_host() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -257,8 +259,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        add_filter( 'site_url', array( $this, 'login_post_url' ), 20, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">                        add_filter( 'register_url', array( $this, 'register_url' ), 20 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Maybe do a Remote SSO login
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Maybe do a Remote SSO login/logout
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->_maybe_perform_remote_login();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $this->_maybe_perform_remote_logout();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( preg_match( '!/wp-signup\.php$!', $_SERVER['REQUEST_URI'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // Note: wp-signup.php is not a physical file, and so it's matched on it's request uri.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -273,11 +276,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                return;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // Allow logout on non-dotorg hosts.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 // Allow logout to process. See self::login_form_logout()
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( isset( $_GET['action'] ) && empty( $_POST ) && 'logout' == $_GET['action'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                if ( ! preg_match( '!wordpress\.org$!', $this->host ) ) {
-                                                       return;
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         return;
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        // If on a WP login screen...
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -487,6 +488,44 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Override the logout process.
+                */
+               public function login_form_logout() {
+                       check_admin_referer( 'log-out' );
+
+                       // The entire remote-logout process isn't needed on local environments.
+                       if ( 'local' === wp_get_environment_type() ) {
+                               $sso = $this;
+                               add_filter( 'logout_redirect', function() use( $sso ) {
+                                       return $sso->sso_host_url . '/loggedout';
+                               } );
+                               return;
+                       }
+
+                       $user = wp_get_current_user();
+
+                       // Perform the logout on this network first.
+                       wp_logout();
+
+                       // Redirect back to the requested location.. the referer.. or failing that, the current sites front page after it's all done.
+                       $logout_redirect = ( wp_unslash( $_REQUEST['redirect_to'] ?? '' ) ?: wp_get_referer() ) ?: home_url( '/' );
+                       $logout_redirect = apply_filters( 'logout_redirect', $logout_redirect, $logout_redirect, $user );
+
+                       $remote_logout_url = add_query_arg(
+                               array(
+                                       'action'         => 'remote-logout',
+                                       'redirect_to'    => urlencode( $logout_redirect ),
+                                       'loggedout_on[]' => urlencode( $this->_get_targetted_host( $this->host ) ),
+                                       'sso_logout'     => urlencode( $this->_generate_remote_token( $user ) )
+                               ),
+                               $this->sso_host_url . '/wp-login.php'
+                       );
+
+                       $this->_safe_redirect( $remote_logout_url );
+                       exit;
+               }
+
+               /**
</ins><span class="cx" style="display: block; padding: 0 10px">                  * Redirects the user back to where they came from (or w.org profile)
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                public function redirect_to_source_or_profile() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -511,6 +550,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->_safe_redirect( 'https://wordpress.org/' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        exit;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                protected function _redirect_to_source_or_profile() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -526,32 +566,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $remote_token = wp_unslash( $_GET['sso_token'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! is_string( $remote_token ) || 3 !== substr_count( $remote_token, '|' ) ) {
-                               wp_die( 'Invalid token.' );
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $remote_token = $this->_validate_remote_token( $remote_token );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        list( $user_id, $sso_hash, $valid_until, $remember_me ) = explode( '|', $remote_token, 4 );
-
-                       $remote_expiration_valid = (
-                               // +/- 5s on a 5s timeout.
-                               $valid_until >= ( time() - 5 ) &&
-                               $valid_until <= ( time() + 10 )
-                       );
-
-                       $valid_remote_hash = false;
-                       $user = get_user_by( 'id', $user_id );
-                       if ( $user ) {
-                               $valid_remote_hash = hash_equals(
-                                       $this->_generate_remote_login_hash( $user, $valid_until, $remember_me ),
-                                       $sso_hash
-                               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Log the user in if successful.
+                       if ( $remote_token && $remote_token['valid'] && $remote_token['user'] ) {
+                               wp_set_current_user( $remote_token['user']->ID );
+                               wp_set_auth_cookie( $remote_token['user']->ID, (bool) $remote_token['remember_me'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( $remote_expiration_valid && $valid_remote_hash ) {
-                               wp_set_current_user( (int) $user_id );
-                               wp_set_auth_cookie( (int) $user_id, (bool) $remember_me );
-                       }
-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( isset( $_GET['redirect_to'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $redirect_to = wp_unslash( $_GET['redirect_to'] );
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -589,26 +611,98 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $redirect_host = parse_url( $redirect, PHP_URL_HOST );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $user && $this->_is_valid_targeted_domain( $redirect_host ) && ! preg_match( '!wordpress.org$!i', $redirect_host ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                $sso_token = $this->_generate_remote_token( $user );
+                               $redirect  = add_query_arg( 'sso_token', urlencode( $sso_token ), $redirect );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // Fetch auth cookie parts to find out if the user has selected 'remember me'.
-                               $auth_cookie_parts = wp_parse_auth_cookie( '', 'secure_auth' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return $redirect;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $valid_until = time() + 5; // Super short timeout.
-                               $remember_me = ! empty( $_POST['rememberme'] ) || ( $auth_cookie_parts && $auth_cookie_parts['expiration'] >= ( time() + ( 2 * DAY_IN_SECONDS ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Log out a user from all sites and networks.
+                *
+                * This works by keeping track of the domains logged out on, and redirecting the user to the next
+                * site in self::VALID_HOSTS. Each is only requested once, so the user should experience minimal
+                * redirects.
+                */
+               protected function _maybe_perform_remote_logout() {
+                       if ( empty( $_GET['sso_logout'] ) ) {
+                               return;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $hash        = $this->_generate_remote_login_hash( $user, $valid_until, $remember_me );
-                               $sso_token   = $user->ID . '|' . $hash . '|' . $valid_until . '|' . $remember_me;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $remote_token = wp_unslash( $_GET['sso_logout'] );
+                       $remote_token = $this->_validate_remote_token( $remote_token );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $redirect = add_query_arg( 'sso_token', urlencode( $sso_token ), $redirect );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! $remote_token || ! $remote_token['valid'] ) {
+                               return;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $user = $remote_token['user'];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return $redirect;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // If the matching user is logged in, log them out.
+                       // If they're logged in as someone else, that's problematic, but we ignore that intentionally.
+                       if ( is_user_logged_in() && get_current_user_id() == $user->ID ) {
+                               wp_logout();
+                       }
+
+                       // Hosts logged out on..
+                       $logged_out_on     = (array) $_REQUEST['loggedout_on'] ?? [];
+                       $logged_out_on[]   = $this->_get_targetted_host( $this->host );
+                       $need_to_logout_on = array_diff( self::VALID_HOSTS, $logged_out_on );
+
+                       // Logged out everywhere, send them over to the logout confirmation screen.
+                       if ( ! $need_to_logout_on ) {
+                               $final_url = $this->sso_host_url . '/loggedout';
+
+                               if ( ! empty( $_REQUEST['redirect_to'] ) ) {
+                                       $final_url = add_query_arg( 'redirect_to', $_REQUEST['redirect_to'], $final_url );
+                               }
+
+                               $this->_safe_redirect( $final_url );
+                               exit;
+                       }
+
+                       // Redirect on to the next host in self::VALID_HOSTS to logout from.
+                       $logout_redirect = add_query_arg(
+                               array(
+                                       'action'       => 'remote-logout',
+                                       'sso_logout'   => urlencode( $this->_generate_remote_token( $user ) ),
+                                       'redirect_to'  => urlencode( wp_unslash( $_REQUEST['redirect_to'] ?? '' ) ),
+                                       'loggedout_on' => array_values( $logged_out_on )
+                               ),
+                               'https://' . reset( $need_to_logout_on ) . '/wp-login.php'
+                       );
+
+                       $this->_safe_redirect( $logout_redirect );
+                       exit;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Generates a remote token for login/logout.
+                *
+                * @param WP_User $user The User for the token.
+                * @return string The SSO token.
+                */
+               protected function _generate_remote_token( $user ) {
+                       // Use a super-short timeout for the token. It's only going to be used once.
+                       $valid_until = time() + self::REMOTE_TOKEN_TIMEOUT;
+
+                       /*
+                        * Fetch auth cookie parts to find out if the user has selected 'remember me'.
+                        * This is only useful for login tokens, but causes no harm for loggout tokens.
+                        */
+                       $auth_cookie_parts = wp_parse_auth_cookie( '', 'secure_auth' );
+                       $remember_me       = ! empty( $_POST['rememberme'] ) || ( $auth_cookie_parts && $auth_cookie_parts['expiration'] >= ( time() + ( 2 * DAY_IN_SECONDS ) ) );
+
+                       $hash        = $this->_generate_remote_token_hash( $user, $valid_until, $remember_me );
+                       $sso_token   = $user->ID . '|' . $hash . '|' . $valid_until . '|' . $remember_me;
+
+                       return $sso_token;
+               }
+
+               /**
</ins><span class="cx" style="display: block; padding: 0 10px">                  * Generate a hash for remote-login for non-wordpress.org domains
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                protected function _generate_remote_login_hash( $user, $valid_until, $remember_me = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         protected function _generate_remote_token_hash( $user, $valid_until, $remember_me = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         // re-use the same frag that Auth cookies use to invalidate sessions.
</span><span class="cx" style="display: block; padding: 0 10px">                        $pass_frag = substr( $user->user_pass, 8, 4 );
</span><span class="cx" style="display: block; padding: 0 10px">                        $key       = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $valid_until, 'wporg_sso' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -618,6 +712,43 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * Validates a SSO token is valid.
+                *
+                * @param string $token The raw token from the URL, wp_unslash() it please.
+                * @return array If the token was valid.
+                */
+               protected function _validate_remote_token( $token ) {
+                       if ( ! is_string( $token ) || 3 !== substr_count( $token, '|' ) ) {
+                               wp_die( 'Invalid token.' );
+                       }
+
+                       list( $user_id, $sso_hash, $valid_until, $remember_me ) = explode( '|', $token, 4 );
+
+                       $expiration_valid = (
+                               // +/- 5s on a 5s timeout.
+                               $valid_until >= ( time() - self::REMOTE_TOKEN_TIMEOUT ) &&
+                               $valid_until <= ( time() + ( self::REMOTE_TOKEN_TIMEOUT * 2 ) )
+                       );
+
+                       $valid_hash = false;
+                       $user       = get_user_by( 'id', $user_id );
+                       if ( $user ) {
+                               $valid_hash = hash_equals(
+                                       $this->_generate_remote_token_hash( $user, $valid_until, $remember_me ),
+                                       $sso_hash
+                               );
+                       }
+
+                       $valid = ( $expiration_valid && $valid_hash );
+
+                       return compact(
+                               'valid',
+                               'user',
+                               'remember_me'
+                       );
+               }
+
+               /**
</ins><span class="cx" style="display: block; padding: 0 10px">                  * Add a custom salt, defined in the config.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                public function salt( $salt, $scheme ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -631,6 +762,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Hooked to 'set_auth_cookie' to provide action to the below function, as the
</span><span class="cx" style="display: block; padding: 0 10px">                 * `send_auth_cookies` filter used for the below function has no user context.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 *
+                * @see https://core.trac.wordpress.org/ticket/56971
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                public function maybe_block_auth_cookies_context_provider( $auth_cookie = null, $expire = null, $expiration = null, $user_id = null ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        static $_user_id_remember_me = false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -649,6 +782,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * Note: This action provides no context about the request, which is why the context is being
</span><span class="cx" style="display: block; padding: 0 10px">                 * provided via the 'set_auth_cookie' filter hook above.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 * @see https://core.trac.wordpress.org/ticket/56971
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                public function maybe_block_auth_cookies( $send_cookies ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $user_id = $this->maybe_block_auth_cookies_context_provider()['user_id'] ?? false;
</span></span></pre>
</div>
</div>

</body>
</html>