Skip to content

Commit

Permalink
Merge pull request #88 from jasonbahl/bug/#87-force-auth-secret-to-be…
Browse files Browse the repository at this point in the history
…-set

#87 - Force Auth Secret to be set, else throw Exception
  • Loading branch information
jasonbahl authored Apr 1, 2020
2 parents 3d6ec67 + 2528f41 commit d0faaab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Auth {
public static function get_secret_key() {

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

}
Expand Down
18 changes: 13 additions & 5 deletions wp-graphql-jwt-authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,20 @@ private static function init() {
* response status to 403.
*/
add_action( 'init_graphql_request', function() {
$token = Auth::validate_token();
if ( is_wp_error( $token ) ) {
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
}, 1 );

$jwt_secret = Auth::get_secret_key();
if ( empty( $jwt_secret ) || 'graphql-jwt-auth' === $jwt_secret ) {
throw new \Exception( __( 'You must define the GraphQL JWT Auth secret to use the WPGraphQL JWT Authentication plugin.', 'graphql-jwt-auth' ) );
} else {
$token = Auth::validate_token();
if ( is_wp_error( $token ) ) {
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
}, 1 );
}
}


} );

}
Expand Down

0 comments on commit d0faaab

Please sign in to comment.