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