1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[Brightbox] Expose expires_in value for the access token

This commit is contained in:
Amitava 2013-05-22 19:29:04 +05:30 committed by Paul Thornthwaite
parent 47216cc22e
commit 9193398fbc
3 changed files with 19 additions and 4 deletions

View file

@ -244,6 +244,12 @@ module Fog
@credentials.refresh_token
end
# Returns the current token expiry time in seconds or nil
# @return [Number,nil]
def expires_in
@credentials.expires_in
end
# Requests a new access token
#
# @return [String] New access token

View file

@ -37,7 +37,7 @@ module Fog::Brightbox::OAuth2
#
class CredentialSet
attr_reader :client_id, :client_secret, :username, :password
attr_reader :access_token, :refresh_token
attr_reader :access_token, :refresh_token, :expires_in
#
# @param [String] client_id
# @param [String] client_secret
@ -52,6 +52,7 @@ module Fog::Brightbox::OAuth2
@password = options[:password]
@access_token = options[:access_token]
@refresh_token = options[:refresh_token]
@expires_in = options[:expires_in]
end
# Returns true if user details are available
@ -71,9 +72,10 @@ module Fog::Brightbox::OAuth2
end
# Updates the credentials with newer tokens
def update_tokens(access_token, refresh_token = nil)
def update_tokens(access_token, refresh_token = nil, expires_in = nil)
@access_token = access_token
@refresh_token = refresh_token
@expires_in = expires_in
end
# Based on available credentials returns the best strategy
@ -159,6 +161,6 @@ private
#
def update_credentials_from_response(credentials, response)
response_data = Fog::JSON.decode(response.body)
credentials.update_tokens(response_data["access_token"], response_data["refresh_token"])
credentials.update_tokens(response_data["access_token"], response_data["refresh_token"], response_data["expires_in"])
end
end

View file

@ -7,6 +7,7 @@ Shindo.tests("Fog::Brightbox::OAuth2", ["brightbox"]) do
@password = "__mushed_keys_321__"
@access_token = "12efde32fdfe4989"
@refresh_token = "7894389f9074f071"
@expires_in = 7200
tests("with client credentials") do
credentials = Fog::Brightbox::OAuth2::CredentialSet.new(@client_id, @client_secret)
@ -30,11 +31,17 @@ Shindo.tests("Fog::Brightbox::OAuth2", ["brightbox"]) do
end
tests("with existing tokens") do
options = {:username => @username, :access_token => @access_token, :refresh_token => @refresh_token}
options = {
:username => @username,
:access_token => @access_token,
:refresh_token => @refresh_token,
:expires_in => @expires_in
}
credentials = Fog::Brightbox::OAuth2::CredentialSet.new(@client_id, @client_secret, options)
tests("#user_details?").returns(false) { credentials.user_details? }
tests("#access_token?").returns(true) { credentials.access_token? }
tests("#refresh_token?").returns(true) { credentials.refresh_token? }
tests("#expires_in").returns(7200) { credentials.expires_in }
tests("#best_grant_strategy").returns(true) do
credentials.best_grant_strategy.is_a?(Fog::Brightbox::OAuth2::RefreshTokenStrategy)
end