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 -1274 12.9.516.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,1268 +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 - * Checks to determine if the user has indicated they want to use the wp-admin interface.
404 - */
405 - private function use_wp_admin_interface() {
406 - return 'wp-admin' === get_option( 'wpcom_admin_interface' );
407 - }
408 -
409 - /**
410 - * Initialization for a SSO request.
411 - */
412 - public function login_init() {
413 - global $action;
414 -
415 - $tracking = new Tracking();
416 -
417 - if ( Jetpack_SSO_Helpers::should_hide_login_form() ) {
418 - /**
419 - * Since the default authenticate filters fire at priority 20 for checking username and password,
420 - * let's fire at priority 30. wp_authenticate_spam_check is fired at priority 99, but since we return a
421 - * WP_Error in disable_default_login_form, then we won't trigger spam processing logic.
422 - */
423 - add_filter( 'authenticate', array( 'Jetpack_SSO_Notices', 'disable_default_login_form' ), 30 );
424 -
425 - /**
426 - * Filter the display of the disclaimer message appearing when default WordPress login form is disabled.
427 - *
428 - * @module sso
429 - *
430 - * @since 2.8.0
431 - *
432 - * @param bool true Should the disclaimer be displayed. Default to true.
433 - */
434 - $display_sso_disclaimer = apply_filters( 'jetpack_sso_display_disclaimer', true );
435 - if ( $display_sso_disclaimer ) {
436 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'msg_login_by_jetpack' ) );
437 - }
438 - }
439 -
440 - if ( 'jetpack-sso' === $action ) {
441 - if ( isset( $_GET['result'] ) && isset( $_GET['user_id'] ) && isset( $_GET['sso_nonce'] ) && 'success' === $_GET['result'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
442 - $this->handle_login();
443 - $this->display_sso_login_form();
444 - } elseif ( ( new Status() )->is_staging_site() ) {
445 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) );
446 - } else {
447 - // Is it wiser to just use wp_redirect than do this runaround to wp_safe_redirect?
448 - add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
449 - $reauth = ! empty( $_GET['force_reauth'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
450 - $sso_url = $this->get_sso_url_or_die( $reauth );
451 -
452 - $tracking->record_user_event( 'sso_login_redirect_success' );
453 - wp_safe_redirect( $sso_url );
454 - exit;
455 - }
456 - } elseif ( Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) {
457 -
458 - // Save cookies so we can handle redirects after SSO.
459 - static::save_cookies();
460 -
461 - /**
462 - * Check to see if the site admin wants to automagically forward the user
463 - * to the WordPress.com login page AND that the request to wp-login.php
464 - * is not something other than login (Like logout!)
465 - */
466 - if ( ! $this->use_wp_admin_interface() && Jetpack_SSO_Helpers::bypass_login_forward_wpcom() && $this->wants_to_login() ) {
467 - add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
468 - $reauth = ! empty( $_GET['force_reauth'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
469 - $sso_url = $this->get_sso_url_or_die( $reauth );
470 - $tracking->record_user_event( 'sso_login_redirect_bypass_success' );
471 - wp_safe_redirect( $sso_url );
472 - exit;
473 - }
474 -
475 - $this->display_sso_login_form();
476 - }
477 - }
478 -
479 - /**
480 - * Ensures that we can get a nonce from WordPress.com via XML-RPC before setting
481 - * up the hooks required to display the SSO form.
482 - */
483 - public function display_sso_login_form() {
484 - add_filter( 'login_body_class', array( $this, 'login_body_class' ) );
485 - add_action( 'login_head', array( $this, 'print_inline_admin_css' ) );
486 -
487 - if ( ( new Status() )->is_staging_site() ) {
488 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) );
489 - return;
490 - }
491 -
492 - $sso_nonce = self::request_initial_nonce();
493 - if ( is_wp_error( $sso_nonce ) ) {
494 - return;
495 - }
496 -
497 - add_action( 'login_form', array( $this, 'login_form' ) );
498 - add_action( 'login_enqueue_scripts', array( $this, 'login_enqueue_scripts' ) );
499 - }
500 -
501 - /**
502 - * Conditionally save the redirect_to url as a cookie.
503 - *
504 - * @since 4.6.0 Renamed to save_cookies from maybe_save_redirect_cookies
505 - */
506 - public static function save_cookies() {
507 - if ( headers_sent() ) {
508 - return new WP_Error( 'headers_sent', __( 'Cannot deal with cookie redirects, as headers are already sent.', 'jetpack' ) );
509 - }
510 -
511 - setcookie(
512 - 'jetpack_sso_original_request',
513 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sniff misses the wrapping esc_url_raw().
514 - esc_url_raw( set_url_scheme( ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ) ),
515 - time() + HOUR_IN_SECONDS,
516 - COOKIEPATH,
517 - COOKIE_DOMAIN,
518 - is_ssl(),
519 - true
520 - );
521 -
522 - if ( ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
523 - // If we have something to redirect to.
524 - $url = esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
525 - setcookie( 'jetpack_sso_redirect_to', $url, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
526 - } elseif ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) {
527 - // Otherwise, if it's already set, purge it.
528 - setcookie( 'jetpack_sso_redirect_to', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
529 - }
530 - }
531 -
532 - /**
533 - * Outputs the Jetpack SSO button and description as well as the toggle link
534 - * for switching between Jetpack SSO and default login.
535 - */
536 - public function login_form() {
537 - $site_name = get_bloginfo( 'name' );
538 - if ( ! $site_name ) {
539 - $site_name = get_bloginfo( 'url' );
540 - }
541 -
542 - $display_name = ! empty( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] )
543 - ? sanitize_text_field( wp_unslash( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) )
544 - : false;
545 - $gravatar = ! empty( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] )
546 - ? esc_url_raw( wp_unslash( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) )
547 - : false;
548 -
549 - ?>
550 - <div id="jetpack-sso-wrap">
551 - <?php
552 - /**
553 - * Allow extension above Jetpack's SSO form.
554 - *
555 - * @module sso
556 - *
557 - * @since 8.6.0
558 - */
559 - do_action( 'jetpack_sso_login_form_above_wpcom' );
560 -
561 - if ( $display_name && $gravatar ) :
562 - ?>
563 - <div id="jetpack-sso-wrap__user">
564 - <img width="72" height="72" src="<?php echo esc_html( $gravatar ); ?>" />
565 -
566 - <h2>
567 - <?php
568 - echo wp_kses(
569 - /* translators: %s a user display name. */
570 - sprintf( __( 'Log in as <span>%s</span>', 'jetpack' ), esc_html( $display_name ) ),
571 - array( 'span' => true )
572 - );
573 - ?>
574 - </h2>
575 - </div>
576 -
577 - <?php endif; ?>
578 -
579 -
580 - <div id="jetpack-sso-wrap__action">
581 - <?php echo $this->build_sso_button( array(), 'is_primary' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping done in build_sso_button() ?>
582 -
583 - <?php if ( $display_name && $gravatar ) : ?>
584 - <a rel="nofollow" class="jetpack-sso-wrap__reauth" href="<?php echo esc_url( $this->build_sso_button_url( array( 'force_reauth' => '1' ) ) ); ?>">
585 - <?php esc_html_e( 'Log in as a different WordPress.com user', 'jetpack' ); ?>
586 - </a>
587 - <?php else : ?>
588 - <p>
589 - <?php
590 - /**
591 - * Filter the messeage displayed below the SSO button.
592 - *
593 - * @module sso
594 - *
595 - * @since 10.3.0
596 - *
597 - * @param string $sso_explanation Message displayed below the SSO button.
598 - */
599 - $sso_explanation = apply_filters(
600 - 'jetpack_sso_login_form_explanation_text',
601 - sprintf(
602 - /* Translators: %s is the name of the site. */
603 - __( 'You can now save time spent logging in by connecting your WordPress.com account to %s.', 'jetpack' ),
604 - esc_html( $site_name )
605 - )
606 - );
607 - echo esc_html( $sso_explanation );
608 - ?>
609 - </p>
610 - <?php endif; ?>
611 - </div>
612 -
613 - <?php
614 - /**
615 - * Allow extension below Jetpack's SSO form.
616 - *
617 - * @module sso
618 - *
619 - * @since 8.6.0
620 - */
621 - do_action( 'jetpack_sso_login_form_below_wpcom' );
622 -
623 - if ( ! Jetpack_SSO_Helpers::should_hide_login_form() ) :
624 - ?>
625 - <div class="jetpack-sso-or">
626 - <span><?php esc_html_e( 'Or', 'jetpack' ); ?></span>
627 - </div>
628 -
629 - <a href="<?php echo esc_url( add_query_arg( 'jetpack-sso-show-default-form', '1' ) ); ?>" class="jetpack-sso-toggle wpcom">
630 - <?php
631 - esc_html_e( 'Log in with username and password', 'jetpack' )
632 - ?>
633 - </a>
634 -
635 - <a href="<?php echo esc_url( add_query_arg( 'jetpack-sso-show-default-form', '0' ) ); ?>" class="jetpack-sso-toggle default">
636 - <?php
637 - esc_html_e( 'Log in with WordPress.com', 'jetpack' )
638 - ?>
639 - </a>
640 - <?php endif; ?>
641 - </div>
642 - <?php
643 - }
644 -
645 - /**
646 - * Clear the cookies that store the profile information for the last
647 - * WPCOM user to connect.
648 - */
649 - public static function clear_wpcom_profile_cookies() {
650 - if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) ) {
651 - setcookie(
652 - 'jetpack_sso_wpcom_name_' . COOKIEHASH,
653 - ' ',
654 - time() - YEAR_IN_SECONDS,
655 - COOKIEPATH,
656 - COOKIE_DOMAIN,
657 - is_ssl(),
658 - true
659 - );
660 - }
661 -
662 - if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) ) {
663 - setcookie(
664 - 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH,
665 - ' ',
666 - time() - YEAR_IN_SECONDS,
667 - COOKIEPATH,
668 - COOKIE_DOMAIN,
669 - is_ssl(),
670 - true
671 - );
672 - }
673 - }
674 -
675 - /**
676 - * Clear cookies that are no longer needed once the user has logged in.
677 - *
678 - * @since 4.8.0
679 - */
680 - public static function clear_cookies_after_login() {
681 - self::clear_wpcom_profile_cookies();
682 - if ( isset( $_COOKIE['jetpack_sso_nonce'] ) ) {
683 - setcookie(
684 - 'jetpack_sso_nonce',
685 - ' ',
686 - time() - YEAR_IN_SECONDS,
687 - COOKIEPATH,
688 - COOKIE_DOMAIN,
689 - is_ssl(),
690 - true
691 - );
692 - }
693 -
694 - if ( isset( $_COOKIE['jetpack_sso_original_request'] ) ) {
695 - setcookie(
696 - 'jetpack_sso_original_request',
697 - ' ',
698 - time() - YEAR_IN_SECONDS,
699 - COOKIEPATH,
700 - COOKIE_DOMAIN,
701 - is_ssl(),
702 - true
703 - );
704 - }
705 -
706 - if ( isset( $_COOKIE['jetpack_sso_redirect_to'] ) ) {
707 - setcookie(
708 - 'jetpack_sso_redirect_to',
709 - ' ',
710 - time() - YEAR_IN_SECONDS,
711 - COOKIEPATH,
712 - COOKIE_DOMAIN,
713 - is_ssl(),
714 - true
715 - );
716 - }
717 - }
718 -
719 - /**
720 - * Clean up after Jetpack gets disconnected.
721 - *
722 - * @since 10.7
723 - */
724 - public static function disconnect() {
725 - if ( Jetpack::connection()->is_user_connected() ) {
726 - static::delete_connection_for_user( get_current_user_id() );
727 - }
728 - }
729 -
730 - /**
731 - * Remove an SSO connection for a user.
732 - *
733 - * @param int $user_id The local user id.
734 - */
735 - public static function delete_connection_for_user( $user_id ) {
736 - $wpcom_user_id = get_user_meta( $user_id, 'wpcom_user_id', true );
737 - if ( ! $wpcom_user_id ) {
738 - return;
739 - }
740 -
741 - $xml = new Jetpack_IXR_Client(
742 - array(
743 - 'wpcom_user_id' => $user_id,
744 - )
745 - );
746 - $xml->query( 'jetpack.sso.removeUser', $wpcom_user_id );
747 -
748 - if ( $xml->isError() ) {
749 - return false;
750 - }
751 -
752 - // Clean up local data stored for SSO.
753 - delete_user_meta( $user_id, 'wpcom_user_id' );
754 - delete_user_meta( $user_id, 'wpcom_user_data' );
755 - self::clear_wpcom_profile_cookies();
756 -
757 - return $xml->getResponse();
758 - }
759 -
760 - /**
761 - * Retrieves nonce used for SSO form.
762 - */
763 - public static function request_initial_nonce() {
764 - $nonce = ! empty( $_COOKIE['jetpack_sso_nonce'] )
765 - ? sanitize_key( wp_unslash( $_COOKIE['jetpack_sso_nonce'] ) )
766 - : false;
767 -
768 - if ( ! $nonce ) {
769 - $xml = new Jetpack_IXR_Client();
770 - $xml->query( 'jetpack.sso.requestNonce' );
771 -
772 - if ( $xml->isError() ) {
773 - return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage() );
774 - }
775 -
776 - $nonce = sanitize_key( $xml->getResponse() );
777 -
778 - setcookie(
779 - 'jetpack_sso_nonce',
780 - $nonce,
781 - time() + ( 10 * MINUTE_IN_SECONDS ),
782 - COOKIEPATH,
783 - COOKIE_DOMAIN,
784 - is_ssl(),
785 - true
786 - );
787 - }
788 -
789 - return $nonce;
790 - }
791 -
792 - /**
793 - * The function that actually handles the login!
794 - */
795 - public function handle_login() {
796 - $wpcom_nonce = isset( $_GET['sso_nonce'] ) ? sanitize_key( $_GET['sso_nonce'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
797 - $wpcom_user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
798 -
799 - $xml = new Jetpack_IXR_Client();
800 - $xml->query( 'jetpack.sso.validateResult', $wpcom_nonce, $wpcom_user_id );
801 -
802 - $user_data = $xml->isError() ? false : $xml->getResponse();
803 - if ( empty( $user_data ) ) {
804 - add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' );
805 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) );
806 - return;
807 - }
808 -
809 - $user_data = (object) $user_data;
810 - $user = null;
811 -
812 - /**
813 - * Fires before Jetpack's SSO modifies the log in form.
814 - *
815 - * @module sso
816 - *
817 - * @since 2.6.0
818 - *
819 - * @param object $user_data WordPress.com User information.
820 - */
821 - do_action( 'jetpack_sso_pre_handle_login', $user_data );
822 -
823 - $tracking = new Tracking();
824 -
825 - if ( Jetpack_SSO_Helpers::is_two_step_required() && 0 === (int) $user_data->two_step_enabled ) {
826 - $this->user_data = $user_data;
827 -
828 - $tracking->record_user_event(
829 - 'sso_login_failed',
830 - array(
831 - 'error_message' => 'error_msg_enable_two_step',
832 - )
833 - );
834 -
835 - $error = new WP_Error( 'two_step_required', __( 'You must have Two-Step Authentication enabled on your WordPress.com account.', 'jetpack' ) );
836 -
837 - /** This filter is documented in core/src/wp-includes/pluggable.php */
838 - do_action( 'wp_login_failed', $user_data->login, $error );
839 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_enable_two_step' ) );
840 - return;
841 - }
842 -
843 - $user_found_with = '';
844 - if ( empty( $user ) && isset( $user_data->external_user_id ) ) {
845 - $user_found_with = 'external_user_id';
846 - $user = get_user_by( 'id', (int) $user_data->external_user_id );
847 - if ( $user ) {
848 - $expected_id = get_user_meta( $user->ID, 'wpcom_user_id', true );
849 - if ( $expected_id && $expected_id != $user_data->ID ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison, Universal.Operators.StrictComparisons.LooseNotEqual
850 - $error = new WP_Error( 'expected_wpcom_user', __( 'Something got a little mixed up and an unexpected WordPress.com user logged in.', 'jetpack' ) );
851 -
852 - $tracking->record_user_event(
853 - 'sso_login_failed',
854 - array(
855 - 'error_message' => 'error_unexpected_wpcom_user',
856 - )
857 - );
858 -
859 - /** This filter is documented in core/src/wp-includes/pluggable.php */
860 - do_action( 'wp_login_failed', $user_data->login, $error );
861 - 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.
862 - return;
863 - }
864 - update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID );
865 - }
866 - }
867 -
868 - // If we don't have one by wpcom_user_id, try by the email?
869 - if ( empty( $user ) && Jetpack_SSO_Helpers::match_by_email() ) {
870 - $user_found_with = 'match_by_email';
871 - $user = get_user_by( 'email', $user_data->email );
872 - if ( $user ) {
873 - update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID );
874 - }
875 - }
876 -
877 - // If we've still got nothing, create the user.
878 - $new_user_override_role = Jetpack_SSO_Helpers::new_user_override( $user_data );
879 - if ( empty( $user ) && ( get_option( 'users_can_register' ) || $new_user_override_role ) ) {
880 - /**
881 - * If not matching by email we still need to verify the email does not exist
882 - * or this blows up
883 - *
884 - * If match_by_email is true, we know the email doesn't exist, as it would have
885 - * been found in the first pass. If get_user_by( 'email' ) doesn't find the
886 - * user, then we know that email is unused, so it's safe to add.
887 - */
888 - if ( Jetpack_SSO_Helpers::match_by_email() || ! get_user_by( 'email', $user_data->email ) ) {
889 -
890 - if ( $new_user_override_role ) {
891 - $user_data->role = $new_user_override_role;
892 - }
893 -
894 - $user = Jetpack_SSO_Helpers::generate_user( $user_data );
895 - if ( ! $user ) {
896 - $tracking->record_user_event(
897 - 'sso_login_failed',
898 - array(
899 - 'error_message' => 'could_not_create_username',
900 - )
901 - );
902 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_unable_to_create_user' ) );
903 - return;
904 - }
905 -
906 - $user_found_with = $new_user_override_role
907 - ? 'user_created_new_user_override'
908 - : 'user_created_users_can_register';
909 - } else {
910 - $tracking->record_user_event(
911 - 'sso_login_failed',
912 - array(
913 - 'error_message' => 'error_msg_email_already_exists',
914 - )
915 - );
916 -
917 - $this->user_data = $user_data;
918 - add_action( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_email_already_exists' ) );
919 - return;
920 - }
921 - }
922 -
923 - /**
924 - * Fires after we got login information from WordPress.com.
925 - *
926 - * @module sso
927 - *
928 - * @since 2.6.0
929 - *
930 - * @param WP_User|false|null $user Local User information.
931 - * @param object $user_data WordPress.com User Login information.
932 - */
933 - do_action( 'jetpack_sso_handle_login', $user, $user_data );
934 -
935 - if ( $user ) {
936 - // Cache the user's details, so we can present it back to them on their user screen.
937 - update_user_meta( $user->ID, 'wpcom_user_data', $user_data );
938 -
939 - add_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) );
940 - wp_set_auth_cookie( $user->ID, true );
941 - remove_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) );
942 -
943 - /** This filter is documented in core/src/wp-includes/user.php */
944 - do_action( 'wp_login', $user->user_login, $user );
945 -
946 - wp_set_current_user( $user->ID );
947 -
948 - $_request_redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
949 - $redirect_to = user_can( $user, 'edit_posts' ) ? admin_url() : self::profile_page_url();
950 -
951 - // If we have a saved redirect to request in a cookie.
952 - if ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) {
953 - // Set that as the requested redirect to.
954 - $redirect_to = esc_url_raw( wp_unslash( $_COOKIE['jetpack_sso_redirect_to'] ) );
955 - $_request_redirect_to = $redirect_to;
956 - }
957 -
958 - $json_api_auth_environment = Jetpack_SSO_Helpers::get_json_api_auth_environment();
959 -
960 - $is_json_api_auth = ! empty( $json_api_auth_environment );
961 - $is_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user->ID );
962 - $roles = new Roles();
963 - $tracking->record_user_event(
964 - 'sso_user_logged_in',
965 - array(
966 - 'user_found_with' => $user_found_with,
967 - 'user_connected' => (bool) $is_user_connected,
968 - 'user_role' => $roles->translate_current_user_to_role(),
969 - 'is_json_api_auth' => (bool) $is_json_api_auth,
970 - )
971 - );
972 -
973 - if ( $is_json_api_auth ) {
974 - Jetpack::init()->verify_json_api_authorization_request( $json_api_auth_environment );
975 - Jetpack::init()->store_json_api_authorization_token( $user->user_login, $user );
976 -
977 - } elseif ( ! $is_user_connected ) {
978 - wp_safe_redirect(
979 - add_query_arg(
980 - array(
981 - 'redirect_to' => $redirect_to,
982 - 'request_redirect_to' => $_request_redirect_to,
983 - 'calypso_env' => ( new Host() )->get_calypso_env(),
984 - 'jetpack-sso-auth-redirect' => '1',
985 - ),
986 - admin_url()
987 - )
988 - );
989 - exit;
990 - }
991 -
992 - add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
993 - wp_safe_redirect(
994 - /** This filter is documented in core/src/wp-login.php */
995 - apply_filters( 'login_redirect', $redirect_to, $_request_redirect_to, $user )
996 - );
997 - exit;
998 - }
999 -
1000 - add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' );
1001 -
1002 - $tracking->record_user_event(
1003 - 'sso_login_failed',
1004 - array(
1005 - 'error_message' => 'cant_find_user',
1006 - )
1007 - );
1008 -
1009 - $this->user_data = $user_data;
1010 -
1011 - $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' ) );
1012 -
1013 - /** This filter is documented in core/src/wp-includes/pluggable.php */
1014 - do_action( 'wp_login_failed', $user_data->login, $error );
1015 - add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'cant_find_user' ) );
1016 - }
1017 -
1018 - /**
1019 - * Retreive the admin profile page URL.
1020 - */
1021 - public static function profile_page_url() {
1022 - return admin_url( 'profile.php' );
1023 - }
1024 -
1025 - /**
1026 - * Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page.
1027 - *
1028 - * @param array $args An array of arguments to add to the SSO URL.
1029 - * @param boolean $is_primary If the button have the `button-primary` class.
1030 - * @return string Returns the HTML markup for the button.
1031 - */
1032 - public function build_sso_button( $args = array(), $is_primary = false ) {
1033 - $url = $this->build_sso_button_url( $args );
1034 - $classes = $is_primary
1035 - ? 'jetpack-sso button button-primary'
1036 - : 'jetpack-sso button';
1037 -
1038 - return sprintf(
1039 - '<a rel="nofollow" href="%1$s" class="%2$s">%3$s %4$s</a>',
1040 - esc_url( $url ),
1041 - $classes,
1042 - '<span class="genericon genericon-wordpress"></span>',
1043 - esc_html__( 'Log in with WordPress.com', 'jetpack' )
1044 - );
1045 - }
1046 -
1047 - /**
1048 - * Builds a URL with `jetpack-sso` action and option args which is used to setup SSO.
1049 - *
1050 - * @param array $args An array of arguments to add to the SSO URL.
1051 - * @return string The URL used for SSO.
1052 - */
1053 - public function build_sso_button_url( $args = array() ) {
1054 - $defaults = array(
1055 - 'action' => 'jetpack-sso',
1056 - );
1057 -
1058 - $args = wp_parse_args( $args, $defaults );
1059 -
1060 - if ( ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1061 - $args['redirect_to'] = rawurlencode( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1062 - }
1063 -
1064 - return add_query_arg( $args, wp_login_url() );
1065 - }
1066 -
1067 - /**
1068 - * Retrieves a WordPress.com SSO URL with appropriate query parameters or dies.
1069 - *
1070 - * @param boolean $reauth If the user be forced to reauthenticate on WordPress.com.
1071 - * @param array $args Optional query parameters.
1072 - * @return string The WordPress.com SSO URL.
1073 - */
1074 - public function get_sso_url_or_die( $reauth = false, $args = array() ) {
1075 - $custom_login_url = Jetpack_SSO_Helpers::get_custom_login_url();
1076 - if ( $custom_login_url ) {
1077 - $args['login_url'] = rawurlencode( $custom_login_url );
1078 - }
1079 -
1080 - if ( empty( $reauth ) ) {
1081 - $sso_redirect = $this->build_sso_url( $args );
1082 - } else {
1083 - self::clear_wpcom_profile_cookies();
1084 - $sso_redirect = $this->build_reauth_and_sso_url( $args );
1085 - }
1086 -
1087 - // If there was an error retrieving the SSO URL, then error.
1088 - if ( is_wp_error( $sso_redirect ) ) {
1089 - $error_message = sanitize_text_field(
1090 - sprintf( '%s: %s', $sso_redirect->get_error_code(), $sso_redirect->get_error_message() )
1091 - );
1092 - $tracking = new Tracking();
1093 - $tracking->record_user_event(
1094 - 'sso_login_redirect_failed',
1095 - array(
1096 - 'error_message' => $error_message,
1097 - )
1098 - );
1099 - wp_die( esc_html( $error_message ) );
1100 - }
1101 -
1102 - return $sso_redirect;
1103 - }
1104 -
1105 - /**
1106 - * Build WordPress.com SSO URL with appropriate query parameters.
1107 - *
1108 - * @param array $args Optional query parameters.
1109 - * @return string WordPress.com SSO URL
1110 - */
1111 - public function build_sso_url( $args = array() ) {
1112 - $sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce();
1113 - $defaults = array(
1114 - 'action' => 'jetpack-sso',
1115 - 'site_id' => Jetpack_Options::get_option( 'id' ),
1116 - 'sso_nonce' => $sso_nonce,
1117 - 'calypso_auth' => '1',
1118 - );
1119 -
1120 - $args = wp_parse_args( $args, $defaults );
1121 -
1122 - if ( is_wp_error( $args['sso_nonce'] ) ) {
1123 - return $args['sso_nonce'];
1124 - }
1125 -
1126 - return add_query_arg( $args, 'https://wordpress.com/wp-login.php' );
1127 - }
1128 -
1129 - /**
1130 - * Build WordPress.com SSO URL with appropriate query parameters,
1131 - * including the parameters necessary to force the user to reauthenticate
1132 - * on WordPress.com.
1133 - *
1134 - * @param array $args Optional query parameters.
1135 - * @return string WordPress.com SSO URL
1136 - */
1137 - public function build_reauth_and_sso_url( $args = array() ) {
1138 - $sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce();
1139 - $redirect = $this->build_sso_url(
1140 - array(
1141 - 'force_auth' => '1',
1142 - 'sso_nonce' => $sso_nonce,
1143 - )
1144 - );
1145 -
1146 - if ( is_wp_error( $redirect ) ) {
1147 - return $redirect;
1148 - }
1149 -
1150 - $defaults = array(
1151 - 'action' => 'jetpack-sso',
1152 - 'site_id' => Jetpack_Options::get_option( 'id' ),
1153 - 'sso_nonce' => $sso_nonce,
1154 - 'reauth' => '1',
1155 - 'redirect_to' => rawurlencode( $redirect ),
1156 - 'calypso_auth' => '1',
1157 - );
1158 -
1159 - $args = wp_parse_args( $args, $defaults );
1160 -
1161 - if ( is_wp_error( $args['sso_nonce'] ) ) {
1162 - return $args['sso_nonce'];
1163 - }
1164 -
1165 - return add_query_arg( $args, 'https://wordpress.com/wp-login.php' );
1166 - }
1167 -
1168 - /**
1169 - * Determines local user associated with a given WordPress.com user ID.
1170 - *
1171 - * @since 2.6.0
1172 - *
1173 - * @param int $wpcom_user_id User ID from WordPress.com.
1174 - * @return object Local user object if found, null if not.
1175 - */
1176 - public static function get_user_by_wpcom_id( $wpcom_user_id ) {
1177 - $user_query = new WP_User_Query(
1178 - array(
1179 - 'meta_key' => 'wpcom_user_id',
1180 - 'meta_value' => (int) $wpcom_user_id,
1181 - 'number' => 1,
1182 - )
1183 - );
1184 -
1185 - $users = $user_query->get_results();
1186 - return $users ? array_shift( $users ) : null;
1187 - }
1188 -
1189 - /**
1190 - * When jetpack-sso-auth-redirect query parameter is set, will redirect user to
1191 - * WordPress.com authorization flow.
1192 - *
1193 - * We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url
1194 - * calls menu_page_url() which doesn't work properly until admin menus are registered.
1195 - */
1196 - public function maybe_authorize_user_after_sso() {
1197 - if ( empty( $_GET['jetpack-sso-auth-redirect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1198 - return;
1199 - }
1200 -
1201 - $redirect_to = ! empty( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : admin_url(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1202 - $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
1203 -
1204 - /** This filter is documented in core/src/wp-login.php */
1205 - $redirect_after_auth = apply_filters( 'login_redirect', $redirect_to, $request_redirect_to, wp_get_current_user() );
1206 -
1207 - /**
1208 - * Since we are passing this redirect to WordPress.com and therefore can not use wp_safe_redirect(),
1209 - * let's sanitize it here to make sure it's safe. If the redirect is not safe, then use admin_url().
1210 - */
1211 - $redirect_after_auth = wp_sanitize_redirect( $redirect_after_auth );
1212 - $redirect_after_auth = wp_validate_redirect( $redirect_after_auth, admin_url() );
1213 -
1214 - /**
1215 - * Return the raw connect URL with our redirect and attribute connection to SSO.
1216 - * We remove any other filters that may be turning on the in-place connection
1217 - * since we will be redirecting the user as opposed to iFraming.
1218 - */
1219 - remove_all_filters( 'jetpack_use_iframe_authorization_flow' );
1220 - add_filter( 'jetpack_use_iframe_authorization_flow', '__return_false' );
1221 - $connect_url = Jetpack::init()->build_connect_url( true, $redirect_after_auth, 'sso' );
1222 -
1223 - add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
1224 - wp_safe_redirect( $connect_url );
1225 - exit;
1226 - }
1227 -
1228 - /**
1229 - * Cache user's display name and Gravatar so it can be displayed on the login screen. These cookies are
1230 - * stored when the user logs out, and then deleted when the user logs in.
1231 - */
1232 - public function store_wpcom_profile_cookies_on_logout() {
1233 - if ( ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected( get_current_user_id() ) ) {
1234 - return;
1235 - }
1236 -
1237 - $user_data = $this->get_user_data( get_current_user_id() );
1238 - if ( ! $user_data ) {
1239 - return;
1240 - }
1241 -
1242 - setcookie(
1243 - 'jetpack_sso_wpcom_name_' . COOKIEHASH,
1244 - $user_data->display_name,
1245 - time() + WEEK_IN_SECONDS,
1246 - COOKIEPATH,
1247 - COOKIE_DOMAIN,
1248 - is_ssl(),
1249 - true
1250 - );
1251 -
1252 - setcookie(
1253 - 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH,
1254 - get_avatar_url(
1255 - $user_data->email,
1256 - array(
1257 - 'size' => 144,
1258 - 'default' => 'mystery',
1259 - )
1260 - ),
1261 - time() + WEEK_IN_SECONDS,
1262 - COOKIEPATH,
1263 - COOKIE_DOMAIN,
1264 - is_ssl(),
1265 - true
1266 - );
1267 - }
1268 -
1269 - /**
1270 - * Determines if a local user is connected to WordPress.com
1271 - *
1272 - * @since 2.8
1273 - * @param integer $user_id - Local user id.
1274 - * @return boolean
1275 - **/
1276 - public function is_user_connected( $user_id ) {
1277 - return $this->get_user_data( $user_id );
1278 - }
1279 -
1280 - /**
1281 - * Retrieves a user's WordPress.com data
1282 - *
1283 - * @since 2.8
1284 - * @param integer $user_id - Local user id.
1285 - * @return mixed null or stdClass
1286 - **/
1287 - public function get_user_data( $user_id ) {
1288 - return get_user_meta( $user_id, 'wpcom_user_data', true );
1289 - }
1290 -}
1291 -
1292 -Jetpack_SSO::get_instance();
31 +);