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