Skip to content

Commit 49897ba

Browse files
authored
Merge pull request #330 from wp-cli/add/application-passwords
Add application password commands
2 parents 42507ed + d5ef2da commit 49897ba

File tree

4 files changed

+838
-4
lines changed

4 files changed

+838
-4
lines changed

entity-command.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
if ( ! class_exists( 'WP_CLI' ) ) {
46
return;
57
}
@@ -63,26 +65,36 @@
6365
'Term_Meta_Command',
6466
array(
6567
'before_invoke' => function() {
66-
if ( \WP_CLI\Utils\wp_version_compare( '4.4', '<' ) ) {
68+
if ( Utils\wp_version_compare( '4.4', '<' ) ) {
6769
WP_CLI::error( 'Requires WordPress 4.4 or greater.' );
6870
}
6971
},
7072
)
7173
);
7274
WP_CLI::add_command( 'user', 'User_Command' );
75+
WP_CLI::add_command(
76+
'user application-password',
77+
'User_Application_Password_Command',
78+
array(
79+
'before_invoke' => function() {
80+
if ( Utils\wp_version_compare( '5.6', '<' ) ) {
81+
WP_CLI::error( 'Requires WordPress 5.6 or greater.' );
82+
}
83+
},
84+
)
85+
);
7386
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
7487
WP_CLI::add_command(
7588
'user session',
7689
'User_Session_Command',
7790
array(
7891
'before_invoke' => function() {
79-
if ( \WP_CLI\Utils\wp_version_compare( '4.0', '<' ) ) {
92+
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
8093
WP_CLI::error( 'Requires WordPress 4.0 or greater.' );
8194
}
8295
},
8396
)
8497
);
85-
8698
WP_CLI::add_command( 'user term', 'User_Term_Command' );
8799

