api-auth 2.2.0 → 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -52
- data/.rubocop_todo.yml +92 -0
- data/.travis.yml +15 -14
- data/CHANGELOG.md +28 -0
- data/Gemfile +1 -1
- data/README.md +77 -38
- data/VERSION +1 -1
- data/api_auth.gemspec +15 -11
- data/gemfiles/http2.gemfile +7 -0
- data/gemfiles/http3.gemfile +7 -0
- data/gemfiles/http4.gemfile +7 -0
- data/gemfiles/rails_5.gemfile +5 -7
- data/gemfiles/rails_51.gemfile +5 -5
- data/gemfiles/rails_52.gemfile +9 -0
- data/gemfiles/rails_60.gemfile +11 -0
- data/lib/api_auth.rb +3 -0
- data/lib/api_auth/base.rb +2 -2
- data/lib/api_auth/headers.rb +19 -8
- data/lib/api_auth/railtie.rb +9 -5
- data/lib/api_auth/request_drivers/action_controller.rb +1 -0
- data/lib/api_auth/request_drivers/faraday.rb +2 -1
- data/lib/api_auth/request_drivers/grape_request.rb +87 -0
- data/lib/api_auth/request_drivers/http.rb +96 -0
- data/lib/api_auth/request_drivers/httpi.rb +1 -0
- data/lib/api_auth/request_drivers/net_http.rb +1 -1
- data/lib/api_auth/request_drivers/rack.rb +1 -0
- data/lib/api_auth/request_drivers/rest_client.rb +3 -2
- data/spec/api_auth_spec.rb +7 -0
- data/spec/headers_spec.rb +26 -8
- data/spec/request_drivers/action_controller_spec.rb +10 -4
- data/spec/request_drivers/action_dispatch_spec.rb +17 -11
- data/spec/request_drivers/curb_spec.rb +9 -3
- data/spec/request_drivers/faraday_spec.rb +6 -0
- data/spec/request_drivers/grape_request_spec.rb +279 -0
- data/spec/request_drivers/http_spec.rb +190 -0
- data/spec/request_drivers/httpi_spec.rb +6 -0
- data/spec/request_drivers/net_http_spec.rb +6 -0
- data/spec/request_drivers/rack_spec.rb +6 -0
- data/spec/request_drivers/rest_client_spec.rb +93 -15
- data/spec/spec_helper.rb +3 -4
- metadata +102 -66
- data/gemfiles/rails_4.gemfile +0 -11
- data/gemfiles/rails_41.gemfile +0 -11
- data/gemfiles/rails_42.gemfile +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60f6b22c31a0167767c8aa90d9e831bde874fd6831798d1c1a5104849005a4f4
|
4
|
+
data.tar.gz: 9be6acbac2c38e6fc0d0af33d6ba9fd6f9bc4ca0dc5dc6050fee1a8e139d94b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30f33a8543297ceb7e99bb028cca8377e5af639957d9edc06e36f1968d252a0012357030e726d9ff7024ebc99da30e9a1bb324eba686ffb6c784931ac2063620
|
7
|
+
data.tar.gz: 00b796d683a878643d152f8bad511f38d92fcf6182e941eff490609b5f122b301b08c8820af7229b9a5b744414ded4069054523244bba571a1bc408da6b4ba06
|
data/.rubocop.yml
CHANGED
@@ -1,63 +1,22 @@
|
|
1
|
-
|
2
|
-
# `rubocop --auto-gen-config`
|
3
|
-
# on 2016-02-10 17:06:30 +0100 using RuboCop version 0.37.1.
|
4
|
-
# The point is for the user to remove these configuration records
|
5
|
-
# one by one as the offenses are removed from the code base.
|
6
|
-
# Note that changes in the inspected code, or installation of new
|
7
|
-
# versions of RuboCop, may require this file to be generated again.
|
1
|
+
inherit_from: .rubocop_todo.yml
|
8
2
|
|
9
|
-
|
10
|
-
|
11
|
-
Lint/AssignmentInCondition:
|
12
|
-
Exclude:
|
13
|
-
- 'lib/api_auth/base.rb'
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.4
|
14
5
|
|
15
|
-
# Offense count: 2
|
16
6
|
Metrics/AbcSize:
|
17
7
|
Max: 25
|
18
8
|
|
19
|
-
#
|
20
|
-
Metrics/CyclomaticComplexity:
|
21
|
-
Max: 13
|
22
|
-
|
23
|
-
# Offense count: 74
|
24
|
-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
9
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
25
10
|
# URISchemes: http, https
|
26
|
-
|
27
|
-
Max:
|
11
|
+
Layout/LineLength:
|
12
|
+
Max: 140
|
28
13
|
|
29
|
-
# Offense count: 4
|
30
|
-
# Configuration parameters: CountComments.
|
31
14
|
Metrics/MethodLength:
|
32
|
-
Max:
|
33
|
-
|
34
|
-
# Offense count: 1
|
35
|
-
Metrics/PerceivedComplexity:
|
36
|
-
Max: 8
|
37
|
-
|
38
|
-
# Offense count: 8
|
39
|
-
Style/AccessorMethodName:
|
40
|
-
Exclude:
|
41
|
-
- 'lib/api_auth/railtie.rb'
|
42
|
-
- 'lib/api_auth/request_drivers/action_controller.rb'
|
43
|
-
- 'lib/api_auth/request_drivers/curb.rb'
|
44
|
-
- 'lib/api_auth/request_drivers/faraday.rb'
|
45
|
-
- 'lib/api_auth/request_drivers/httpi.rb'
|
46
|
-
- 'lib/api_auth/request_drivers/net_http.rb'
|
47
|
-
- 'lib/api_auth/request_drivers/rack.rb'
|
48
|
-
- 'lib/api_auth/request_drivers/rest_client.rb'
|
49
|
-
|
50
|
-
# Offense count: 4
|
51
|
-
Style/Documentation:
|
52
|
-
Exclude:
|
53
|
-
- 'spec/**/*'
|
54
|
-
- 'test/**/*'
|
55
|
-
- 'lib/api_auth/railtie.rb'
|
56
|
-
- 'lib/api_auth/request_drivers/rest_client.rb'
|
15
|
+
Max: 40
|
57
16
|
|
58
|
-
|
59
|
-
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
|
60
|
-
Style/FileName:
|
17
|
+
Naming/FileName:
|
61
18
|
Exclude:
|
62
19
|
- 'lib/api-auth.rb'
|
63
|
-
|
20
|
+
|
21
|
+
Style/FrozenStringLiteralComment:
|
22
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-10-22 20:30:52 +0700 using RuboCop version 0.59.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Cop supports --auto-correct.
|
11
|
+
# Configuration parameters: Include, TreatCommentsAsGroupSeparators.
|
12
|
+
# Include: **/*.gemspec
|
13
|
+
Gemspec/OrderedDependencies:
|
14
|
+
Exclude:
|
15
|
+
- 'api_auth.gemspec'
|
16
|
+
|
17
|
+
# Offense count: 1
|
18
|
+
# Configuration parameters: AllowSafeAssignment.
|
19
|
+
Lint/AssignmentInCondition:
|
20
|
+
Exclude:
|
21
|
+
- 'lib/api_auth/base.rb'
|
22
|
+
|
23
|
+
# Offense count: 9
|
24
|
+
# Configuration parameters: CheckForMethodsWithNoSideEffects.
|
25
|
+
Lint/Void:
|
26
|
+
Exclude:
|
27
|
+
- 'lib/api_auth/headers.rb'
|
28
|
+
- 'lib/api_auth/request_drivers/action_controller.rb'
|
29
|
+
- 'lib/api_auth/request_drivers/curb.rb'
|
30
|
+
- 'lib/api_auth/request_drivers/faraday.rb'
|
31
|
+
- 'lib/api_auth/request_drivers/grape_request.rb'
|
32
|
+
- 'lib/api_auth/request_drivers/httpi.rb'
|
33
|
+
- 'lib/api_auth/request_drivers/net_http.rb'
|
34
|
+
- 'lib/api_auth/request_drivers/rack.rb'
|
35
|
+
- 'lib/api_auth/request_drivers/rest_client.rb'
|
36
|
+
|
37
|
+
# Offense count: 1
|
38
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
39
|
+
# ExcludedMethods: refine
|
40
|
+
Metrics/BlockLength:
|
41
|
+
Max: 27
|
42
|
+
|
43
|
+
# Offense count: 1
|
44
|
+
Metrics/CyclomaticComplexity:
|
45
|
+
Max: 15
|
46
|
+
|
47
|
+
# Offense count: 1
|
48
|
+
Metrics/PerceivedComplexity:
|
49
|
+
Max: 8
|
50
|
+
|
51
|
+
# Offense count: 10
|
52
|
+
Naming/AccessorMethodName:
|
53
|
+
Exclude:
|
54
|
+
- 'lib/api_auth/railtie.rb'
|
55
|
+
- 'lib/api_auth/request_drivers/action_controller.rb'
|
56
|
+
- 'lib/api_auth/request_drivers/curb.rb'
|
57
|
+
- 'lib/api_auth/request_drivers/faraday.rb'
|
58
|
+
- 'lib/api_auth/request_drivers/grape_request.rb'
|
59
|
+
- 'lib/api_auth/request_drivers/http.rb'
|
60
|
+
- 'lib/api_auth/request_drivers/httpi.rb'
|
61
|
+
- 'lib/api_auth/request_drivers/net_http.rb'
|
62
|
+
- 'lib/api_auth/request_drivers/rack.rb'
|
63
|
+
- 'lib/api_auth/request_drivers/rest_client.rb'
|
64
|
+
|
65
|
+
# Offense count: 3
|
66
|
+
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
|
67
|
+
# AllowedNames: io, id, to, by, on, in, at, ip, db
|
68
|
+
Naming/MethodParameterName:
|
69
|
+
Exclude:
|
70
|
+
- 'lib/api_auth/base.rb'
|
71
|
+
- 'spec/railtie_spec.rb'
|
72
|
+
|
73
|
+
# Offense count: 1
|
74
|
+
# Configuration parameters: EnforcedStyle.
|
75
|
+
# SupportedStyles: inline, group
|
76
|
+
Style/AccessModifierDeclarations:
|
77
|
+
Exclude:
|
78
|
+
- 'lib/api_auth/headers.rb'
|
79
|
+
|
80
|
+
# Offense count: 9
|
81
|
+
Style/CommentedKeyword:
|
82
|
+
Exclude:
|
83
|
+
- 'lib/api_auth/base.rb'
|
84
|
+
- 'lib/api_auth/railtie.rb'
|
85
|
+
|
86
|
+
# Offense count: 4
|
87
|
+
Style/Documentation:
|
88
|
+
Exclude:
|
89
|
+
- 'spec/**/*'
|
90
|
+
- 'test/**/*'
|
91
|
+
- 'lib/api_auth/railtie.rb'
|
92
|
+
- 'lib/api_auth/request_drivers/rest_client.rb'
|
data/.travis.yml
CHANGED
@@ -2,16 +2,18 @@ language: ruby
|
|
2
2
|
sudo: false
|
3
3
|
cache: bundler
|
4
4
|
rvm:
|
5
|
-
- 2.
|
6
|
-
- 2.
|
7
|
-
- 2.
|
8
|
-
- 2.
|
5
|
+
- 2.4.3
|
6
|
+
- 2.5.3
|
7
|
+
- 2.6.1
|
8
|
+
- 2.7.1
|
9
9
|
gemfile:
|
10
|
-
- gemfiles/rails_4.gemfile
|
11
|
-
- gemfiles/rails_41.gemfile
|
12
|
-
- gemfiles/rails_42.gemfile
|
13
10
|
- gemfiles/rails_5.gemfile
|
14
11
|
- gemfiles/rails_51.gemfile
|
12
|
+
- gemfiles/rails_52.gemfile
|
13
|
+
- gemfiles/rails_60.gemfile
|
14
|
+
- gemfiles/http2.gemfile
|
15
|
+
- gemfiles/http3.gemfile
|
16
|
+
- gemfiles/http4.gemfile
|
15
17
|
env:
|
16
18
|
- TEST_SUITE=rake
|
17
19
|
|
@@ -22,15 +24,14 @@ script:
|
|
22
24
|
- bundle exec $TEST_SUITE
|
23
25
|
|
24
26
|
matrix:
|
25
|
-
exclude:
|
26
|
-
- rvm: 2.1.9
|
27
|
-
gemfile: gemfiles/rails_5.gemfile
|
28
|
-
- rvm: 2.1.9
|
29
|
-
gemfile: gemfiles/rails_51.gemfile
|
30
27
|
include:
|
31
|
-
- rvm: 2.
|
32
|
-
gemfile: gemfiles/
|
28
|
+
- rvm: 2.7.1
|
29
|
+
gemfile: gemfiles/rails_60.gemfile
|
33
30
|
env: TEST_SUITE="rubocop lib/ spec/"
|
31
|
+
exclude:
|
32
|
+
- rvm: 2.4.3
|
33
|
+
gemfile: gemfiles/rails_60.gemfile
|
34
|
+
env: TEST_SUITE=rake
|
34
35
|
|
35
36
|
notifications:
|
36
37
|
email: false
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
+
# 2.4.1 (2020-06-23)
|
2
|
+
- Fix inadvertant ActiveSupport dependecy (#189 taylorthurlow)
|
3
|
+
|
4
|
+
# 2.4.0 (2020-05-05)
|
5
|
+
- Improved support for Rails 6.0 (#179 taylorthurlow, #177 fwininger)
|
6
|
+
- Added Ruby 2.6.0 support (#174 fwininger)
|
7
|
+
- README updates (#186 iranthau)
|
8
|
+
|
9
|
+
# 2.3.1 (2018-11-06)
|
10
|
+
- Fixed a regression in the http.rb driver (#173 tycooon)
|
11
|
+
|
12
|
+
# 2.3.0 (2018-10-23)
|
13
|
+
- Added support for Grape API (#169 phuongnd08 & dunghuynh)
|
14
|
+
- Added option for specifying customer headers to sign via new `headers_to_sign`
|
15
|
+
argument (#170 fakenine)
|
16
|
+
- Fix tests and drop support for Ruby < 2.3 (#171 fwininger)
|
17
|
+
|
18
|
+
# 2.2.0 (2018-03-12)
|
19
|
+
- Drop support ruby 1.x, rails 2.x, rails 3.x (#141 fwininger)
|
20
|
+
- Add http.rb request driver (#164 tycooon)
|
21
|
+
- Fix POST and PUT requests in RestClient (#151 fwininger)
|
22
|
+
- Allow clock skew to be user-defined (#136 mlarraz)
|
23
|
+
- Adds #original_uri method to all request drivers (#137 iMacTia)
|
24
|
+
- Rubocop and test fixes (fwininger & nicolasleger)
|
25
|
+
- Changed return type for request #content_md5 #timestamp #content_type (fwininger)
|
26
|
+
- Fix URI edge case where a URI contains another URI (zfletch)
|
27
|
+
- Updates to the README (zfletch)
|
28
|
+
|
1
29
|
# 2.1.0 (2016-12-22)
|
2
30
|
- Fixed a NoMethodError that might occur when using the NetHttp Driver (#130 grahamkenville)
|
3
31
|
- More securely compare signatures in a way that prevents timing attacks (#56 leishman, #133 will0)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# ApiAuth
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/mgomes/api_auth.
|
3
|
+
[![Build Status](https://travis-ci.org/mgomes/api_auth.svg?branch=master)](https://travis-ci.org/mgomes/api_auth)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/api-auth.svg)](https://badge.fury.io/rb/api-auth)
|
4
5
|
|
5
6
|
Logins and passwords are for humans. Communication between applications need to
|
6
7
|
be protected through different means.
|
@@ -20,19 +21,33 @@ have to be written in the same language as the clients.
|
|
20
21
|
## How it works
|
21
22
|
|
22
23
|
1. A canonical string is first created using your HTTP headers containing the
|
23
|
-
content-type, content-MD5, request
|
24
|
+
content-type, content-MD5, request path and the date/time stamp. If content-type or
|
24
25
|
content-MD5 are not present, then a blank string is used in their place. If the
|
25
26
|
timestamp isn't present, a valid HTTP date is automatically added to the
|
26
27
|
request. The canonical string is computed as follows:
|
27
28
|
|
28
|
-
|
29
|
+
```
|
30
|
+
canonical_string = "#{http method},#{content-type},#{content-MD5},#{request URI},#{timestamp}"
|
31
|
+
|
32
|
+
e.g.,
|
33
|
+
|
34
|
+
canonical_string = 'POST,application/json,,request_path,Tue, 30 May 2017 03:51:43 GMT'
|
35
|
+
```
|
29
36
|
|
30
37
|
2. This string is then used to create the signature which is a Base64 encoded
|
31
38
|
SHA1 HMAC, using the client's private secret key.
|
32
39
|
|
33
40
|
3. This signature is then added as the `Authorization` HTTP header in the form:
|
34
41
|
|
35
|
-
|
42
|
+
```
|
43
|
+
Authorization = APIAuth "#{client access id}:#{signature from step 2}"
|
44
|
+
```
|
45
|
+
|
46
|
+
A cURL request would look like:
|
47
|
+
|
48
|
+
```
|
49
|
+
curl -X POST --header 'Content-Type: application/json' --header "Date: Tue, 30 May 2017 03:51:43 GMT" --header "Authorization: ${AUTHORIZATION}" http://my-app.com/request_path`
|
50
|
+
```
|
36
51
|
|
37
52
|
5. On the server side, the SHA1 HMAC is computed in the same way using the
|
38
53
|
request headers and the client's secret key, which is known to only
|
@@ -51,9 +66,9 @@ minutes in order to avoid replay attacks.
|
|
51
66
|
|
52
67
|
## Requirement
|
53
68
|
|
54
|
-
|
69
|
+
This gem require Ruby >= 2.3 and Rails >= 4.0 if you use rails.
|
55
70
|
|
56
|
-
For older version of Ruby or Rails, please use ApiAuth v2.
|
71
|
+
For older version of Ruby or Rails, please use ApiAuth v2.1 and older.
|
57
72
|
|
58
73
|
**IMPORTANT: v2.0.0 is backwards incompatible with the default settings of v1.x to address a security vulnerability. See [CHANGELOG.md](/CHANGELOG.md) for security update information.**
|
59
74
|
|
@@ -62,7 +77,9 @@ For older version of Ruby or Rails, please use ApiAuth v2.X.
|
|
62
77
|
The gem doesn't have any dependencies outside of having a working OpenSSL
|
63
78
|
configuration for your Ruby VM. To install:
|
64
79
|
|
65
|
-
|
80
|
+
```bash
|
81
|
+
[sudo] gem install api-auth
|
82
|
+
```
|
66
83
|
|
67
84
|
Please note the dash in the name versus the underscore.
|
68
85
|
|
@@ -78,8 +95,8 @@ Here is the current list of supported request objects:
|
|
78
95
|
* Curb (Curl::Easy)
|
79
96
|
* RestClient
|
80
97
|
* Faraday
|
81
|
-
*
|
82
|
-
*
|
98
|
+
* HTTPI
|
99
|
+
* HTTP
|
83
100
|
|
84
101
|
### HTTP Client Objects
|
85
102
|
|
@@ -88,25 +105,29 @@ Here's a sample implementation of signing a request created with RestClient.
|
|
88
105
|
Assuming you have a client access id and secret as follows:
|
89
106
|
|
90
107
|
``` ruby
|
91
|
-
|
92
|
-
|
108
|
+
@access_id = "1044"
|
109
|
+
@secret_key = ApiAuth.generate_secret_key
|
93
110
|
```
|
94
111
|
|
95
112
|
A typical RestClient PUT request may look like:
|
96
113
|
|
97
114
|
``` ruby
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
115
|
+
headers = { 'Content-MD5' => "e59ff97941044f85df5297e1c302d260",
|
116
|
+
'Content-Type' => "text/plain",
|
117
|
+
'Date' => "Mon, 23 Jan 1984 03:29:56 GMT"
|
118
|
+
}
|
119
|
+
|
120
|
+
@request = RestClient::Request.new(
|
121
|
+
url: "/resource.xml?foo=bar&bar=foo",
|
122
|
+
headers: headers,
|
123
|
+
method: :put
|
124
|
+
)
|
104
125
|
```
|
105
126
|
|
106
127
|
To sign that request, simply call the `sign!` method as follows:
|
107
128
|
|
108
129
|
``` ruby
|
109
|
-
|
130
|
+
@signed_request = ApiAuth.sign!(@request, @access_id, @secret_key)
|
110
131
|
```
|
111
132
|
|
112
133
|
The proper `Authorization` request header has now been added to that request
|
@@ -120,23 +141,27 @@ method detection (like Curb or httpi), you can pass the http method as an option
|
|
120
141
|
into the sign! method like so:
|
121
142
|
|
122
143
|
``` ruby
|
123
|
-
|
144
|
+
@signed_request = ApiAuth.sign!(@request, @access_id, @secret_key, :override_http_method => "PUT")
|
124
145
|
```
|
125
146
|
|
126
147
|
If you want to use another digest existing in `OpenSSL::Digest`,
|
127
148
|
you can pass the http method as an option into the sign! method like so:
|
128
149
|
|
129
150
|
``` ruby
|
130
|
-
|
151
|
+
@signed_request = ApiAuth.sign!(@request, @access_id, @secret_key, :digest => 'sha256')
|
131
152
|
```
|
132
153
|
|
133
154
|
With the `digest` option, the `Authorization` header will be change from:
|
134
155
|
|
135
|
-
|
156
|
+
```
|
157
|
+
Authorization = APIAuth 'client access id':'signature'
|
158
|
+
```
|
136
159
|
|
137
160
|
to:
|
138
161
|
|
139
|
-
|
162
|
+
```
|
163
|
+
Authorization = APIAuth-HMAC-DIGEST_NAME 'client access id':'signature'
|
164
|
+
```
|
140
165
|
|
141
166
|
### ActiveResource Clients
|
142
167
|
|
@@ -144,9 +169,9 @@ ApiAuth can transparently protect your ActiveResource communications with a
|
|
144
169
|
single configuration line:
|
145
170
|
|
146
171
|
``` ruby
|
147
|
-
|
148
|
-
|
149
|
-
|
172
|
+
class MyResource < ActiveResource::Base
|
173
|
+
with_api_auth(access_id, secret_key)
|
174
|
+
end
|
150
175
|
```
|
151
176
|
|
152
177
|
This will automatically sign all outgoing ActiveResource requests from your app.
|
@@ -168,26 +193,28 @@ clients as well as verifying incoming API requests.
|
|
168
193
|
To generate a Base64 encoded API key for a client:
|
169
194
|
|
170
195
|
``` ruby
|
171
|
-
|
196
|
+
ApiAuth.generate_secret_key
|
172
197
|
```
|
173
198
|
|
174
199
|
To validate whether or not a request is authentic:
|
175
200
|
|
176
201
|
``` ruby
|
177
|
-
|
202
|
+
ApiAuth.authentic?(signed_request, secret_key)
|
178
203
|
```
|
179
204
|
|
180
205
|
The `authentic?` method uses the digest specified in the `Authorization` header.
|
181
206
|
For example SHA256 for:
|
182
207
|
|
183
|
-
|
208
|
+
```
|
209
|
+
Authorization = APIAuth-HMAC-SHA256 'client access id':'signature'
|
210
|
+
```
|
184
211
|
|
185
212
|
And by default SHA1 if the HMAC-DIGEST is not specified.
|
186
213
|
|
187
214
|
If you want to force the usage of another digest method, you should pass it as an option parameter:
|
188
215
|
|
189
216
|
``` ruby
|
190
|
-
|
217
|
+
ApiAuth.authentic?(signed_request, secret_key, :digest => 'sha256')
|
191
218
|
```
|
192
219
|
|
193
220
|
For security, requests dated older or newer than a certain timespan are considered inauthentic.
|
@@ -198,16 +225,24 @@ can't be dated into the far future.
|
|
198
225
|
The default span is 15 minutes, but you can override this:
|
199
226
|
|
200
227
|
```ruby
|
201
|
-
|
228
|
+
ApiAuth.authentic?(signed_request, secret_key, :clock_skew => 60) # or 1.minute in ActiveSupport
|
202
229
|
```
|
203
230
|
|
231
|
+
If you want to sign custom headers, you can pass them as an array of strings in the options like so:
|
232
|
+
|
233
|
+
``` ruby
|
234
|
+
ApiAuth.authentic?(signed_request, secret_key, headers_to_sign: %w[HTTP_HEADER_NAME])
|
235
|
+
```
|
236
|
+
|
237
|
+
With the specified headers values being at the end of the canonical string in the same order.
|
238
|
+
|
204
239
|
If your server is a Rails app, the signed request will be the `request` object.
|
205
240
|
|
206
241
|
In order to obtain the secret key for the client, you first need to look up the
|
207
242
|
client's access_id. ApiAuth can pull that from the request headers for you:
|
208
243
|
|
209
244
|
``` ruby
|
210
|
-
|
245
|
+
ApiAuth.access_id(signed_request)
|
211
246
|
```
|
212
247
|
|
213
248
|
Once you've looked up the client's record via the access id, you can then verify
|
@@ -219,12 +254,12 @@ Here's a sample method that can be used in a `before_action` if your server is a
|
|
219
254
|
Rails app:
|
220
255
|
|
221
256
|
``` ruby
|
222
|
-
|
257
|
+
before_action :api_authenticate
|
223
258
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
259
|
+
def api_authenticate
|
260
|
+
@current_account = Account.find_by_access_id(ApiAuth.access_id(request))
|
261
|
+
head(:unauthorized) unless @current_account && ApiAuth.authentic?(request, @current_account.secret_key)
|
262
|
+
end
|
228
263
|
```
|
229
264
|
|
230
265
|
## Development
|
@@ -237,11 +272,15 @@ To run the tests:
|
|
237
272
|
|
238
273
|
Install the dependencies for a particular Rails version by specifying a gemfile in `gemfiles` directory:
|
239
274
|
|
240
|
-
|
275
|
+
```
|
276
|
+
BUNDLE_GEMFILE=gemfiles/rails_5.gemfile bundle install
|
277
|
+
```
|
241
278
|
|
242
279
|
Run the tests with those dependencies:
|
243
280
|
|
244
|
-
|
281
|
+
```
|
282
|
+
BUNDLE_GEMFILE=gemfiles/rails_5.gemfile bundle exec rake
|
283
|
+
```
|
245
284
|
|
246
285
|
If you'd like to add support for additional HTTP clients, check out the already
|
247
286
|
implemented drivers in `lib/api_auth/request_drivers` for reference. All of
|