-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathUser_Application_Password_Command.php
More file actions
576 lines (510 loc) · 16.9 KB
/
User_Application_Password_Command.php
File metadata and controls
576 lines (510 loc) · 16.9 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
use WP_CLI\ExitException;
use WP_CLI\Fetchers\User as UserFetcher;
use WP_CLI\Formatter;
use WP_CLI\Utils;
/**
* Creates, updates, deletes, lists and retrieves application passwords.
*
* ## EXAMPLES
*
* # List user application passwords and only show app name and password hash
* $ wp user application-password list 123 --fields=name,password
* +--------+------------------------------------+
* | name | password |
* +--------+------------------------------------+
* | myapp | $P$BVGeou1CUot114YohIemgpwxQCzb8O/ |
* +--------+------------------------------------+
*
* # Get a specific application password and only show app name and created timestamp
* $ wp user application-password get 123 6633824d-c1d7-4f79-9dd5-4586f734d69e --fields=name,created
* +--------+------------+
* | name | created |
* +--------+------------+
* | myapp | 1638395611 |
* +--------+------------+
*
* # Create user application password
* $ wp user application-password create 123 myapp
* Success: Created application password.
* Password: ZG1bxdxdzjTwhsY8vK8l1C65
*
* # Only print the password without any chrome
* $ wp user application-password create 123 myapp --porcelain
* ZG1bxdxdzjTwhsY8vK8l1C65
*
* # Update an existing application password
* $ wp user application-password update 123 6633824d-c1d7-4f79-9dd5-4586f734d69e --name=newappname
* Success: Updated application password.
*
* # Delete an existing application password
* $ wp user application-password delete 123 6633824d-c1d7-4f79-9dd5-4586f734d69e
* Success: Deleted 1 of 1 application password.
*
* # Check if an application password for a given application exists
* $ wp user application-password exists 123 myapp
* $ echo $?
* 1
*
* # Bash script for checking whether an application password exists and creating one if not
* if ! wp user application-password exists 123 myapp; then
* PASSWORD=$(wp user application-password create 123 myapp --porcelain)
* fi
*/
final class User_Application_Password_Command {
/**
* List of application password fields.
*
* @var array<string>
*/
const APPLICATION_PASSWORD_FIELDS = [
'uuid',
'app_id',
'name',
'password',
'created',
'last_used',
'last_ip',
];
/**
* Lists all application passwords associated with a user.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to get application passwords for.
*
* [--<field>=<value>]
* : Filter the list by a specific field.
*
* [--field=<field>]
* : Prints the value of a single field for each application password.
*
* [--fields=<fields>]
* : Limit the output to specific fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* - ids
* ---
*
* [--orderby=<fields>]
* : Set orderby which field.
* ---
* default: created
* options:
* - uuid
* - app_id
* - name
* - password
* - created
* - last_used
* - last_ip
* ---
*
* [--order=<order>]
* : Set ascending or descending order.
* ---
* default: desc
* options:
* - asc
* - desc
* ---
*
* ## EXAMPLES
*
* # List user application passwords and only show app name and password hash
* $ wp user application-password list 123 --fields=name,password
* +--------+------------------------------------+
* | name | password |
* +--------+------------------------------------+
* | myapp | $P$BVGeou1CUot114YohIemgpwxQCzb8O/ |
* +--------+------------------------------------+
*
* @subcommand list
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the user could not be found/parsed.
* @throws ExitException If the application passwords could not be retrieved.
*/
public function list_( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
list( $user_id ) = $args;
$application_passwords = WP_Application_Passwords::get_user_application_passwords( $user_id );
if ( $application_passwords instanceof WP_Error ) {
WP_CLI::error( $application_passwords );
}
if ( empty( $application_passwords ) ) {
$application_passwords = [];
}
$order = Utils\get_flag_value( $assoc_args, 'order' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby' );
usort(
$application_passwords,
static function ( $a, $b ) use ( $orderby, $order ) {
// Sort array.
return 'asc' === $order
? $a[ $orderby ] <=> $b[ $orderby ]
: $b[ $orderby ] <=> $a[ $orderby ];
}
);
$fields = self::APPLICATION_PASSWORD_FIELDS;
// Avoid confusion regarding the dash/underscore usage.
foreach ( [ 'app-id', 'last-used', 'last-ip' ] as $flag ) {
if ( array_key_exists( $flag, $assoc_args ) ) {
$underscored_flag = str_replace( '-', '_', $flag );
$assoc_args[ $underscored_flag ] = $assoc_args[ $flag ];
unset( $assoc_args[ $flag ] );
}
}
foreach ( $fields as $field ) {
if ( ! array_key_exists( $field, $assoc_args ) ) {
continue;
}
$value = Utils\get_flag_value( $assoc_args, $field );
$application_passwords = array_filter(
$application_passwords,
static function ( $application_password ) use ( $field, $value ) {
return $application_password[ $field ] === $value;
}
);
}
if ( ! empty( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
}
$format = Utils\get_flag_value( $assoc_args, 'format', 'table' );
$formatter = new Formatter( $assoc_args, $fields );
if ( 'ids' === $format ) {
$formatter->display_items( wp_list_pluck( $application_passwords, 'uuid' ) );
} else {
$formatter->display_items( $application_passwords );
}
}
/**
* Gets a specific application password.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to get the application password for.
*
* <uuid>
* : The universally unique ID of the application password.
*
* [--field=<field>]
* : Prints the value of a single field for the application password.
*
* [--fields=<fields>]
* : Limit the output to specific fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get a specific application password and only show app name and created timestamp
* $ wp user application-password get 123 6633824d-c1d7-4f79-9dd5-4586f734d69e --fields=name,created
* +--------+------------+
* | name | created |
* +--------+------------+
* | myapp | 1638395611 |
* +--------+------------+
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application passwords could not be retrieved.
*/
public function get( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
list( $user_id, $uuid ) = $args;
$application_password = WP_Application_Passwords::get_user_application_password( $user_id, $uuid );
if ( $application_password instanceof WP_Error ) {
WP_CLI::error( $application_password );
}
if ( null === $application_password ) {
WP_CLI::error( 'No application password found for this user ID and UUID.' );
}
$fields = self::APPLICATION_PASSWORD_FIELDS;
if ( ! empty( $assoc_args['fields'] ) ) {
$fields = explode( ',', $assoc_args['fields'] );
}
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( [ $application_password ] );
}
/**
* Creates a new application password.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to create a new application password for.
*
* <app-name>
* : Unique name of the application to create an application password for.
*
* [--app-id=<app-id>]
* : Application ID to attribute to the application password.
*
* [--porcelain]
* : Output just the new password.
*
* ## EXAMPLES
*
* # Create user application password
* $ wp user application-password create 123 myapp
* Success: Created application password.
* Password: ZG1bxdxdzjTwhsY8vK8l1C65
*
* # Only print the password without any chrome
* $ wp user application-password create 123 myapp --porcelain
* ZG1bxdxdzjTwhsY8vK8l1C65
*
* # Create user application with a custom application ID for internal tracking
* $ wp user application-password create 123 myapp --app-id=42 --porcelain
* ZG1bxdxdzjTwhsY8vK8l1C65
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application password could not be created.
*/
public function create( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
/**
* @var string $user_id
* @var string $app_name
*/
list( $user_id, $app_name ) = $args;
/**
* @var string $app_id
*/
$app_id = Utils\get_flag_value( $assoc_args, 'app-id', '' );
$arguments = [
'name' => $app_name,
'app_id' => $app_id,
];
$result = WP_Application_Passwords::create_new_application_password( (int) $user_id, $arguments );
if ( $result instanceof WP_Error ) {
WP_CLI::error( $result );
}
if ( Utils\get_flag_value( $assoc_args, 'porcelain', false ) ) {
WP_CLI::line( $result[0] );
WP_CLI::halt( 0 );
}
WP_CLI::success( 'Created application password.' );
WP_CLI::line( "Password: {$result[0]}" );
}
/**
* Updates an existing application password.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to update the application password for.
*
* <uuid>
* : The universally unique ID of the application password.
*
* [--<field>=<value>]
* : Update the <field> with a new <value>. Currently supported fields: name.
*
* ## EXAMPLES
*
* # Update an existing application password
* $ wp user application-password update 123 6633824d-c1d7-4f79-9dd5-4586f734d69e --name=newappname
* Success: Updated application password.
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application password could not be created.
*/
public function update( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
list( $user_id, $uuid ) = $args;
$result = WP_Application_Passwords::update_application_password( $user_id, $uuid, $assoc_args );
if ( $result instanceof WP_Error ) {
WP_CLI::error( $result );
}
WP_CLI::success( 'Updated application password.' );
}
/**
* Record usage of an application password.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to update the application password for.
*
* <uuid>
* : The universally unique ID of the application password.
*
* ## EXAMPLES
*
* # Record usage of an application password
* $ wp user application-password record-usage 123 6633824d-c1d7-4f79-9dd5-4586f734d69e
* Success: Recorded application password usage.
*
* @subcommand record-usage
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application password could not be created.
*/
public function record_usage( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$args = $this->replace_login_with_user_id( $args );
list( $user_id, $uuid ) = $args;
$result = WP_Application_Passwords::record_application_password_usage( $user_id, $uuid );
if ( $result instanceof WP_Error ) {
WP_CLI::error( $result );
}
WP_CLI::success( 'Recorded application password usage.' );
}
/**
* Delete an existing application password.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to delete the application password for.
*
* [<uuid>...]
* : Comma-separated list of UUIDs of the application passwords to delete.
*
* [--all]
* : Delete all of the user's application password.
*
* ## EXAMPLES
*
* # Delete an existing application password
* $ wp user application-password delete 123 6633824d-c1d7-4f79-9dd5-4586f734d69e
* Success: Deleted 1 of 1 application password.
*
* # Delete all of the user's application passwords
* $ wp user application-password delete 123 --all
* Success: Deleted all application passwords.
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application password could not be created.
*/
public function delete( $args, $assoc_args ) {
$args = $this->replace_login_with_user_id( $args );
$user_id = array_shift( $args );
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$count = count( $args );
if ( ( 0 < $count && true === $all ) || ( 0 === $count && true !== $all ) ) {
WP_CLI::error( 'You need to specify either one or more UUIDS or provide the --all flag' );
}
if ( true === $all ) {
$result = WP_Application_Passwords::delete_all_application_passwords( $user_id );
if ( $result instanceof WP_Error ) {
WP_CLI::error( $result );
}
WP_CLI::success( 'Deleted all application passwords.' );
WP_CLI::halt( 0 );
}
$errors = 0;
foreach ( $args as $uuid ) {
$result = WP_Application_Passwords::delete_application_password( $user_id, $uuid );
if ( $result instanceof WP_Error ) {
WP_CLI::warning( "Failed to delete UUID {$uuid}: " . $result->get_error_message() );
++$errors;
}
}
WP_CLI::success(
sprintf(
'Deleted %d of %d application %s.',
$count - $errors,
$count,
Utils\pluralize( 'password', $count )
)
);
WP_CLI::halt( 0 === $errors ? 0 : 1 );
}
/**
* Checks whether an application password for a given application exists.
*
* ## OPTIONS
*
* <user>
* : The user login, user email, or user ID of the user to check the existence of an application password for.
*
* <app-name>
* : Name of the application to check the existence of an application password for.
*
* ## EXAMPLES
*
* # Check if an application password for a given application exists
* $ wp user application-password exists 123 myapp
* $ echo $?
* 1
*
* # Bash script for checking whether an application password exists and creating one if not
* if ! wp user application-password exists 123 myapp; then
* PASSWORD=$(wp user application-password create 123 myapp --porcelain)
* fi
*
* @param array $args Indexed array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
* @throws ExitException If the application password could not be created.
*/
public function exists( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$args = $this->replace_login_with_user_id( $args );
list( $user_id, $app_name ) = $args;
$result = $this->application_name_exists_for_user( $user_id, $app_name );
if ( $result instanceof WP_Error ) {
WP_CLI::error( $result );
}
WP_CLI::halt( $result ? 0 : 1 );
}
/**
* Replaces user_login value with user ID.
*
* @param array $args Associative array of arguments.
* @return array Associative array of arguments with the user login replaced with an ID.
* @throws ExitException If the user is not found.
*/
private function replace_login_with_user_id( $args ) {
$user = ( new UserFetcher() )->get_check( $args[0] );
$args[0] = $user->ID;
return $args;
}
/**
* Checks if application name exists for the given user.
*
* This is a polyfill for WP_Application_Passwords::get_user_application_passwords(), which was only added for
* WordPress 5.7+, but we're already supporting application passwords for WordPress 5.6+.
*
* @param int $user_id User ID to check the application passwords for.
* @param string $app_name Application name to look for.
* @return bool
*/
private function application_name_exists_for_user( $user_id, $app_name ) {
if ( Utils\wp_version_compare( '5.7', '<' ) ) {
$passwords = WP_Application_Passwords::get_user_application_passwords( $user_id );
foreach ( $passwords as $password ) {
if ( strtolower( $password['name'] ) === strtolower( $app_name ) ) {
return true;
}
}
return false;
}
return WP_Application_Passwords::application_name_exists_for_user( $user_id, $app_name );
}
}