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

Add object temp url generation capability with mock support.

This commit is contained in:
Rupak Ganguly 2012-09-26 13:01:32 -04:00
parent 369692a7a1
commit 271bc985fa
2 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,34 @@
module Fog
module Storage
class HP
class Real
# Generate a temporary url for an object
#
# ==== Parameters
# * container<~String> - Name of container
# * object<~String> - Name of object
# * expires<~Integer> - Time the temporary url expire in secs.
# * method<~String> - Allowed HTTP method GET, PUT, HEAD only
def get_object_temp_url(container, object, expires, method)
generate_object_temp_url(container, object, expires, method)
end
end
class Mock # :nodoc:all
def get_object_temp_url(container, object, expires, method)
@scheme = "https"
@host = "swift-cluster.example.com"
@port = "443"
@path = "/v1/account"
generate_object_temp_url(container, object, expires, method)
end
end
end
end
end

View file

@ -20,6 +20,7 @@ module Fog
request :get_container
request :get_containers
request :get_object
request :get_object_temp_url
request :head_container
request :head_containers
request :head_object
@ -81,6 +82,32 @@ module Fog
acl = "public-read-write"
end
end
def generate_object_temp_url(container, object, expires_secs, method)
return unless (container && object && expires_secs && method)
# POST not allowed
allowed_methods = %w{GET PUT HEAD}
unless allowed_methods.include?(method)
raise ArgumentError.new("Invalid method '#{method}' specified. Valid methods are: #{allowed_methods.join(', ')}")
end
expires = (Time.now + expires_secs.to_i).to_i
# do not encode before signature generation, encode after
path = "#{@path}/#{container}/#{object}"
encoded_path = "#{@path}/#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
string_to_sign = "#{method}\n#{expires}\n#{path}"
signed_string = Digest::HMAC.hexdigest(string_to_sign, @hp_secret_key, Digest::SHA1)
signature = @hp_account_id.to_s + ":" + signed_string
signature = Fog::HP.escape(signature)
# generate the temp url using the signature and expiry
temp_url = "#{@scheme}://#{@host}:#{@port}#{encoded_path}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
end
end
class Mock