forked from WordPress/two-factor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.application-passwords.php
339 lines (306 loc) · 10.1 KB
/
class.application-passwords.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Class for displaying, modifying, & sanitizing application passwords.
*
* @since 0.1-dev
*
* @package Two_Factor
*/
class Application_Passwords {
/**
* The user meta application password key.
* @type string
*/
const USERMETA_KEY_APPLICATION_PASSWORDS = '_application_passwords';
/**
* Add various hooks.
*
* @since 0.1-dev
*
* @access public
* @static
*/
public static function add_hooks() {
add_filter( 'authenticate', array( __CLASS__, 'authenticate' ), 10, 3 );
add_action( 'show_user_security_settings', array( __CLASS__, 'show_user_profile' ) );
add_action( 'personal_options_update', array( __CLASS__, 'catch_submission' ), 0 );
add_action( 'edit_user_profile_update', array( __CLASS__, 'catch_submission' ), 0 );
add_action( 'load-profile.php', array( __CLASS__, 'catch_delete_application_password' ) );
add_action( 'load-user-edit.php', array( __CLASS__, 'catch_delete_application_password' ) );
}
/**
* Filter the user to authenticate.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param WP_User $input_user User to authenticate.
* @param string $username User login.
* @param string $password User password.
*/
public static function authenticate( $input_user, $username, $password ) {
$api_request = ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST );
if ( ! apply_filters( 'application_password_is_api_request', $api_request ) ) {
return $input_user;
}
$user = get_user_by( 'login', $username );
// If the login name is invalid, short circuit.
if ( ! $user ) {
return $input_user;
}
/*
* Strip out anything non-alphanumeric. This is so passwords can be used with
* or without spaces to indicate the groupings for readability.
*/
$password = preg_replace( '/[^a-z\d]/i', '', $password );
$hashed_passwords = get_user_meta( $user->ID, self::USERMETA_KEY_APPLICATION_PASSWORDS, true );
foreach ( $hashed_passwords as $key => $item ) {
if ( wp_check_password( $password, $item['password'], $user->ID ) ) {
$item['last_used'] = time();
$item['last_ip'] = $_SERVER['REMOTE_ADDR'];
$hashed_passwords[ $key ] = $item;
update_user_meta( $user->ID, self::USERMETA_KEY_APPLICATION_PASSWORDS, $hashed_passwords );
return $user;
}
}
// By default, return what we've been passed.
return $input_user;
}
/**
* Display the application password section in a users profile.
*
* This executes during the `show_user_security_settings` action.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param WP_User $user WP_User object of the logged-in user.
*/
public static function show_user_profile( $user ) {
wp_nonce_field( "user_application_passwords-{$user->ID}", '_nonce_user_application_passwords' );
$new_password = null;
$new_password_name = null;
$application_passwords = self::get_user_application_passwords( $user->ID );
if ( $application_passwords ) {
foreach ( $application_passwords as &$application_password ) {
if ( ! empty( $application_password['raw'] ) ) {
$new_password = $application_password['raw'];
$new_password_name = $application_password['name'];
unset( $application_password['raw'] );
}
}
unset( $application_password );
}
// If we've got a new one, update the db record to not save it there any longer.
if ( $new_password ) {
self::set_user_application_passwords( $user->ID, $application_passwords );
}
?>
<div class="application-passwords" id="application-passwords-section">
<h3><?php esc_html_e( 'Application Passwords' ); ?></h3>
<p><?php esc_html_e( 'Application Passwords are used to allow authentication via non-interactive systems, such as XMLRPC, where you would not otherwise be able to use your normal password due to the inability to complete the second factor of authentication.' ); ?></p>
<div class="create-application-password">
<input type="text" size="30" name="new_application_password_name" placeholder="<?php esc_attr_e( 'New Application Password Name' ); ?>" />
<?php submit_button( __( 'Add New' ), 'secondary', 'do_new_application_password', false ); ?>
</div>
<?php if ( $new_password ) : ?>
<p class="new-application-password">
<?php
printf(
esc_html_x( 'Your new password for %1$s is %2$s.', 'application, password' ),
'<strong>' . esc_html( $new_password_name ) . '</strong>',
'<kbd>' . esc_html( self::chunk_password( $new_password ) ) . '</kbd>'
);
?>
</p>
<?php endif; ?>
<?php
require( dirname( __FILE__ ) . '/class.application-passwords-list-table.php' );
// @todo Isn't this class already loaded in Two_Factor_Core::get_providers()?
$application_passwords_list_table = new Application_Passwords_List_Table();
$application_passwords_list_table->items = $application_passwords;
$application_passwords_list_table->prepare_items();
$application_passwords_list_table->display();
?>
</div>
<?php
}
/**
* Catch the non-ajax submission from the new form.
*
* This executes during the `personal_options_update` & `edit_user_profile_update` actions.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param int $user_id User ID.
*/
public static function catch_submission( $user_id ) {
if ( ! empty( $_REQUEST['do_new_application_password'] ) ) {
check_admin_referer( "user_application_passwords-{$user_id}", '_nonce_user_application_passwords' );
self::create_new_application_password( $user_id, sanitize_text_field( $_POST['new_application_password_name'] ) );
wp_safe_redirect( add_query_arg( array(
'new_app_pass' => 1,
), wp_get_referer() ) . '#application-passwords-section' );
exit;
}
}
/**
* Catch the delete application password request.
*
* This executes during the `load-profile.php` & `load-user-edit.php` actions.
*
* @since 0.1-dev
*
* @access public
* @static
*/
public static function catch_delete_application_password() {
$user_id = get_current_user_id();
if ( ! empty( $_REQUEST['delete_application_password'] ) ) {
$slug = $_REQUEST['delete_application_password'];
check_admin_referer( "delete_application_password-{$slug}", '_nonce_delete_application_password' );
self::delete_application_password( $user_id, $slug );
wp_safe_redirect( remove_query_arg( 'new_app_pass', wp_get_referer() ) . '#application-passwords-section' );
}
}
/**
* Generate a new application password.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param int $user_id User ID.
* @param string $name Password name.
* @return string
*/
public static function create_new_application_password( $user_id, $name ) {
$passwords = self::get_user_application_passwords( $user_id );
$new_password = wp_generate_password( 16, false );
$hashed_password = wp_hash_password( $new_password );
$new_item = array(
'name' => $name,
'raw' => $new_password, // THIS LINE GETS DELETED IN SUBSEQUENT REQUEST.
'password' => $hashed_password,
'created' => time(),
'last_used' => null,
'last_ip' => null,
);
if ( ! $passwords ) {
$passwords = array();
}
$passwords[] = $new_item;
self::set_user_application_passwords( $user_id, $passwords );
return self::chunk_password( $new_password );
}
/**
* Generate a link to delete a specified application password.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param array $item The current item.
* @return string
*/
public static function delete_link( $item ) {
$slug = self::password_unique_slug( $item );
$delete_link = add_query_arg( 'delete_application_password', $slug );
$delete_link = wp_nonce_url( $delete_link, "delete_application_password-{$slug}", '_nonce_delete_application_password' );
return sprintf( '<a href="%1$s">%2$s</a>', esc_url( $delete_link ), esc_html__( 'Delete' ) );
}
/**
* Delete a specified application password.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @see Application_Passwords::password_unique_slug()
*
* @param int $user_id User ID.
* @param string $slug The generated slug of the password in question.
* @return bool Whether the password was successfully found and deleted.
*/
public static function delete_application_password( $user_id, $slug ) {
$passwords = self::get_user_application_passwords( $user_id );
foreach ( $passwords as $key => $item ) {
if ( self::password_unique_slug( $item ) === $slug ) {
unset( $passwords[ $key ] );
self::set_user_application_passwords( $user_id, $passwords );
return true;
}
}
// Specified Application Password not found!
return false;
}
/**
* Generate a unique repeateable slug from the hashed password, name, and when it was created.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param array $item The current item.
* @return string
*/
public static function password_unique_slug( $item ) {
$concat = $item['name'] . '|' . $item['password'] . '|' . $item['created'];
$hash = md5( $concat );
return substr( $hash, 0, 12 );
}
/**
* Sanitize and then split a passowrd into smaller chunks.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param string $raw_password Users raw password.
* @return string
*/
public static function chunk_password( $raw_password ) {
$raw_password = preg_replace( '/[^a-z\d]/i', '', $raw_password );
return trim( chunk_split( $raw_password, 4, ' ' ) );
}
/**
* Get a users application passwords.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param int $user_id User ID.
* @return array
*/
public static function get_user_application_passwords( $user_id ) {
return get_user_meta( $user_id, self::USERMETA_KEY_APPLICATION_PASSWORDS, true );
}
/**
* Set a users application passwords.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param int $user_id User ID.
* @param array $passwords Application passwords.
*/
public static function set_user_application_passwords( $user_id, $passwords ) {
return update_user_meta( $user_id, self::USERMETA_KEY_APPLICATION_PASSWORDS, $passwords );
}
}