azure-signature 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88506245b215c239f08a1c85f3970c1ae0b0aa61
4
- data.tar.gz: 9ebc3da7b6ccd44d3004e44dd3e4d49fa8a71a00
3
+ metadata.gz: d7a1b158412c4274214bec5787766675894710b2
4
+ data.tar.gz: be496da96d1f71a5f51d458c2a17dff98f0cba2d
5
5
  SHA512:
6
- metadata.gz: 5fdb546f7a4af2496b7e902aedcdd4ee5b0d6bb069f9eef1b99bd75731158de052fb90374d7d3dd10e5d70a9a66e19b003a9701213b5bec48cf80522e1ec48da
7
- data.tar.gz: b76a9ba3cfa945e8932dba42ec3d00854972fe867b9bc5990d9c58be461f631ce2384db1d0b333108b1f4ce2055d2f0b6e4f7f66a0cee0516bdb33e2baa64669
6
+ metadata.gz: 755801a587abb2d6e700da4efd375da537944aced3638102aeae7c495aa6592a1650ea920a4ce859a0d3bcad2ae05fef6b173c4a42837dbe6cd4917bc5cb4886
7
+ data.tar.gz: d7bd143b29cdaafa758475a7576c990826049efdcf451ebe24d7692dad7a0adadc31001ff009fdd04e6d02b1a56e78254b9ef2e07c39f5e8604ce8a50b528bc7
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ = 0.2.1 - 13-June-2016
2
+ * The signature methods now accept standard header strings as key arguments
3
+ as well as symbols, e.g. 'auth-type' vs :auth_type.
4
+ * Replaced URI with Addressable::URI since it's more robust.
5
+
1
6
  = 0.2.0 - 13-Oct-2015
2
7
  * Added support for other types of signatures (blobs, queues, files).
3
8
  * The :auth_string argument no longer returns the word "Authorization".
data/README CHANGED
@@ -26,7 +26,7 @@
26
26
  azure-sdk-for-ruby project.
27
27
 
28
28
  = Copyright
29
- Copyright (c) 2015, Daniel J. Berger.
29
+ Copyright (c) 2015-2016, Daniel J. Berger.
30
30
  All rights reserved.
31
31
 
32
32
  = License
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'azure-signature'
5
- gem.version = '0.2.0'
5
+ gem.version = '0.2.1'
6
6
  gem.author = 'Daniel J. Berger'
7
7
  gem.license = 'Apache 2.0'
8
8
  gem.email = 'djberg96@gmail.com'
@@ -13,6 +13,7 @@ Gem::Specification.new do |gem|
13
13
 
14
14
  gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
15
 
16
+ gem.add_dependency('addressable')
16
17
  gem.add_development_dependency('test-unit')
17
18
 
18
19
  gem.description = <<-EOF
@@ -1,4 +1,4 @@
1
- require 'uri'
1
+ require 'addressable'
2
2
  require 'openssl'
3
3
  require 'base64'
4
4
  require 'cgi'
@@ -9,7 +9,7 @@ module Azure
9
9
  # The Signature class encapsulates an canonicalized resource string.
10
10
  class Signature
11
11
  # The version of the azure-signature library.
12
- VERSION = '0.2.0'
12
+ VERSION = '0.2.1'
13
13
 
14
14
  # The resource (URL) passed to the constructor.
15
15
  attr_reader :resource
@@ -37,10 +37,10 @@ module Azure
37
37
  #
38
38
  def initialize(resource, key)
39
39
  @resource = resource
40
- @uri = URI.parse(resource)
40
+ @uri = Addressable::URI.parse(resource)
41
41
  @account_name = @uri.host.split(".").first.split("-").first
42
42
  @key = Base64.strict_decode64(key)
43
- @canonical_resource = canonicalize_resource(resource)
43
+ @canonical_resource = canonicalize_resource(@uri)
44
44
  end
45
45
 
46
46
  # Generate a signature for use with the table service. Use the +options+
@@ -54,16 +54,21 @@ module Azure
54
54
  # - :auth_string. If true, prepends the auth_type + account name to the
55
55
  # result and returns a string. The default is false.
56
56
  #
57
+ # You may also use the string forms as keys, e.g. "Auth-Type" instead of
58
+ # :auth_type, if you prefer.
59
+ #
57
60
  # The result is a digest string that you can use as an authorization header
58
61
  # for future http requests to (presumably) Azure storage endpoints.
59
62
  #
60
63
  def table_signature(options = {})
61
- auth_type = options[:auth_type] || 'SharedKey'
62
- verb = options[:verb] || 'GET'
63
- date = options[:date] || Time.now.httpdate
64
- auth_string = options[:auth_string] || false
65
- content_md5 = options[:content_md5] || options['Content-MD5']
66
- content_type = options[:content_type] || options['Content-Type']
64
+ options.clone.each{ |k,v| options[k.to_s.downcase.tr('_', '-')] = v }
65
+
66
+ auth_type = options['auth-type'] || 'SharedKey'
67
+ verb = options['verb'] || 'GET'
68
+ date = options['date'] || Time.now.httpdate
69
+ auth_string = options['auth-string'] || false
70
+ content_md5 = options['content-md5']
71
+ content_type = options['content-type']
67
72
 
68
73
  unless ['SharedKey', 'SharedKeyLight'].include?(auth_type)
69
74
  raise ArgumentError, "auth type must be SharedKey or SharedKeyLight"
@@ -93,6 +98,9 @@ module Azure
93
98
  # - :auth_string. If true, prepends the auth_type + account name to the
94
99
  # result and returns a string. The default is false.
95
100
  #
101
+ # You may also use the string forms as keys, e.g. "Auth-Type" instead of
102
+ # :auth_type, if you prefer.
103
+ #
96
104
  # The other headers of potential significance are below. Note that you
97
105
  # are not required to set any of them.
98
106
  #
@@ -131,7 +139,7 @@ module Azure
131
139
  # }
132
140
  #
133
141
  # sig = sig.blob_signature(headers)
134
- # headers['Authorization'] = sig
142
+ # headers['Authorization'] = sig
135
143
  #
136
144
  # req = RestClient::Request.new(
137
145
  # :method => 'get',
@@ -139,23 +147,25 @@ module Azure
139
147
  # :headers => headers
140
148
  # )
141
149
  #
142
- # response = req.execute
143
- # p response.body
150
+ # response = req.execute
151
+ # p response.body
144
152
  #
145
153
  def blob_signature(headers = {})
146
- auth_string = headers.delete(:auth_string) || false
147
- auth_type = headers.delete(:auth_type) || 'SharedKey'
148
- verb = headers.delete(:verb) || 'GET'
154
+ headers.clone.each{ |k,v| headers[k.to_s.downcase.tr('_', '-')] = v }
155
+
156
+ auth_string = headers.delete('auth-string') || false
157
+ auth_type = headers.delete('auth_type') || 'SharedKey'
158
+ verb = headers.delete('verb') || 'GET'
149
159
 
150
160
  unless ['SharedKey', 'SharedKeyLight'].include?(auth_type)
151
161
  raise ArgumentError, "auth type must be SharedKey or SharedKeyLight"
152
162
  end
153
163
 
154
- headers['x-ms-date'] ||= headers.delete(:x_ms_date) || Time.now.httpdate
155
- headers['x-ms-version'] ||= headers.delete(:x_ms_version) || '2015-02-21'
164
+ headers['x-ms-date'] ||= Time.now.httpdate
165
+ headers['x-ms-version'] ||= '2015-02-21'
156
166
 
157
167
  if auth_type == 'SharedKeyLight'
158
- headers['Date'] ||= headers['x-ms-date'] || headers[:date] || Time.now.httpdate
168
+ headers['date'] ||= headers['x-ms-date'] || Time.now.httpdate
159
169
  end
160
170
 
161
171
  body = generate_string(verb, headers, auth_type).encode('UTF-8')
@@ -187,31 +197,32 @@ module Azure
187
197
  private
188
198
 
189
199
  def generate_string(verb, headers, auth_type)
200
+ headers.clone.keys.each{ |k| headers[k.to_s.downcase] = headers[k] }
190
201
  canonical_headers = canonicalize_headers(headers)
191
202
 
192
203
  if auth_type == 'SharedKeyLight'
