Skip to content

Commit

Permalink
link values of expires_in and expires_at (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRoyalTnetennba authored Aug 17, 2018
1 parent 097e24f commit 8fa49a1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: ruby
rvm:
- 2.4.2
- 2.3.5
- 2.2.8
- 2.1
- 2.5.1
- 2.4.4
- 2.3.7
- 2.2.10
- 2.1.10
- 2.0.0
- 1.9.3
- jruby-9.1.9.0
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.9.0 (2018-08-16)
* Add RemoteServerError class for 5xx level errors.
* Allow to_json to be called with arguments
* Expires_in now sets and reflects current expires_at value
* Expires_within(0) now returns false when expires_at is nil.

## 0.8.1 (2017-10-13)

* Restore support for Ruby 1.9.3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<dt>License</dt><dd>Apache 2.0</dd>
</dl>

[![Gem Version](https://badge.fury.io/rb/signet.svg)](https://badge.fury.io/rb/signet)
[![Build Status](https://secure.travis-ci.org/google/signet.png)](http://travis-ci.org/google/signet)
[![Dependency Status](https://gemnasium.com/google/signet.png)](https://gemnasium.com/google/signet)

## Description

Expand Down
53 changes: 25 additions & 28 deletions lib/signet/oauth_2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def initialize(options={})
@client_secret = nil
@code = nil
@expires_at = nil
@expires_in = nil
@issued_at = nil
@issuer = nil
@password = nil
Expand Down Expand Up @@ -723,32 +722,37 @@ def decoded_id_token(public_key=nil, options = {}, &keyfinder)

##
# Returns the lifetime of the access token in seconds.
# Returns nil if the token does not expire.
#
# @return [Integer] The access token lifetime.
# @return [Integer, nil] The access token lifetime.
def expires_in
return @expires_in
if @expires_at.nil? || @issued_at.nil?
nil
else
(@expires_at - @issued_at).to_i
end
end

##
# Sets the lifetime of the access token in seconds. Resets the issued
# timestamp.
# Sets the lifetime of the access token in seconds. Resets the issued_at
# timestamp. Nil values will be treated as though the token does
# not expire.
#
# @param [String, Integer] new_expires_in
# @param [String, Integer, nil] new_expires_in
# The access token lifetime.
def expires_in=(new_expires_in)
if new_expires_in != nil
@expires_in = new_expires_in.to_i
@issued_at = Time.now
@expires_at = @issued_at + new_expires_in.to_i
else
@expires_in, @issued_at = nil, nil
@expires_at, @issued_at = nil, nil
end
@expires_at = nil
end

##
# Returns the timestamp the access token was issued at.
#
# @return [Time] The access token issuance time.
# @return [Time, nil] The access token issuance time.
def issued_at
return @issued_at
end
Expand All @@ -764,29 +768,26 @@ def issued_at=(new_issued_at)

##
# Returns the timestamp the access token will expire at.
# Returns nil if the token does not expire.
#
# @return [Time] The access token lifetime.
# @return [Time, nil] The access token lifetime.
def expires_at
if @expires_at
@expires_at
elsif @issued_at && @expires_in
return @issued_at + @expires_in
else
return nil
end
@expires_at
end

##
# Limits the lifetime of the access token as number of seconds since
# the Epoch
# @param [String,Integer,Time] new_expires_at
# The access token issuance time.
# the Epoch. Nil values will be treated as though the token does
# not expire.
# @param [String,Integer,Time, nil] new_expires_at
# The access token expiration time.
def expires_at=(new_expires_at)
@expires_at = normalize_timestamp(new_expires_at)
@expires_at = normalize_timestamp new_expires_at
end

##
# Returns true if the access token has expired.
# Returns false if the token has not expired or has an nil @expires_at.
#
# @return [TrueClass, FalseClass]
# The expiration state of the access token.
Expand All @@ -796,7 +797,7 @@ def expired?

##
# Returns true if the access token has expired or expires within
# the next n seconds
# the next n seconds. Returns false for tokens with a nil @expires_at.
#
# @param [Integer] sec
# Max number of seconds from now where a token is still considered
Expand All @@ -817,7 +818,7 @@ def clear_credentials!
@password = nil
@code = nil
@issued_at = nil
@expires_in = nil
@expires_at = nil
end


Expand Down Expand Up @@ -1006,8 +1007,6 @@ def fetch_access_token(options={})
end

def fetch_access_token!(options={})
options = deep_hash_normalize(options)

token_hash = self.fetch_access_token(options)
if token_hash
# No-op for grant types other than `authorization_code`.
Expand All @@ -1023,8 +1022,6 @@ def fetch_access_token!(options={})
##
# Refresh the access token, if possible
def refresh!(options={})
options = deep_hash_normalize(options)

self.fetch_access_token!(options)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/signet/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
module Signet
module VERSION
MAJOR = 0
MINOR = 8
TINY = 1
MINOR = 9
TINY = 0
PRE = nil

STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
Expand Down
31 changes: 30 additions & 1 deletion spec/signet/oauth_2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,38 @@ def build_form_encoded_response(payload)
expect(@client).to_not be_expired
end

it 'should set expires_in when expires_at is set' do
issued_at = Time.now
expires_at = Time.now+100
@client.expires_at = expires_at.to_i
@client.issued_at = issued_at
expect(@client.expires_in).to be_within(1).of (expires_at - issued_at).to_i
@client.expires_at = nil
expect(@client.expires_in).to be_nil
end

it 'should set expires_in to nil when expires_at is set to nil' do
@client.expires_at = nil
expect(@client.expires_in).to be_nil
end

it 'should set expires_at when expires_in is set' do
expires_in = 100
@client.expires_in = expires_in
expect(@client.expires_at).to eq (@client.issued_at + expires_in)
@client.expires_in = nil
expect(@client.expires_at).to be_nil
end

it 'should set expires_at to nil when expires_in is set to nil' do
@client.expires_in = nil
expect(@client.expires_at).to be_nil
end

it 'should indicate the token is not expired if expired_at nil' do
@client.expires_at = nil
expect(@client.expires_within?(60)).to be false
expect(@client.expired?).to be false
end

it 'should indicate the token is not expiring when expiry beyond window' do
Expand Down Expand Up @@ -880,7 +909,7 @@ def build_form_encoded_response(payload)
end

it 'should raise an error if the id token cannot be verified' do
pending "Need to update test data"
pending "Need to set test data"
@client.client_id = 'client-12345'
@client.client_secret = 'secret-12345'
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
Expand Down

0 comments on commit 8fa49a1

Please sign in to comment.