Skip to content

fix(expiry): fix an issue where token expiration checks were too permissive #49

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

Merged
merged 1 commit into from
Oct 21, 2014
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def token_is_current?(token, client_id)
self.tokens[client_id]['expiry'] and
self.tokens[client_id]['token'] and

# ensure that the token was created within the last two weeks
DateTime.strptime(self.tokens[client_id]['expiry'].to_s, '%s') > DeviseTokenAuth.token_lifespan.ago and
# ensure that the token has not yet expired
DateTime.strptime(self.tokens[client_id]['expiry'].to_s, '%s') > Time.now and

# ensure that the token is valid
BCrypt::Password.new(self.tokens[client_id]['token']) == token
Expand Down
20 changes: 20 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ class UserTest < ActiveSupport::TestCase
end
end

describe 'token expiry' do
before do
@user = users(:confirmed_email_user)
@user.skip_confirmation!
@user.save!

@auth_headers = @user.create_new_auth_token

@token = @auth_headers['access-token']
@client_id = @auth_headers['client']
end

test 'should properly indicate whether token is current' do
assert @user.token_is_current?(@token, @client_id)
# we want to update the expiry without forcing a cleanup (see below)
@user.tokens[@client_id]['expiry'] = Time.now.to_i - 10.seconds
refute @user.token_is_current?(@token, @client_id)
end
end

describe 'expired tokens are destroyed on save' do
before do
@user = users(:confirmed_email_user)
Expand Down