lhc 15.0.1 → 15.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9747364f9001f462e35cdc26b209f078daac57834bb4c1a801866f29abc2ba7
4
- data.tar.gz: e03286ea873459b7093baef6c47807edb4a1543889e887bf0d708c0144b6a087
3
+ metadata.gz: 329757321e8e5c7d51dc13addb0309cf887c25e052733c134c512e3fdc8025f9
4
+ data.tar.gz: bf32d7ceaf7b395066db2d04b8d225c2bfe863d9173f12853a4742256bb9d0db
5
5
  SHA512:
6
- metadata.gz: bc2238780fe87969d1dea621d44960febb3a19e7b650a5e6ce7939f942e47acd01a825e64c39ab4fb4dbeb5636aef1e0819b3f874b95c22461cee4d63ad44368
7
- data.tar.gz: cc5a2707f9772cc79a3caa3aa96b3c0ffe1a0984fa06446e48a61a9c08240145d772b35b993f11ec24c278367c9db4d2bf35dafee1aa444818323e2aab3c99b6
6
+ metadata.gz: 30e0b077eb4417c4825bdb493c4b3d6573bf7ccc5eccda46fb4296ac2b077b90103469cecc8d9fc0daea77f04993f63323934acb9183a0937563ce9813a92780
7
+ data.tar.gz: 98246e237ca676d313d82670257566a182e7099af102721b36b4fb339823c7bdbb54d579d652d71d188b9d9b0d3e2e1b9c741a67eed4db689399e385a59eb836
data/.rubocop.yml CHANGED
@@ -26,6 +26,9 @@ require:
26
26
  Bundler/OrderedGems:
27
27
  Enabled: false
28
28
 
29
+ Gemspec/RequireMFA:
30
+ Enabled: false
31
+
29
32
  Lint/SuppressedException:
30
33
  Exclude:
31
34
  - spec/**/*
@@ -260,7 +263,7 @@ Style/CaseLikeIf:
260
263
  Enabled: false
261
264
 
262
265
  Style/HashEachMethods:
263
- Enabled: false
266
+ Enabled: false
264
267
 
265
268
  Style/HashConversion: # (new in 1.10)
266
269
  Enabled: false
@@ -268,6 +271,9 @@ Style/HashConversion: # (new in 1.10)
268
271
  Style/NilLambda: # (new in 1.3)
269
272
  Enabled: false
270
273
 
274
+ Style/OpenStructUse:
275
+ Enabled: false
276
+
271
277
  RSpec/DescribeClass:
272
278
  Exclude:
273
279
  - spec/views/**/*
data/README.md CHANGED
@@ -618,12 +618,18 @@ Adds the following to body of all requests:
618
618
 
619
619
  ##### Reauthenticate
620
620
 
621
- The current implementation can only offer reauthenticate for _client access tokens_. For this to work the following has to be given:
622
-
623
- * You have configured and implemented `LHC::Auth.refresh_client_token = -> { TokenRefreshUtil.client_access_token(true) }` which when called will force a refresh of the token and return the new value. It is also expected that this implementation will handle invalidating caches if necessary.
624
- * Your interceptors contain `LHC::Auth` and `LHC::Retry`, whereas `LHC::Retry` comes _after_ `LHC::Auth` in the chain.
621
+ The current implementation offers only reauthentication for _client access tokens_.
622
+ Make sure that your interceptors contain `LHC::Auth` and `LHC::Retry`, whereas `LHC::Retry` comes _after_ `LHC::Auth` in the chain.
623
+ Provide the refresh token as following:
624
+ ```ruby
625
+ LHC.get('http://local.ch', auth: { bearer: -> { access_token }, refresh_client_token: -> { TokenRefreshUtil.client_access_token(true) })
626
+ ```
627
+ Where `TokenRefreshUtil.client_access_token(true)` forces a refresh of the token and returns the new token. It is also expected that this implementation will handle invalidating caches if necessary.
625
628
 
626
- ##### Bearer Authentication with client access token
629
+ You can also set a global `refresh_client_token`. This is not recommended for apps with multiple endpoint and different access tokens.
630
+ ```ruby
631
+ LHC::Auth.refresh_client_token = -> { TokenRefreshUtil.client_access_token(true) }
632
+ ```
627
633
 
628
634
  Reauthentication will be initiated if:
629
635
 
data/lhc.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_dependency 'activesupport', '>= 5.2'
26
26
  s.add_dependency 'addressable'
27
+ s.add_dependency 'local_uri'
27
28
  s.add_dependency 'typhoeus', '>= 0.11'
28
29
 
29
30
  s.add_development_dependency 'geminabox'
@@ -35,6 +36,7 @@ Gem::Specification.new do |s|
35
36
  s.add_development_dependency 'rubocop', '~> 1.0'
