hmac_signature 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +80 -17
- data/lib/hmac_signature/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,29 +1,92 @@
|
|
1
|
-
|
1
|
+
HmacSignature
|
2
|
+
=============
|
2
3
|
|
3
|
-
|
4
|
+
Credit to Martyn Loughran and the 'signature' gem, which HmacSignature is based on.
|
4
5
|
|
5
|
-
|
6
|
+
Examples
|
7
|
+
--------
|
6
8
|
|
7
|
-
|
9
|
+
Client example (Sending auth via query_string/post body)
|
8
10
|
|
9
|
-
|
11
|
+
```ruby
|
12
|
+
params = {:some => 'parameters'}
|
13
|
+
token = HmacSignature::Token.new('my_key', 'my_secret')
|
14
|
+
request = HmacSignature::Strategy::Params::Request.new('POST', '/api/thing', params)
|
15
|
+
auth_hash = request.sign(token)
|
16
|
+
query_params = params.merge(auth_hash)
|
10
17
|
|
11
|
-
|
18
|
+
HTTParty.post('http://myservice/api/thing', {
|
19
|
+
:query => query_params
|
20
|
+
})
|
21
|
+
```
|
12
22
|
|
13
|
-
|
23
|
+
`query_params` looks like:
|
14
24
|
|
15
|
-
|
25
|
+
```ruby
|
26
|
+
{
|
27
|
+
:some => "parameters",
|
28
|
+
:auth_timestamp => 1273231888,
|
29
|
+
:auth_signature => "28b6bb0f242f71064916fad6ae463fe91f5adc302222dfc02c348ae1941eaf80",
|
30
|
+
:auth_version => "1.0",
|
31
|
+
:auth_key => "my_key"
|
32
|
+
}
|
16
33
|
|
17
|
-
|
34
|
+
```
|
18
35
|
|
19
|
-
|
36
|
+
Client example (Sending auth via headers)
|
20
37
|
|
21
|
-
|
38
|
+
```ruby
|
39
|
+
params = {:some => 'parameters'}
|
40
|
+
token = HmacSignature::Token.new('my_key', 'my_secret')
|
41
|
+
request = HmacSignature::Strategy::Headers::Request.new('POST', '/api/thing', params)
|
42
|
+
auth_headers = request.sign(token)
|
22
43
|
|
23
|
-
|
44
|
+
HTTParty.post('http://myservice/api/thing', {
|
45
|
+
:query => params,
|
46
|
+
:headers => auth_headers
|
47
|
+
})
|
48
|
+
```
|
24
49
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
50
|
+
`auth_headers` looks like:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
{
|
54
|
+
'X-Auth-Expires' => 1273231888,
|
55
|
+
'X-Auth-Signature' => "28b6bb0f242f71064916fad6ae463fe91f5adc302222dfc02c348ae1941eaf80",
|
56
|
+
'X-Auth-Version' => "1.0",
|
57
|
+
'X-Auth-Key' => "my_key"
|
58
|
+
}
|
59
|
+
|
60
|
+
```
|
61
|
+
|
62
|
+
Server example (sinatra)
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
error HmacSignature::AuthenticationError do |controller|
|
66
|
+
error = controller.env["sinatra.error"]
|
67
|
+
halt 401, "401 UNAUTHORIZED: #{error.message}\n"
|
68
|
+
end
|
69
|
+
|
70
|
+
post '/api/thing' do
|
71
|
+
request = HmacSignature::Strategy::Params::Request.new('POST', env["REQUEST_PATH"], params)
|
72
|
+
# This will raise a HmacSignature::AuthenticationError if request does not authenticate
|
73
|
+
token = request.authenticate do |key|
|
74
|
+
HmacSignature::Token.new(key, lookup_secret(key))
|
75
|
+
end
|
76
|
+
|
77
|
+
# Do whatever you need to do
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Developing
|
82
|
+
----------
|
83
|
+
|
84
|
+
bundle
|
85
|
+
bundle exec rspec spec/*_spec.rb
|
86
|
+
|
87
|
+
Please see the travis status for a list of rubies tested against
|
88
|
+
|
89
|
+
Copyright
|
90
|
+
---------
|
91
|
+
|
92
|
+
Copyright (c) 2013 Erik Lott. See LICENSE for details.
|