From 9193398fbcfbd25418c42815da133aef3d15dd7a Mon Sep 17 00:00:00 2001 From: Amitava Date: Wed, 22 May 2013 19:29:04 +0530 Subject: [PATCH] [Brightbox] Expose expires_in value for the access token --- lib/fog/brightbox/compute.rb | 6 ++++++ lib/fog/brightbox/oauth2.rb | 8 +++++--- tests/brightbox/oauth2_tests.rb | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/fog/brightbox/compute.rb b/lib/fog/brightbox/compute.rb index 5369a9a8e..3e2162821 100644 --- a/lib/fog/brightbox/compute.rb +++ b/lib/fog/brightbox/compute.rb @@ -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 diff --git a/lib/fog/brightbox/oauth2.rb b/lib/fog/brightbox/oauth2.rb index 45886e08b..e291a3d08 100644 --- a/lib/fog/brightbox/oauth2.rb +++ b/lib/fog/brightbox/oauth2.rb @@ -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 diff --git a/tests/brightbox/oauth2_tests.rb b/tests/brightbox/oauth2_tests.rb index 137cc147d..cead120b3 100644 --- a/tests/brightbox/oauth2_tests.rb +++ b/tests/brightbox/oauth2_tests.rb @@ -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