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.
Files changed (68) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +71 -0
  3. data/.gitignore +13 -44
  4. data/.rubocop.yml +39 -0
  5. data/.rubocop_todo.yml +83 -0
  6. data/Appraisals +12 -36
  7. data/CHANGELOG.md +75 -1
  8. data/README.md +155 -52
  9. data/Rakefile +1 -1
  10. data/VERSION +1 -1
  11. data/api_auth.gemspec +35 -23
  12. data/gemfiles/rails_60.gemfile +9 -0
  13. data/gemfiles/rails_61.gemfile +9 -0
  14. data/gemfiles/rails_70.gemfile +9 -0
  15. data/lib/api-auth.rb +1 -1
  16. data/lib/api_auth/base.rb +41 -35
  17. data/lib/api_auth/errors.rb +4 -3
  18. data/lib/api_auth/headers.rb +38 -42
  19. data/lib/api_auth/helpers.rb +7 -16
  20. data/lib/api_auth/railtie.rb +34 -74
  21. data/lib/api_auth/request_drivers/action_controller.rb +27 -27
  22. data/lib/api_auth/request_drivers/action_dispatch.rb +0 -6
  23. data/lib/api_auth/request_drivers/curb.rb +16 -21
  24. data/lib/api_auth/request_drivers/faraday.rb +25 -34
  25. data/lib/api_auth/request_drivers/faraday_env.rb +102 -0
  26. data/lib/api_auth/request_drivers/grape_request.rb +87 -0
  27. data/lib/api_auth/request_drivers/http.rb +96 -0
  28. data/lib/api_auth/request_drivers/httpi.rb +22 -27
  29. data/lib/api_auth/request_drivers/net_http.rb +21 -26
  30. data/lib/api_auth/request_drivers/rack.rb +23 -28
  31. data/lib/api_auth/request_drivers/rest_client.rb +24 -29
  32. data/lib/api_auth.rb +4 -0
  33. data/lib/faraday/api_auth/middleware.rb +35 -0
  34. data/lib/faraday/api_auth.rb +8 -0
  35. data/spec/api_auth_spec.rb +135 -96
  36. data/spec/faraday_middleware_spec.rb +17 -0
  37. data/spec/headers_spec.rb +148 -108
  38. data/spec/helpers_spec.rb +8 -10
  39. data/spec/railtie_spec.rb +80 -99
  40. data/spec/request_drivers/action_controller_spec.rb +122 -79
  41. data/spec/request_drivers/action_dispatch_spec.rb +212 -85
  42. data/spec/request_drivers/curb_spec.rb +36 -33
  43. data/spec/request_drivers/faraday_env_spec.rb +188 -0
  44. data/spec/request_drivers/faraday_spec.rb +87 -83
  45. data/spec/request_drivers/grape_request_spec.rb +280 -0
  46. data/spec/request_drivers/http_spec.rb +190 -0
  47. data/spec/request_drivers/httpi_spec.rb +59 -59
  48. data/spec/request_drivers/net_http_spec.rb +70 -66
  49. data/spec/request_drivers/rack_spec.rb +101 -97
  50. data/spec/request_drivers/rest_client_spec.rb +218 -144
  51. data/spec/spec_helper.rb +15 -12
  52. metadata +144 -83
  53. data/.travis.yml +0 -40
  54. data/Gemfile.lock +0 -115
  55. data/gemfiles/rails_23.gemfile +0 -9
  56. data/gemfiles/rails_23.gemfile.lock +0 -70
  57. data/gemfiles/rails_30.gemfile +0 -9
  58. data/gemfiles/rails_30.gemfile.lock +0 -92
  59. data/gemfiles/rails_31.gemfile +0 -9
  60. data/gemfiles/rails_31.gemfile.lock +0 -98
  61. data/gemfiles/rails_32.gemfile +0 -9
  62. data/gemfiles/rails_32.gemfile.lock +0 -97
  63. data/gemfiles/rails_4.gemfile +0 -9
  64. data/gemfiles/rails_4.gemfile.lock +0 -94
  65. data/gemfiles/rails_41.gemfile +0 -9
  66. data/gemfiles/rails_41.gemfile.lock +0 -98
  67. data/gemfiles/rails_42.gemfile +0 -9
  68. 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(:timestamp){ Time.now.utc.httpdate }
