Skip to content
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
9 changes: 6 additions & 3 deletions lib/oauth2/access_token.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module OAuth2
class AccessToken
attr_reader :client, :token, :expires_in, :expires_at, :params
attr_reader :client, :token, :expires_in, :expires_at, :expires_latency, :params
attr_accessor :options, :refresh_token, :response

class << self
Expand Down Expand Up @@ -32,22 +32,25 @@ def from_kvform(client, kvform)
# @option opts [String] :refresh_token (nil) the refresh_token value
# @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire
# @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire
# @option opts [FixNum, String] :expires_latency (nil) the number of seconds by which AccessToken validity will be reduced to offset latency
# @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value
# one of :header, :body or :query
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
# Access Token value in :body or :query transmission mode
def initialize(client, token, opts = {}) # rubocop:disable Metrics/AbcSize
def initialize(client, token, opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
@client = client
@token = token.to_s
opts = opts.dup
[:refresh_token, :expires_in, :expires_at].each do |arg|
[:refresh_token, :expires_in, :expires_at, :expires_latency].each do |arg|
instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
end
@expires_in ||= opts.delete('expires')
@expires_in &&= @expires_in.to_i
@expires_at &&= @expires_at.to_i
@expires_latency &&= @expires_latency.to_i
@expires_at ||= Time.now.to_i + @expires_in if @expires_in
@expires_at -= @expires_latency if @expires_latency
@options = {:mode => opts.delete(:mode) || :header,
:header_format => opts.delete(:header_format) || 'Bearer %s',
:param_name => opts.delete(:param_name) || 'access_token'}
Expand Down
35 changes: 35 additions & 0 deletions spec/oauth2/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ def assert_initialized_token(target) # rubocop:disable Metrics/AbcSize
assert_initialized_token(target)
expect(target.expires_at).to be_a(Integer)
end

describe 'expires_latency' do
let(:expires_at) { 1_530_000_000 }
let(:expires_in) { 100 }
let(:expires_latency) { 10 }
let(:hash) do
{
:access_token => token,
:expires_latency => expires_latency,
:expires_in => expires_in,
}
end

it 'sets it via options' do
target = described_class.from_hash(client, hash.merge(:expires_latency => expires_latency.to_s))
expect(target.expires_latency).to eq expires_latency
end

it 'sets it nil by default' do
hash.delete(:expires_latency)
target = described_class.from_hash(client, hash)
expect(target.expires_latency).to be_nil
end

it 'reduces expires_at by the given amount' do
allow(Time).to receive(:now).and_return(expires_at)
target = described_class.from_hash(client, hash)
expect(target.expires_at).to eq(expires_at + expires_in - expires_latency)
end

it 'reduces expires_at by the given amount if expires_at is provided as option' do
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps one more additional test - reduces expires_at by the given amount if expires_at is not provided as option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think reduces expires_at by the given amount covers this case since expires_at is not provided by default and this test explicitly says that it is provided, and adds it to the hash.

target = described_class.from_hash(client, hash.merge(:expires_at => expires_at))
expect(target.expires_at).to eq(expires_at - expires_latency)
end
end
end

describe '#request' do
Expand Down