Class: Azure::Core::Auth::SharedKey

Inherits:
Signer
  • Object
show all
Defined in:
lib/azure/core/auth/shared_key.rb

Direct Known Subclasses

SharedKeyLite

Instance Attribute Summary collapse

Attributes inherited from Signer

#access_key

Instance Method Summary collapse

Constructor Details

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

Initialize the Signer.

Parameters:

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

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

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

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



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

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

Instance Attribute Details

#account_nameObject (readonly)

The Azure account’s name.



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

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.



95
96
97
98
99
100
101
# 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] }
  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.



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

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,_| 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:



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

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



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

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

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

Sign the request

Parameters:

Returns:



59
60
61
62
# 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) ⇒ 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:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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