-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrest-api.php
288 lines (254 loc) · 7.91 KB
/
rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace WordPressdotorg\Two_Factor;
use Two_Factor_Core, Two_Factor_Totp, Two_Factor_Backup_Codes;
use WildWolf\WordPress\TwoFactorWebAuthn\{ WebAuthn_Credential_Store };
use WP_REST_Server, WP_REST_Request;
defined( 'WPINC' ) || die();
add_action( 'rest_api_init', __NAMESPACE__ . '\register_rest_routes' );
add_action( 'rest_api_init', __NAMESPACE__ . '\register_user_fields' );
add_filter( 'rest_pre_insert_user', __NAMESPACE__ . '\require_email_confirmation', 10, 2 );
/**
* Register/Output some REST-API calls to be pre-loaded.
*
* This prevents the browser having to make the HTTP call before the React UI can be ready.
* This duplicates block_editor_rest_api_preload() as there is no generic function for performing this preloading.
* WARNING: This will output Javascript immediately if called during the page load if the wp-api-fetch script has already been output.
*
* @codeCoverageIgnore
*
* @param array $preload_paths The REST API paths to be pre-loaded, must include prefixed slash.
* @return void
*/
function preload_api_requests( array $preload_paths ) : void {
global $post, $wp_scripts, $wp_styles;
/*
* Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
* API data is preloaded.
* Because API preloading can call the_content and other filters, plugins
* can unexpectedly modify the global $post or enqueue assets which are not
* intended for the block editor.
*/
$backup_global_post = ! empty( $post ) ? clone $post : $post;
$backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
$backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
[]
);
// Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
$post = $backup_global_post;
$wp_scripts = $backup_wp_scripts;
$wp_styles = $backup_wp_styles;
$preload_js = sprintf(
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
wp_json_encode( $preload_data )
);
// The script may have already been printed. In that case, add the pre-loading inline now.
if ( wp_script_is( 'wp-api-fetch', 'done' ) ) {
printf(
'<script>%s</script>',
$preload_js
);
} else {
wp_enqueue_script( 'wp-api-fetch' );
wp_add_inline_script(
'wp-api-fetch',
$preload_js,
'after'
);
}
}
/**
* Register the rest-api endpoints required for this provider.
*/
function register_rest_routes() : void {
register_rest_route(
'wporg-two-factor/1.0',
'/totp-setup',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_get_totp_setup',
'permission_callback' => function( $request ) {
return current_user_can( 'edit_user', $request['user_id'] );
},
'args' => array(
'user_id' => array(
'required' => true,
'type' => 'number',
),
),
),
);
}
/**
* Rest API endpoint for supplying data needed to set up TOTP.
*/
function rest_get_totp_setup( WP_REST_Request $request ) : array {
$user_id = absint( $request['user_id'] );
$user = get_user_by( 'id', $user_id );
$key = Two_Factor_Totp::generate_key();
return array(
'secret_key' => $key,
'qr_code_url' => Two_Factor_Totp::generate_qr_code_url( $user, $key ),
);
}
/**
* Register any user meta that needs to be exposed.
*
* @codeCoverageIgnore
*/
function register_user_fields(): void {
// Expose the `_new_email` user meta through the rest api as a custom user field.
// This is for "The user has a pending email change"
register_rest_field(
'user',
'pending_email',
[
'get_callback' => function( $user ) {
return get_user_meta( $user['id'], '_new_email', true )['newemail'] ?? false;
},
'update_callback' => function( $value, $user ) {
if ( '' === $value ) {
delete_user_meta( $user->ID, '_new_email' );
return true;
}
},
'schema' => [
'type' => 'string',
'context' => [ 'edit' ],
]
]
);
register_rest_field(
'user',
'2fa_required',
[
'get_callback' => function( $user ) {
return user_requires_2fa( get_userdata( $user['id'] ) );
},
'schema' => [
'type' => 'boolean',
'context' => [ 'edit' ],
]
]
);
// Available providers are enabled _and_ configured.
register_rest_field(
'user',
'2fa_available_providers',
[
'get_callback' => function( $user ) {
return array_keys( Two_Factor_Core::get_available_providers_for_user( get_userdata( $user['id'] ) ) );
},
'schema' => [
'type' => 'array',
'context' => [ 'edit' ],
]
]
);
register_rest_field(
'user',
'2fa_backup_codes_remaining',
[
'get_callback' => function( $user ) {
return Two_Factor_Backup_Codes::codes_remaining_for_user( get_userdata( $user['id'] ) );
},
'schema' => [
'type' => 'int',
'context' => [ 'edit' ],
],
]
);
register_rest_field(
'user',
'2fa_revalidation',
[
'get_callback' => function() {
/*
* Note: This field is always about the authenticated user, NOT the user being requested.
* This should likely be it's own endpoint, but it's here for now, to ensure that
* when an admin is editing other users, they get prompted to update their 2FA as well.
*/
$last_validated = Two_Factor_Core::is_current_user_session_two_factor();
if ( ! $last_validated ) {
return false;
}
$revalidate_url = Two_Factor_Core::get_user_two_factor_revalidate_url( true );
$expiry = apply_filters( 'two_factor_revalidate_time', 10 * MINUTE_IN_SECONDS, get_current_user_id(), '' );
$expires_at = $last_validated + $expiry;
return compact( 'revalidate_url', 'expires_at' );
},
'schema' => [
'type' => 'array',
'context' => [ 'edit' ],
],
]
);
register_rest_field(
'user',
'2fa_webauthn_keys',
[
'get_callback' => function( $user ) {
$keys = WebAuthn_Credential_Store::get_user_keys( get_userdata( $user['id'] ) );
array_walk( $keys, function( & $key ) {
$key->delete_nonce = wp_create_nonce( 'delete-key_' . $key->credential_id );
// Remove unnecessary data.
$key = array_intersect_key( (array) $key, array_flip( [ 'id', 'credential_id', 'name', 'delete_nonce' ] ) );
} );
return $keys;
},
'schema' => [
'type' => 'array',
'context' => [ 'edit' ],
],
]
);
}
/**
* Implement the "Require email confirmation" functionality for the REST API.
*
* TODO: This is a core bug. This should be handled by core.
* TODO: This should be moved to a WordPress.org mu-plugin.
* TODO: This generates urls to /support/wp-admin/profile.php?newuseremail=%s
* bbPress also implements this functionality, through bbp_edit_user_email_send_notification()
*
* @codeCoverageIgnore
*
* @param array $insert_data The user data being updated.
* @return array
*/
function require_email_confirmation( $insert_data, $request ) {
global $errors;
if ( isset( $insert_data->user_email ) ) {
$post_backup = $_POST;
// Switch out the core wp-admin confirmation url with bbPress's.
if ( function_exists( 'bbp_get_user_profile_edit_url' ) ) {
add_filter( 'new_user_email_content', function( $email_text ) {
$user_hash = get_user_meta( $_POST['user_id'], '_new_email', true );
$confirm_url = add_query_arg(
[
'action' => 'bbp-update-user-email',
'newuseremail' => $user_hash['hash']
],
bbp_get_user_profile_edit_url( $_POST['user_id'] )
);
$email_text = str_replace(
'###ADMIN_URL###',
esc_url_raw( $confirm_url ),
$email_text
);
return $email_text;
} );
}
// The POST fields needed by send_confirmation_on_profile_email().
$_POST['user_id'] = $insert_data->ID;
$_POST['email'] = $insert_data->user_email;
send_confirmation_on_profile_email();
if ( $_POST['email'] !== $insert_data->user_email || $errors->has_errors() ) {
$insert_data->user_email = $_POST['email'];
}
$_POST = $post_backup;
}
return $insert_data;
}