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

Merge pull request #1873 from brightbox/expose_token_expiry

[Brightbox] Expose expires_in value for the access token
This commit is contained in:
Paul Thornthwaite 2013-06-10 08:18:30 -07:00
commit 47a1a930fd
3 changed files with 19 additions and 4 deletions

View file

@ -244,6 +244,12 @@ module Fog
@credentials.refresh_token @credentials.refresh_token
end 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 # Requests a new access token
# #
# @return [String] New access token # @return [String] New access token

View file

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

View file

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