193
204
  [
194
205
  verb.to_s.upcase,
195
- headers['Content-MD5'],
196
- headers['Content-Type'],
197
- headers['Date'],
206
+ headers['content-md5'],
207
+ headers['content-type'],
208
+ headers['date'],
198
209
  canonical_headers,
199
210
  canonical_resource
200
211
  ].join("\n")
201
212
  else
202
213
  [
203
214
  verb.to_s.upcase,
204
- headers['Content-Encoding'],
205
- headers['Content-Language'],
206
- headers['Content-Length'],
207
- headers['Content-MD5'],
208
- headers['Content-Type'],
209
- headers['Date'],
210
- headers['If-Modified-Since'],
211
- headers['If-Match'],
212
- headers['If-None-Match'],
213
- headers['If-Unmodified-Since'],
214
- headers['Range'],
215
+ headers['content-encoding'],
216
+ headers['content-language'],
217
+ headers['content-length'],
218
+ headers['content-md5'],
219
+ headers['content-type'],
220
+ headers['date'],
221
+ headers['if-modified-since'],
222
+ headers['if-match'],
223
+ headers['if-none-match'],
224
+ headers['if-unmodified-since'],
225
+ headers['range'],
215
226
  canonical_headers,
216
227
  canonical_resource,
217
228
  ].join("\n")
@@ -222,7 +233,7 @@ module Azure
222
233
  #--
223
234
  # Borrowed from azure-sdk-for-ruby. I had my own, but this was nicer.
224
235
  #
225
- def canonicalize_resource(url)
236
+ def canonicalize_resource(uri)
226
237
  resource = '/' + account_name + (uri.path.empty? ? '/' : uri.path)
227
238
  params = CGI.parse(uri.query.to_s).map { |k,v| [k.downcase, v] }
228
239
  params.sort_by! { |k,v| k }
@@ -236,7 +247,7 @@ module Azure
236
247
  #
237
248
  def canonicalize_headers(headers)
238
249
  headers = headers.map { |k,v| [k.to_s.gsub('_', '-').downcase, v] }
239
- headers.select! { |k,v| k =~ /^x-ms-/ }
250
+ headers.select! { |k,v| k =~ /^x-ms-/i }
240
251
  headers.sort_by! { |k,v| k }
241
252
  headers.map! { |k,v| '%s:%s' % [k, v] }
242
253
  headers.map! { |h| h.gsub(/\s+/, ' ') }.join("\n")
@@ -9,7 +9,7 @@ class TC_Azure_Signature < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  test "version constant is set to expected value" do
12
- assert_equal("0.2.0", Azure::Signature::VERSION)
12
+ assert_equal("0.2.1", Azure::Signature::VERSION)
13
13
  end
14
14
 
15
15
  test "key method basic functionality" do
@@ -34,7 +34,7 @@ class TC_Azure_Signature < Test::Unit::TestCase
34
34
 
35
35
  test "uri method basic functionality" do
36
36
  assert_respond_to(@sig, :uri)
37
- assert_kind_of(URI, @sig.uri)
37
+ assert_kind_of(Addressable::URI, @sig.uri)
38
38
  end
39
39
 
40
40
  test "canonical_resource method basic functionality" do
@@ -95,6 +95,13 @@ class TC_Azure_Signature < Test::Unit::TestCase
95
95
  assert_alias_method(@sig, :blob_signature, :queue_signature)
96
96
  end
97
97
 
98
+ test "signature with strings or symbols is identical" do
99
+ date = '2016-01-01'
100
+ sig_symbol = @sig.signature(:table, :auth_string => true, :date => date, :verb => 'PUT')
101
+ sig_string = @sig.signature(:table, 'Auth-String' => true, 'Date' => date, 'Verb' => 'PUT')
102
+ assert_equal(sig_symbol, sig_string)
103
+ end
104
+
98
105
  def teardown
99
106
  @key = nil
100
107
  @url = nil
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure-signature
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-13 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: test-unit
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -64,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
78
  version: '0'
65
79
  requirements: []
66
80
  rubyforge_project:
67
- rubygems_version: 2.4.5.1
81
+ rubygems_version: 2.6.4
68
82
  signing_key:
69
83
  specification_version: 4
70
84
  summary: Generate authentication signatures for Azure