Skip to content

Commit 44f7e1d

Browse files
committed
- update firebase/php-jwt to v6.1.0
- change message about defining JWT Secret from exception to graphql_debug message - update encode/decode signatures to use new Key method - remove HTTP_AUTHORIZATION from test setup - update some tests
1 parent f451835 commit 44f7e1d

File tree

14 files changed

+362
-265
lines changed

14 files changed

+362
-265
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"wpunit-test": "vendor/bin/codecept run wpunit"
1818
},
1919
"require": {
20-
"firebase/php-jwt": "^5.0"
20+
"firebase/php-jwt": "6.1.0"
2121
},
2222
"require-dev": {
2323
"lucatume/wp-browser": "3.1.0",

src/Auth.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace WPGraphQL\JWT_Authentication;
44

5+
use Exception;
56
use Firebase\JWT\JWT;
7+
use Firebase\JWT\Key;
68
use GraphQL\Error\UserError;
7-
use WPGraphQL\Data\DataSource;
9+
use WPGraphQL\Model\User;
810

911
class Auth {
1012

@@ -35,7 +37,7 @@ public static function get_secret_key() {
3537
* @param string $password
3638
*
3739
* @return mixed
38-
* @throws \Exception
40+
* @throws Exception
3941
* @since 0.0.1
4042
*/
4143
public static function login_and_get_token( $username, $password ) {
@@ -44,7 +46,7 @@ public static function login_and_get_token( $username, $password ) {
4446
* First thing, check the secret key if not exist return a error
4547
*/
4648
if ( empty( self::get_secret_key() ) ) {
47-
throw new UserError( __( 'JWT Auth is not configured correctly. Please contact a site administrator.', 'wp-graphql-jwt-authentication' ) );
49+
return new UserError( __( 'JWT Auth is not configured correctly. Please contact a site administrator.', 'wp-graphql-jwt-authentication' ) );
4850
}
4951

5052
/**
@@ -78,7 +80,7 @@ public static function login_and_get_token( $username, $password ) {
7880
$response = [
7981
'authToken' => self::get_signed_token( wp_get_current_user() ),
8082
'refreshToken' => self::get_refresh_token( wp_get_current_user() ),
81-
'user' => DataSource::resolve_user( $user->data->ID, \WPGraphQL::get_app_context() ),
83+
'user' => new User( $user ),
8284
'id' => $user->data->ID,
8385
];
8486

@@ -88,7 +90,7 @@ public static function login_and_get_token( $username, $password ) {
8890
* @param \WP_User $user The authenticated user
8991
* @param array $response The default response
9092
*/
91-
$response = apply_filters( 'graphql_jwt_auth_after_authenticate', $user, $response );
93+
$response = apply_filters( 'graphql_jwt_auth_after_authenticate', $response, $user );
9294

9395
return ! empty( $response ) ? $response : [];
9496
}
@@ -186,7 +188,7 @@ protected static function get_signed_token( $user, $cap_check = true ) {
186188
* Encode the token
187189
*/
188190
JWT::$leeway = 60;
189-
$token = JWT::encode( $token, self::get_secret_key() );
191+
$token = JWT::encode( $token, self::get_secret_key(), 'HS256' );
190192

191193
/**
192194
* Filter the token before returning it, allowing for individual systems to override what's returned.
@@ -392,7 +394,7 @@ protected static function authenticate_user( $username, $password ) {
392394
* @param (int|bool) $user Logged User ID
393395
*
394396
* @return mixed|false|\WP_User
395-
* @throws \Exception
397+
* @throws Exception
396398
*/
397399
public static function filter_determine_current_user( $user ) {
398400

@@ -531,7 +533,7 @@ protected static function set_status( $status_code ) {
531533
*
532534
* @param string $token The encoded JWT Token
533535
*
534-
* @throws \Exception
536+
* @throws Exception
535537
* @return mixed|boolean|string
536538
*/
537539
public static function validate_token( $token = null, $refresh = false ) {
@@ -577,26 +579,24 @@ public static function validate_token( $token = null, $refresh = false ) {
577579
return new \WP_Error( 'invalid-secret-key', __( 'JWT is not configured properly', 'wp-graphql-jwt-authentication' ) );
578580
}
579581

580-
581-
582582
/**
583583
* Decode the Token
584584
*/
585585
JWT::$leeway = 60;
586586

587-
$secret = self::get_secret_key();
587+
codecept_debug( [ 'tokenYo' => $token ] );
588588

589589
try {
590-
$token = ! empty( $token ) ? JWT::decode( $token, $secret, [ 'HS256' ] ) : null;
591-
} catch ( \Exception $exception ) {
592-
return new \WP_Error( 'invalid-secret-key', $exception->getMessage() );
590+
$token = ! empty( $token ) ? JWT::decode( $token, new Key( self::get_secret_key(), 'HS256') ) : null;
591+
} catch ( Exception $exception ) {
592+
$token = new \WP_Error( 'invalid-secret-key', $exception->getMessage() );
593593
}
594594

595595
/**
596596
* If there's no token listed, just bail now before validating an empty token.
597597
* This will treat the request as a public request
598598
*/
599-
if ( empty( $token ) ) {
599+
if ( empty( $token ) || is_wp_error( $token ) ) {
600600
return $token;
601601
}
602602

@@ -614,7 +614,7 @@ public static function validate_token( $token = null, $refresh = false ) {
614614
* The Token is decoded now validate the iss
615615
*/
616616

617-
if ( ! isset( $token->iss ) || !in_array($token->iss, $allowed_domains) ) {
617+
if ( ! isset( $token->iss ) || ! in_array( $token->iss, $allowed_domains ) ) {
618618
// See https://github.com/wp-graphql/wp-graphql-jwt-authentication/issues/111
619619
self::set_status(401);
620620
return new \WP_Error( 'invalid-jwt', __( 'The iss do not match with this server', 'wp-graphql-jwt-authentication' ) );

tests/wpunit/AuthenticationTest.php

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ class AuthenticationTest extends \Codeception\TestCase\WPTestCase {
1212

1313
public $admin;
1414
public $login_mutation;
15+
public $admin_username;
16+
public $admin_password;
1517

1618
/**
1719
* This function is run before each method
1820
* @since 0.0.5
1921
*/
2022
public function setUp(): void {
2123

22-
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer goo';
2324

2425
add_filter( 'graphql_debug_enabled', '__return_true' );
2526
add_filter( 'graphql_jwt_auth_secret_key', function() {
@@ -28,10 +29,13 @@ public function setUp(): void {
2829

2930
parent::setUp();
3031

32+
$this->admin_password = 'testPassword';
33+
$this->admin_username = 'testuser';
34+
3135
$this->admin = $this->factory->user->create( [
3236
'role' => 'administrator',
33-
'user_login' => 'testuser',
34-
'user_pass' => 'testPassword',
37+
'user_login' => $this->admin_username,
38+
'user_pass' => $this->admin_password,
3539
] );
3640

3741

@@ -44,9 +48,13 @@ public function setUp(): void {
4448
pages{
4549
edges{
4650
node{
47-
id
4851
title
4952
content
53+
author {
54+
node {
55+
databaseId
56+
}
57+
}
5058
}
5159
}
5260
}
@@ -77,9 +85,9 @@ public function testLoginWithBadCredentials() {
7785
'query' => $this->login_mutation,
7886
'variables' => [
7987
'input' => [
80-
'username' => 'testuser',
88+
'username' => $this->admin_username,
8189
'password' => 'badPassword',
82-
'clientMutationId' => uniqid(),
90+
'clientMutationId' => uniqid( '', true ),
8391
]
8492
]
8593
]);
@@ -125,9 +133,8 @@ public function testLoginWithPage() {
125133
'query' => $this->login_mutation,
126134
'variables' => [
127135
'input' => [
128-
'username' => 'testuser',
129-
'password' => 'testPassword',
130-
'clientMutationId' => uniqid(),
136+
'username' => $this->admin_username,
137+
'password' => $this->admin_password,
131138
]
132139
]
133140
]);
@@ -138,14 +145,18 @@ public function testLoginWithPage() {
138145
* Establish the expectation for the output of the query
139146
*/
140147
$expected_user = [
141-
'username' => 'testuser',
148+
'username' => $this->admin_username,
142149
'pages' => [
143150
'edges' => [
144151
[
145152
'node' => [
146-
'id' => $global_id,
147153
'title' => 'Test Page Title',
148154
'content' => apply_filters( 'the_content', $args['post_content'] ),
155+
'author' => [
156+
'node' => [
157+
'databaseId' => $this->admin
158+
],
159+
],
149160
],
150161
],
151162
],
@@ -175,9 +186,9 @@ public function testLoginWithNoSecretKeyConfigured() {
175186
'query' => $this->login_mutation,
176187
'variables' => [
177188
'input' => [
178-
'username' => 'testuser',
179-
'password' => 'testPassword',
180-
'clientMutationId' => uniqid(),
189+
'username' => $this->admin_username,
190+
'password' => $this->admin_password,
191+
'clientMutationId' => uniqid( '', true ),
181192
]
182193
]
183194
] );
@@ -189,14 +200,16 @@ public function testLoginWithNoSecretKeyConfigured() {
189200

190201
}
191202

203+
public function filter_authentication () {
204+
return 'goo';
205+
}
206+
192207
public function testLoginWithValidUserThatWasJustDeleted() {
193208

194209
/**
195210
* Filter the authentication to make sure it returns an error
196211
*/
197-
add_filter( 'authenticate', function() {
198-
return 'goo';
199-
}, 9999 );
212+
add_filter( 'authenticate', [ $this, 'filter_authentication'], 9999 );
200213

201214
/**
202215
* Run the GraphQL query
@@ -205,9 +218,9 @@ public function testLoginWithValidUserThatWasJustDeleted() {
205218
'query' => $this->login_mutation,
206219
'variables' => [
207220
'input' => [
208-
'username' => 'testuser',
209-
'password' => 'testPassword',
210-
'clientMutationId' => uniqid(),
221+
'username' => $this->admin_username,
222+
'password' => $this->admin_password,
223+
'clientMutationId' => uniqid( '', true ),
211224
]
212225
]
213226
]);
@@ -217,6 +230,8 @@ public function testLoginWithValidUserThatWasJustDeleted() {
217230
*/
218231
$this->assertArrayHasKey( 'errors', $actual );
219232

233+
remove_filter( 'authenticate', [ $this, 'filter_authentication'], 9999 );
234+
220235
}
221236

222237
public function testNonAuthenticatedRequest() {
@@ -267,6 +282,8 @@ public function testRequestWithNoToken() {
267282

268283
public function testRequestWithInvalidToken() {
269284

285+
wp_set_current_user( $this->admin );
286+
270287
add_filter( 'graphql_jwt_auth_token_before_sign', function( $token ) {
271288
$token['iss'] = null;
272289
return $token;
@@ -279,11 +296,15 @@ public function testRequestWithInvalidToken() {
279296
return 'Bearer ' . $token;
280297
} );
281298

299+
codecept_debug( [ 'invalidToken' => $token ]);
300+
282301
/**
283302
* Validate the token (should not work because we filtered the iss to make it invalid)
284303
*/
285304
$token = \WPGraphQL\JWT_Authentication\Auth::validate_token( $token );
286305

306+
codecept_debug( $token );
307+
287308
/**
288309
* Validate token should return nothing if it can't be validated properly
289310
*/
@@ -296,27 +317,40 @@ public function testRequestWithInvalidToken() {
296317
*/
297318
public function testNoSecretKey() {
298319

320+
// $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer goo';
321+
299322
/**
300323
* Filter the secret key to return null, which should cause an exception to be thrown
301324
*/
302325
add_filter( 'graphql_jwt_auth_secret_key', function() {
303326
return null;
304327
} );
305328

306-
/**
307-
* Set our expected exception
308-
*/
309-
$this->expectException( 'Exception', 'JWT is not configured properly' );
310329

311330
/**
312331
* Run the function to determine the current user
313332
*/
314333
$user = \WPGraphQL\JWT_Authentication\Auth::filter_determine_current_user( 0 );
315334

335+
codecept_debug( [ 'user' => $user ] );
336+
337+
$actual = graphql([
338+
'query' => $this->login_mutation,
339+
'variables' => [
340+
'input' => [
341+
'username' => $this->admin_username,
342+
'password' => $this->admin_password,
343+
]
344+
]
345+
]);
346+
347+
codecept_debug( $actual );
348+
316349
/**
317350
* Ensure that the Exception prevented any user from being authenticated
318351
*/
319-
$this->assertEquals( 0, $user );
352+
$this->assertNull( $actual['data']['login'] );
353+
$this->assertArrayHasKey( 'errors', $actual );
320354

321355
}
322356

vendor/composer/InstalledVersions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class InstalledVersions
3030
'aliases' =>
3131
array (
3232
),
33-
'reference' => 'e09174b283f67cd17517d966c90d8800f66e1854',
33+
'reference' => 'f4518357b5349b7325e8c974f5deec2cdcf1ae1a',
3434
'name' => 'wp-graphql/wp-graphql-jwt-authentication',
3535
),
3636
'versions' =>
@@ -215,12 +215,12 @@ class InstalledVersions
215215
),
216216
'firebase/php-jwt' =>
217217
array (
218-
'pretty_version' => 'v5.5.1',
219-
'version' => '5.5.1.0',
218+
'pretty_version' => 'v6.1.0',
219+
'version' => '6.1.0.0',
220220
'aliases' =>
221221
array (
222222
),
223-
'reference' => '83b609028194aa042ea33b5af2d41a7427de80e6',
223+
'reference' => 'fbb2967a3a68b07e37678c00c0cf51165051495f',
224224
),
225225
'flow/jsonpath' =>
226226
array (
@@ -971,7 +971,7 @@ class InstalledVersions
971971
'aliases' =>
972972
array (
973973
),
974-
'reference' => 'e09174b283f67cd17517d966c90d8800f66e1854',
974+
'reference' => 'f4518357b5349b7325e8c974f5deec2cdcf1ae1a',
975975
),
976976
'zordius/lightncandy' =>
977977
array (

0 commit comments

Comments
 (0)