Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds user_secret validation for auth_token and refresh_token #80

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

henrikwirth
Copy link
Collaborator

@henrikwirth henrikwirth commented Feb 15, 2020

Related issue: #79

So I tried myself on some php today and implemented the validation for user_secrets.

  • Added user_secret to auth_token, not just refresh_token
  • Added a validation step, that throws an error if no user_secret is present in the token or the user_secret doesn't match the one in graphql_jwt_auth_secret meta data.
  • Oh and I changed the expiry time to 30 days instead of 365. When I read about expiry times, it was mostly not more than 30 days I think.

If we want to keep the statelessness of auth_tokens, we could make this optional, so you could opt in, to have the user_secret in auth_tokens?

I'd like to give that option actually, so the users can decide themselves. Also we could let them override the expiry time days. What would be the best option to give that choice? A backend option_page or just a define in config.php?

Well it is not the most beautiful code and I haven't written tests for it, but @jasonbahl said the code will likely be refactored and we just wanna get things working for now.

…uth_token, so that the user_secret in the token is the same as the in the DB. If a user refreshes the user_secret, it therefore invalidates all tokens.
@henrikwirth henrikwirth self-assigned this Feb 15, 2020
@henrikwirth
Copy link
Collaborator Author

I just added $auth_token_is_stateful, so we can decide on one point in the code, if the auth_token is stateful or not. Then we only need a way for the user to define this flag.

@jasonbahl
Copy link
Collaborator

I'd like to give that option actually, so the users can decide themselves. Also we could let them override the expiry time days. What would be the best option to give that choice? A backend option_page or just a define in config.php?

For now, if we provide proper hooks/filters, folks can use them to override these types of settings.

Some sort of backend option page I think will need a larger discussion on how to best approach that. Do we want the main WPGraphQL plugin to add a graphql menu item, and extensions can have pages below it? Do we want to have some admin ui class that can be re-used for extension plugins for building setting pages, etc?

I'm hesitant to add admin pages for these plugins as it creates a lot of maintenance overhead, etc.

@jasonbahl
Copy link
Collaborator

Added user_secret to auth_token, not just refresh_token

If we go this way, we can probably go to a single-token flow then? 🤔

The 2-token approach was because the access token couldn't be revoked or otherwise invalidated, but was short lived, so the 2nd token allowed to refresh, but do nothing else.

If the auth-token now has the user_secret as well, the 2 tokens are the same and there's not really any need for both types of tokens, or a refresh flow at all, eh?

Copy link
Collaborator

@jasonbahl jasonbahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments to think through and discuss.

@@ -156,15 +176,17 @@ protected static function get_signed_token( $user, $cap_check = true ) {
'exp' => self::get_token_expiration(),
'data' => [
'user' => [
'id' => $user->data->ID,
'id' => $user->data->ID,
'user_secret' => $secret,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, this is the one thing that makes a refresh token different than an access token. Having this in both tokens makes the tokens identical.

@@ -11,6 +11,8 @@ class Auth {
protected static $issued;
protected static $expiration;
protected static $is_refresh_token = false;
protected static $refresh_token_valid_days = 30;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 30 days is probably a fine default instead of a year. If you're not using an application for more than 30 days, the application asking you to login again seems reasonable.

throw new \Exception( __( 'The User Secret does not match for this user', 'wp-graphql-jwt-authentication' ) );
}
} else if (self::$auth_token_is_stateful || self::$is_refresh_token) {
throw new \Exception( __( 'The User Secret is missing in the token.', 'wp-graphql-jwt-authentication' ) );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. If the secret exists for both auth tokens and refresh tokens, the tokens are the same 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants