Class: Azure::Contrib::Auth::SharedAccessSignature
- Inherits:
-
Object
- Object
- Azure::Contrib::Auth::SharedAccessSignature
- Defined in:
- lib/azure-contrib/shared_access_signature.rb
Defined Under Namespace
Classes: Version20130815
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#canonicalized_resource(uri, account = ENV['AZURE_STORAGE_ACCOUNT'], is_blob = false) ⇒ Object
Create a “canonicalized resource” from the full uri to be signed.
-
#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.
- #create_signature(options = Version20130815) ⇒ Object
-
#initialize(uri, options = {}, account = ENV['AZURE_STORAGE_ACCOUNT']) ⇒ SharedAccessSignature
constructor
A new instance of SharedAccessSignature.
- #sign ⇒ Object
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, = {}, account = ENV['AZURE_STORAGE_ACCOUNT']) # This is the uri that we are signing @uri = Addressable::URI.parse(uri) is_blob = [:resource] == 'b' # Create the options hash that will be turned into a query string @options = Version20130815.new(.merge(canonicalized_resource: canonicalized_resource(@uri, account, is_blob))) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/azure-contrib/shared_access_signature.rb', line 22 def @options end |
#uri ⇒ Object
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, account = ENV['AZURE_STORAGE_ACCOUNT'], is_blob = false) path = URI.unescape(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('/', account, container, path_array.join('/')) else File.join('/', account, 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( = Version20130815.new) # Parts parts = {} parts[:st] = URI.unescape([:start]) unless [:start] == '' parts[:se] = URI.unescape([:expiry]) parts[:sr] = URI.unescape([:resource]) parts[:sp] = URI.unescape([:permissions]) parts[:si] = URI.unescape([:identifier]) unless [:identifier] == '' parts[:sig] = URI.unescape( create_signature() ) 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( = Version20130815) string_to_sign = [] string_to_sign << [:permissions] string_to_sign << [:start] string_to_sign << [:expiry] string_to_sign << [:canonicalized_resource] string_to_sign << [:identifier] Azure::Core::Auth::Signer.new.sign(string_to_sign.join("\n").force_encoding("UTF-8")) end |
#sign ⇒ Object
78 79 80 81 |
# File 'lib/azure-contrib/shared_access_signature.rb', line 78 def sign @uri.query_values = (@uri.query_values || {}).merge(create_query_values(@options)) @uri.to_s end |