signature 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/signature.rb +7 -1
- data/lib/signature/query_encoder.rb +47 -0
- data/lib/signature/version.rb +1 -1
- data/spec/signature_spec.rb +15 -0
- metadata +46 -61
data/Gemfile.lock
CHANGED
data/lib/signature.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
|
+
require 'signature/query_encoder'
|
4
|
+
|
3
5
|
module Signature
|
4
6
|
class AuthenticationError < RuntimeError; end
|
5
7
|
|
@@ -18,6 +20,8 @@ module Signature
|
|
18
20
|
class Request
|
19
21
|
attr_accessor :path, :query_hash
|
20
22
|
|
23
|
+
include QueryEncoder
|
24
|
+
|
21
25
|
# http://www.w3.org/TR/NOTE-datetime
|
22
26
|
ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
|
23
27
|
|
@@ -182,7 +186,9 @@ module Signature
|
|
182
186
|
# Exclude signature from signature generation!
|
183
187
|
hash.delete("auth_signature")
|
184
188
|
|
185
|
-
hash.
|
189
|
+
hash.sort.map do |k, v|
|
190
|
+
QueryEncoder.encode_param_without_escaping(k, v)
|
191
|
+
end.join('&')
|
186
192
|
end
|
187
193
|
|
188
194
|
def validate_version!
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Signature
|
2
|
+
# Query string encoding extracted with thanks from em-http-request
|
3
|
+
module QueryEncoder
|
4
|
+
class << self
|
5
|
+
# URL encodes query parameters:
|
6
|
+
# single k=v, or a URL encoded array, if v is an array of values
|
7
|
+
def encode_param(k, v)
|
8
|
+
if v.is_a?(Array)
|
9
|
+
v.map { |e| escape(k) + "[]=" + escape(e) }.join("&")
|
10
|
+
else
|
11
|
+
escape(k) + "=" + escape(v)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Like encode_param, but doesn't url escape keys or values
|
16
|
+
def encode_param_without_escaping(k, v)
|
17
|
+
if v.is_a?(Array)
|
18
|
+
v.map { |e| k + "[]=" + e }.join("&")
|
19
|
+
else
|
20
|
+
k + "=" + v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def escape(s)
|
27
|
+
if defined?(EscapeUtils)
|
28
|
+
EscapeUtils.escape_url(s.to_s)
|
29
|
+
else
|
30
|
+
s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) {
|
31
|
+
'%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if ''.respond_to?(:bytesize)
|
37
|
+
def bytesize(string)
|
38
|
+
string.bytesize
|
39
|
+
end
|
40
|
+
else
|
41
|
+
def bytesize(string)
|
42
|
+
string.size
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/signature/version.rb
CHANGED
data/spec/signature_spec.rb
CHANGED
@@ -60,6 +60,21 @@ describe Signature do
|
|
60
60
|
@request.sign(@token)[:auth_signature].should == @signature
|
61
61
|
end
|
62
62
|
|
63
|
+
it "should generate correct string when query hash contains array" do
|
64
|
+
@request.query_hash = {
|
65
|
+
"things" => ["thing1", "thing2"]
|
66
|
+
}
|
67
|
+
@request.send(:string_to_sign).should == "POST\n/some/path\nthings[]=thing1&things[]=thing2"
|
68
|
+
end
|
69
|
+
|
70
|
+
# This may well change in auth version 2
|
71
|
+
it "should not escape keys or values in the query string" do
|
72
|
+
@request.query_hash = {
|
73
|
+
"key;" => "value@"
|
74
|
+
}
|
75
|
+
@request.send(:string_to_sign).should == "POST\n/some/path\nkey;=value@"
|
76
|
+
end
|
77
|
+
|
63
78
|
it "should use the path to generate signature" do
|
64
79
|
@request.path = '/some/other/path'
|
65
80
|
@request.sign(@token)[:auth_signature].should_not == @signature
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: signature
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 4
|
10
|
-
version: 0.1.4
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Martyn Loughran
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: rspec
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 1156501143490752456
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 9
|
32
|
-
- 0
|
20
|
+
- !ruby/object:Gem::Version
|
33
21
|
version: 2.9.0
|
34
22
|
type: :development
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: em-spec
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.9.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: em-spec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
40
33
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
|
45
|
-
segments:
|
46
|
-
- 0
|
47
|
-
version: "0"
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
48
38
|
type: :development
|
49
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
50
46
|
description: Simple key/secret based authentication for apis
|
51
|
-
email:
|
47
|
+
email:
|
52
48
|
- me@mloughran.com
|
53
49
|
executables: []
|
54
|
-
|
55
50
|
extensions: []
|
56
|
-
|
57
51
|
extra_rdoc_files: []
|
58
|
-
|
59
|
-
files:
|
52
|
+
files:
|
60
53
|
- .gitignore
|
61
54
|
- .travis.yml
|
62
55
|
- Gemfile
|
@@ -65,43 +58,35 @@ files:
|
|
65
58
|
- README.md
|
66
59
|
- Rakefile
|
67
60
|
- lib/signature.rb
|
61
|
+
- lib/signature/query_encoder.rb
|
68
62
|
- lib/signature/version.rb
|
69
63
|
- signature.gemspec
|
70
64
|
- spec/signature_spec.rb
|
71
65
|
- spec/spec_helper.rb
|
72
66
|
homepage: http://github.com/mloughran/signature
|
73
67
|
licenses: []
|
74
|
-
|
75
68
|
post_install_message:
|
76
69
|
rdoc_options: []
|
77
|
-
|
78
|
-
require_paths:
|
70
|
+
require_paths:
|
79
71
|
- lib
|
80
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
73
|
none: false
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
|
87
|
-
- 0
|
88
|
-
version: "0"
|
89
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
79
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
|
95
|
-
segments:
|
96
|
-
- 0
|
97
|
-
version: "0"
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
98
84
|
requirements: []
|
99
|
-
|
100
85
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.8.
|
86
|
+
rubygems_version: 1.8.24
|
102
87
|
signing_key:
|
103
88
|
specification_version: 3
|
104
89
|
summary: Simple key/secret based authentication for apis
|
105
|
-
test_files:
|
90
|
+
test_files:
|
106
91
|
- spec/signature_spec.rb
|
107
92
|
- spec/spec_helper.rb
|