Нема описа

sso.php 34KB

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