Skip to content

Commit ad6db03

Browse files
committed
Take min validity into consideration
1 parent 0cc3bc7 commit ad6db03

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ Style/EachWithObject:
1414
Style/ExpandPathArguments:
1515
Enabled: false
1616

17-
Metrics/CyclomaticComplexity:
18-
Max: 7
19-
2017
Metrics/ClassLength:
21-
Max: 104
18+
Max: 105

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
1515
- Fix logging to `$stdout` of request and response bodies via Faraday's logger and `ENV["OAUTH_DEBUG"] == 'true'`
1616
- Read issued_at and expires_at from the token instead of using Time.now [#391]
1717
- Take clock skew into consideration when checking expired? [#391]
18+
- Take minimum validity into consideration when checking expired? [#391]
1819

1920
## [1.4.0] - 2017-06-09
2021

lib/oauth2/access_token.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module OAuth2
22
class AccessToken
3+
MIN_VALIDITY = 30
4+
35
attr_reader :client, :token, :expires_in, :issued_at, :expires_at, :params, :time_skew
46
attr_accessor :options, :refresh_token, :response
57

@@ -81,7 +83,7 @@ def expires?
8183
#
8284
# @return [Boolean]
8385
def expired?
84-
expires? && (expires_at + time_skew <= Time.now.to_i)
86+
expires? && (expires_at + time_skew - MIN_VALIDITY <= Time.now.to_i)
8587
end
8688

8789
# Refreshes the current Access Token

spec/oauth2/access_token_spec.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,23 @@ def assert_initialized_token(target) # rubocop:disable Metrics/AbcSize
152152
expect(access).to be_expired
153153
end
154154

155-
describe 'time skew' do
156-
let(:time_skew) { 10 }
155+
describe 'min validity' do
156+
let(:old_now) { 1_528_454_438 }
157157
let(:expires_in) { 300 }
158-
let(:expires_at) { Time.now.to_i - 10 + expires_in }
158+
let(:expires_at) { 1_528_454_438 + expires_in }
159159
let!(:access) { described_class.new(client, token, :refresh_token => 'abaca', :expires_at => expires_at, :expires_in => expires_in) }
160+
let(:now) { Time.at(expires_at) - AccessToken::MIN_VALIDITY }
160161

161-
context 'when not within time skew correction' do
162-
let(:now) { Time.at(expires_at) + time_skew + 1 }
163-
162+
context 'when not within min validity correction' do
164163
it 'access is expired' do
165164
allow(Time).to receive(:now).and_return(now)
166165
expect(access).to be_expired
167166
end
168167
end
169168

170-
context 'when within time skew correction' do
171-
let(:now) { Time.at(expires_at) + time_skew - 1 }
172-
169+
context 'when within min validity correction' do
173170
it 'access is not expired' do
171+
allow(Time).to receive(:now).and_return(now - 1)
174172
expect(access).not_to be_expired
175173
end
176174
end
@@ -256,7 +254,7 @@ def assert_initialized_token(target) # rubocop:disable Metrics/AbcSize
256254
end
257255

258256
context 'when not within time skew correction' do
259-
let(:local_now) { Time.at(exp) + time_skew + 1 }
257+
let(:local_now) { Time.at(exp) + time_skew - AccessToken::MIN_VALIDITY }
260258

261259
it 'access is expired' do
262260
allow(Time).to receive(:now).and_return(local_now)
@@ -265,7 +263,7 @@ def assert_initialized_token(target) # rubocop:disable Metrics/AbcSize
265263
end
266264

267265
context 'when within time skew correction' do
268-
let(:local_now) { Time.at(exp) + time_skew - 1 }
266+
let(:local_now) { Time.at(exp) + time_skew - AccessToken::MIN_VALIDITY - 1 }
269267

270268
it 'access is not expired' do
271269
allow(Time).to receive(:now).and_return(local_now)

0 commit comments

Comments
 (0)