From 7487cf356ff333fab2220ab6b31e6d26aa81483d Mon Sep 17 00:00:00 2001 From: Eric Hankins Date: Thu, 17 May 2012 11:07:30 -0500 Subject: [PATCH] Copied auth token reauthentication from rackspace|compute --- lib/fog/rackspace/storage.rb | 43 +++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/fog/rackspace/storage.rb b/lib/fog/rackspace/storage.rb index 1db50ae41..18ece3ae9 100644 --- a/lib/fog/rackspace/storage.rb +++ b/lib/fog/rackspace/storage.rb @@ -83,16 +83,13 @@ module Fog @rackspace_username = options[:rackspace_username] @rackspace_cdn_ssl = options[:rackspace_cdn_ssl] @rackspace_auth_url = options[:rackspace_auth_url] + @rackspace_servicenet = options[:rackspace_servicenet] + @rackspace_auth_token = options[:rackspace_auth_token] + @rackspace_storage_url = options[:rackspace_storage_url] + @rackspace_must_reauthenticate = false @connection_options = options[:connection_options] || {} - credentials = Fog::Rackspace.authenticate(options, @connection_options) - @auth_token = credentials['X-Auth-Token'] - - uri = URI.parse(credentials['X-Storage-Url']) - @host = options[:rackspace_servicenet] == true ? "snet-#{uri.host}" : uri.host - @path = uri.path + authenticate @persistent = options[:persistent] || false - @port = uri.port - @scheme = uri.scheme Excon.ssl_verify_peer = false if options[:rackspace_servicenet] == true @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options) end @@ -111,6 +108,14 @@ module Fog :host => @host, :path => "#{@path}/#{params[:path]}", }), &block) + rescue Excon::Errors::Unauthorized => error + if error.response.body != 'Bad username or password' # token expiration + @rackspace_must_reauthenticate = true + authenticate + retry + else # bad credentials + raise error + end rescue Excon::Errors::HTTPStatusError => error raise case error when Excon::Errors::NotFound @@ -125,6 +130,28 @@ module Fog response end + private + + def authenticate + if @rackspace_must_reauthenticate || @rackspace_auth_token.nil? + options = { + :rackspace_api_key => @rackspace_api_key, + :rackspace_username => @rackspace_username, + :rackspace_auth_url => @rackspace_auth_url + } + credentials = Fog::Rackspace.authenticate(options, @connection_options) + @auth_token = credentials['X-Auth-Token'] + uri = URI.parse(credentials['X-Storage-Url']) + else + @auth_token = @rackspace_auth_token + uri = URI.parse(@rackspace_storage_url) + end + @host = @rackspace_servicenet == true ? "snet-#{uri.host}" : uri.host + @path = uri.path + @port = uri.port + @scheme = uri.scheme + end + end end end