Skip to content

Commit 1ef08e8

Browse files
Merge pull request #49 from booleanbetrayal/expiry_check_too_permissive
fix(expiry): fix an issue where token expiration checks were too permissive
2 parents a118b95 + 718a4f3 commit 1ef08e8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

app/models/devise_token_auth/concerns/user.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def token_is_current?(token, client_id)
9999
self.tokens[client_id]['expiry'] and
100100
self.tokens[client_id]['token'] and
101101

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

105105
# ensure that the token is valid
106106
BCrypt::Password.new(self.tokens[client_id]['token']) == token

test/models/user_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ class UserTest < ActiveSupport::TestCase
3737
end
3838
end
3939

40+
describe 'token expiry' do
41+
before do
42+
@user = users(:confirmed_email_user)
43+
@user.skip_confirmation!
44+
@user.save!
45+
46+
@auth_headers = @user.create_new_auth_token
47+
48+
@token = @auth_headers['access-token']
49+
@client_id = @auth_headers['client']
50+
end
51+
52+
test 'should properly indicate whether token is current' do
53+
assert @user.token_is_current?(@token, @client_id)
54+
# we want to update the expiry without forcing a cleanup (see below)
55+
@user.tokens[@client_id]['expiry'] = Time.now.to_i - 10.seconds
56+
refute @user.token_is_current?(@token, @client_id)
57+
end
58+
end
59+
4060
describe 'expired tokens are destroyed on save' do
4161
before do
4262
@user = users(:confirmed_email_user)

0 commit comments

Comments
 (0)