lhc 12.0.1 → 12.1.2

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: 640050193c04d507d98ed7538bc526c3693dcef5cb1ee2c07ff99c908c171f96
4
- data.tar.gz: e8c0772f3d7258f16a8c2b069b2f00040f6592d55d7daecbccc6ec58cb15b736
3
+ metadata.gz: 7c7bdb4bd4a91af430b69b11f503806f9fdc55cae4154dad422ac425c91b5c14
4
+ data.tar.gz: 625949e2cdbf59636411283b7ce9e6db35273ffb54612c9e3aa5a52d49c0d928
5
5
  SHA512:
6
- metadata.gz: f184fa4ea6eb799b474a747c1dbd519c4ddbf5d47867cca507525892c204d71bb7cc537411dbbf6afe4385d9f827413c8e918d19440fdcc47bc0b3c9b911050f
7
- data.tar.gz: a7770bacfad84efe0a1c821bc2d8c02c1f3903ed2c3754f23ca5a4b290c66f975bd2af2773456b334fe35c4a0166b5ee55daff030965a62e670d472946dc1351
6
+ metadata.gz: 0a88ba1534ec293a977484aea27613fc99c7329368b210924b74325904f28a77ac8d6b7e504b3228ee8a39b3f453b8efca1d4fdf354872b0d35177625803b68d
7
+ data.tar.gz: 2af73f13c98546e6cca30e35ca3bba4c7b2980790fe1ac35de3a339d63883f9d55e5d624932bdafac315dda1c58fa9e809739a5364f289ec73556c213c78f731
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:
@@ -35,5 +35,5 @@ Gem::Specification.new do |s|
35
35
  s.add_development_dependency 'timecop'
36
36
  s.add_development_dependency 'webmock'
37
37
 
38
- s.license = 'GPL-3'
38
+ s.license = 'GPL-3.0'
39
39
  end
@@ -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.1'
4
+ VERSION ||= '12.1.2'
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.1
4
+ version: 12.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: 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
@@ -384,7 +387,7 @@ files:
384
387
  - spec/timeouts/timings_spec.rb
385
388
  homepage: https://github.com/local-ch/lhc
386
389
  licenses:
387
- - GPL-3
390
+ - GPL-3.0
388
391
  metadata: {}
389
392
  post_install_message:
390
393
  rdoc_options: []
@@ -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