login; /** * Determines how many times the SSO module can attempt to randomly generate a user. * * @module sso * * @since 4.3.2 * * @param int 5 By default, SSO will attempt to random generate a user up to 5 times. */ $num_tries = intval( apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 ) ); $tries = 0; while ( ( $exists = username_exists( $username ) ) && $tries++ < $num_tries ) { $username = $user_data->login . '_' . $user_data->ID . '_' . mt_rand(); } if ( $exists ) { return false; } $user = (object) array(); $user->user_pass = wp_generate_password( 20 ); $user->user_login = wp_slash( $username ); $user->user_email = wp_slash( $user_data->email ); $user->display_name = $user_data->display_name; $user->first_name = $user_data->first_name; $user->last_name = $user_data->last_name; $user->url = $user_data->url; $user->description = $user_data->description; if ( isset( $user_data->role ) && $user_data->role ) { $user->role = $user_data->role; } $created_user_id = wp_insert_user( $user ); update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID ); return get_userdata( $created_user_id ); } static function extend_auth_cookie_expiration_for_sso() { /** * Determines how long the auth cookie is valid for when a user logs in with SSO. * * @module sso * * @since 4.4.0 * @since 6.1.0 Fixed a typo. Filter was previously jetpack_sso_auth_cookie_expirtation. * * @param int YEAR_IN_SECONDS */ return intval( apply_filters( 'jetpack_sso_auth_cookie_expiration', YEAR_IN_SECONDS ) ); } /** * Determines if the SSO form should be displayed for the current action. * * @since 4.6.0 * * @param string $action * * @return bool Is SSO allowed for the current action? */ static function display_sso_form_for_action( $action ) { /** * Allows plugins the ability to overwrite actions where the SSO form is allowed to be used. * * @module sso * * @since 4.6.0 * * @param array $allowed_actions_for_sso */ $allowed_actions_for_sso = (array) apply_filters( 'jetpack_sso_allowed_actions', array( 'login', 'jetpack-sso', 'jetpack_json_api_authorization', ) ); return in_array( $action, $allowed_actions_for_sso ); } /** * This method returns an environment array that is meant to simulate `$_REQUEST` when the initial * JSON API auth request was made. * * @since 4.6.0 * * @return array|bool */ static function get_json_api_auth_environment() { if ( empty( $_COOKIE['jetpack_sso_original_request'] ) ) { return false; } $original_request = esc_url_raw( $_COOKIE['jetpack_sso_original_request'] ); $parsed_url = wp_parse_url( $original_request ); if ( empty( $parsed_url ) || empty( $parsed_url['query'] ) ) { return false; } $args = array(); wp_parse_str( $parsed_url['query'], $args ); if ( empty( $args ) || empty( $args['action'] ) ) { return false; } if ( 'jetpack_json_api_authorization' != $args['action'] ) { return false; } return array_merge( $args, array( 'jetpack_json_api_original_query' => $original_request ) ); } } endif;