Skip to content

Commit d0faaab

Browse files
authored
Merge pull request #88 from jasonbahl/bug/#87-force-auth-secret-to-be-set
#87 - Force Auth Secret to be set, else throw Exception
2 parents 3d6ec67 + 2528f41 commit d0faaab

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Auth {
2323
public static function get_secret_key() {
2424

2525
// Use the defined secret key, if it exists
26-
$secret_key = defined( 'GRAPHQL_JWT_AUTH_SECRET_KEY' ) && ! empty( GRAPHQL_JWT_AUTH_SECRET_KEY ) ? GRAPHQL_JWT_AUTH_SECRET_KEY : 'graphql-jwt-auth';
26+
$secret_key = defined( 'GRAPHQL_JWT_AUTH_SECRET_KEY' ) && ! empty( GRAPHQL_JWT_AUTH_SECRET_KEY ) ? GRAPHQL_JWT_AUTH_SECRET_KEY : null;
2727
return apply_filters( 'graphql_jwt_auth_secret_key', $secret_key );
2828

2929
}

wp-graphql-jwt-authentication.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,20 @@ private static function init() {
189189
* response status to 403.
190190
*/
191191
add_action( 'init_graphql_request', function() {
192-
$token = Auth::validate_token();
193-
if ( is_wp_error( $token ) ) {
194-
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
195-
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
196-
}, 1 );
192+
193+
$jwt_secret = Auth::get_secret_key();
194+
if ( empty( $jwt_secret ) || 'graphql-jwt-auth' === $jwt_secret ) {
195+
throw new \Exception( __( 'You must define the GraphQL JWT Auth secret to use the WPGraphQL JWT Authentication plugin.', 'graphql-jwt-auth' ) );
196+
} else {
197+
$token = Auth::validate_token();
198+
if ( is_wp_error( $token ) ) {
199+
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
200+
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
201+
}, 1 );
202+
}
197203
}
204+
205+
198206
} );
199207

200208
}

0 commit comments

Comments
 (0)