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

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

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.



103
104
105
106
107
108
# File 'lib/azure/core/auth/shared_key.rb', line 103

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) ⇒ 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.



115
116
117
118
119
120
121
# File 'lib/azure/core/auth/shared_key.rb', line 115

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
63
64
65
66
67
68
69
70
# File 'lib/azure/core/auth/shared_key.rb', line 59

def sign_request(req)
  # Need to make sure Content-Length is correctly set.
  if ((!req.body.nil?)) then
    if (req.body.respond_to? :bytesize) then
      req.headers['Content-Length'] = req.body.bytesize.to_s
    elsif (req.body.respond_to? :size)
      req.headers['Content-Length'] = req.body.size.to_s
    end
  end
  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:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/azure/core/auth/shared_key.rb', line 79

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