api-auth 1.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +71 -0
- data/.gitignore +13 -44
- data/.rubocop.yml +39 -0
- data/.rubocop_todo.yml +83 -0
- data/Appraisals +12 -36
- data/CHANGELOG.md +75 -1
- data/README.md +155 -52
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/api_auth.gemspec +35 -23
- data/gemfiles/rails_60.gemfile +9 -0
- data/gemfiles/rails_61.gemfile +9 -0
- data/gemfiles/rails_70.gemfile +9 -0
- data/lib/api-auth.rb +1 -1
- data/lib/api_auth/base.rb +41 -35
- data/lib/api_auth/errors.rb +4 -3
- data/lib/api_auth/headers.rb +38 -42
- data/lib/api_auth/helpers.rb +7 -16
- data/lib/api_auth/railtie.rb +34 -74
- data/lib/api_auth/request_drivers/action_controller.rb +27 -27
- data/lib/api_auth/request_drivers/action_dispatch.rb +0 -6
- data/lib/api_auth/request_drivers/curb.rb +16 -21
- data/lib/api_auth/request_drivers/faraday.rb +25 -34
- data/lib/api_auth/request_drivers/faraday_env.rb +102 -0
- 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 +22 -27
- data/lib/api_auth/request_drivers/net_http.rb +21 -26
- data/lib/api_auth/request_drivers/rack.rb +23 -28
- data/lib/api_auth/request_drivers/rest_client.rb +24 -29
- data/lib/api_auth.rb +4 -0
- data/lib/faraday/api_auth/middleware.rb +35 -0
- data/lib/faraday/api_auth.rb +8 -0
- data/spec/api_auth_spec.rb +135 -96
- data/spec/faraday_middleware_spec.rb +17 -0
- data/spec/headers_spec.rb +148 -108
- data/spec/helpers_spec.rb +8 -10
- data/spec/railtie_spec.rb +80 -99
- data/spec/request_drivers/action_controller_spec.rb +122 -79
- data/spec/request_drivers/action_dispatch_spec.rb +212 -85
- data/spec/request_drivers/curb_spec.rb +36 -33
- data/spec/request_drivers/faraday_env_spec.rb +188 -0
- data/spec/request_drivers/faraday_spec.rb +87 -83
- data/spec/request_drivers/grape_request_spec.rb +280 -0
- data/spec/request_drivers/http_spec.rb +190 -0
- data/spec/request_drivers/httpi_spec.rb +59 -59
- data/spec/request_drivers/net_http_spec.rb +70 -66
- data/spec/request_drivers/rack_spec.rb +101 -97
- data/spec/request_drivers/rest_client_spec.rb +218 -144
- data/spec/spec_helper.rb +15 -12
- metadata +144 -83
- data/.travis.yml +0 -40
- data/Gemfile.lock +0 -115
- data/gemfiles/rails_23.gemfile +0 -9
- data/gemfiles/rails_23.gemfile.lock +0 -70
- data/gemfiles/rails_30.gemfile +0 -9
- data/gemfiles/rails_30.gemfile.lock +0 -92
- data/gemfiles/rails_31.gemfile +0 -9
- data/gemfiles/rails_31.gemfile.lock +0 -98
- data/gemfiles/rails_32.gemfile +0 -9
- data/gemfiles/rails_32.gemfile.lock +0 -97
- data/gemfiles/rails_4.gemfile +0 -9
- data/gemfiles/rails_4.gemfile.lock +0 -94
- data/gemfiles/rails_41.gemfile +0 -9
- data/gemfiles/rails_41.gemfile.lock +0 -98
- data/gemfiles/rails_42.gemfile +0 -9
- data/gemfiles/rails_42.gemfile.lock +0 -115
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ApiAuth::RequestDrivers::NetHttpRequest do
|
|
4
|
+
let(:timestamp) { Time.now.utc.httpdate }
|
|
4
5
|
|
|
5
|
-
let(:
|
|
6
|
+
let(:request_path) { '/resource.xml?foo=bar&bar=foo' }
|
|
6
7
|
|
|
7
|
-
let(:
|
|
8
|
-
|
|
9
|
-
let(:request_headers){
|
|
8
|
+
let(:request_headers) do
|
|
10
9
|
{
|
|
11
|
-
'Authorization'
|
|
12
|
-
'
|
|
10
|
+
'Authorization' => 'APIAuth 1044:12345',
|
|
11
|
+
'X-Authorization-Content-SHA256' => '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=',
|
|
13
12
|
'content-type' => 'text/plain',
|
|
14
13
|
'date' => timestamp
|
|
15
14
|
}
|
|
16
|
-
|
|
15
|
+
end
|
|
17
16
|
|
|
18
17
|
let(:request) do
|
|
19
18
|
net_http_request = Net::HTTP::Put.new(request_path, request_headers)
|
|
@@ -21,178 +20,183 @@ describe ApiAuth::RequestDrivers::NetHttpRequest do
|
|
|
21
20
|
net_http_request
|
|
22
21
|
end
|
|
23
22
|
|
|
24
|
-
subject(:driven_request){ ApiAuth::RequestDrivers::NetHttpRequest.new(request) }
|
|
23
|
+
subject(:driven_request) { ApiAuth::RequestDrivers::NetHttpRequest.new(request) }
|
|
25
24
|
|
|
26
|
-
describe
|
|
27
|
-
describe
|
|
28
|
-
it
|
|
25
|
+
describe 'getting headers correctly' do
|
|
26
|
+
describe '#content_type' do
|
|
27
|
+
it 'gets the content_type' do
|
|
29
28
|
expect(driven_request.content_type).to eq('text/plain')
|
|
30
29
|
end
|
|
31
30
|
|
|
32
|
-
it
|
|
33
|
-
request = Net::HTTP::Put::Multipart.new(
|
|
34
|
-
|
|
31
|
+
it 'gets multipart content_type' do
|
|
32
|
+
request = Net::HTTP::Put::Multipart.new('/resource.xml?foo=bar&bar=foo',
|
|
33
|
+
'file' => UploadIO.new(File.new('spec/fixtures/upload.png'), 'image/png', 'upload.png'))
|
|
35
34
|
driven_request = ApiAuth::RequestDrivers::NetHttpRequest.new(request)
|
|
36
35
|
expect(driven_request.content_type).to match 'multipart/form-data; boundary='
|
|
37
36
|
end
|
|
38
37
|
end
|
|
39
38
|
|
|
40
|
-
it
|
|
41
|
-
expect(driven_request.
|
|
39
|
+
it 'gets the content_hash' do
|
|
40
|
+
expect(driven_request.content_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
|
|
42
41
|
end
|
|
43
42
|
|
|
44
|
-
it
|
|
43
|
+
it 'gets the request_uri' do
|
|
45
44
|
expect(driven_request.request_uri).to eq('/resource.xml?foo=bar&bar=foo')
|
|
46
45
|
end
|
|
47
46
|
|
|
48
|
-
it
|
|
47
|
+
it 'gets the timestamp' do
|
|
49
48
|
expect(driven_request.timestamp).to eq(timestamp)
|
|
50
49
|
end
|
|
51
50
|
|
|
52
|
-
it
|
|
51
|
+
it 'gets the authorization_header' do
|
|
53
52
|
expect(driven_request.authorization_header).to eq('APIAuth 1044:12345')
|
|
54
53
|
end
|
|
55
54
|
|
|
56
|
-
describe
|
|
57
|
-
it
|
|
58
|
-
expect(driven_request.
|
|
55
|
+
describe '#calculated_hash' do
|
|
56
|
+
it 'calculate content hash from the body' do
|
|
57
|
+
expect(driven_request.calculated_hash).to eq('JsYKYdAdtYNspw/v1EpqAWYgQTyO9fJZpsVhLU9507g=')
|
|
59
58
|
end
|
|
60
59
|
|
|
61
|
-
it
|
|
60
|
+
it 'treats no body as empty string' do
|
|
62
61
|
request.body = nil
|
|
63
|
-
expect(driven_request.
|
|
62
|
+
expect(driven_request.calculated_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
|
|
64
63
|
end
|
|
65
64
|
|
|
66
|
-
it
|
|
65
|
+
it 'calculates correctly for multipart content' do
|
|
67
66
|
request.body = nil
|
|
68
67
|
request.body_stream = File.new('spec/fixtures/upload.png')
|
|
69
|
-
expect(driven_request.
|
|
68
|
+
expect(driven_request.calculated_hash).to eq('AlKDe7kjMQhuKgKuNG8I7GA93MasHcaVJkJLaUT7+dY=')
|
|
70
69
|
end
|
|
71
70
|
end
|
|
72
71
|
|
|
73
|
-
describe
|
|
74
|
-
context
|
|
75
|
-
let(:request){ Net::HTTP::Put.new(request_path, request_headers) }
|
|
72
|
+
describe 'http_method' do
|
|
73
|
+
context 'when put request' do
|
|
74
|
+
let(:request) { Net::HTTP::Put.new(request_path, request_headers) }
|
|
76
75
|
|
|
77
|
-
it
|
|
76
|
+
it 'returns upcased put' do
|
|
78
77
|
expect(driven_request.http_method).to eq('PUT')
|
|
79
78
|
end
|
|
80
79
|
end
|
|
81
80
|
|
|
82
|
-
context
|
|
83
|
-
let(:request){ Net::HTTP::Get.new(request_path, request_headers) }
|
|
81
|
+
context 'when get request' do
|
|
82
|
+
let(:request) { Net::HTTP::Get.new(request_path, request_headers) }
|
|
84
83
|
|
|
85
|
-
it
|
|
84
|
+
it 'returns upcased get' do
|
|
86
85
|
expect(driven_request.http_method).to eq('GET')
|
|
87
86
|
end
|
|
88
87
|
end
|
|
89
88
|
end
|
|
90
89
|
end
|
|
91
90
|
|
|
92
|
-
describe
|
|
93
|
-
let(:request_headers)
|
|
91
|
+
describe 'setting headers correctly' do
|
|
92
|
+
let(:request_headers) do
|
|
94
93
|
{
|
|
95
94
|
'content-type' => 'text/plain'
|
|
96
95
|
}
|
|
97
|
-
|
|
96
|
+
end
|
|
98
97
|
|
|
99
98
|
let(:request) do
|
|
100
99
|
Net::HTTP::Put.new(request_path, request_headers)
|
|
101
100
|
end
|
|
102
101
|
|
|
103
|
-
describe
|
|
104
|
-
context
|
|
102
|
+
describe '#populate_content_hash' do
|
|
103
|
+
context 'when request type has no body' do
|
|
105
104
|
let(:request) do
|
|
106
105
|
Net::HTTP::Get.new(request_path, request_headers)
|
|
107
106
|
end
|
|
108
107
|
|
|
109
|
-
it "doesn't populate content
|
|
110
|
-
driven_request.
|
|
111
|
-
expect(request[
|
|
108
|
+
it "doesn't populate content hash" do
|
|
109
|
+
driven_request.populate_content_hash
|
|
110
|
+
expect(request['X-Authorization-Content-SHA256']).to be_nil
|
|
112
111
|
end
|
|
113
112
|
end
|
|
114
113
|
|
|
115
|
-
context
|
|
114
|
+
context 'when request type has a body' do
|
|
116
115
|
let(:request) do
|
|
117
116
|
net_http_request = Net::HTTP::Put.new(request_path, request_headers)
|
|
118
117
|
net_http_request.body = "hello\nworld"
|
|
119
118
|
net_http_request
|
|
120
119
|
end
|
|
121
120
|
|
|
122
|
-
it
|
|
123
|
-
driven_request.
|
|
124
|
-
expect(request[
|
|
121
|
+
it 'populates content hash' do
|
|
122
|
+
driven_request.populate_content_hash
|
|
123
|
+
expect(request['X-Authorization-Content-SHA256']).to eq('JsYKYdAdtYNspw/v1EpqAWYgQTyO9fJZpsVhLU9507g=')
|
|
125
124
|
end
|
|
126
125
|
|
|
127
|
-
it
|
|
128
|
-
driven_request.
|
|
129
|
-
expect(driven_request.
|
|
126
|
+
it 'refreshes the cached headers' do
|
|
127
|
+
driven_request.populate_content_hash
|
|
128
|
+
expect(driven_request.content_hash).to eq('JsYKYdAdtYNspw/v1EpqAWYgQTyO9fJZpsVhLU9507g=')
|
|
130
129
|
end
|
|
131
130
|
end
|
|
132
131
|
end
|
|
133
132
|
|
|
134
|
-
describe
|
|
133
|
+
describe '#set_date' do
|
|
135
134
|
before do
|
|
136
135
|
allow(Time).to receive_message_chain(:now, :utc, :httpdate).and_return(timestamp)
|
|
137
136
|
end
|
|
138
137
|
|
|
139
|
-
it
|
|
138
|
+
it 'sets the date header of the request' do
|
|
140
139
|
driven_request.set_date
|
|
141
140
|
expect(request['DATE']).to eq(timestamp)
|
|
142
141
|
end
|
|
143
142
|
|
|
144
|
-
it
|
|
143
|
+
it 'refreshes the cached headers' do
|
|
145
144
|
driven_request.set_date
|
|
146
145
|
expect(driven_request.timestamp).to eq(timestamp)
|
|
147
146
|
end
|
|
148
147
|
end
|
|
149
148
|
|
|
150
|
-
describe
|
|
151
|
-
it
|
|
149
|
+
describe '#set_auth_header' do
|
|
150
|
+
it 'sets the auth header' do
|
|
152
151
|
driven_request.set_auth_header('APIAuth 1044:54321')
|
|
153
152
|
expect(request['Authorization']).to eq('APIAuth 1044:54321')
|
|
154
153
|
end
|
|
155
154
|
end
|
|
156
155
|
end
|
|
157
156
|
|
|
158
|
-
describe
|
|
159
|
-
context
|
|
157
|
+
describe 'content_hash_mismatch?' do
|
|
158
|
+
context 'when request type has no body' do
|
|
160
159
|
let(:request) do
|
|
161
160
|
Net::HTTP::Get.new(request_path, request_headers)
|
|
162
161
|
end
|
|
163
162
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
expect(driven_request.md5_mismatch?).to be false
|
|
163
|
+
it 'is false' do
|
|
164
|
+
expect(driven_request.content_hash_mismatch?).to be false
|
|
167
165
|
end
|
|
168
166
|
end
|
|
169
167
|
|
|
170
|
-
context
|
|
168
|
+
context 'when request type has a body' do
|
|
171
169
|
let(:request) do
|
|
172
170
|
net_http_request = Net::HTTP::Put.new(request_path, request_headers)
|
|
173
171
|
net_http_request.body = "hello\nworld"
|
|
174
172
|
net_http_request
|
|
175
173
|
end
|
|
176
174
|
|
|
177
|
-
context
|
|
175
|
+
context 'when calculated matches sent' do
|
|
178
176
|
before do
|
|
179
|
-
request[
|
|
177
|
+
request['X-Authorization-Content-SHA256'] = 'JsYKYdAdtYNspw/v1EpqAWYgQTyO9fJZpsVhLU9507g='
|
|
180
178
|
end
|
|
181
179
|
|
|
182
|
-
it
|
|
183
|
-
expect(driven_request.
|
|
180
|
+
it 'is false' do
|
|
181
|
+
expect(driven_request.content_hash_mismatch?).to be false
|
|
184
182
|
end
|
|
185
183
|
end
|
|
186
184
|
|
|
187
185
|
context "when calculated doesn't match sent" do
|
|
188
186
|
before do
|
|
189
|
-
request[
|
|
187
|
+
request['X-Authorization-Content-SHA256'] = '3'
|
|
190
188
|
end
|
|
191
189
|
|
|
192
|
-
it
|
|
193
|
-
expect(driven_request.
|
|
190
|
+
it 'is true' do
|
|
191
|
+
expect(driven_request.content_hash_mismatch?).to be true
|
|
194
192
|
end
|
|
195
193
|
end
|
|
196
194
|
end
|
|
197
195
|
end
|
|
196
|
+
|
|
197
|
+
describe 'fetch_headers' do
|
|
198
|
+
it 'returns request headers' do
|
|
199
|
+
expect(driven_request.fetch_headers).to include('content-type' => ['text/plain'])
|
|
200
|
+
end
|
|
201
|
+
end
|
|
198
202
|
end
|