lhc 15.0.0 → 15.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a34aeee4fa910b8099acfdd73813103beb630400b92b449dbfbf4d87de4e041
4
- data.tar.gz: 846b8cc9e24b9b0b9189b24d38c3e7b485096b4afd970cfc6fe48c8ee3a76d7e
3
+ metadata.gz: 40a7523182b4a8d9a3a93cbc667d35e8df394f4751a1c6e9c0a6ccf71771f136
4
+ data.tar.gz: d1c16ba4e9cf1ba5cbdaf0833b1c04daf48e3d039cdf4e445c249a6792c9a67f
5
5
  SHA512:
6
- metadata.gz: 067d2e56385e931f00caa829411888881705ec19a702979332ad5074bf1a6515f0893e09111314b825fa7dd6d559518c997ac309f17e06eee245faffe93fcebd
7
- data.tar.gz: 5284f04c83ca525e774775c41bd88d9f2dc8feeaf8f1fb47d3b9bde5677eeaad9bec6be6213597c4d1f34a2a22f8703b9f8e7fef0a4a09a58f24a6f72b2bf66b
6
+ metadata.gz: 3ab7127d698a59dfeeefaa06d8f80a8d787a7fde26017f4a026473826681a0d85373a560cdc82b8a7ef551be990149a26025a5d77af77b5b54b350f4ef528d78
7
+ data.tar.gz: 12d1e327c10768fca97287b0efae094d966a3b291b5cf46933181a255a3245312b7fe68f63a232cd6aff50b10085e6dd1f9dfea2c6382fb1559075a9bb89fc23
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
@@ -53,7 +53,7 @@ class LHC::Auth < LHC::Interceptor
53
53
  request.options[:auth].merge!(bearer_token: token)
54
54
  set_authorization_header("Bearer #{token}")
55
55
  end
56
- # rubocop:enable Style/AccessorMethodName
56
+ # rubocop:enable Naming/AccessorMethodName
57
57
 
58
58
  def reauthenticate!
59
59
  # refresh token and update header
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
@@ -25,14 +25,26 @@ class LHC::HeadersScrubber < LHC::Scrubber
25
25
  end
26
26
 
27
27
  def scrub_basic_authentication_headers!
28
- return if auth_options[:basic].blank? || scrubbed['Authorization'].blank?
28
+ return if !scrub_basic_authentication_headers?
29
29
 
30
30
  scrubbed['Authorization'].gsub!(auth_options[:basic][:base_64_encoded_credentials], SCRUB_DISPLAY)
31
31
  end
32
32
 
33
33
  def scrub_bearer_authentication_headers!
34
- return if auth_options[:bearer].blank? || scrubbed['Authorization'].blank?
34
+ return if !scrub_bearer_authentication_headers?
35
35
 
36
36
  scrubbed['Authorization'].gsub!(auth_options[:bearer_token], SCRUB_DISPLAY)
37
37
  end
38
+
39
+ def scrub_basic_authentication_headers?
40
+ auth_options[:basic].present? &&
41
+ scrubbed['Authorization'].present? &&
42
+ scrubbed['Authorization'].include?(auth_options[:basic][:base_64_encoded_credentials])
43
+ end
44
+
45
+ def scrub_bearer_authentication_headers?
46
+ auth_options[:bearer].present? &&
47
+ scrubbed['Authorization'].present? &&
48
+ scrubbed['Authorization'].include?(auth_options[:bearer_token])
49
+ end
38
50
  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.0'
4
+ VERSION ||= '15.1.2'
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
 
@@ -59,19 +59,20 @@ describe LHC::Request do
59
59
  let(:authorization_header) { { 'Authorization' => "Bearer #{bearer_token}" } }
60
60
  let(:auth) { { bearer: -> { bearer_token } } }
61
61
 
62
- it 'provides srubbed request headers' do
62
+ it 'scrubs only the bearer token' do
63
63
  expect(request.scrubbed_headers).to include('Authorization' => "Bearer #{LHC::Scrubber::SCRUB_DISPLAY}")
64
64
  expect(request.headers).to include(authorization_header)
65
65
  end
66
66
 
67
- context 'when nothing should get scrubbed' do
68
- before :each do
69
- LHC.config.scrubs = {}
70
- end
67
+ it 'scrubs whole "Authorization" header' do
68
+ LHC.config.scrubs[:headers] << 'Authorization'
69
+ expect(request.scrubbed_headers).to include('Authorization' => LHC::Scrubber::SCRUB_DISPLAY)
70
+ expect(request.headers).to include(authorization_header)
71
+ end
71
72
 
72
- it 'does not filter beaerer auth' do
73
- expect(request.scrubbed_headers).to include(authorization_header)
74
- end
73
+ it 'scrubs nothing' do
74
+ LHC.config.scrubs = {}
75
+ expect(request.scrubbed_headers).to include(authorization_header)
75
76
  end
76
77
  end
77
78
 
@@ -82,19 +83,20 @@ describe LHC::Request do
82
83
  let(:authorization_header) { { 'Authorization' => "Basic #{credentials_base_64_codiert}" } }
83
84
  let(:auth) { { basic: { username: username, password: password } } }
84
85
 
85
- it 'provides srubbed request headers' do
86
+ it 'scrubs only credentials' do
86
87
  expect(request.scrubbed_headers).to include('Authorization' => "Basic #{LHC::Scrubber::SCRUB_DISPLAY}")
87
88
  expect(request.headers).to include(authorization_header)
88
89
  end
89
90
 
90
- context 'when nothing should get scrubbed' do
91
- before :each do
92
- LHC.config.scrubs = {}
93
- end
91
+ it 'scrubs whole "Authorization" header' do
92
+ LHC.config.scrubs[:headers] << 'Authorization'
93
+ expect(request.scrubbed_headers).to include('Authorization' => LHC::Scrubber::SCRUB_DISPLAY)
94
+ expect(request.headers).to include(authorization_header)
95
+ end
94
96
 
95
- it 'does not filter basic auth' do
96
- expect(request.scrubbed_headers).to include(authorization_header)
97
- end
97
+ it 'scrubs nothing' do
98
+ LHC.config.scrubs = {}
99
+ expect(request.scrubbed_headers).to include(authorization_header)
98
100
  end
99
101
  end
100
102
  end
@@ -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.0
4
+ version: 15.1.2
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