36
37
  s.add_development_dependency 'rubocop-performance', '~> 1.0'
37
38
  s.add_development_dependency 'rubocop-rspec', '~> 1.26.0'
39
+ s.add_development_dependency 'sprockets-rails'
38
40
  s.add_development_dependency 'timecop'
39
41
  s.add_development_dependency 'webmock'
40
42
 
data/lib/lhc/error.rb CHANGED
@@ -75,7 +75,7 @@ class LHC::Error < StandardError
75
75
  debug << "Options: #{request.scrubbed_options}"
76
76
  debug << "Headers: #{request.scrubbed_headers}"
77
77
  debug << "Response Code: #{response.code} (#{response.options[:return_code]})"
78
- debug << "Response Options: #{response.options}"
78
+ debug << "Response Options: #{response.scrubbed_options}"
79
79
  debug << response.body
80
80
  debug << _message
81
81
  debug.map { |str| self.class.fix_invalid_encoding(str) }.join("\n")
@@ -39,7 +39,7 @@ class LHC::Auth < LHC::Interceptor
39
39
  set_bearer_authorization_header(token)
40
40
  end
41
41
 
42
- # rubocop:disable Style/AccessorMethodName
42
+ # rubocop:disable Naming/AccessorMethodName
43
43
  def set_authorization_header(value)
44
44
  request.headers['Authorization'] = value
45
45
  end
@@ -50,10 +50,13 @@ class LHC::Auth < LHC::Interceptor
50
50
  end
51
51
 
52
52
  def set_bearer_authorization_header(token)
53
- request.options[:auth].merge!(bearer_token: token)
53
+ auth_options = request.options[:auth] || {}
54
+ auth_options.merge!(bearer_token: token)
55
+ request.options[:auth] = auth_options unless request.options.key?(:auth)
56
+
54
57
  set_authorization_header("Bearer #{token}")
55
58
  end
56
- # rubocop:enable Style/AccessorMethodName
59
+ # rubocop:enable Naming/AccessorMethodName
57
60
 
58
61
  def reauthenticate!
59
62
  # refresh token and update header
@@ -7,7 +7,7 @@ class LHC::Rollbar < LHC::Interceptor
7
7
  include LHC::FixInvalidEncodingConcern
8
8
 
9
9
  def after_response
10
- return unless Object.const_defined?('Rollbar')
10
+ return unless Object.const_defined?(:Rollbar)
11
11
  return if response.success?
12
12
 
13
13
  request = response.request
data/lib/lhc/request.rb CHANGED
@@ -72,6 +72,7 @@ class LHC::Request
72
72
 
73
73
  def scrubbed_options
74
74
  scrubbed_options = options.deep_dup
75
+ scrubbed_options[:cache] = LHC::CacheScrubber.new(scrubbed_options[:cache]).scrubbed
75
76
  scrubbed_options[:params] = LHC::ParamsScrubber.new(scrubbed_options[:params]).scrubbed
76
77
  scrubbed_options[:headers] = LHC::HeadersScrubber.new(scrubbed_options[:headers], scrubbed_options[:auth]).scrubbed
77
78
  scrubbed_options[:auth] = LHC::AuthScrubber.new(scrubbed_options[:auth]).scrubbed
data/lib/lhc/response.rb CHANGED
@@ -54,6 +54,12 @@ class LHC::Response
54
54
  request.format
55
55
  end
56
56
 
57
+ def scrubbed_options
58
+ scrubbed_options = options.deep_dup
59
+ scrubbed_options[:effective_url] = LHC::EffectiveUrlScrubber.new(scrubbed_options[:effective_url]).scrubbed
60
+ scrubbed_options
61
+ end
62
+
57
63
  private
58
64
 
