lhc 12.0.2 → 12.1.3

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: 6eb0a56bdedb5ee97b2ab47ea911fe69419679add3702442659dd29722676495
4
- data.tar.gz: e8d95c1c772a91592ada2f0062e1b9fe463c5b1bffe600e745e799ed8e1db068
3
+ metadata.gz: c1d43867c7519eb1697f118fb44668142aa46bcdfb13d725be6617c74f4ed47f
4
+ data.tar.gz: cdfa710bc5c7ae5297eeceec6858738ad414e828237038b92a3dcbaa9c088fed
5
5
  SHA512:
6
- metadata.gz: 612db75bd05d1b172cb8282ecc2129e752b1d35b7c5514827fc984ea5da449bc52796d4e2f455fa14bbf752bf72f65c50673668259aba85547e84d5843d12df9
7
- data.tar.gz: 4eceeabc7780e0fa1b2a9039515a9e8f6fdbae86b7b50e09e82afec1fefa7a4c2309175ba52ef195ce182dc6caf63d8edf0721fe5b23d5f96b65516291956493
6
+ metadata.gz: 9e11b8821556d2bb0e40b91cb080379fc82a3a90453fe36dd6127b4d26a571d065637275988ba03b46d96f6217a697022d0a6aa46532ab20a5d46e1ef5dbadfd
7
+ data.tar.gz: 192cabec15b2d1881a14505343f2e2f4218fad00ffaac4935dd3d1edbdc6132edb6b9ae0834f2d556dfbcacca9221f473f39dba37071ec40949e407bc953813f
data/README.md CHANGED
@@ -58,6 +58,7 @@ use it like:
58
58
  * [Authentication Interceptor](#authentication-interceptor)
59
59
  * [Bearer Authentication](#bearer-authentication)
60
60
  * [Basic Authentication](#basic-authentication)
61
+ * [Body Authentication](#body-authentication)
61
62
  * [Reauthenticate](#reauthenticate)
62
63
  * [Bearer Authentication with client access token](#bearer-authentication-with-client-access-token)
63
64
  * [Caching Interceptor](#caching-interceptor)
@@ -92,6 +93,7 @@ use it like:
92
93
 
93
94
 
94
95
 
96
+
95
97
  ## Basic methods
96
98
 
97
99
  Available are `get`, `post`, `put` & `delete`.
@@ -549,6 +551,20 @@ Adds the following header to the request:
549
551
 
550
552
  Which is the base64 encoded credentials "username:password".
551
553
 
554
+ ##### Body Authentication
555
+
556
+ ```ruby
557
+ LHC.post('http://local.ch', auth: { body: { userToken: 'dheur5hrk3' } })
558
+ ```
559
+
560
+ Adds the following to body of all requests:
561
+
562
+ ```
563
+ {
564
+ "userToken": "dheur5hrk3"
565
+ }
566
+ ```
567
+
552
568
  ##### Reauthenticate
553
569
 
554
570
  The current implementation can only offer reauthenticate for _client access tokens_. For this to work the following has to be given:
@@ -4,8 +4,13 @@ class LHC::Auth < LHC::Interceptor
4
4
  include ActiveSupport::Configurable
5
5
  config_accessor :refresh_client_token
6
6
 
7
+ def before_raw_request
8
+ body_authentication! if auth_options[:body]
9
+ end
10
+
7
11
  def before_request
8
- authenticate!
12
+ bearer_authentication! if auth_options[:bearer]
13
+ basic_authentication! if auth_options[:basic]
9
14
  end
10
15
 
11
16
  def after_response
@@ -16,18 +21,15 @@ class LHC::Auth < LHC::Interceptor
16
21
 
17
22
  private
18
23
 
19
- def authenticate!
20
- if auth_options[:bearer]
21
- bearer_authentication!
22
- elsif auth_options[:basic]
23
- basic_authentication!
24
- end
24
+ def body_authentication!
25
+ auth = auth_options[:body]
26
+ request.options[:body] = (request.options[:body] || {}).merge(auth)
25
27
  end
26
28
 
27
29
  def basic_authentication!
28
30
  auth = auth_options[:basic]
29
31
  credentials = "#{auth[:username]}:#{auth[:password]}"
30
- set_authorization_header("Basic #{Base64.encode64(credentials).chomp}")
32
+ set_authorization_header("Basic #{Base64.strict_encode64(credentials).chomp}")
31
33
  end
32
34
 
33
35
  def bearer_authentication!
@@ -78,7 +80,7 @@ class LHC::Auth < LHC::Interceptor
78
80
  end
79
81
 
80
82
  def auth_options
81
- @auth_options ||= request.options[:auth].dup || {}
83
+ request.options[:auth] || {}
82
84
  end
83
85
 
84
86
  def configuration_correct?
@@ -12,9 +12,9 @@ class LHC::Response::Data
12
12
  @data = data
13
13
 
14
14
  if as_json.is_a?(Hash)
15
- @base = LHC::Response::Data::Item.new(response, data: data)
15
+ @base = LHC::Response::Data::Item.new(@response, data: data)
16
16
  elsif as_json.is_a?(Array)
17
- @base = LHC::Response::Data::Collection.new(response, data: data)
17
+ @base = LHC::Response::Data::Collection.new(@response, data: data)
18
18
  end
19
19
  end
20
20
 
@@ -4,7 +4,7 @@
4
4
  # but made accssible in the ruby world
5
5
  module LHC::Response::Data::Base
6
6
  def as_json
7
- @json ||= (@data || response.format.as_json(response.body))
7
+ @json ||= (@data || @response.format.as_json(@response.body))
8
8
  end
9
9
 
10
10
  def as_open_struct
@@ -12,11 +12,7 @@ module LHC::Response::Data::Base
12
12
  if @data
13
13
  JSON.parse(@data.to_json, object_class: OpenStruct)
14
14
  else
15
- response.format.as_open_struct(response.body)
15
+ @response.format.as_open_struct(@response.body)
16
16
  end
17
17
  end
18
-
19
- private
20
-
21
- attr_reader :response
22
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHC
4
- VERSION ||= '12.0.2'
4
+ VERSION ||= '12.1.3'
5
5
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Auth do
6
+ before(:each) do
7
+ LHC.config.interceptors = [LHC::Auth]
8
+ end
9
+
10
+ it 'adds body authentication to the existing request body' do
11
+ stub_request(:post, "http://local.ch/")
12
+ .with(body: {
13
+ message: 'body',
14
+ userToken: 'dheur5hrk3'
15
+ }.to_json)
16
+
17
+ LHC.post('http://local.ch', auth: { body: { userToken: 'dheur5hrk3' } }, body: {
18
+ message: 'body'
19
+ })
20
+ end
21
+
22
+ it 'adds body authentication to an empty request body' do
23
+ stub_request(:post, "http://local.ch/")
24
+ .with(body: {
25
+ userToken: 'dheur5hrk3'
26
+ }.to_json)
27
+
28
+ LHC.post('http://local.ch', auth: { body: { userToken: 'dheur5hrk3' } })
29
+ end
30
+
31
+ it 'adds nothing if request method is GET' do
32
+ stub_request(:get, "http://local.ch/")
33
+
34
+ LHC.get('http://local.ch', auth: { body: { userToken: 'dheur5hrk3' } })
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Auth do
6
+ before(:each) do
7
+ LHC.config.interceptors = [LHC::Auth]
8
+ end
9
+
10
+ it 'adds basic auth in a correct way even if username and password are especially long' do
11
+ options = { basic: { username: '123456789101234', password: '12345678901234567890123456789012' } }
12
+ LHC.config.endpoint(:local, 'http://local.ch', auth: options)
13
+ stub_request(:get, 'http://local.ch')
14
+ .with(headers: { 'Authorization' => 'Basic MTIzNDU2Nzg5MTAxMjM0OjEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEy' })
15
+ LHC.get(:local)
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHC::Auth do
6
+ before(:each) do
7
+ class AuthPrepInterceptor < LHC::Interceptor
8
+
9
+ def before_request
10
+ request.options[:auth] = { bearer: 'sometoken' }
11
+ end
12
+ end
13
+
14
+ LHC.config.interceptors = [AuthPrepInterceptor, LHC::Auth]
15
+ end
16
+
17
+ after do
18
+ LHC.config.reset
19
+ end
20
+
21
+ it 'does not use instance variables internally so that other interceptors can still change auth options' do
22
+ stub_request(:get, "http://local.ch/")
23
+ .with(headers: { 'Authorization' => 'Bearer sometoken' })
24
+ .to_return(status: 200)
25
+ LHC.get('http://local.ch')
26
+ end
27
+ end
@@ -58,4 +58,28 @@ describe LHC::Response do
58
58
  end
59
59
  end
60
60
  end
61
+
62
+ context 'response data if responding error data contains a response' do
63
+ before do
64
+ stub_request(:get, "http://listings/")
65
+ .to_return(status: 404, body: {
66
+ meta: {
67
+ errors: [
68
+ { code: 2000, msg: 'I like to hide error messages (this is meta).' }
69
+ ]
70
+ },
71
+ response: 'why not?'
72
+ }.to_json)
73
+ end
74
+
75
+ it 'does not throw a stack level to deep issue when accessing data in a rescue context' do
76
+ begin
77
+ LHC.get('http://listings')
78
+ rescue LHC::Error => error
79
+ expect(
80
+ error.response.request.response.data.meta.errors.detect { |item| item.code == 2000 }.msg
81
+ ).to eq 'I like to hide error messages (this is meta).'
82
+ end
83
+ end
84
+ end
61
85
  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: 12.0.2
4
+ version: 12.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: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -325,6 +325,9 @@ files:
325
325
  - spec/interceptors/after_response_spec.rb
326
326
  - spec/interceptors/auth/basic_auth_spec.rb
327
327
  - spec/interceptors/auth/bearer_spec.rb
328
+ - spec/interceptors/auth/body_spec.rb
329
+ - spec/interceptors/auth/long_basic_auth_credentials_spec.rb
330
+ - spec/interceptors/auth/no_instance_var_for_options_spec.rb
328
331
  - spec/interceptors/auth/reauthentication_configuration_spec.rb
329
332
  - spec/interceptors/auth/reauthentication_spec.rb
330
333
  - spec/interceptors/before_request_spec.rb
@@ -475,6 +478,9 @@ test_files:
475
478
  - spec/interceptors/after_response_spec.rb
476
479
  - spec/interceptors/auth/basic_auth_spec.rb
477
480
  - spec/interceptors/auth/bearer_spec.rb
481
+ - spec/interceptors/auth/body_spec.rb
482
+ - spec/interceptors/auth/long_basic_auth_credentials_spec.rb
483
+ - spec/interceptors/auth/no_instance_var_for_options_spec.rb
478
484
  - spec/interceptors/auth/reauthentication_configuration_spec.rb
479
485
  - spec/interceptors/auth/reauthentication_spec.rb
480
486
  - spec/interceptors/before_request_spec.rb