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

Always ensure the newly created token expiry is not behind from exist… #1643

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ def send_unlock_instructions(opts = {})
def create_token(client: nil, lifespan: nil, cost: nil, **token_extras)
token = DeviseTokenAuth::TokenFactory.create(client: client, lifespan: lifespan, cost: cost)

max_expiration_token = tokens.max_by { |_, token_info| token_info['expiry'] }
max_expiry = max_expiration_token&.dig(1, 'expiry') || 0

tokens[token.client] = {
token: token.token_hash,
expiry: token.expiry
token: token.token_hash,
expiry: [token.expiry, max_expiry + 1].max
Copy link
Collaborator

Choose a reason for hiding this comment

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

why + 1 ?

Copy link
Author

Choose a reason for hiding this comment

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

max_expiry is a timestamp and the expiry for the new token should be bigger than the current one and 1 is enough to achieve the goal

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds good, can you add a test?

Copy link
Author

Choose a reason for hiding this comment

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

@MaicolBen I tried to add but unfortunately cannot run test in my local. there is an instruction error in low level. How can I get help with this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That isn't enough details, anyway if you get an idea on how write it, share it here and I can make it work.

}.merge!(token_extras)

clean_old_tokens
Expand Down