Completed
Push — update/invite-user-sync-events ( fad03f...617dec )
by
unknown
84:44 queued 76:42
created

Jetpack_SSO_Helpers::generate_user()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 6
nop 1
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
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 ( false === strpos( $api_base, 'jetpack.wordpress.com/jetpack' ) ) {
195
			$base_url_parts = parse_url( esc_url_raw( $api_base ) );
196
			if ( $base_url_parts && ! empty( $base_url_parts[ 'host' ] ) ) {
197
				$hosts[] = $base_url_parts[ 'host' ];
198
			}
199
		}
200
201
		return array_unique( $hosts );
202
	}
203
204
	static function generate_user( $user_data ) {
205
		$username = $user_data->login;
206
		/**
207
		 * Determines how many times the SSO module can attempt to randomly generate a user.
208
		 *
209
		 * @module sso
210
		 *
211
		 * @since 4.3.2
212
		 *
213
		 * @param int 5 By default, SSO will attempt to random generate a user up to 5 times.
214
		 */
215
		$num_tries = intval( apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 ) );
216
217
		$tries = 0;
218
		while ( ( $exists = username_exists( $username ) ) && $tries++ < $num_tries ) {
219
			$username = $user_data->login . '_' . $user_data->ID . '_' . mt_rand();
220
		}
221
222
		if ( $exists ) {
223
			return false;
224
		}
225
226
		$user = (object) array();
227
		$user->user_pass    = wp_generate_password( 20 );
228
		$user->user_login   = wp_slash( $username );
229
		$user->user_email   = wp_slash( $user_data->email );
230
		$user->display_name = $user_data->display_name;
231
		$user->first_name   = $user_data->first_name;
232
		$user->last_name    = $user_data->last_name;
233
		$user->url          = $user_data->url;
234
		$user->description  = $user_data->description;
235
236
		if ( isset( $user_data->role ) && $user_data->role ) {
237
			$user->role     = $user_data->role;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 5 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
238
		}
239
240
		$created_user_id = wp_insert_user( $user );
241
242
		update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID );
243
		return get_userdata( $created_user_id );
244
	}
245
246
	static function extend_auth_cookie_expiration_for_sso() {
247
		/**
248
		 * Determines how long the auth cookie is valid for when a user logs in with SSO.
249
		 *
250
		 * @module sso
251
		 *
252
		 * @since 4.4.0
253
		 *
254
		 * @param int YEAR_IN_SECONDS
255
		 */
256
		return intval( apply_filters( 'jetpack_sso_auth_cookie_expirtation', YEAR_IN_SECONDS ) );
257
	}
258
259
	/**
260
	 * Determines if the SSO form should be displayed for the current action.
261
	 *
262
	 * @since 4.6.0
263
	 *
264
	 * @param string $action
265
	 *
266
	 * @return bool  Is SSO allowed for the current action?
267
	 */
268
	static function display_sso_form_for_action( $action ) {
269
		/**
270
		 * Allows plugins the ability to overwrite actions where the SSO form is allowed to be used.
271
		 *
272
		 * @module sso
273
		 *
274
		 * @since 4.6.0
275
		 *
276
		 * @param array $allowed_actions_for_sso
277
		 */
278
		$allowed_actions_for_sso = (array) apply_filters( 'jetpack_sso_allowed_actions', array(
279
			'login',
280
			'jetpack-sso',
281
			'jetpack_json_api_authorization',
282
		) );
283
		return in_array( $action, $allowed_actions_for_sso );
284
	}
285
286
	/**
287
	 * This method returns an environment array that is meant to simulate `$_REQUEST` when the initial
288
	 * JSON API auth request was made.
289
	 *
290
	 * @since 4.6.0
291
	 *
292
	 * @return array|bool
293
	 */
294
	static function get_json_api_auth_environment() {
295
		if ( empty( $_COOKIE['jetpack_sso_original_request'] ) ) {
296
			return false;
297
		}
298
299
		$original_request = esc_url_raw( $_COOKIE['jetpack_sso_original_request'] );
300
301
		$parsed_url = wp_parse_url( $original_request );
302
		if ( empty( $parsed_url ) || empty( $parsed_url['query'] ) ) {
303
			return false;
304
		}
305
306
		$args = array();
307
		wp_parse_str( $parsed_url['query'], $args );
308
309
		if ( empty( $args ) || empty( $args['action'] ) ) {
310
			return false;
311
		}
312
313
		if ( 'jetpack_json_api_authorization' != $args['action'] ) {
314
			return false;
315
		}
316
317
		return array_merge(
318
			$args,
319
			array( 'jetpack_json_api_original_query' => $original_request )
320
		);
321
	}
322
}
323
324
endif;
325