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