class Azure::Core::Auth::SharedKey
Attributes
The Azure account's name.
Public Class Methods
Initialize the Signer.
@param #account_name [String] The account name. Defaults to the one in the
global configuration.
@param access_key [String] The access_key encoded in Base64. Defaults to the
one in the global configuration.
# File lib/azure/core/auth/shared_key.rb, line 31 def initialize(account_name=Azure.storage_account_name, access_key=Azure.storage_access_key) @account_name = account_name super(access_key) end
Public Instance Methods
Calculate the Canonicalized Headers string for a request.
@param headers [Hash] HTTP request headers.
@return [String] a string with the canonicalized headers.
# File lib/azure/core/auth/shared_key.rb, line 95 def canonicalized_headers(headers) headers = headers.map { |k,v| [k.to_s.downcase, v] } headers.select! { |k,v| k =~ /^x-ms-/ } headers.sort_by! { |(k,v)| k } headers.map! { |k,v| '%s:%s' % [k, v] } headers.map! { |h| h.gsub(/\s+/, ' ') }.join("\n") end
Calculate the Canonicalized Resource string for a request.
@param uri [URI] URI of the request we're signing.
@return [String] a string with the canonicalized resource.
# File lib/azure/core/auth/shared_key.rb, line 108 def canonicalized_resource(uri) resource = '/' + account_name + (uri.path.empty? ? '/' : uri.path) params = CGI.parse(uri.query.to_s).map { |k,v| [k.downcase, v] } params.sort_by! { |k,v| k } params.map! { |k,v| '%s:%s' % [k, v.map(&:strip).sort.join(',')] } [resource, *params].join("\n") end
The name of the strategy.
@return [String]
# File lib/azure/core/auth/shared_key.rb, line 39 def name 'SharedKey' end
Create the signature for the request parameters
@param method [Symbol] HTTP request method. @param uri [URI] URI of the request we're signing. @param headers [Hash] HTTP request headers.
@return [String] base64 encoded signature
# File lib/azure/core/auth/shared_key.rb, line 50 def sign(method, uri, headers) "#{account_name}:#{super(signable_string(method, uri, headers))}" end
Sign the request
@param req [Azure::Core::Http::HttpRequest] HTTP request to sign
@return [Azure::Core::Http::HttpRequest]
# File lib/azure/core/auth/shared_key.rb, line 59 def sign_request(req) req.headers['Authorization'] = "#{name} #{sign(req.method, req.uri, req.headers)}" req end
Generate the string to sign.
@param method [Symbol] HTTP request method. @param uri [URI] URI of the request we're signing. @param headers [Hash] HTTP request headers.
@return [String]
# File lib/azure/core/auth/shared_key.rb, line 71 def signable_string(method, uri, headers) [ method.to_s.upcase, headers.fetch('Content-Encoding', ''), headers.fetch('Content-Language', ''), headers.fetch('Content-Length', ''), headers.fetch('Content-MD5', ''), headers.fetch('Content-Type', ''), headers.fetch('Date', ''), headers.fetch('If-Modified-Since', ''), headers.fetch('If-Match', ''), headers.fetch('If-None-Match', ''), headers.fetch('If-Unmodified-Since', ''), headers.fetch('Range', ''), canonicalized_headers(headers), canonicalized_resource(uri) ].join("\n") end