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

[Brightbox] Extracts parts of request out of compute

* Adds simple helper to get token for service
* Low level request #request_access_token is provided to request
  access tokens
This commit is contained in:
Paul Thornthwaite 2012-11-21 18:05:41 +00:00
parent a1f1a8b8ce
commit d833c83169
3 changed files with 78 additions and 32 deletions

View file

@ -276,38 +276,37 @@ module Fog
@credentials.refresh_token
end
private
def get_oauth_token
if authenticating_as_user?
token_strategy = UserCredentialsStrategy.new(@credentials)
else
token_strategy = ClientCredentialsStrategy.new(@credentials)
# Requests a new access token
#
# @return [String] New access token
def get_access_token
begin
get_access_token!
rescue Excon::Errors::Unauthorized, Excon::Errors::BadRequest
@credentials.update_tokens(nil, nil)
end
basic_header_to_encode = "#{@credentials.client_id}:#{@credentials.client_secret}"
response = @auth_connection.request({
:path => "/token",
:expects => 200,
:headers => {
'Authorization' => "Basic " + Base64.encode64(basic_header_to_encode).chomp,
'Content-Type' => 'application/json'
},
:method => 'POST',
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
})
response_data = Fog::JSON.decode(response.body)
@credentials.update_tokens(response_data["access_token"], response_data["refresh_token"])
@credentials.access_token
end
# Requests a new access token and raises if there is a problem
#
# @return [String] New access token
# @raise [Excon::Errors::BadRequest] The credentials are expired or incorrect
#
def get_access_token!
response = request_access_token(@auth_connection, @credentials)
update_credentials_from_response(@credentials, response)
@credentials.access_token
end
private
def make_request(params)
begin
get_oauth_token unless access_token_available?
get_access_token unless access_token_available?
response = authenticated_request(params)
rescue Excon::Errors::Unauthorized
get_oauth_token
get_access_token
response = authenticated_request(params)
end
unless response.body.empty?

View file

@ -5,6 +5,31 @@
#
module Fog::Brightbox::OAuth2
# This builds the simplest form of requesting an access token
# based on the arguments passed in
#
# @param [Fog::Connection] connection
# @param [CredentialSet] credentials
#
# @return [Excon::Response]
def request_access_token(connection, credentials)
token_strategy = credentials.best_grant_strategy
header_content = "#{credentials.client_id}:#{credentials.client_secret}"
encoded_credentials = Base64.encode64(header_content).chomp
connection.request({
:path => "/token",
:expects => 200,
:headers => {
'Authorization' => "Basic #{encoded_credentials}",
'Content-Type' => 'application/json'
},
:method => 'POST',
:body => Fog::JSON.encode(token_strategy.authorization_body_data)
})
end
# Encapsulates credentials required to request access tokens from the
# Brightbox authorisation servers
#
@ -50,6 +75,18 @@ module Fog::Brightbox::OAuth2
@access_token = access_token
@refresh_token = refresh_token
end
# Based on available credentials returns the best strategy
#
# @todo Add a means to dictate which should or shouldn't be used
#
def best_grant_strategy
if user_details?
UserCredentialsStrategy.new(self)
else
ClientCredentialsStrategy.new(self)
end
end
end
# This strategy class is the basis for OAuth2 grant types
@ -97,4 +134,16 @@ module Fog::Brightbox::OAuth2
}
end
end
private
# This updates the current credentials if passed a valid response
#
# @param [CredentialSet] credentials Credentials to update
# @param [Excon::Response] response Response object to parse value from
#
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"])
end
end

View file

@ -13,6 +13,9 @@ Shindo.tests("Fog::Brightbox::OAuth2", ["brightbox"]) do
tests("#user_details?").returns(false) { credentials.user_details? }
tests("#access_token?").returns(false) { credentials.access_token? }
tests("#refresh_token?").returns(false) { credentials.refresh_token? }
tests("#best_grant_strategy").returns(true) do
credentials.best_grant_strategy.is_a?(Fog::Brightbox::OAuth2::ClientCredentialsStrategy)
end
end
tests("with user credentials") do
@ -21,14 +24,9 @@ Shindo.tests("Fog::Brightbox::OAuth2", ["brightbox"]) do
tests("#user_details?").returns(true) { credentials.user_details? }
tests("#access_token?").returns(false) { credentials.access_token? }
tests("#refresh_token?").returns(false) { credentials.refresh_token? }
end
tests("with existing tokens") do
options = {:username => @username, :access_token => @access_token, :refresh_token => @refresh_token}
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("#best_grant_strategy").returns(true) do
credentials.best_grant_strategy.is_a?(Fog::Brightbox::OAuth2::UserCredentialsStrategy)
end
end
end