|
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
|
|
|
* |
|
179
|
|
|
* @param array $hosts |
|
180
|
|
|
* @param string $api_base |
|
181
|
|
|
* |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
|
|
static function allowed_redirect_hosts( $hosts, $api_base = JETPACK__API_BASE ) { |
|
185
|
|
|
if ( empty( $hosts ) ) { |
|
186
|
|
|
$hosts = array(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$hosts[] = 'wordpress.com'; |
|
190
|
|
|
$hosts[] = 'jetpack.wordpress.com'; |
|
191
|
|
|
|
|
192
|
|
|
if ( |
|
193
|
|
|
( Jetpack::is_development_mode() || Jetpack::is_development_version() ) && |
|
194
|
|
|
( false === strpos( $api_base, 'jetpack.wordpress.com/jetpack' ) ) |
|
195
|
|
|
) { |
|
196
|
|
|
$base_url_parts = parse_url( esc_url_raw( $api_base ) ); |
|
197
|
|
|
if ( $base_url_parts && ! empty( $base_url_parts[ 'host' ] ) ) { |
|
198
|
|
|
$hosts[] = $base_url_parts[ 'host' ]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return array_unique( $hosts ); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
static function generate_user( $user_data ) { |
|
206
|
|
|
$username = $user_data->login; |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Determines how many times the SSO module can attempt to randomly generate a user. |
|
210
|
|
|
* |
|
211
|
|
|
* @module sso |
|
212
|
|
|
* |
|
213
|
|
|
* @since 4.3.2 |
|
214
|
|
|
* |
|
215
|
|
|
* @param int 5 By default, SSO will attempt to random generate a user up to 5 times. |
|
216
|
|
|
*/ |
|
217
|
|
|
$num_tries = intval( apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 ) ); |
|
218
|
|
|
|
|
219
|
|
|
$tries = 0; |
|
220
|
|
|
while ( ( $exists = username_exists( $username ) ) && $tries++ < $num_tries ) { |
|
221
|
|
|
$username = $user_data->login . '_' . $user_data->ID . '_' . mt_rand(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if ( $exists ) { |
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$password = wp_generate_password( 20 ); |
|
229
|
|
|
$user_id = wp_create_user( $username, $password, $user_data->email ); |
|
230
|
|
|
$user = get_userdata( $user_id ); |
|
231
|
|
|
|
|
232
|
|
|
$user->display_name = $user_data->display_name; |
|
233
|
|
|
$user->first_name = $user_data->first_name; |
|
234
|
|
|
$user->last_name = $user_data->last_name; |
|
235
|
|
|
$user->url = $user_data->url; |
|
236
|
|
|
$user->description = $user_data->description; |
|
237
|
|
|
wp_update_user( $user ); |
|
238
|
|
|
|
|
239
|
|
|
update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
|
240
|
|
|
|
|
241
|
|
|
return $user; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
static function extend_auth_cookie_expiration_for_sso() { |
|
245
|
|
|
/** |
|
246
|
|
|
* Determines how long the auth cookie is valid for when a user logs in with SSO. |
|
247
|
|
|
* |
|
248
|
|
|
* @module sso |
|
249
|
|
|
* |
|
250
|
|
|
* @since 4.4.0 |
|
251
|
|
|
* |
|
252
|
|
|
* @param int YEAR_IN_SECONDS |
|
253
|
|
|
*/ |
|
254
|
|
|
return intval( apply_filters( 'jetpack_sso_auth_cookie_expirtation', YEAR_IN_SECONDS ) ); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
endif; |
|
259
|
|
|
|