6
+ let(:request_path) { '/resource.xml?foo=bar&bar=foo' }
6
7
 
7
- let(:request_path){ "/resource.xml?foo=bar&bar=foo" }
8
-
9
- let(:request_headers){
8
+ let(:request_headers) do
10
9
  {
11
- 'Authorization' => 'APIAuth 1044:12345',
12
- 'content-md5' => '1B2M2Y8AsgTpgAmY7PhCfg==',
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 "getting headers correctly" do
27
- describe "#content_type" do
28
- it "gets the content_type" do
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 "gets multipart content_type" do
33
- request = Net::HTTP::Put::Multipart.new("/resource.xml?foo=bar&bar=foo",
34
- 'file' => UploadIO.new(File.new('spec/fixtures/upload.png'), 'image/png', 'upload.png'))
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 "gets the content_md5" do
41
- expect(driven_request.content_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==')
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 "gets the request_uri" do
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 "gets the timestamp" do
47
+ it 'gets the timestamp' do
49
48
  expect(driven_request.timestamp).to eq(timestamp)
50
49
  end
51
50
 
52
- it "gets the authorization_header" do
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 "#calculated_md5" do
57
- it "calculates md5 from the body" do
58
- expect(driven_request.calculated_md5).to eq('kZXQvrKoieG+Be1rsZVINw==')
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 "treats no body as empty string" do
60
+ it 'treats no body as empty string' do
62
61
  request.body = nil
63
- expect(driven_request.calculated_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==')
62
+ expect(driven_request.calculated_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
64
63
  end
65
64
 
66
- it "calculates correctly for multipart content" do
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.calculated_md5).to eq('k4U8MTA3RHDcewBzymVNEQ==')
68
+ expect(driven_request.calculated_hash).to eq('AlKDe7kjMQhuKgKuNG8I7GA93MasHcaVJkJLaUT7+dY=')
70
69
  end
71
70
  end
72
71
 
73
- describe "http_method" do
74
- context "when put request" do
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 "returns upcased put" do
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 "when get request" do
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 "returns upcased get" do
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 "setting headers correctly" do
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 "#populate_content_md5" do
104
- context "when request type has no body" do
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-md5" do
110
- driven_request.populate_content_md5
111
- expect(request["Content-MD5"]).to be_nil
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 "when request type has a body" do
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 "populates content-md5" do
123
- driven_request.populate_content_md5
124
- expect(request["Content-MD5"]).to eq('kZXQvrKoieG+Be1rsZVINw==')
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 "refreshes the cached headers" do
128
- driven_request.populate_content_md5
129
- expect(driven_request.content_md5).to eq('kZXQvrKoieG+Be1rsZVINw==')
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 "#set_date" do
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 "sets the date header of the request" do
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 "refreshes the cached headers" do
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 "#set_auth_header" do
151
- it "sets the auth header" do
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 "md5_mismatch?" do
159
- context "when request type has no body" do
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
- it "is false" do
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 "when request type has a body" do
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 "when calculated matches sent" do
175
+ context 'when calculated matches sent' do
178
176
  before do
179
- request["Content-MD5"] = 'kZXQvrKoieG+Be1rsZVINw=='
177
+ request['X-Authorization-Content-SHA256'] = 'JsYKYdAdtYNspw/v1EpqAWYgQTyO9fJZpsVhLU9507g='
180
178
  end
181
179
 
182
- it "is false" do
183
- expect(driven_request.md5_mismatch?).to be false
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["Content-MD5"] = "3"
187
+ request['X-Authorization-Content-SHA256'] = '3'
190
188
  end
191
189
 
192
- it "is true" do
193
- expect(driven_request.md5_mismatch?).to be true
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