Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | if ( ! class_exists( 'Jetpack_SSO_Helpers' ) ) : |
||
| 4 | |||
| 5 | /** |
||
| 6 | * A collection of helper functions used in the SSO module. |
||
| 7 | * |
||
| 8 | * @since 4.1.0 |
||
| 9 | */ |
||
| 10 | class Jetpack_SSO_Helpers { |
||
| 11 | /** |
||
| 12 | * Determine if the login form should be hidden or not |
||
| 13 | * |
||
| 14 | * @return bool |
||
| 15 | **/ |
||
| 16 | static function should_hide_login_form() { |
||
| 17 | /** |
||
| 18 | * Remove the default log in form, only leave the WordPress.com log in button. |
||
| 19 | * |
||
| 20 | * @module sso |
||
| 21 | * |
||
| 22 | * @since 3.1.0 |
||
| 23 | * |
||
| 24 | * @param bool get_option( 'jetpack_sso_remove_login_form', false ) Should the default log in form be removed. Default to false. |
||
| 25 | */ |
||
| 26 | return (bool) apply_filters( 'jetpack_remove_login_form', get_option( 'jetpack_sso_remove_login_form', false ) ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Returns a boolean value for whether logging in by matching the WordPress.com user email to a |
||
| 31 | * Jetpack site user's email is allowed. |
||
| 32 | * |
||
| 33 | * @return bool |
||
| 34 | */ |
||
| 35 | static function match_by_email() { |
||
| 36 | $match_by_email = ( 1 == get_option( 'jetpack_sso_match_by_email', true ) ) ? true: false; |
||
| 37 | $match_by_email = defined( 'WPCC_MATCH_BY_EMAIL' ) ? WPCC_MATCH_BY_EMAIL : $match_by_email; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Link the local account to an account on WordPress.com using the same email address. |
||
| 41 | * |
||
| 42 | * @module sso |
||
| 43 | * |
||
| 44 | * @since 2.6.0 |
||
| 45 | * |
||
| 46 | * @param bool $match_by_email Should we link the local account to an account on WordPress.com using the same email address. Default to false. |
||
| 47 | */ |
||
| 48 | return (bool) apply_filters( 'jetpack_sso_match_by_email', $match_by_email ); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns a boolean for whether users are allowed to register on the Jetpack site with SSO, |
||
| 53 | * even though the site disallows normal registrations. |
||
| 54 | * |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | static function new_user_override( $user_data = null ) { |
||
| 58 | $new_user_override = defined( 'WPCC_NEW_USER_OVERRIDE' ) ? WPCC_NEW_USER_OVERRIDE : false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Allow users to register on your site with a WordPress.com account, even though you disallow normal registrations. |
||
| 62 | * If you return a string that corresponds to a user role, the user will be given that role. |
||
| 63 | * |
||
| 64 | * @module sso |
||
| 65 | * |
||
| 66 | * @since 2.6.0 |
||
| 67 | * @since 4.6 $user_data object is now passed to the jetpack_sso_new_user_override filter |
||
| 68 | * |
||
| 69 | * @param bool $new_user_override Allow users to register on your site with a WordPress.com account. Default to false. |
||
| 70 | * @param object|null $user_data An object containing the user data returned from WordPress.com. |
||
| 71 | */ |
||
| 72 | $role = apply_filters( 'jetpack_sso_new_user_override', $new_user_override, $user_data ); |
||
| 73 | |||
| 74 | if ( $role ) { |
||
| 75 | if ( is_string( $role ) && get_role( $role ) ) { |
||
| 76 | return $role; |
||
| 77 | } else { |
||
| 78 | return get_option( 'default_role' ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns a boolean value for whether two-step authentication is required for SSO. |
||
| 87 | * |
||
| 88 | * @since 4.1.0 |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | static function is_two_step_required() { |
||
| 93 | /** |
||
| 94 | * Is it required to have 2-step authentication enabled on WordPress.com to use SSO? |
||
| 95 | * |
||
| 96 | * @module sso |
||
| 97 | * |
||
| 98 | * @since 2.8.0 |
||
| 99 | * |
||
| 100 | * @param bool get_option( 'jetpack_sso_require_two_step' ) Does SSO require 2-step authentication? |
||
| 101 | */ |
||
| 102 | return (bool) apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step', false ) ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns a boolean for whether a user that is attempting to log in will be automatically |
||
| 107 | * redirected to WordPress.com to begin the SSO flow. |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | static function bypass_login_forward_wpcom() { |
||
| 112 | /** |
||
| 113 | * Redirect the site's log in form to WordPress.com's log in form. |
||
| 114 | * |
||
| 115 | * @module sso |
||
| 116 | * |
||
| 117 | * @since 3.1.0 |
||
| 118 | * |
||
| 119 | * @param bool false Should the site's log in form be automatically forwarded to WordPress.com's log in form. |
||
| 120 | */ |
||
| 121 | return (bool) apply_filters( 'jetpack_sso_bypass_login_forward_wpcom', false ); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a boolean for whether the SSO login form should be displayed as the default |
||
| 126 | * when both the default and SSO login form allowed. |
||
| 127 | * |
||
| 128 | * @since 4.1.0 |
||
| 129 | * |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | static function show_sso_login() { |
||
| 133 | if ( self::should_hide_login_form() ) { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Display the SSO login form as the default when both the default and SSO login forms are enabled. |
||
| 139 | * |
||
| 140 | * @module sso |
||
| 141 | * |
||
| 142 | * @since 4.1.0 |
||
| 143 | * |
||
| 144 | * @param bool true Should the SSO login form be displayed by default when the default login form is also enabled? |
||
| 145 | */ |
||
| 146 | return (bool) apply_filters( 'jetpack_sso_default_to_sso_login', true ); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns a boolean for whether the two step required checkbox, displayed on the Jetpack admin page, should be disabled. |
||
| 151 | * |
||
| 152 | * @since 4.1.0 |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | static function is_require_two_step_checkbox_disabled() { |
||
| 157 | return (bool) has_filter( 'jetpack_sso_require_two_step' ); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns a boolean for whether the match by email checkbox, displayed on the Jetpack admin page, should be disabled. |
||
| 162 | * |
||
| 163 | * @since 4.1.0 |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | static function is_match_by_email_checkbox_disabled() { |
||
| 168 | return defined( 'WPCC_MATCH_BY_EMAIL' ) || has_filter( 'jetpack_sso_match_by_email' ); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns an array of hosts that SSO will redirect to. |
||
| 173 | * |
||
| 174 | * Instead of accessing JETPACK__API_BASE within the method directly, we set it as the |
||
| 175 | * default for $api_base due to restrictions with testing constants in our tests. |
||
| 176 | * |
||
| 177 | * @since 4.3.0 |
||
| 178 | * @since 4.6.0 Added public-api.wordpress.com as an allowed redirect |
||
| 179 | * |
||
| 180 | * @param array $hosts |
||
| 181 | * @param string $api_base |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | static function allowed_redirect_hosts( $hosts, $api_base = JETPACK__API_BASE ) { |
||
| 186 | if ( empty( $hosts ) ) { |
||
| 187 | $hosts = array(); |
||
| 188 | } |
||
| 189 | |||
| 190 | $hosts[] = 'wordpress.com'; |
||
| 191 | $hosts[] = 'jetpack.wordpress.com'; |
||
| 192 | $hosts[] = 'public-api.wordpress.com'; |
||
| 193 | |||
| 194 | if ( |
||
| 195 | ( Jetpack::is_development_mode() || Jetpack::is_development_version() ) && |
||
| 196 | ( false === strpos( $api_base, 'jetpack.wordpress.com/jetpack' ) ) |
||
| 197 | ) { |
||
| 198 | $base_url_parts = parse_url( esc_url_raw( $api_base ) ); |
||
| 199 | if ( $base_url_parts && ! empty( $base_url_parts[ 'host' ] ) ) { |
||
| 200 | $hosts[] = $base_url_parts[ 'host' ]; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | return array_unique( $hosts ); |
||
| 205 | } |
||
| 206 | |||
| 207 | static function generate_user( $user_data ) { |
||
| 208 | $username = $user_data->login; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Determines how many times the SSO module can attempt to randomly generate a user. |
||
| 212 | * |
||
| 213 | * @module sso |
||
| 214 | * |
||
| 215 | * @since 4.3.2 |
||
| 216 | * |
||
| 217 | * @param int 5 By default, SSO will attempt to random generate a user up to 5 times. |
||
| 218 | */ |
||
| 219 | $num_tries = intval( apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 ) ); |
||
| 220 | |||
| 221 | $tries = 0; |
||
| 222 | while ( ( $exists = username_exists( $username ) ) && $tries++ < $num_tries ) { |
||
| 223 | $username = $user_data->login . '_' . $user_data->ID . '_' . mt_rand(); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ( $exists ) { |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | |||
| 230 | $password = wp_generate_password( 20 ); |
||
| 231 | $user_id = wp_create_user( $username, $password, $user_data->email ); |
||
| 232 | $user = get_userdata( $user_id ); |
||
| 233 | |||
| 234 | $user->display_name = $user_data->display_name; |
||
| 235 | $user->first_name = $user_data->first_name; |
||
| 236 | $user->last_name = $user_data->last_name; |
||
| 237 | $user->url = $user_data->url; |
||
| 238 | $user->description = $user_data->description; |
||
| 239 | |||
| 240 | if ( isset( $user_data->role ) && $user_data->role ) { |
||
| 241 | $user->role = $user_data->role; |
||
|
0 ignored issues
–
show
|
|||
| 242 | } |
||
| 243 | |||
| 244 | wp_update_user( $user ); |
||
| 245 | |||
| 246 | update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
||
| 247 | |||
| 248 | return $user; |
||
| 249 | } |
||
| 250 | |||
| 251 | static function extend_auth_cookie_expiration_for_sso() { |
||
| 252 | /** |
||
| 253 | * Determines how long the auth cookie is valid for when a user logs in with SSO. |
||
| 254 | * |
||
| 255 | * @module sso |
||
| 256 | * |
||
| 257 | * @since 4.4.0 |
||
| 258 | * |
||
| 259 | * @param int YEAR_IN_SECONDS |
||
| 260 | */ |
||
| 261 | return intval( apply_filters( 'jetpack_sso_auth_cookie_expirtation', YEAR_IN_SECONDS ) ); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Determines if the SSO form should be displayed for the current action. |
||
| 266 | * |
||
| 267 | * @since 4.6.0 |
||
| 268 | * |
||
| 269 | * @param string $action |
||
| 270 | * |
||
| 271 | * @return bool Is SSO allowed for the current action? |
||
| 272 | */ |
||
| 273 | static function display_sso_form_for_action( $action ) { |
||
| 274 | /** |
||
| 275 | * Allows plugins the ability to overwrite actions where the SSO form is allowed to be used. |
||
| 276 | * |
||
| 277 | * @module sso |
||
| 278 | * |
||
| 279 | * @since 4.6.0 |
||
| 280 | * |
||
| 281 | * @param array $allowed_actions_for_sso |
||
| 282 | */ |
||
| 283 | $allowed_actions_for_sso = (array) apply_filters( 'jetpack_sso_allowed_actions', array( |
||
| 284 | 'login', |
||
| 285 | 'jetpack-sso', |
||
| 286 | 'jetpack_json_api_authorization', |
||
| 287 | ) ); |
||
| 288 | return in_array( $action, $allowed_actions_for_sso ); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * This method returns an environment array that is meant to simulate `$_REQUEST` when the initial |
||
| 293 | * JSON API auth request was made. |
||
| 294 | * |
||
| 295 | * @since 4.6.0 |
||
| 296 | * |
||
| 297 | * @return array|bool |
||
| 298 | */ |
||
| 299 | static function get_json_api_auth_environment() { |
||
| 300 | if ( empty( $_COOKIE['jetpack_sso_original_request'] ) ) { |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | |||
| 304 | $original_request = esc_url_raw( $_COOKIE['jetpack_sso_original_request'] ); |
||
| 305 | |||
| 306 | $parsed_url = wp_parse_url( $original_request ); |
||
| 307 | if ( empty( $parsed_url ) || empty( $parsed_url['query'] ) ) { |
||
| 308 | return false; |
||
| 309 | } |
||
| 310 | |||
| 311 | $args = array(); |
||
| 312 | wp_parse_str( $parsed_url['query'], $args ); |
||
| 313 | |||
| 314 | if ( empty( $args ) || empty( $args['action'] ) ) { |
||
| 315 | return false; |
||
| 316 | } |
||
| 317 | |||
| 318 | if ( 'jetpack_json_api_authorization' != $args['action'] ) { |
||
| 319 | return false; |
||
| 320 | } |
||
| 321 | |||
| 322 | return array_merge( |
||
| 323 | $args, |
||
| 324 | array( 'jetpack_json_api_original_query' => $original_request ) |
||
| 325 | ); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | endif; |
||
| 330 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.