59
65
  attr_accessor :raw
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LHC::CacheScrubber < LHC::Scrubber
4
+ def initialize(data)
5
+ super(data)
6
+ scrub_cache_options!
7
+ end
8
+
9
+ private
10
+
11
+ def scrub_cache_options!
12
+ return if scrubbed.blank?
13
+ return if scrub_elements.blank?
14
+
15
+ scrub_cache_key!
16
+ end
17
+
18
+ def scrub_cache_key!
19
+ return if scrubbed[:key].blank?
20
+
21
+ scrub_elements.each do |scrub_element|
22
+ matches = scrubbed[:key].match(/:#{scrub_element}=>"(.*?)"/)
23
+ next if matches.nil?
24
+
25
+ value = matches[-1]
26
+ scrubbed[:key].gsub!(value, SCRUB_DISPLAY)
27
+ end
28
+ end
29
+
30
+ def scrub_elements
31
+ # The cache key includes the whole request url inklusive params.
32
+ # We need to scrub those params from the cache key.
33
+ LHC.config.scrubs[:params]
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LHC::EffectiveUrlScrubber < LHC::Scrubber
4
+ def initialize(data)
5
+ super(data)
6
+ scrub_effective_url_options!
7
+ end
8
+
9
+ private
10
+
11
+ def scrub_effective_url_options!
12
+ return if scrubbed.blank?
13
+ return if scrub_elements.blank?
14
+
15
+ scrub_effective_url!
16
+ end
17
+
18
+ def scrub_effective_url!
19
+ return if scrubbed.blank?
20
+
21
+ scrub_elements.each do |scrub_element|
22
+ uri = LocalUri::URI.new(scrubbed)
23
+ self.scrubbed = CGI.unescape(uri.query.merge(scrub_element => SCRUB_DISPLAY).to_s)
24
+ end
25
+ end
26
+
27
+ def scrub_elements
28
+ # The effective url includes the params of the request
29
+ # so we need to scrub those params from the effective url.
30
+ LHC.config.scrubs[:params]
31
+ end
32
+ end
data/lib/lhc/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC
4
- VERSION ||= '15.0.1'
4
+ VERSION ||= '15.1.3'
5
5
  end
data/lib/lhc.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'local_uri'
3
4
  require 'typhoeus'
4
5
  require 'active_support/core_ext/object/blank'
5
6
  require 'active_support/core_ext/hash/keys'
@@ -119,6 +120,10 @@ module LHC
119
120
  'lhc/scrubbers/auth_scrubber'
120
121
  autoload :BodyScrubber,
121
122
  'lhc/scrubbers/body_scrubber'
123
+ autoload :CacheScrubber,
124
+ 'lhc/scrubbers/cache_scrubber'
125
+ autoload :EffectiveUrlScrubber,
126
+ 'lhc/scrubbers/effective_url_scrubber'
122
127
  autoload :HeadersScrubber,
123
128
  'lhc/scrubbers/headers_scrubber'
124
129
  autoload :ParamsScrubber,
@@ -55,10 +55,12 @@ describe LHC::Error do
55
55
  end
56
56
 
57
57
  let(:response) do
58
+ options = { return_code: :internal_error, response_headers: "" }
58
59
  double('LHC::Response',
59
60
  request: request,
60
61
  code: 500,
61
- options: { return_code: :internal_error, response_headers: "" },
62
+ options: options,
63
+ scrubbed_options: options,
62
64
  body: '{"status":500,"message":"undefined"}')
63
65
  end
64
66
 
@@ -27,6 +27,27 @@ describe LHC::Auth do
27
27
  expect(auth_suceeding_after_recovery).to have_been_made.once
28
28
  end
29
29
 
30
+ context 'without `auth` options' do
31
+ let(:headers) do
32
+ { 'Authorization' => "Bearer #{initial_token}" }
33
+ end
34
+
35
+ before do
36
+ LHC::Auth.refresh_client_token = -> { refresh_token }
37
+ end
38
+
39
+ after do
40
+ LHC::Auth.refresh_client_token = -> { nil }
41
+ end
42
+
43
+ it 'recovery is attempted' do
44
+ LHC.config.endpoint(:local, 'http://local.ch')
45
+ # the retried request (with updated Bearer), that should work
46
+ LHC.get(:local, { headers: headers })
47
+ expect(auth_suceeding_after_recovery).to have_been_made.once
48
+ end
49
+ end
50
+
30
51
  it "recovery is not attempted again when the request has reauthenticated: true " do
31
52
  LHC.config.endpoint(:local, 'http://local.ch', auth: options.merge(reauthenticated: true))
32
53
  expect { LHC.get(:local) }.to raise_error(LHC::Unauthorized)
@@ -9,7 +9,7 @@ describe LHC::Rollbar do
9
9
 
10
10
  context 'Rollbar is undefined' do
11
11
  before(:each) do
12
- Object.send(:remove_const, 'Rollbar') if Object.const_defined?('Rollbar')
12
+ Object.send(:remove_const, 'Rollbar') if Object.const_defined?(:Rollbar)
13
13
  end
14
14
  it 'does not report' do
15
15
  stub_request(:get, 'http://local.ch').to_return(status: 400)
@@ -18,9 +18,12 @@ describe LHC::Request do
18
18
  let(:params) { { api_key: 'api-key-params' } }
19
19
  let(:headers) { { private_key: 'private-key-header' } }
20
20
  let(:body) { { user_token: 'user-token-body' } }
21
+ let(:cache) do
22
+ { key: "LHS_REQUEST_CYCLE_CACHE(v1) POST http://local.ch?#{params}" }
23
+ end
21
24
 
22
25
  let(:request) do
23
- response = LHC.post(:local, params: params, headers: headers, body: body)
26
+ response = LHC.post(:local, params: params, headers: headers, body: body, cache: cache)
24
27
  response.request
25
28
  end
26
29
 
@@ -30,6 +33,8 @@ describe LHC::Request do
30
33
  expect(request.scrubbed_options[:body]).to include(user_token: LHC::Scrubber::SCRUB_DISPLAY)
31
34
  expect(request.scrubbed_options[:auth][:bearer_token]).to eq(LHC::Scrubber::SCRUB_DISPLAY)
32
35
  expect(request.scrubbed_options[:auth][:basic]).to be nil
36
+ expect(request.scrubbed_options[:cache])
37
+ .to include(key: "LHS_REQUEST_CYCLE_CACHE(v1) POST http://local.ch?{:api_key=>\"[FILTERED]\"}")
33
38
  end
34
39
 
35
40
  context 'when bearer auth is not a proc' do
@@ -53,6 +58,19 @@ describe LHC::Request do
53
58
  end
54
59
  end
55
60
 
61
+ context 'when parameter should not get scrubbed' do
62
+ let(:params) { { any_parameter: 'any-parameter' } }
63
+
64
+ let(:cache) do
65
+ { key: "LHS_REQUEST_CYCLE_CACHE(v1) POST http://local.ch?#{params}" }
66
+ end
67
+
68
+ it 'does not scrubb the parameter' do
69
+ expect(request.scrubbed_options[:cache])
70
+ .to include(key: "LHS_REQUEST_CYCLE_CACHE(v1) POST http://local.ch?#{params}")
71
+ end
72
+ end
73
+
56
74
  context 'when body data is nested' do
57
75
  let(:body) do
58
76
  {
@@ -104,6 +122,8 @@ describe LHC::Request do
104
122
  expect(request.scrubbed_options[:headers]).not_to include(private_key: LHC::Scrubber::SCRUB_DISPLAY)
105
123
  expect(request.scrubbed_options[:body]).not_to include(user_token: LHC::Scrubber::SCRUB_DISPLAY)
106
124
  expect(request.scrubbed_options[:auth][:bearer_token]).not_to eq(LHC::Scrubber::SCRUB_DISPLAY)
125
+ expect(request.scrubbed_options[:cache])
126
+ .not_to include(key: "LHS_REQUEST_CYCLE_CACHE(v1) POST http://local.ch?{:api_key=>\"[FILTERED]\"}")
107
127
  end
108
128
  end
109
129
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Response do
6
+ let(:options) do
7
+ { effective_url: 'http://local.ch?api_key=api-key' }
8
+ end
9
+
10
+ let(:raw_response) { OpenStruct.new(options: options) }
11
+
12
+ before do
13
+ LHC.config.scrubs[:params] << 'api_key'
14
+ end
15
+
16
+ it 'scrubbes effective url' do
17
+ response = LHC::Response.new(raw_response, nil)
18
+ expect(response.scrubbed_options[:effective_url]).to eq "http://local.ch?api_key=#{LHC::Scrubber::SCRUB_DISPLAY}"
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 15.0.1
4
+ version: 15.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-12 00:00:00.000000000 Z
11
+ date: 2022-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: local_uri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: typhoeus
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +192,20 @@ dependencies:
178
192
  - - "~>"
179
193
  - !ruby/object:Gem::Version
180
194
  version: 1.26.0
195
+ - !ruby/object:Gem::Dependency
196
+ name: sprockets-rails
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
181
209
  - !ruby/object:Gem::Dependency
182
210
  name: timecop
183
211
  requirement: !ruby/object:Gem::Requirement
@@ -273,6 +301,8 @@ files:
273
301
  - lib/lhc/scrubber.rb
274
302
  - lib/lhc/scrubbers/auth_scrubber.rb
275
303
  - lib/lhc/scrubbers/body_scrubber.rb
304
+ - lib/lhc/scrubbers/cache_scrubber.rb
305
+ - lib/lhc/scrubbers/effective_url_scrubber.rb
276
306
  - lib/lhc/scrubbers/headers_scrubber.rb
277
307
  - lib/lhc/scrubbers/params_scrubber.rb
278
308
  - lib/lhc/test/cache_helper.rb
@@ -401,6 +431,7 @@ files:
401
431
  - spec/response/effective_url_spec.rb
402
432
  - spec/response/headers_spec.rb
403
433
  - spec/response/options_spec.rb
434
+ - spec/response/scrubbed_options_spec.rb
404
435
  - spec/response/success_spec.rb
405
436
  - spec/response/time_spec.rb
406
437
  - spec/spec_helper.rb
@@ -560,6 +591,7 @@ test_files:
560
591
  - spec/response/effective_url_spec.rb
561
592
  - spec/response/headers_spec.rb
562
593
  - spec/response/options_spec.rb
594
+ - spec/response/scrubbed_options_spec.rb
563
595
  - spec/response/success_spec.rb
564
596
  - spec/response/time_spec.rb
565
597
  - spec/spec_helper.rb