Class: Azure::Storage::Auth::SharedKey

Inherits:
Core::Auth::Signer show all
Defined in:
lib/azure/storage/core/auth/shared_key.rb

Direct Known Subclasses

SharedKeyLite, Table::Auth::SharedKey

Instance Attribute Summary collapse

Attributes inherited from Core::Auth::Signer

#access_key

Instance Method Summary collapse

Constructor Details

#initialize(account_name = Azure::Storage.config.storage_account_name, access_key = Azure::Storage.config.storage_access_key) ⇒ SharedKey

Initialize the Signer.

Parameters:

  • account_name (String) (defaults to: Azure::Storage.config.storage_account_name)

    The account name. Defaults to the one in the global configuration.

  • access_key (String) (defaults to: Azure::Storage.config.storage_access_key)

    The access_key encoded in Base64. Defaults to the one in the global configuration.


30
31
32
33
# File 'lib/azure/storage/core/auth/shared_key.rb', line 30

def initialize(=Azure::Storage.config., access_key=Azure::Storage.config.storage_access_key)
  @account_name = 
  super(access_key)
end

Instance Attribute Details

#account_nameObject (readonly)

The Azure account's name.


22
23
24
# File 'lib/azure/storage/core/auth/shared_key.rb', line 22

def 
  @account_name
end

Instance Method Details

#canonicalized_headers(headers) ⇒ String

Calculate the Canonicalized Headers string for a request.

Parameters:

  • headers (Hash)

    HTTP request headers.

Returns:

  • (String)

    a string with the canonicalized headers.


94
95
96
97
98
99
100
# File 'lib/azure/storage/core/auth/shared_key.rb', line 94

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

#canonicalized_resource(uri) ⇒ String

Calculate the Canonicalized Resource string for a request.

Parameters:

  • uri (URI)

    URI of the request we're signing.

Returns:

  • (String)

    a string with the canonicalized resource.


107
108
109
110
111
112
113
# File 'lib/azure/storage/core/auth/shared_key.rb', line 107

def canonicalized_resource(uri)
  resource = '/' +  + (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

#nameString

The name of the strategy.

Returns:


38
39
40
# File 'lib/azure/storage/core/auth/shared_key.rb', line 38

def name
  'SharedKey'
end

#sign(method, uri, headers) ⇒ String

Create the signature for the request parameters

Parameters:

  • method (Symbol)

    HTTP request method.

  • uri (URI)

    URI of the request we're signing.

  • headers (Hash)

    HTTP request headers.

Returns:

  • (String)

    base64 encoded signature


49
50
51
# File 'lib/azure/storage/core/auth/shared_key.rb', line 49

def sign(method, uri, headers)
  "#{}:#{super(signable_string(method, uri, headers))}"
end

#sign_request(req) ⇒ Azure::Core::Http::HttpRequest

Sign the request

Parameters:

Returns:


58
59
60
61
# File 'lib/azure/storage/core/auth/shared_key.rb', line 58

def sign_request(req)
  req.headers['Authorization'] = "#{name} #{sign(req.method, req.uri, req.headers)}"
  req
end

#signable_string(method, uri, headers) ⇒ String

Generate the string to sign.

Parameters:

  • method (Symbol)

    HTTP request method.

  • uri (URI)

    URI of the request we're signing.

  • headers (Hash)

    HTTP request headers.

Returns:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/azure/storage/core/auth/shared_key.rb', line 70

def signable_string(method, uri, headers)
  [
    method.to_s.upcase,
    headers.fetch('Content-Encoding', ''),
    headers.fetch('Content-Language', ''),
    headers.fetch('Content-Length', '').sub(/^0+/,''), # from 2015-02-21, if Content-Length == 0, it won't be signed
    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