signauth 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,8 +6,8 @@ module Signauth
6
6
  attr_accessor :path
7
7
  attr_accessor :params
8
8
 
9
- def initialize(signature_version = Signauth::Signature::Version1)
10
- extend(signature_version)
9
+ def initialize(signature_version = 1)
10
+ extend(Signature.const_get("Version#{signature_version}"))
11
11
  @method = "GET"
12
12
  @host = ""
13
13
  @path = "/"
@@ -4,17 +4,18 @@ module Signauth
4
4
 
5
5
  def add_authorization!(credentials)
6
6
  params['access_key_id'] = credentials.access_key_id
7
- params['signature_version'] = '1'
8
- params['signature_method'] = 'HmacSHA256'
7
+ params['signature_version'] = version
8
+ params['signature_method'] = 'HMAC-SHA-256'
9
9
 
10
10
  params.delete('signature')
11
11
  params['signature'] = signature(credentials)
12
+ params
12
13
  end
13
14
 
14
15
  def authenticate(credentials)
15
16
  given = params.delete('signature')
16
17
  computed = signature(credentials)
17
- unless given == computed
18
+ unless slow_string_comparison(given, computed)
18
19
  raise Errors::SignatureDoesNotMatch,
19
20
  "Invalid signature: should have sent Base64(HmacSHA256(secret, #{string_to_sign.inspect}))"\
20
21
  ", but given #{given}"
@@ -26,8 +27,12 @@ module Signauth
26
27
 
27
28
  protected
28
29
 
30
+ def version
31
+ "1"
32
+ end
33
+
29
34
  def signature(credentials)
30
- Signer.sign(credentials.secret_access_key, string_to_sign)
35
+ Signer.sign(credentials.secret_access_key, string_to_sign, params['signature_method'])
31
36
  end
32
37
 
33
38
  def string_to_sign
@@ -43,6 +48,13 @@ module Signauth
43
48
  "#{URI.escape(name)}=#{URI.escape(value)}"
44
49
  end
45
50
 
51
+ def slow_string_comparison(given, computed)
52
+ return false if given.nil? || computed.nil? || given.length != computed.length
53
+ match = true
54
+ computed.chars.each_with_index{|c, i| match &= c == given[i] }
55
+ match
56
+ end
57
+
46
58
  end
47
59
  end
48
60
  end
@@ -19,7 +19,11 @@ module Signauth
19
19
  end
20
20
 
21
21
  protected
22
-
22
+
23
+ def version
24
+ "2"
25
+ end
26
+
23
27
  def validate_timestamp(skew)
24
28
  begin
25
29
  timestamp = Time.iso8601(params['timestamp'])
@@ -1,6 +1,7 @@
1
+ require "signauth/signature/version_1"
2
+ require "signauth/signature/version_2"
3
+
1
4
  module Signauth
2
5
  module Signature
3
- autoload :Version1, "signauth/signature/version_1"
4
- autoload :Version2, "signauth/signature/version_2"
5
6
  end
6
7
  end
@@ -5,13 +5,28 @@ module Signauth
5
5
  module Signer
6
6
  extend self
7
7
 
8
- def sign(secret, string_to_sign, digest_method = 'sha256')
9
- Base64.encode64(hmac(secret, string_to_sign, digest_method)).strip
8
+ def sign(secret, string_to_sign, algorithm = 'HMAC-SHA-256')
9
+ Base64.encode64(hmac(secret, string_to_sign, algorithm)).strip
10
10
  end
11
11
 
12
- def hmac(key, value, digest = 'sha256')
12
+ def hmac(key, value, algorithm = 'HMAC-SHA-256')
13
+ digest = digest_name(algorithm)
13
14
  OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(digest), key, value)
14
15
  end
15
16
 
17
+ private
18
+
19
+ def digest_name(algorithm)
20
+ ALGORITHM_DIGEST_MAPPING[algorithm]
21
+ end
22
+
23
+ ALGORITHM_DIGEST_MAPPING = {
24
+ "HMAC-MD5" => "md5",
25
+ "HMAC-SHA-1" => "sha1",
26
+ "HMAC-SHA-224" => "sha224",
27
+ "HMAC-SHA-256" => "sha256",
28
+ "HMAC-SHA-384" => "sha384",
29
+ "HMAC-SHA-512" => "sha512",
30
+ }
16
31
  end
17
32
  end
@@ -1,3 +1,3 @@
1
1
  module Signauth
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-23 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -82,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
82
  version: '0'
83
83
  segments:
84
84
  - 0
85
- hash: 566629137
85
+ hash: -585195729
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  none: false
88
88
  requirements:
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  segments:
93
93
  - 0
94
- hash: 566629137
94
+ hash: -585195729
95
95
  requirements: []
96
96
  rubyforge_project:
97
97
  rubygems_version: 1.8.24