class Azure::Core::Auth::SharedKey

Attributes

account_name[R]

The Azure account's name.

Public Class Methods

new(account_name=Azure.storage_account_name, access_key=Azure.storage_access_key) click to toggle source

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.
Calls superclass method
# 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

canonicalized_headers(headers) click to toggle source

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,_| k =~ /^x-ms-/ }
  headers.sort_by! { |(k,_)| k }
  headers.map! { |k,v| '%s:%s' % [k, v] }.join("\n")
end
canonicalized_resource(uri) click to toggle source

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 107
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,_| k }
  params.map! { |k,v| '%s:%s' % [k, v.map(&:strip).sort.join(',')] }
  [resource, *params].join("\n")
end
name() click to toggle source

The name of the strategy.

@return [String]

# File lib/azure/core/auth/shared_key.rb, line 39
def name
  'SharedKey'
end
sign(method, uri, headers) click to toggle source

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_request(req) click to toggle source

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
signable_string(method, uri, headers) click to toggle source

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