@@ -114,6 +114,7 @@ public function test_authenticate() {
114
114
'role ' => 'administrator ' ,
115
115
'user_login ' => 'testuser ' ,
116
116
'user_pass ' => 'testpassword ' ,
117
+ 'user_email ' =>
'[email protected] ' ,
117
118
);
118
119
119
120
$ user_id = $ this ->factory ->user ->create ( $ user_data );
@@ -123,14 +124,18 @@ public function test_authenticate() {
123
124
array (
124
125
'data ' => array (
125
126
'user ' => array (
126
- 'id ' => $ user_id ,
127
- 'type ' => 'wp_user ' ,
127
+ 'id ' => $ user_id ,
128
+ 'type ' => 'wp_user ' ,
129
+ 'user_login ' => 'testuser ' ,
130
+ 'user_email ' =>
'[email protected] ' ,
128
131
),
129
132
),
130
133
)
131
134
)
132
135
);
133
136
137
+ add_filter ( 'rest_authentication_is_api_request ' , '__return_true ' );
138
+
134
139
// Another authentication method was used.
135
140
$ this ->assertEquals ( 'alt_auth ' , $ this ->token ->authenticate ( 'alt_auth ' ) );
136
141
@@ -186,6 +191,10 @@ public function test_authenticate() {
186
191
$ authenticate = $ mock ->authenticate ( null );
187
192
$ this ->assertTrue ( $ authenticate );
188
193
$ this ->assertEquals ( $ user_id , get_current_user_id () );
194
+ remove_filter ( 'rest_authentication_is_api_request ' , '__return_true ' );
195
+
196
+ $ authenticate = $ mock ->authenticate ( null );
197
+ $ this ->assertNull ( $ authenticate );
189
198
}
190
199
191
200
/**
@@ -202,6 +211,7 @@ public function test_require_token() {
202
211
'role ' => 'administrator ' ,
203
212
'user_login ' => 'testuser ' ,
204
213
'user_pass ' => 'testpassword ' ,
214
+ 'user_email ' =>
'[email protected] ' ,
205
215
);
206
216
207
217
// @codingStandardsIgnoreStart
@@ -315,8 +325,8 @@ public function test_generate_token() {
315
325
$ user_data = array (
316
326
'role ' => 'administrator ' ,
317
327
'user_login ' => 'testuser ' ,
318
- 'user_email ' =>
'[email protected] ' ,
319
328
'user_pass ' => 'testpassword ' ,
329
+ 'user_email ' =>
'[email protected] ' ,
320
330
);
321
331
322
332
$ request = new WP_REST_Request ( 'POST ' , 'wp/v2/token ' );
@@ -379,6 +389,7 @@ public function test_validate_token() {
379
389
'role ' => 'administrator ' ,
380
390
'user_login ' => 'testuser ' ,
381
391
'user_pass ' => 'testpassword ' ,
392
+ 'user_email ' =>
'[email protected] ' ,
382
393
);
383
394
384
395
$ user_id = $ this ->factory ->user ->create ( $ user_data );
@@ -390,8 +401,10 @@ public function test_validate_token() {
390
401
'exp ' => time () - 1 ,
391
402
'data ' => array (
392
403
'user ' => array (
393
- 'id ' => 10 ,
394
- 'type ' => 'wp_user ' ,
404
+ 'id ' => 10 ,
405
+ 'type ' => 'wp_user ' ,
406
+ 'user_login ' => 'testuser ' ,
407
+ 'user_email ' =>
'[email protected] ' ,
395
408
),
396
409
),
397
410
)
@@ -521,6 +534,43 @@ public function test_validate_token() {
521
534
$ this ->assertTrue ( is_wp_error ( $ validate_token ) );
522
535
$ this ->assertEquals ( $ validate_token ->get_error_code (), 'rest_authentication_token_error ' );
523
536
537
+ // Invalid token, user email has changed.
538
+ wp_update_user (
539
+ array (
540
+ 'ID ' => $ user_id ,
541
+ 'user_email ' =>
'[email protected] ' ,
542
+ )
543
+ );
544
+
545
+ $ mock = $ this ->getMockBuilder ( get_class ( $ this ->token ) )
546
+ ->setMethods (
547
+ array (
548
+ 'jwt ' ,
549
+ )
550
+ )
551
+ ->getMock ();
552
+ $ mock ->method ( 'jwt ' )->willReturn ( $ jwt );
553
+
554
+ $ validate_token = $ mock ->validate_token ();
555
+ $ this ->assertTrue ( is_wp_error ( $ validate_token ) );
556
+ $ this ->assertEquals ( $ validate_token ->get_error_code (), 'rest_authentication_invalid_token_user_email ' );
557
+
558
+ // Invalid token, user login has changed. You cannot change your login, but better safe than sorry.
559
+ $ jwt ->data ->user ->user_login = 'testuser1 ' ;
560
+
561
+ $ mock = $ this ->getMockBuilder ( get_class ( $ this ->token ) )
562
+ ->setMethods (
563
+ array (
564
+ 'jwt ' ,
565
+ )
566
+ )
567
+ ->getMock ();
568
+ $ mock ->method ( 'jwt ' )->willReturn ( $ jwt );
569
+
570
+ $ validate_token = $ mock ->validate_token ();
571
+ $ this ->assertTrue ( is_wp_error ( $ validate_token ) );
572
+ $ this ->assertEquals ( $ validate_token ->get_error_code (), 'rest_authentication_invalid_token_user_login ' );
573
+
524
574
// @codingStandardsIgnoreStart
525
575
unset( $ _SERVER ['HTTP_AUTHORIZATION ' ] );
526
576
// @codingStandardsIgnoreEnd
@@ -590,15 +640,18 @@ public function test_validate_user() {
590
640
'role ' => 'administrator ' ,
591
641
'user_login ' => 'testuser ' ,
592
642
'user_pass ' => 'testpassword ' ,
643
+ 'user_email ' =>
'[email protected] ' ,
593
644
);
594
645
595
646
$ jwt = json_decode (
596
647
wp_json_encode (
597
648
array (
598
649
'data ' => array (
599
650
'user ' => array (
600
- 'id ' => 10 ,
601
- 'type ' => 'wp_user ' ,
651
+ 'id ' => 10 ,
652
+ 'type ' => 'wp_user ' ,
653
+ 'user_login ' => 'testuser ' ,
654
+ 'user_email ' =>
'[email protected] ' ,
602
655
),
603
656
),
604
657
)
@@ -613,8 +666,26 @@ public function test_validate_user() {
613
666
$ this ->assertTrue ( is_wp_error ( $ user_valid ) );
614
667
$ this ->assertEquals ( $ user_valid ->get_error_code (), 'rest_authentication_invalid_token_wp_user ' );
615
668
616
- $ jwt ->data ->user ->id = $ this ->factory ->user ->create ( $ user_data );
617
- $ user_valid = $ this ->token ->validate_user ( $ jwt );
669
+ // Create the user.
670
+ $ jwt ->data ->user ->id = $ this ->factory ->user ->create ( $ user_data );
671
+ $ jwt ->data ->user ->user_login = 'testuser1 ' ;
672
+
673
+ $ user_valid = $ this ->token ->validate_user ( $ jwt );
674
+ $ this ->assertTrue ( is_wp_error ( $ user_valid ) );
675
+ $ this ->assertEquals ( $ user_valid ->get_error_code (), 'rest_authentication_invalid_token_user_login ' );
676
+
677
+ // Change user values.
678
+ $ jwt ->data ->user ->user_login = 'testuser ' ;
679
+ $ jwt->
data ->
user ->
user_email =
'[email protected] ' ;
680
+
681
+ $ user_valid = $ this ->token ->validate_user ( $ jwt );
682
+ $ this ->assertTrue ( is_wp_error ( $ user_valid ) );
683
+ $ this ->assertEquals ( $ user_valid ->get_error_code (), 'rest_authentication_invalid_token_user_email ' );
684
+
685
+ // Reset user email.
686
+ $ jwt->
data ->
user ->
user_email =
'[email protected] ' ;
687
+
688
+ $ user_valid = $ this ->token ->validate_user ( $ jwt );
618
689
$ this ->assertTrue ( $ user_valid );
619
690
}
620
691
0 commit comments