| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Jetpack_SSO module main class file. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 9 |
use Automattic\Jetpack\Roles; |
| 10 |
use Automattic\Jetpack\Status; |
| 11 |
use Automattic\Jetpack\Tracking; |
| 12 |
|
| 13 |
require_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php'; |
| 14 |
require_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-notices.php'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Module Name: Secure Sign On |
| 18 |
* Module Description: Allow users to log in to this site using WordPress.com accounts |
| 19 |
* Sort Order: 30 |
| 20 |
* Recommendation Order: 5 |
| 21 |
* First Introduced: 2.6 |
| 22 |
* Requires Connection: Yes |
| 23 |
* Requires User Connection: Yes |
| 24 |
* Auto Activate: No |
| 25 |
* Module Tags: Developers |
| 26 |
* Feature: Security |
| 27 |
* Additional Search Queries: sso, single sign on, login, log in, 2fa, two-factor |
| 28 |
*/ |
| 29 |
class Jetpack_SSO { |
| 30 |
/** |
| 31 |
* Jetpack_SSO instance. |
| 32 |
* |
| 33 |
* @var Jetpack_SSO |
| 34 |
*/ |
| 35 |
public static $instance = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Jetpack_SSO constructor. |
| 39 |
*/ |
| 40 |
private function __construct() { |
| 41 |
|
| 42 |
self::$instance = $this; |
| 43 |
|
| 44 |
add_action( 'admin_init', array( $this, 'maybe_authorize_user_after_sso' ), 1 ); |
| 45 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 46 |
add_action( 'login_init', array( $this, 'login_init' ) ); |
| 47 |
add_action( 'delete_user', array( $this, 'delete_connection_for_user' ) ); |
| 48 |
add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
| 49 |
add_action( 'init', array( $this, 'maybe_logout_user' ), 5 ); |
| 50 |
add_action( 'jetpack_modules_loaded', array( $this, 'module_configure_button' ) ); |
| 51 |
add_action( 'login_form_logout', array( $this, 'store_wpcom_profile_cookies_on_logout' ) ); |
| 52 |
add_action( 'jetpack_unlinked_user', array( $this, 'delete_connection_for_user' ) ); |
| 53 |
add_action( 'jetpack_site_before_disconnected', array( static::class, 'disconnect' ) ); |
| 54 |
add_action( 'wp_login', array( 'Jetpack_SSO', 'clear_cookies_after_login' ) ); |
| 55 |
|
| 56 |
// Adding this action so that on login_init, the action won't be sanitized out of the $action global. |
| 57 |
add_action( 'login_form_jetpack-sso', '__return_true' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the single instance of the Jetpack_SSO object |
| 62 |
* |
| 63 |
* @since 2.8 |
| 64 |
* @return Jetpack_SSO |
| 65 |
**/ |
| 66 |
public static function get_instance() { |
| 67 |
if ( self::$instance !== null ) { |
| 68 |
return self::$instance; |
| 69 |
} |
| 70 |
|
| 71 |
self::$instance = new Jetpack_SSO(); |
| 72 |
return self::$instance; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add configure button and functionality to the module card on the Jetpack screen |
| 77 |
**/ |
| 78 |
public static function module_configure_button() { |
| 79 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* If jetpack_force_logout == 1 in current user meta the user will be forced |
| 84 |
* to logout and reauthenticate with the site. |
| 85 |
**/ |
| 86 |
public function maybe_logout_user() { |
| 87 |
global $current_user; |
| 88 |
|
| 89 |
if ( 1 === (int) $current_user->jetpack_force_logout ) { |
| 90 |
delete_user_meta( $current_user->ID, 'jetpack_force_logout' ); |
| 91 |
self::delete_connection_for_user( $current_user->ID ); |
| 92 |
wp_logout(); |
| 93 |
wp_safe_redirect( wp_login_url() ); |
| 94 |
exit; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Adds additional methods the WordPress xmlrpc API for handling SSO specific features |
| 100 |
* |
| 101 |
* @param array $methods API methods. |
| 102 |
* @return array |
| 103 |
**/ |
| 104 |
public function xmlrpc_methods( $methods ) { |
| 105 |
$methods['jetpack.userDisconnect'] = array( $this, 'xmlrpc_user_disconnect' ); |
| 106 |
return $methods; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Marks a user's profile for disconnect from WordPress.com and forces a logout |
| 111 |
* the next time the user visits the site. |
| 112 |
* |
| 113 |
* @param int $user_id User to disconnect from the site. |
| 114 |
**/ |
| 115 |
public function xmlrpc_user_disconnect( $user_id ) { |
| 116 |
$user_query = new WP_User_Query( |
| 117 |
array( |
| 118 |
'meta_key' => 'wpcom_user_id', |
| 119 |
'meta_value' => $user_id, |
| 120 |
) |
| 121 |
); |
| 122 |
$user = $user_query->get_results(); |
| 123 |
$user = $user[0]; |
| 124 |
|
| 125 |
if ( $user instanceof WP_User ) { |
| 126 |
$user = wp_set_current_user( $user->ID ); |
| 127 |
update_user_meta( $user->ID, 'jetpack_force_logout', '1' ); |
| 128 |
self::delete_connection_for_user( $user->ID ); |
| 129 |
return true; |
| 130 |
} |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Enqueues scripts and styles necessary for SSO login. |
| 136 |
*/ |
| 137 |
public function login_enqueue_scripts() { |
| 138 |
global $action; |
| 139 |
|
| 140 |
if ( ! Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if ( is_rtl() ) { |
| 145 |
wp_enqueue_style( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login-rtl.css', JETPACK__PLUGIN_FILE ), array( 'login', 'genericons' ), JETPACK__VERSION ); |
| 146 |
} else { |
| 147 |
wp_enqueue_style( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.css', JETPACK__PLUGIN_FILE ), array( 'login', 'genericons' ), JETPACK__VERSION ); |
| 148 |
} |
| 149 |
|
| 150 |
wp_enqueue_script( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION, false ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Adds Jetpack SSO classes to login body |
| 155 |
* |
| 156 |
* @param array $classes Array of classes to add to body tag. |
| 157 |
* @return array Array of classes to add to body tag. |
| 158 |
*/ |
| 159 |
public function login_body_class( $classes ) { |
| 160 |
global $action; |
| 161 |
|
| 162 |
if ( ! Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
| 163 |
return $classes; |
| 164 |
} |
| 165 |
|
| 166 |
// Always add the jetpack-sso class so that we can add SSO specific styling even when the SSO form isn't being displayed. |
| 167 |
$classes[] = 'jetpack-sso'; |
| 168 |
|
| 169 |
if ( ! ( new Status() )->is_staging_site() ) { |
| 170 |
/** |
| 171 |
* Should we show the SSO login form? |
| 172 |
* |
| 173 |
* $_GET['jetpack-sso-default-form'] is used to provide a fallback in case JavaScript is not enabled. |
| 174 |
* |
| 175 |
* The default_to_sso_login() method allows us to dynamically decide whether we show the SSO login form or not. |
| 176 |
* The SSO module uses the method to display the default login form if we can not find a user to log in via SSO. |
| 177 |
* But, the method could be filtered by a site admin to always show the default login form if that is preferred. |
| 178 |
*/ |
| 179 |
if ( empty( $_GET['jetpack-sso-show-default-form'] ) && Jetpack_SSO_Helpers::show_sso_login() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 180 |
$classes[] = 'jetpack-sso-form-display'; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $classes; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Inlined admin styles for SSO. |
| 189 |
*/ |
| 190 |
public function print_inline_admin_css() { |
| 191 |
?> |
| 192 |
<style> |
| 193 |
.jetpack-sso .message { |
| 194 |
margin-top: 20px; |
| 195 |
} |
| 196 |
|
| 197 |
.jetpack-sso #login .message:first-child, |
| 198 |
.jetpack-sso #login h1 + .message { |
| 199 |
margin-top: 0; |
| 200 |
} |
| 201 |
</style> |
| 202 |
<?php |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Adds settings fields to Settings > General > Secure Sign On that allows users to |
| 207 |
* turn off the login form on wp-login.php |
| 208 |
* |
| 209 |
* @since 2.7 |
| 210 |
**/ |
| 211 |
public function register_settings() { |
| 212 |
|
| 213 |
add_settings_section( |
| 214 |
'jetpack_sso_settings', |
| 215 |
__( 'Secure Sign On', 'jetpack' ), |
| 216 |
'__return_false', |
| 217 |
'jetpack-sso' |
| 218 |
); |
| 219 |
|
| 220 |
/* |
| 221 |
* Settings > General > Secure Sign On |
| 222 |
* Require two step authentication |
| 223 |
*/ |
| 224 |
register_setting( |
| 225 |
'jetpack-sso', |
| 226 |
'jetpack_sso_require_two_step', |
| 227 |
array( $this, 'validate_jetpack_sso_require_two_step' ) |
| 228 |
); |
| 229 |
|
| 230 |
add_settings_field( |
| 231 |
'jetpack_sso_require_two_step', |
| 232 |
'', // Output done in render $callback: __( 'Require Two-Step Authentication' , 'jetpack' ). |
| 233 |
array( $this, 'render_require_two_step' ), |
| 234 |
'jetpack-sso', |
| 235 |
'jetpack_sso_settings' |
| 236 |
); |
| 237 |
|
| 238 |
/* |
| 239 |
* Settings > General > Secure Sign On |
| 240 |
*/ |
| 241 |
register_setting( |
| 242 |
'jetpack-sso', |
| 243 |
'jetpack_sso_match_by_email', |
| 244 |
array( $this, 'validate_jetpack_sso_match_by_email' ) |
| 245 |
); |
| 246 |
|
| 247 |
add_settings_field( |
| 248 |
'jetpack_sso_match_by_email', |
| 249 |
'', // Output done in render $callback: __( 'Match by Email' , 'jetpack' ). |
| 250 |
array( $this, 'render_match_by_email' ), |
| 251 |
'jetpack-sso', |
| 252 |
'jetpack_sso_settings' |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Builds the display for the checkbox allowing user to require two step |
| 258 |
* auth be enabled on WordPress.com accounts before login. Displays in Settings > General |
| 259 |
* |
| 260 |
* @since 2.7 |
| 261 |
**/ |
| 262 |
public function render_require_two_step() { |
| 263 |
?> |
| 264 |
<label> |
| 265 |
<input |
| 266 |
type="checkbox" |
| 267 |
name="jetpack_sso_require_two_step" |
| 268 |
<?php checked( Jetpack_SSO_Helpers::is_two_step_required() ); ?> |
| 269 |
<?php disabled( Jetpack_SSO_Helpers::is_require_two_step_checkbox_disabled() ); ?> |
| 270 |
> |
| 271 |
<?php esc_html_e( 'Require Two-Step Authentication', 'jetpack' ); ?> |
| 272 |
</label> |
| 273 |
<?php |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Validate the require two step checkbox in Settings > General. |
| 278 |
* |
| 279 |
* @param bool $input The jetpack_sso_require_two_step option setting. |
| 280 |
* |
| 281 |
* @since 2.7 |
| 282 |
* @return boolean |
| 283 |
**/ |
| 284 |
public function validate_jetpack_sso_require_two_step( $input ) { |
| 285 |
return ( ! empty( $input ) ) ? 1 : 0; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Builds the display for the checkbox allowing the user to allow matching logins by email |
| 290 |
* Displays in Settings > General |
| 291 |
* |
| 292 |
* @since 2.9 |
| 293 |
**/ |
| 294 |
public function render_match_by_email() { |
| 295 |
?> |
| 296 |
<label> |
| 297 |
<input |
| 298 |
type="checkbox" |
| 299 |
name="jetpack_sso_match_by_email" |
| 300 |
<?php checked( Jetpack_SSO_Helpers::match_by_email() ); ?> |
| 301 |
<?php disabled( Jetpack_SSO_Helpers::is_match_by_email_checkbox_disabled() ); ?> |
| 302 |
> |
| 303 |
<?php esc_html_e( 'Match by Email', 'jetpack' ); ?> |
| 304 |
</label> |
| 305 |
<?php |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Validate the match by email check in Settings > General. |
| 310 |
* |
| 311 |
* @param bool $input The jetpack_sso_match_by_email option setting. |
| 312 |
* |
| 313 |
* @since 2.9 |
| 314 |
* @return boolean |
| 315 |
**/ |
| 316 |
public function validate_jetpack_sso_match_by_email( $input ) { |
| 317 |
return ( ! empty( $input ) ) ? 1 : 0; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Checks to determine if the user wants to login on wp-login |
| 322 |
* |
| 323 |
* This function mostly exists to cover the exceptions to login |
| 324 |
* that may exist as other parameters to $_GET[action] as $_GET[action] |
| 325 |
* does not have to exist. By default WordPress assumes login if an action |
| 326 |
* is not set, however this may not be true, as in the case of logout |
| 327 |
* where $_GET[loggedout] is instead set |
| 328 |
* |
| 329 |
* @return boolean |
| 330 |
**/ |
| 331 |
private function wants_to_login() { |
| 332 |
$wants_to_login = false; |
| 333 |
|
| 334 |
// Cover default WordPress behavior. |
| 335 |
$action = isset( $_REQUEST['action'] ) ? filter_var( wp_unslash( $_REQUEST['action'] ) ) : 'login'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 336 |
|
| 337 |
// And now the exceptions. |
| 338 |
$action = isset( $_GET['loggedout'] ) ? 'loggedout' : $action; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 339 |
|
| 340 |
if ( Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
| 341 |
$wants_to_login = true; |
| 342 |
} |
| 343 |
|
| 344 |
return $wants_to_login; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Initialization for a SSO request. |
| 349 |
*/ |
| 350 |
public function login_init() { |
| 351 |
global $action; |
| 352 |
|
| 353 |
$tracking = new Tracking(); |
| 354 |
|
| 355 |
if ( Jetpack_SSO_Helpers::should_hide_login_form() ) { |
| 356 |
/** |
| 357 |
* Since the default authenticate filters fire at priority 20 for checking username and password, |
| 358 |
* let's fire at priority 30. wp_authenticate_spam_check is fired at priority 99, but since we return a |
| 359 |
* WP_Error in disable_default_login_form, then we won't trigger spam processing logic. |
| 360 |
*/ |
| 361 |
add_filter( 'authenticate', array( 'Jetpack_SSO_Notices', 'disable_default_login_form' ), 30 ); |
| 362 |
|
| 363 |
/** |
| 364 |
* Filter the display of the disclaimer message appearing when default WordPress login form is disabled. |
| 365 |
* |
| 366 |
* @module sso |
| 367 |
* |
| 368 |
* @since 2.8.0 |
| 369 |
* |
| 370 |
* @param bool true Should the disclaimer be displayed. Default to true. |
| 371 |
*/ |
| 372 |
$display_sso_disclaimer = apply_filters( 'jetpack_sso_display_disclaimer', true ); |
| 373 |
if ( $display_sso_disclaimer ) { |
| 374 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'msg_login_by_jetpack' ) ); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if ( 'jetpack-sso' === $action ) { |
| 379 |
if ( isset( $_GET['result'], $_GET['user_id'], $_GET['sso_nonce'] ) && 'success' === $_GET['result'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 380 |
$this->handle_login(); |
| 381 |
$this->display_sso_login_form(); |
| 382 |
} elseif ( ( new Status() )->is_staging_site() ) { |
| 383 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) ); |
| 384 |
} else { |
| 385 |
// Is it wiser to just use wp_redirect than do this runaround to wp_safe_redirect? |
| 386 |
add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
| 387 |
$reauth = ! empty( $_GET['force_reauth'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 388 |
$sso_url = $this->get_sso_url_or_die( $reauth ); |
| 389 |
|
| 390 |
$tracking->record_user_event( 'sso_login_redirect_success' ); |
| 391 |
wp_safe_redirect( $sso_url ); |
| 392 |
exit; |
| 393 |
} |
| 394 |
} elseif ( Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
| 395 |
|
| 396 |
// Save cookies so we can handle redirects after SSO. |
| 397 |
$this->save_cookies(); |
| 398 |
|
| 399 |
/** |
| 400 |
* Check to see if the site admin wants to automagically forward the user |
| 401 |
* to the WordPress.com login page AND that the request to wp-login.php |
| 402 |
* is not something other than login (Like logout!) |
| 403 |
*/ |
| 404 |
if ( Jetpack_SSO_Helpers::bypass_login_forward_wpcom() && $this->wants_to_login() ) { |
| 405 |
add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
| 406 |
$reauth = ! empty( $_GET['force_reauth'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 407 |
$sso_url = $this->get_sso_url_or_die( $reauth ); |
| 408 |
$tracking->record_user_event( 'sso_login_redirect_bypass_success' ); |
| 409 |
wp_safe_redirect( $sso_url ); |
| 410 |
exit; |
| 411 |
} |
| 412 |
|
| 413 |
$this->display_sso_login_form(); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Ensures that we can get a nonce from WordPress.com via XML-RPC before setting |
| 419 |
* up the hooks required to display the SSO form. |
| 420 |
*/ |
| 421 |
public function display_sso_login_form() { |
| 422 |
add_filter( 'login_body_class', array( $this, 'login_body_class' ) ); |
| 423 |
add_action( 'login_head', array( $this, 'print_inline_admin_css' ) ); |
| 424 |
|
| 425 |
if ( ( new Status() )->is_staging_site() ) { |
| 426 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) ); |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
$sso_nonce = self::request_initial_nonce(); |
| 431 |
if ( is_wp_error( $sso_nonce ) ) { |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
add_action( 'login_form', array( $this, 'login_form' ) ); |
| 436 |
add_action( 'login_enqueue_scripts', array( $this, 'login_enqueue_scripts' ) ); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Conditionally save the redirect_to url as a cookie. |
| 441 |
* |
| 442 |
* @since 4.6.0 Renamed to save_cookies from maybe_save_redirect_cookies |
| 443 |
*/ |
| 444 |
public static function save_cookies() { |
| 445 |
if ( headers_sent() ) { |
| 446 |
return new WP_Error( 'headers_sent', __( 'Cannot deal with cookie redirects, as headers are already sent.', 'jetpack' ) ); |
| 447 |
} |
| 448 |
|
| 449 |
setcookie( |
| 450 |
'jetpack_sso_original_request', |
| 451 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sniff misses the wrapping esc_url_raw(). |
| 452 |
esc_url_raw( set_url_scheme( ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ) ), |
| 453 |
time() + HOUR_IN_SECONDS, |
| 454 |
COOKIEPATH, |
| 455 |
COOKIE_DOMAIN, |
| 456 |
is_ssl(), |
| 457 |
true |
| 458 |
); |
| 459 |
|
| 460 |
if ( ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 461 |
// If we have something to redirect to. |
| 462 |
$url = esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 463 |
setcookie( 'jetpack_sso_redirect_to', $url, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); |
| 464 |
} elseif ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
| 465 |
// Otherwise, if it's already set, purge it. |
| 466 |
setcookie( 'jetpack_sso_redirect_to', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true ); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Outputs the Jetpack SSO button and description as well as the toggle link |
| 472 |
* for switching between Jetpack SSO and default login. |
| 473 |
*/ |
| 474 |
public function login_form() { |
| 475 |
$site_name = get_bloginfo( 'name' ); |
| 476 |
if ( ! $site_name ) { |
| 477 |
$site_name = get_bloginfo( 'url' ); |
| 478 |
} |
| 479 |
|
| 480 |
$display_name = ! empty( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) |
| 481 |
? sanitize_text_field( wp_unslash( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) ) |
| 482 |
: false; |
| 483 |
$gravatar = ! empty( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) |
| 484 |
? esc_url_raw( wp_unslash( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) ) |
| 485 |
: false; |
| 486 |
|
| 487 |
?> |
| 488 |
<div id="jetpack-sso-wrap"> |
| 489 |
<?php |
| 490 |
/** |
| 491 |
* Allow extension above Jetpack's SSO form. |
| 492 |
* |
| 493 |
* @module sso |
| 494 |
* |
| 495 |
* @since 8.6.0 |
| 496 |
*/ |
| 497 |
do_action( 'jetpack_sso_login_form_above_wpcom' ); |
| 498 |
|
| 499 |
if ( $display_name && $gravatar ) : |
| 500 |
?> |
| 501 |
<div id="jetpack-sso-wrap__user"> |
| 502 |
<img width="72" height="72" src="<?php echo esc_html( $gravatar ); ?>" /> |
| 503 |
|
| 504 |
<h2> |
| 505 |
<?php |
| 506 |
echo wp_kses( |
| 507 |
/* translators: %s a user display name. */ |
| 508 |
sprintf( __( 'Log in as <span>%s</span>', 'jetpack' ), esc_html( $display_name ) ), |
| 509 |
array( 'span' => true ) |
| 510 |
); |
| 511 |
?> |
| 512 |
</h2> |
| 513 |
</div> |
| 514 |
|
| 515 |
<?php endif; ?> |
| 516 |
|
| 517 |
|
| 518 |
<div id="jetpack-sso-wrap__action"> |
| 519 |
<?php echo $this->build_sso_button( array(), 'is_primary' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping done in build_sso_button() ?> |
| 520 |
|
| 521 |
<?php if ( $display_name && $gravatar ) : ?> |
| 522 |
<a rel="nofollow" class="jetpack-sso-wrap__reauth" href="<?php echo esc_url( $this->build_sso_button_url( array( 'force_reauth' => '1' ) ) ); ?>"> |
| 523 |
<?php esc_html_e( 'Log in as a different WordPress.com user', 'jetpack' ); ?> |
| 524 |
</a> |
| 525 |
<?php else : ?> |
| 526 |
<p> |
| 527 |
<?php |
| 528 |
/** |
| 529 |
* Filter the messeage displayed below the SSO button. |
| 530 |
* |
| 531 |
* @module sso |
| 532 |
* |
| 533 |
* @since 10.3.0 |
| 534 |
* |
| 535 |
* @param string $sso_explanation Message displayed below the SSO button. |
| 536 |
*/ |
| 537 |
$sso_explanation = apply_filters( |
| 538 |
'jetpack_sso_login_form_explanation_text', |
| 539 |
sprintf( |
| 540 |
/* Translators: %s is the name of the site. */ |
| 541 |
__( 'You can now save time spent logging in by connecting your WordPress.com account to %s.', 'jetpack' ), |
| 542 |
esc_html( $site_name ) |
| 543 |
) |
| 544 |
); |
| 545 |
echo esc_html( $sso_explanation ); |
| 546 |
?> |
| 547 |
</p> |
| 548 |
<?php endif; ?> |
| 549 |
</div> |
| 550 |
|
| 551 |
<?php |
| 552 |
/** |
| 553 |
* Allow extension below Jetpack's SSO form. |
| 554 |
* |
| 555 |
* @module sso |
| 556 |
* |
| 557 |
* @since 8.6.0 |
| 558 |
*/ |
| 559 |
do_action( 'jetpack_sso_login_form_below_wpcom' ); |
| 560 |
|
| 561 |
if ( ! Jetpack_SSO_Helpers::should_hide_login_form() ) : |
| 562 |
?> |
| 563 |
<div class="jetpack-sso-or"> |
| 564 |
<span><?php esc_html_e( 'Or', 'jetpack' ); ?></span> |
| 565 |
</div> |
| 566 |
|
| 567 |
<a href="<?php echo esc_url( add_query_arg( 'jetpack-sso-show-default-form', '1' ) ); ?>" class="jetpack-sso-toggle wpcom"> |
| 568 |
<?php |
| 569 |
esc_html_e( 'Log in with username and password', 'jetpack' ) |
| 570 |
?> |
| 571 |
</a> |
| 572 |
|
| 573 |
<a href="<?php echo esc_url( add_query_arg( 'jetpack-sso-show-default-form', '0' ) ); ?>" class="jetpack-sso-toggle default"> |
| 574 |
<?php |
| 575 |
esc_html_e( 'Log in with WordPress.com', 'jetpack' ) |
| 576 |
?> |
| 577 |
</a> |
| 578 |
<?php endif; ?> |
| 579 |
</div> |
| 580 |
<?php |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* Clear the cookies that store the profile information for the last |
| 585 |
* WPCOM user to connect. |
| 586 |
*/ |
| 587 |
public static function clear_wpcom_profile_cookies() { |
| 588 |
if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) ) { |
| 589 |
setcookie( |
| 590 |
'jetpack_sso_wpcom_name_' . COOKIEHASH, |
| 591 |
' ', |
| 592 |
time() - YEAR_IN_SECONDS, |
| 593 |
COOKIEPATH, |
| 594 |
COOKIE_DOMAIN, |
| 595 |
is_ssl(), |
| 596 |
true |
| 597 |
); |
| 598 |
} |
| 599 |
|
| 600 |
if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) ) { |
| 601 |
setcookie( |
| 602 |
'jetpack_sso_wpcom_gravatar_' . COOKIEHASH, |
| 603 |
' ', |
| 604 |
time() - YEAR_IN_SECONDS, |
| 605 |
COOKIEPATH, |
| 606 |
COOKIE_DOMAIN, |
| 607 |
is_ssl(), |
| 608 |
true |
| 609 |
); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Clear cookies that are no longer needed once the user has logged in. |
| 615 |
* |
| 616 |
* @since 4.8.0 |
| 617 |
*/ |
| 618 |
public static function clear_cookies_after_login() { |
| 619 |
self::clear_wpcom_profile_cookies(); |
| 620 |
if ( isset( $_COOKIE['jetpack_sso_nonce'] ) ) { |
| 621 |
setcookie( |
| 622 |
'jetpack_sso_nonce', |
| 623 |
' ', |
| 624 |
time() - YEAR_IN_SECONDS, |
| 625 |
COOKIEPATH, |
| 626 |
COOKIE_DOMAIN, |
| 627 |
is_ssl(), |
| 628 |
true |
| 629 |
); |
| 630 |
} |
| 631 |
|
| 632 |
if ( isset( $_COOKIE['jetpack_sso_original_request'] ) ) { |
| 633 |
setcookie( |
| 634 |
'jetpack_sso_original_request', |
| 635 |
' ', |
| 636 |
time() - YEAR_IN_SECONDS, |
| 637 |
COOKIEPATH, |
| 638 |
COOKIE_DOMAIN, |
| 639 |
is_ssl(), |
| 640 |
true |
| 641 |
); |
| 642 |
} |
| 643 |
|
| 644 |
if ( isset( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
| 645 |
setcookie( |
| 646 |
'jetpack_sso_redirect_to', |
| 647 |
' ', |
| 648 |
time() - YEAR_IN_SECONDS, |
| 649 |
COOKIEPATH, |
| 650 |
COOKIE_DOMAIN, |
| 651 |
is_ssl(), |
| 652 |
true |
| 653 |
); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Clean up after Jetpack gets disconnected. |
| 659 |
* |
| 660 |
* @since 10.7 |
| 661 |
*/ |
| 662 |
public static function disconnect() { |
| 663 |
if ( Jetpack::connection()->is_user_connected() ) { |
| 664 |
static::delete_connection_for_user( get_current_user_id() ); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Remove an SSO connection for a user. |
| 670 |
* |
| 671 |
* @param int $user_id The local user id. |
| 672 |
*/ |
| 673 |
public static function delete_connection_for_user( $user_id ) { |
| 674 |
$wpcom_user_id = get_user_meta( $user_id, 'wpcom_user_id', true ); |
| 675 |
if ( ! $wpcom_user_id ) { |
| 676 |
return; |
| 677 |
} |
| 678 |
|
| 679 |
$xml = new Jetpack_IXR_Client( |
| 680 |
array( |
| 681 |
'wpcom_user_id' => $user_id, |
| 682 |
) |
| 683 |
); |
| 684 |
$xml->query( 'jetpack.sso.removeUser', $wpcom_user_id ); |
| 685 |
|
| 686 |
if ( $xml->isError() ) { |
| 687 |
return false; |
| 688 |
} |
| 689 |
|
| 690 |
// Clean up local data stored for SSO. |
| 691 |
delete_user_meta( $user_id, 'wpcom_user_id' ); |
| 692 |
delete_user_meta( $user_id, 'wpcom_user_data' ); |
| 693 |
self::clear_wpcom_profile_cookies(); |
| 694 |
|
| 695 |
return $xml->getResponse(); |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Retrieves nonce used for SSO form. |
| 700 |
*/ |
| 701 |
public static function request_initial_nonce() { |
| 702 |
$nonce = ! empty( $_COOKIE['jetpack_sso_nonce'] ) |
| 703 |
? sanitize_key( wp_unslash( $_COOKIE['jetpack_sso_nonce'] ) ) |
| 704 |
: false; |
| 705 |
|
| 706 |
if ( ! $nonce ) { |
| 707 |
$xml = new Jetpack_IXR_Client(); |
| 708 |
$xml->query( 'jetpack.sso.requestNonce' ); |
| 709 |
|
| 710 |
if ( $xml->isError() ) { |
| 711 |
return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage() ); |
| 712 |
} |
| 713 |
|
| 714 |
$nonce = sanitize_key( $xml->getResponse() ); |
| 715 |
|
| 716 |
setcookie( |
| 717 |
'jetpack_sso_nonce', |
| 718 |
$nonce, |
| 719 |
time() + ( 10 * MINUTE_IN_SECONDS ), |
| 720 |
COOKIEPATH, |
| 721 |
COOKIE_DOMAIN, |
| 722 |
is_ssl(), |
| 723 |
true |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
return $nonce; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* The function that actually handles the login! |
| 732 |
*/ |
| 733 |
public function handle_login() { |
| 734 |
$wpcom_nonce = isset( $_GET['sso_nonce'] ) ? sanitize_key( $_GET['sso_nonce'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 735 |
$wpcom_user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 736 |
|
| 737 |
$xml = new Jetpack_IXR_Client(); |
| 738 |
$xml->query( 'jetpack.sso.validateResult', $wpcom_nonce, $wpcom_user_id ); |
| 739 |
|
| 740 |
$user_data = $xml->isError() ? false : $xml->getResponse(); |
| 741 |
if ( empty( $user_data ) ) { |
| 742 |
add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' ); |
| 743 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) ); |
| 744 |
return; |
| 745 |
} |
| 746 |
|
| 747 |
$user_data = (object) $user_data; |
| 748 |
$user = null; |
| 749 |
|
| 750 |
/** |
| 751 |
* Fires before Jetpack's SSO modifies the log in form. |
| 752 |
* |
| 753 |
* @module sso |
| 754 |
* |
| 755 |
* @since 2.6.0 |
| 756 |
* |
| 757 |
* @param object $user_data WordPress.com User information. |
| 758 |
*/ |
| 759 |
do_action( 'jetpack_sso_pre_handle_login', $user_data ); |
| 760 |
|
| 761 |
$tracking = new Tracking(); |
| 762 |
|
| 763 |
if ( Jetpack_SSO_Helpers::is_two_step_required() && 0 === (int) $user_data->two_step_enabled ) { |
| 764 |
$this->user_data = $user_data; |
| 765 |
|
| 766 |
$tracking->record_user_event( |
| 767 |
'sso_login_failed', |
| 768 |
array( |
| 769 |
'error_message' => 'error_msg_enable_two_step', |
| 770 |
) |
| 771 |
); |
| 772 |
|
| 773 |
$error = new WP_Error( 'two_step_required', __( 'You must have Two-Step Authentication enabled on your WordPress.com account.', 'jetpack' ) ); |
| 774 |
|
| 775 |
/** This filter is documented in core/src/wp-includes/pluggable.php */ |
| 776 |
do_action( 'wp_login_failed', $user_data->login, $error ); |
| 777 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_enable_two_step' ) ); |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
$user_found_with = ''; |
| 782 |
if ( empty( $user ) && isset( $user_data->external_user_id ) ) { |
| 783 |
$user_found_with = 'external_user_id'; |
| 784 |
$user = get_user_by( 'id', (int) $user_data->external_user_id ); |
| 785 |
if ( $user ) { |
| 786 |
$expected_id = get_user_meta( $user->ID, 'wpcom_user_id', true ); |
| 787 |
if ( $expected_id && $expected_id != $user_data->ID ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison, Universal.Operators.StrictComparisons.LooseNotEqual |
| 788 |
$error = new WP_Error( 'expected_wpcom_user', __( 'Something got a little mixed up and an unexpected WordPress.com user logged in.', 'jetpack' ) ); |
| 789 |
|
| 790 |
$tracking->record_user_event( |
| 791 |
'sso_login_failed', |
| 792 |
array( |
| 793 |
'error_message' => 'error_unexpected_wpcom_user', |
| 794 |
) |
| 795 |
); |
| 796 |
|
| 797 |
/** This filter is documented in core/src/wp-includes/pluggable.php */ |
| 798 |
do_action( 'wp_login_failed', $user_data->login, $error ); |
| 799 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) ); // @todo Need to have a better notice. This is only for the sake of testing the validation. |
| 800 |
return; |
| 801 |
} |
| 802 |
update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
// If we don't have one by wpcom_user_id, try by the email? |
| 807 |
if ( empty( $user ) && Jetpack_SSO_Helpers::match_by_email() ) { |
| 808 |
$user_found_with = 'match_by_email'; |
| 809 |
$user = get_user_by( 'email', $user_data->email ); |
| 810 |
if ( $user ) { |
| 811 |
update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
// If we've still got nothing, create the user. |
| 816 |
$new_user_override_role = Jetpack_SSO_Helpers::new_user_override( $user_data ); |
| 817 |
if ( empty( $user ) && ( get_option( 'users_can_register' ) || $new_user_override_role ) ) { |
| 818 |
/** |
| 819 |
* If not matching by email we still need to verify the email does not exist |
| 820 |
* or this blows up |
| 821 |
* |
| 822 |
* If match_by_email is true, we know the email doesn't exist, as it would have |
| 823 |
* been found in the first pass. If get_user_by( 'email' ) doesn't find the |
| 824 |
* user, then we know that email is unused, so it's safe to add. |
| 825 |
*/ |
| 826 |
if ( Jetpack_SSO_Helpers::match_by_email() || ! get_user_by( 'email', $user_data->email ) ) { |
| 827 |
|
| 828 |
if ( $new_user_override_role ) { |
| 829 |
$user_data->role = $new_user_override_role; |
| 830 |
} |
| 831 |
|
| 832 |
$user = Jetpack_SSO_Helpers::generate_user( $user_data ); |
| 833 |
if ( ! $user ) { |
| 834 |
$tracking->record_user_event( |
| 835 |
'sso_login_failed', |
| 836 |
array( |
| 837 |
'error_message' => 'could_not_create_username', |
| 838 |
) |
| 839 |
); |
| 840 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_unable_to_create_user' ) ); |
| 841 |
return; |
| 842 |
} |
| 843 |
|
| 844 |
$user_found_with = $new_user_override_role |
| 845 |
? 'user_created_new_user_override' |
| 846 |
: 'user_created_users_can_register'; |
| 847 |
} else { |
| 848 |
$tracking->record_user_event( |
| 849 |
'sso_login_failed', |
| 850 |
array( |
| 851 |
'error_message' => 'error_msg_email_already_exists', |
| 852 |
) |
| 853 |
); |
| 854 |
|
| 855 |
$this->user_data = $user_data; |
| 856 |
add_action( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_email_already_exists' ) ); |
| 857 |
return; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
/** |
| 862 |
* Fires after we got login information from WordPress.com. |
| 863 |
* |
| 864 |
* @module sso |
| 865 |
* |
| 866 |
* @since 2.6.0 |
| 867 |
* |
| 868 |
* @param WP_User|false|null $user Local User information. |
| 869 |
* @param object $user_data WordPress.com User Login information. |
| 870 |
*/ |
| 871 |
do_action( 'jetpack_sso_handle_login', $user, $user_data ); |
| 872 |
|
| 873 |
if ( $user ) { |
| 874 |
// Cache the user's details, so we can present it back to them on their user screen. |
| 875 |
update_user_meta( $user->ID, 'wpcom_user_data', $user_data ); |
| 876 |
|
| 877 |
add_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) ); |
| 878 |
wp_set_auth_cookie( $user->ID, true ); |
| 879 |
remove_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) ); |
| 880 |
|
| 881 |
/** This filter is documented in core/src/wp-includes/user.php */ |
| 882 |
do_action( 'wp_login', $user->user_login, $user ); |
| 883 |
|
| 884 |
wp_set_current_user( $user->ID ); |
| 885 |
|
| 886 |
$_request_redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 887 |
$redirect_to = user_can( $user, 'edit_posts' ) ? admin_url() : self::profile_page_url(); |
| 888 |
|
| 889 |
// If we have a saved redirect to request in a cookie. |
| 890 |
if ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
| 891 |
// Set that as the requested redirect to. |
| 892 |
$redirect_to = esc_url_raw( wp_unslash( $_COOKIE['jetpack_sso_redirect_to'] ) ); |
| 893 |
$_request_redirect_to = $redirect_to; |
| 894 |
} |
| 895 |
|
| 896 |
$json_api_auth_environment = Jetpack_SSO_Helpers::get_json_api_auth_environment(); |
| 897 |
|
| 898 |
$is_json_api_auth = ! empty( $json_api_auth_environment ); |
| 899 |
$is_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user->ID ); |
| 900 |
$roles = new Roles(); |
| 901 |
$tracking->record_user_event( |
| 902 |
'sso_user_logged_in', |
| 903 |
array( |
| 904 |
'user_found_with' => $user_found_with, |
| 905 |
'user_connected' => (bool) $is_user_connected, |
| 906 |
'user_role' => $roles->translate_current_user_to_role(), |
| 907 |
'is_json_api_auth' => (bool) $is_json_api_auth, |
| 908 |
) |
| 909 |
); |
| 910 |
|
| 911 |
if ( $is_json_api_auth ) { |
| 912 |
Jetpack::init()->verify_json_api_authorization_request( $json_api_auth_environment ); |
| 913 |
Jetpack::init()->store_json_api_authorization_token( $user->user_login, $user ); |
| 914 |
|
| 915 |
} elseif ( ! $is_user_connected ) { |
| 916 |
wp_safe_redirect( |
| 917 |
add_query_arg( |
| 918 |
array( |
| 919 |
'redirect_to' => $redirect_to, |
| 920 |
'request_redirect_to' => $_request_redirect_to, |
| 921 |
'calypso_env' => Jetpack::get_calypso_env(), |
| 922 |
'jetpack-sso-auth-redirect' => '1', |
| 923 |
), |
| 924 |
admin_url() |
| 925 |
) |
| 926 |
); |
| 927 |
exit; |
| 928 |
} |
| 929 |
|
| 930 |
add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
| 931 |
wp_safe_redirect( |
| 932 |
/** This filter is documented in core/src/wp-login.php */ |
| 933 |
apply_filters( 'login_redirect', $redirect_to, $_request_redirect_to, $user ) |
| 934 |
); |
| 935 |
exit; |
| 936 |
} |
| 937 |
|
| 938 |
add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' ); |
| 939 |
|
| 940 |
$tracking->record_user_event( |
| 941 |
'sso_login_failed', |
| 942 |
array( |
| 943 |
'error_message' => 'cant_find_user', |
| 944 |
) |
| 945 |
); |
| 946 |
|
| 947 |
$this->user_data = $user_data; |
| 948 |
|
| 949 |
$error = new WP_Error( 'account_not_found', __( 'Account not found. If you already have an account, make sure you have connected to WordPress.com.', 'jetpack' ) ); |
| 950 |
|
| 951 |
/** This filter is documented in core/src/wp-includes/pluggable.php */ |
| 952 |
do_action( 'wp_login_failed', $user_data->login, $error ); |
| 953 |
add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'cant_find_user' ) ); |
| 954 |
} |
| 955 |
|
| 956 |
/** |
| 957 |
* Retreive the admin profile page URL. |
| 958 |
*/ |
| 959 |
public static function profile_page_url() { |
| 960 |
return admin_url( 'profile.php' ); |
| 961 |
} |
| 962 |
|
| 963 |
/** |
| 964 |
* Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page. |
| 965 |
* |
| 966 |
* @param array $args An array of arguments to add to the SSO URL. |
| 967 |
* @param boolean $is_primary If the button have the `button-primary` class. |
| 968 |
* @return string Returns the HTML markup for the button. |
| 969 |
*/ |
| 970 |
public function build_sso_button( $args = array(), $is_primary = false ) { |
| 971 |
$url = $this->build_sso_button_url( $args ); |
| 972 |
$classes = $is_primary |
| 973 |
? 'jetpack-sso button button-primary' |
| 974 |
: 'jetpack-sso button'; |
| 975 |
|
| 976 |
return sprintf( |
| 977 |
'<a rel="nofollow" href="%1$s" class="%2$s">%3$s %4$s</a>', |
| 978 |
esc_url( $url ), |
| 979 |
$classes, |
| 980 |
'<span class="genericon genericon-wordpress"></span>', |
| 981 |
esc_html__( 'Log in with WordPress.com', 'jetpack' ) |
| 982 |
); |
| 983 |
} |
| 984 |
|
| 985 |
/** |
| 986 |
* Builds a URL with `jetpack-sso` action and option args which is used to setup SSO. |
| 987 |
* |
| 988 |
* @param array $args An array of arguments to add to the SSO URL. |
| 989 |
* @return string The URL used for SSO. |
| 990 |
*/ |
| 991 |
public function build_sso_button_url( $args = array() ) { |
| 992 |
$defaults = array( |
| 993 |
'action' => 'jetpack-sso', |
| 994 |
); |
| 995 |
|
| 996 |
$args = wp_parse_args( $args, $defaults ); |
| 997 |
|
| 998 |
if ( ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 999 |
$args['redirect_to'] = rawurlencode( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1000 |
} |
| 1001 |
|
| 1002 |
return add_query_arg( $args, wp_login_url() ); |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* Retrieves a WordPress.com SSO URL with appropriate query parameters or dies. |
| 1007 |
* |
| 1008 |
* @param boolean $reauth If the user be forced to reauthenticate on WordPress.com. |
| 1009 |
* @param array $args Optional query parameters. |
| 1010 |
* @return string The WordPress.com SSO URL. |
| 1011 |
*/ |
| 1012 |
public function get_sso_url_or_die( $reauth = false, $args = array() ) { |
| 1013 |
$custom_login_url = Jetpack_SSO_Helpers::get_custom_login_url(); |
| 1014 |
if ( $custom_login_url ) { |
| 1015 |
$args['login_url'] = rawurlencode( $custom_login_url ); |
| 1016 |
} |
| 1017 |
|
| 1018 |
if ( empty( $reauth ) ) { |
| 1019 |
$sso_redirect = $this->build_sso_url( $args ); |
| 1020 |
} else { |
| 1021 |
self::clear_wpcom_profile_cookies(); |
| 1022 |
$sso_redirect = $this->build_reauth_and_sso_url( $args ); |
| 1023 |
} |
| 1024 |
|
| 1025 |
// If there was an error retrieving the SSO URL, then error. |
| 1026 |
if ( is_wp_error( $sso_redirect ) ) { |
| 1027 |
$error_message = sanitize_text_field( |
| 1028 |
sprintf( '%s: %s', $sso_redirect->get_error_code(), $sso_redirect->get_error_message() ) |
| 1029 |
); |
| 1030 |
$tracking = new Tracking(); |
| 1031 |
$tracking->record_user_event( |
| 1032 |
'sso_login_redirect_failed', |
| 1033 |
array( |
| 1034 |
'error_message' => $error_message, |
| 1035 |
) |
| 1036 |
); |
| 1037 |
wp_die( esc_html( $error_message ) ); |
| 1038 |
} |
| 1039 |
|
| 1040 |
return $sso_redirect; |
| 1041 |
} |
| 1042 |
|
| 1043 |
/** |
| 1044 |
* Build WordPress.com SSO URL with appropriate query parameters. |
| 1045 |
* |
| 1046 |
* @param array $args Optional query parameters. |
| 1047 |
* @return string WordPress.com SSO URL |
| 1048 |
*/ |
| 1049 |
public function build_sso_url( $args = array() ) { |
| 1050 |
$sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce(); |
| 1051 |
$defaults = array( |
| 1052 |
'action' => 'jetpack-sso', |
| 1053 |
'site_id' => Jetpack_Options::get_option( 'id' ), |
| 1054 |
'sso_nonce' => $sso_nonce, |
| 1055 |
'calypso_auth' => '1', |
| 1056 |
); |
| 1057 |
|
| 1058 |
$args = wp_parse_args( $args, $defaults ); |
| 1059 |
|
| 1060 |
if ( is_wp_error( $args['sso_nonce'] ) ) { |
| 1061 |
return $args['sso_nonce']; |
| 1062 |
} |
| 1063 |
|
| 1064 |
return add_query_arg( $args, 'https://wordpress.com/wp-login.php' ); |
| 1065 |
} |
| 1066 |
|
| 1067 |
/** |
| 1068 |
* Build WordPress.com SSO URL with appropriate query parameters, |
| 1069 |
* including the parameters necessary to force the user to reauthenticate |
| 1070 |
* on WordPress.com. |
| 1071 |
* |
| 1072 |
* @param array $args Optional query parameters. |
| 1073 |
* @return string WordPress.com SSO URL |
| 1074 |
*/ |
| 1075 |
public function build_reauth_and_sso_url( $args = array() ) { |
| 1076 |
$sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce(); |
| 1077 |
$redirect = $this->build_sso_url( |
| 1078 |
array( |
| 1079 |
'force_auth' => '1', |
| 1080 |
'sso_nonce' => $sso_nonce, |
| 1081 |
) |
| 1082 |
); |
| 1083 |
|
| 1084 |
if ( is_wp_error( $redirect ) ) { |
| 1085 |
return $redirect; |
| 1086 |
} |
| 1087 |
|
| 1088 |
$defaults = array( |
| 1089 |
'action' => 'jetpack-sso', |
| 1090 |
'site_id' => Jetpack_Options::get_option( 'id' ), |
| 1091 |
'sso_nonce' => $sso_nonce, |
| 1092 |
'reauth' => '1', |
| 1093 |
'redirect_to' => rawurlencode( $redirect ), |
| 1094 |
'calypso_auth' => '1', |
| 1095 |
); |
| 1096 |
|
| 1097 |
$args = wp_parse_args( $args, $defaults ); |
| 1098 |
|
| 1099 |
if ( is_wp_error( $args['sso_nonce'] ) ) { |
| 1100 |
return $args['sso_nonce']; |
| 1101 |
} |
| 1102 |
|
| 1103 |
return add_query_arg( $args, 'https://wordpress.com/wp-login.php' ); |
| 1104 |
} |
| 1105 |
|
| 1106 |
/** |
| 1107 |
* Determines local user associated with a given WordPress.com user ID. |
| 1108 |
* |
| 1109 |
* @since 2.6.0 |
| 1110 |
* |
| 1111 |
* @param int $wpcom_user_id User ID from WordPress.com. |
| 1112 |
* @return object Local user object if found, null if not. |
| 1113 |
*/ |
| 1114 |
public static function get_user_by_wpcom_id( $wpcom_user_id ) { |
| 1115 |
$user_query = new WP_User_Query( |
| 1116 |
array( |
| 1117 |
'meta_key' => 'wpcom_user_id', |
| 1118 |
'meta_value' => (int) $wpcom_user_id, |
| 1119 |
'number' => 1, |
| 1120 |
) |
| 1121 |
); |
| 1122 |
|
| 1123 |
$users = $user_query->get_results(); |
| 1124 |
return $users ? array_shift( $users ) : null; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** |
| 1128 |
* When jetpack-sso-auth-redirect query parameter is set, will redirect user to |
| 1129 |
* WordPress.com authorization flow. |
| 1130 |
* |
| 1131 |
* We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url |
| 1132 |
* calls menu_page_url() which doesn't work properly until admin menus are registered. |
| 1133 |
*/ |
| 1134 |
public function maybe_authorize_user_after_sso() { |
| 1135 |
if ( empty( $_GET['jetpack-sso-auth-redirect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1136 |
return; |
| 1137 |
} |
| 1138 |
|
| 1139 |
$redirect_to = ! empty( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : admin_url(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1140 |
$request_redirect_to = ! empty( $_GET['request_redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['request_redirect_to'] ) ) : $redirect_to; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 1141 |
|
| 1142 |
/** This filter is documented in core/src/wp-login.php */ |
| 1143 |
$redirect_after_auth = apply_filters( 'login_redirect', $redirect_to, $request_redirect_to, wp_get_current_user() ); |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* Since we are passing this redirect to WordPress.com and therefore can not use wp_safe_redirect(), |
| 1147 |
* let's sanitize it here to make sure it's safe. If the redirect is not safe, then use admin_url(). |
| 1148 |
*/ |
| 1149 |
$redirect_after_auth = wp_sanitize_redirect( $redirect_after_auth ); |
| 1150 |
$redirect_after_auth = wp_validate_redirect( $redirect_after_auth, admin_url() ); |
| 1151 |
|
| 1152 |
/** |
| 1153 |
* Return the raw connect URL with our redirect and attribute connection to SSO. |
| 1154 |
* We remove any other filters that may be turning on the in-place connection |
| 1155 |
* since we will be redirecting the user as opposed to iFraming. |
| 1156 |
*/ |
| 1157 |
remove_all_filters( 'jetpack_use_iframe_authorization_flow' ); |
| 1158 |
add_filter( 'jetpack_use_iframe_authorization_flow', '__return_false' ); |
| 1159 |
$connect_url = Jetpack::init()->build_connect_url( true, $redirect_after_auth, 'sso' ); |
| 1160 |
|
| 1161 |
add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
| 1162 |
wp_safe_redirect( $connect_url ); |
| 1163 |
exit; |
| 1164 |
} |
| 1165 |
|
| 1166 |
/** |
| 1167 |
* Cache user's display name and Gravatar so it can be displayed on the login screen. These cookies are |
| 1168 |
* stored when the user logs out, and then deleted when the user logs in. |
| 1169 |
*/ |
| 1170 |
public function store_wpcom_profile_cookies_on_logout() { |
| 1171 |
if ( ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected( get_current_user_id() ) ) { |
| 1172 |
return; |
| 1173 |
} |
| 1174 |
|
| 1175 |
$user_data = $this->get_user_data( get_current_user_id() ); |
| 1176 |
if ( ! $user_data ) { |
| 1177 |
return; |
| 1178 |
} |
| 1179 |
|
| 1180 |
setcookie( |
| 1181 |
'jetpack_sso_wpcom_name_' . COOKIEHASH, |
| 1182 |
$user_data->display_name, |
| 1183 |
time() + WEEK_IN_SECONDS, |
| 1184 |
COOKIEPATH, |
| 1185 |
COOKIE_DOMAIN, |
| 1186 |
is_ssl(), |
| 1187 |
true |
| 1188 |
); |
| 1189 |
|
| 1190 |
setcookie( |
| 1191 |
'jetpack_sso_wpcom_gravatar_' . COOKIEHASH, |
| 1192 |
get_avatar_url( |
| 1193 |
$user_data->email, |
| 1194 |
array( |
| 1195 |
'size' => 144, |
| 1196 |
'default' => 'mystery', |
| 1197 |
) |
| 1198 |
), |
| 1199 |
time() + WEEK_IN_SECONDS, |
| 1200 |
COOKIEPATH, |
| 1201 |
COOKIE_DOMAIN, |
| 1202 |
is_ssl(), |
| 1203 |
true |
| 1204 |
); |
| 1205 |
} |
| 1206 |
|
| 1207 |
/** |
| 1208 |
* Determines if a local user is connected to WordPress.com |
| 1209 |
* |
| 1210 |
* @since 2.8 |
| 1211 |
* @param integer $user_id - Local user id. |
| 1212 |
* @return boolean |
| 1213 |
**/ |
| 1214 |
public function is_user_connected( $user_id ) { |
| 1215 |
return $this->get_user_data( $user_id ); |
| 1216 |
} |
| 1217 |
|
| 1218 |
/** |
| 1219 |
* Retrieves a user's WordPress.com data |
| 1220 |
* |
| 1221 |
* @since 2.8 |
| 1222 |
* @param integer $user_id - Local user id. |
| 1223 |
* @return mixed null or stdClass |
| 1224 |
**/ |
| 1225 |
public function get_user_data( $user_id ) { |
| 1226 |
return get_user_meta( $user_id, 'wpcom_user_data', true ); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
Jetpack_SSO::get_instance(); |
| 1231 |
|