88100
if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
Feature: Manage user custom fields
2+
3+
@less-than-php-8.0
4+
Scenario: User application passwords are disabled for WordPress lower than 5.6
5+
Given a WP install
6+
And I try `wp theme install twentytwenty --activate`
7+
And I run `wp core download --version=5.5 --force`
8+
9+
When I try `wp user application-password create 1 myapp`
10+
Then STDERR should contain:
11+
"""
12+
Error: Requires WordPress 5.6 or greater.
13+
"""
14+
15+
When I try `wp user application-password list 1`
16+
Then STDERR should contain:
17+
"""
18+
Error: Requires WordPress 5.6 or greater.
19+
"""
20+
21+
When I try `wp user application-password get 1 123`
22+
Then STDERR should contain:
23+
"""
24+
Error: Requires WordPress 5.6 or greater.
25+
"""
26+
27+
When I try `wp user application-password delete 1 123`
28+
Then STDERR should contain:
29+
"""
30+
Error: Requires WordPress 5.6 or greater.
31+
"""
32+
33+
@require-wp-5.6
34+
Scenario: User application password CRUD
35+
Given a WP install
36+
37+
When I run `wp user application-password create 1 myapp`
38+
Then STDOUT should not be empty
39+
40+
When I run `wp user application-password list 1`
41+
Then STDOUT should contain:
42+
"""
43+
myapp
44+
"""
45+
46+
When I run `wp user application-password list 1 --name=myapp --field=uuid`
47+
And save STDOUT as {UUID}
48+
And I run `wp user application-password get 1 {UUID}`
49+
Then STDOUT should contain:
50+
"""
51+
myapp
52+
"""
53+
54+
When I try `wp user application-password get 2 {UUID}`
55+
Then STDERR should be:
56+
"""
57+
Error: Invalid user ID, email or login: '2'
58+
"""
59+
And the return code should be 1
60+
61+
When I try `wp user application-password get 1 123`
62+
Then STDERR should be:
63+
"""
64+
Error: No application password found for this user ID and UUID.
65+
"""
66+
And the return code should be 1
67+
68+
When I run `wp user application-password update 1 {UUID} --name=anotherapp`
69+
Then STDOUT should not be empty
70+
71+
When I run `wp user application-password get 1 {UUID}`
72+
Then STDOUT should contain:
73+
"""
74+
anotherapp
75+
"""
76+
Then STDOUT should not contain:
77+
"""
78+
myapp
79+
"""
80+
81+
When I run `wp user application-password delete 1 {UUID}`
82+
Then STDOUT should contain:
83+
"""
84+
Success: Deleted 1 of 1 application password.
85+
"""
86+
87+
When I try `wp user application-password get 1 {UUID}`
88+
Then the return code should be 1
89+
90+
When I run `wp user application-password create 1 myapp1`
91+
And I run `wp user application-password create 1 myapp2`
92+
And I run `wp user application-password create 1 myapp3`
93+
And I run `wp user application-password create 1 myapp4`
94+
And I run `wp user application-password create 1 myapp5`
95+
Then STDOUT should not be empty
96+
97+
When I run `wp user application-password list 1 --format=count`
98+
Then STDOUT should be:
99+
"""
100+
5
101+
"""
102+
103+
When I run `wp user application-password list 1 --name=myapp1 --field=uuid`
104+
And save STDOUT as {UUID1}
105+
And I run `wp user application-password list 1 --name=myapp2 --field=uuid`
106+
And save STDOUT as {UUID2}
107+
When I try `wp user application-password delete 1 {UUID1} {UUID2} nonsense`
108+
Then STDERR should contain:
109+
"""
110+
Warning: Failed to delete UUID nonsense
111+
"""
112+
Then STDOUT should contain:
113+
"""
114+
Success: Deleted 2 of 3 application passwords.
115+
"""
116+
117+
When I run `wp user application-password list 1 --format=count`
118+
Then STDOUT should be:
119+
"""
120+
3
121+
"""
122+
123+
When I run `wp user application-password delete 1 --all`
124+
And I run `wp user application-password list 1 --format=count`
125+
Then STDOUT should be:
126+
"""
127+
0
128+
"""
129+
130+
@require-wp-5.6
131+
Scenario: List user application passwords
132+
Given a WP install
133+
134+
When I run `wp user application-password create 1 myapp1`
135+
Then STDOUT should not be empty
136+
137+
When I run `wp user application-password create 1 myapp2 --app-id=42`
138+
Then STDOUT should not be empty
139+
140+
When I run `wp user application-password list 1 --name=myapp1 --field=uuid`
141+
Then save STDOUT as {UUID1}
142+
143+
When I run `wp user application-password list 1 --name=myapp2 --field=uuid`
144+
Then save STDOUT as {UUID2}
145+
146+
When I run `wp user application-password list 1 --name=myapp1 --field=password`
147+
Then save STDOUT as {HASH1}
148+
149+
When I run `wp user application-password list 1 --name=myapp1 --field=password | sed 's#/#\\\/#g'`
150+
Then save STDOUT as {JSONHASH1}
151+
152+
When I run `wp user application-password list 1 --name=myapp2 --field=password`
153+
Then save STDOUT as {HASH2}
154+
155+
When I run `wp user application-password list 1 --name=myapp2 --field=password | sed 's#/#\\\/#g'`
156+
Then save STDOUT as {JSONHASH2}
157+
158+
When I run `wp user application-password list 1 --name=myapp1 --field=created`
159+
Then save STDOUT as {CREATED1}
160+
161+
When I run `wp user application-password list 1 --name=myapp2 --field=created`
162+
Then save STDOUT as {CREATED2}
163+
164+
When I run `wp user application-password list 1 --format=json`
165+
Then STDOUT should contain:
166+
"""
167+
{"uuid":"{UUID1}","app_id":"","name":"myapp1","password":"{JSONHASH1}","created":{CREATED1},"last_used":null,"last_ip":null}
168+
"""
169+
And STDOUT should contain:
170+
"""
171+
{"uuid":"{UUID2}","app_id":"42","name":"myapp2","password":"{JSONHASH2}","created":{CREATED2},"last_used":null,"last_ip":null}
172+
"""
173+
174+
When I run `wp user application-password list 1 --format=json --fields=uuid,name`
175+
Then STDOUT should contain:
176+
"""
177+
{"uuid":"{UUID1}","name":"myapp1"}
178+
"""
179+
And STDOUT should contain:
180+
"""
181+
{"uuid":"{UUID2}","name":"myapp2"}
182+
"""
183+
184+
When I run `wp user application-password list 1`
185+
Then STDOUT should be a table containing rows:
186+
| uuid | app_id | name | password | created | last_used | last_ip |
187+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
188+
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
189+
190+
When I run `wp user application-password list 1 --fields=uuid,app_id,name`
191+
Then STDOUT should be a table containing rows:
192+
| uuid | app_id | name |
193+
| {UUID2} | 42 | myapp2 |
194+
| {UUID1} | | myapp1 |
195+
196+
When I run `wp user application-password list admin`
197+
Then STDOUT should be a table containing rows:
198+
| uuid | app_id | name | password | created | last_used | last_ip |
199+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
200+
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
201+
202+
When I run `wp user application-password list admin --orderby=created --order=asc`
203+
Then STDOUT should be a table containing rows:
204+
| uuid | app_id | name | password | created | last_used | last_ip |
205+
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
206+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
207+
208+
When I run `wp user application-password list admin --orderby=name --order=asc`
209+
Then STDOUT should be a table containing rows:
210+
| uuid | app_id | name | password | created | last_used | last_ip |
211+
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
212+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
213+
214+
When I run `wp user application-password list admin --orderby=name --order=desc`
215+
Then STDOUT should be a table containing rows:
216+
| uuid | app_id | name | password | created | last_used | last_ip |
217+
| {UUID2} | 42 | myapp2 | {HASH2} | {CREATED2} | | |
218+
| {UUID1} | | myapp1 | {HASH1} | {CREATED1} | | |
219+
220+
When I run `wp user application-password list admin --name=myapp2 --format=json`
221+
Then STDOUT should contain:
222+
"""
223+
myapp2
224+
"""
225+
And STDOUT should not contain:
226+
"""
227+
myapp1
228+
"""
229+
230+
When I run `wp user application-password list admin --field=name`
231+
Then STDOUT should contain:
232+
"""
233+
myapp1
234+
"""
235+
And STDOUT should contain:
236+
"""
237+
myapp2
238+
"""
239+
240+
When I run `wp user application-password list 1 --field=name --app-id=42`
241+
Then STDOUT should be:
242+
"""
243+
myapp2
244+
"""
245+
246+
@require-wp-5.6
247+
Scenario: Get particular user application password hash
248+
Given a WP install
249+
250+
When I run `wp user create testuser testuser@example.com --porcelain`
251+
Then STDOUT should be a number
252+
And save STDOUT as {USER_ID}
253+
254+
When I try the previous command again
255+
Then the return code should be 1
256+
257+
Given I run `wp user application-password create {USER_ID} someapp --porcelain`
258+
And save STDOUT as {PASSWORD}
259+
And I run `wp user application-password list {USER_ID} --name=someapp --field=uuid`
260+
And save STDOUT as {UUID}
261+
262+
Given I run `wp user application-password get {USER_ID} {UUID} --field=password | sed 's/\$/\\\$/g'`
263+
And save STDOUT as {HASH}
264+
265+
When I run `wp eval "var_export( wp_check_password( '{PASSWORD}', '{HASH}', {USER_ID} ) );"`
266+
Then STDOUT should contain:
267+
"""
268+
true
269+
"""

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<exclude-pattern>*/src/Post(_Meta|_Term|_Type)?_Command\.php$</exclude-pattern>
6868
<exclude-pattern>*/src/Site(_Meta|_Option)?_Command\.php$</exclude-pattern>
6969
<exclude-pattern>*/src/Term(_Meta)?_Command\.php$</exclude-pattern>
70-
<exclude-pattern>*/src/User(_Meta|_Session|_Term)?_Command\.php$</exclude-pattern>
70+
<exclude-pattern>*/src/User(_Application_Password|_Meta|_Session|_Term)?_Command\.php$</exclude-pattern>
7171
</rule>
7272

7373
<!-- Whitelisting to provide backward compatibility to classes possibly extending this class. -->

0 commit comments

Comments
 (0)