Class: Miasma::Contrib::AzureApiCore::SignatureAzure

Inherits:
Signature
  • Object
show all
Defined in:
lib/miasma/contrib/azure.rb

Direct Known Subclasses

SasBlob

Defined Under Namespace

Classes: SasBlob

Constant Summary collapse

SIGNATURE_HEADERS =

Required Header Items

[
  'Content-Encoding',
  'Content-Language',
  'Content-Length',
  'Content-MD5',
  'Content-Type',
  'Date',
  'If-Modified-Since',
  'If-Match',
  'If-None-Match',
  'If-Unmodified-Since',
  'Range'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Signature

#safe_escape

Constructor Details

#initialize(shared_key, account_name) ⇒ SignatureAzure



132
133
134
135
136
137
# File 'lib/miasma/contrib/azure.rb', line 132

def initialize(shared_key, )
  shared_key = Base64.decode64(shared_key)
  @hmac = Hmac.new('sha256', shared_key)
  @shared_key = shared_key
  @account_name = 
end

Instance Attribute Details

#account_nameString (readonly)



130
131
132
# File 'lib/miasma/contrib/azure.rb', line 130

def 
  @account_name
end

#hmacHmac (readonly)



126
127
128
# File 'lib/miasma/contrib/azure.rb', line 126

def hmac
  @hmac
end

#shared_keyString (readonly)



128
129
130
# File 'lib/miasma/contrib/azure.rb', line 128

def shared_key
  @shared_key
end

Instance Method Details

#build_canonical_headers(headers) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/miasma/contrib/azure.rb', line 167

def build_canonical_headers(headers)
  headers.map do |key, value|
    key = key.to_s.downcase
    if(key.start_with?('x-ms-'))
      [key, value].map(&:strip).join(':')
    end
  end.compact.sort.join("\n")
end

#build_canonical_resource(resource) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/miasma/contrib/azure.rb', line 176

def build_canonical_resource(resource)
  [
    "/#{account_name}#{resource[:path]}",
    *resource.fetch(:params, {}).map{|key, value|
      key = key.downcase.strip
      value = value.is_a?(Array) ? value.map(&:strip).sort.join(',') : value
      [key, value].join(':')
    }.sort
  ].join("\n")
end

#generate(http_method, path, opts) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/miasma/contrib/azure.rb', line 139

def generate(http_method, path, opts)
  signature = generate_signature(
    http_method,
    opts[:headers],
    opts.merge(:path => path)
  )
  "SharedKey #{account_name}:#{signature}"
end

#generate_signature(http_method, headers, resource) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/miasma/contrib/azure.rb', line 148

def generate_signature(http_method, headers, resource)
  headers = headers.to_smash
  headers.delete('Content-Length') if headers['Content-Length'].to_s == '0'
  to_sign = [
    http_method.to_s.upcase,
    *self.class.const_get(:SIGNATURE_HEADERS).map{|head_name|
      headers.fetch(head_name, '')
    },
    build_canonical_headers(headers),
    build_canonical_resource(resource)
  ].join("\n")
  signature = sign_request(to_sign)
end

#sign_request(request) ⇒ Object



162
163
164
165
# File 'lib/miasma/contrib/azure.rb', line 162

def sign_request(request)
  result = hmac.sign(request)
  Base64.encode64(result).strip
end