Class: Azure::Contrib::Auth::SharedAccessSignature

Inherits:
Object
  • Object
show all
Defined in:
lib/azure-contrib/shared_access_signature.rb

Defined Under Namespace

Classes: Version20130815

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}, account = ENV['AZURE_STORAGE_ACCOUNT']) ⇒ SharedAccessSignature

Returns a new instance of SharedAccessSignature.


24
25
26
27
28
29
30
31
32
# File 'lib/azure-contrib/shared_access_signature.rb', line 24

def initialize(uri, options = {},  =  ENV['AZURE_STORAGE_ACCOUNT'])
  # This is the uri that we are signing
  @uri = Addressable::URI.parse(uri)

  is_blob = options[:resource] == 'b'

  # Create the options hash that will be turned into a query string
  @options = Version20130815.new(options.merge(canonicalized_resource: canonicalized_resource(@uri, , is_blob)))
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options


22
23
24
# File 'lib/azure-contrib/shared_access_signature.rb', line 22

def options
  @options
end

#uriObject

Returns the value of attribute uri


22
23
24
# File 'lib/azure-contrib/shared_access_signature.rb', line 22

def uri
  @uri
end

Instance Method Details

#canonicalized_resource(uri, account = ENV['AZURE_STORAGE_ACCOUNT'], is_blob = false) ⇒ Object

Create a “canonicalized resource” from the full uri to be signed


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/azure-contrib/shared_access_signature.rb', line 35

def canonicalized_resource(uri,  = ENV['AZURE_STORAGE_ACCOUNT'], is_blob = false)
  path = uri.path # Addressable::URI
  # There is only really one level deep for containers, the remainder is the BLOB key (that looks like a path)
  path_array = path.split('/').reject {|p| p == ''}
  container = path_array.shift

  string = if is_blob
    File.join('/', , container, path_array.join('/'))
  else
    File.join('/', , container)
  end

  string
end

#create_query_values(options = Version20130815.new) ⇒ Object

When creating the query string from the options, we only include the no-empty fields

  • this is opposed to the string that gets signed which includes them as blank.


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/azure-contrib/shared_access_signature.rb', line 52

def create_query_values(options = Version20130815.new)
  # Parts
  parts       = {}
  parts[:st]  = URI.unescape(options[:start]) unless options[:start] == ''
  parts[:se]  = URI.unescape(options[:expiry])
  parts[:sr]  = URI.unescape(options[:resource])
  parts[:sp]  = URI.unescape(options[:permissions])
  parts[:si]  = URI.unescape(options[:identifier]) unless options[:identifier] == ''
  parts[:sig] = URI.unescape( create_signature(options) )

  parts
end

#create_signature(options = Version20130815) ⇒ Object


65
66
67
68
69
70
71
72
73
74
# File 'lib/azure-contrib/shared_access_signature.rb', line 65

def create_signature(options = Version20130815)
  string_to_sign  = []
  string_to_sign << options[:permissions]
  string_to_sign << options[:start]
  string_to_sign << options[:expiry]
  string_to_sign << options[:canonicalized_resource]
  string_to_sign << options[:identifier]

  Azure::Core::Auth::Signer.new.sign(string_to_sign.join("\n").force_encoding("UTF-8"))
end

#signObject


78
79
80
81
# File 'lib/azure-contrib/shared_access_signature.rb', line 78

def sign
  @uri.query_values = create_query_values(@options)
  @uri.to_s
end