dhc 2.2.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/dhc/interceptors/auth.rb +28 -3
- data/lib/dhc/interceptors/rollbar.rb +1 -1
- data/lib/dhc/version.rb +1 -1
- data/spec/error/to_s_spec.rb +2 -2
- data/spec/interceptors/auth/bearer_spec.rb +53 -9
- data/spec/interceptors/rollbar/invalid_encoding_spec.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9547c0b934f2ac6841be72e6f63213dc50d005c6f8559909869dd2ecb12e1e60
|
4
|
+
data.tar.gz: 3b7335a60aaffe92da269dd0a8fb7a51e66471136a506f0ae4de651a2ac95d8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39ed2e1baec1153872532441114157e7c7170f76746c4f8e9f77b585c36d0999f3047298d05daee2128dd13b74f21c87ba7b3822bbc703c5aa0c4da390511f4f
|
7
|
+
data.tar.gz: 9a6ae9ca323bf5d69d1668e72ceaa5969d79e2dc315a575df805b53d48c24321f537d6ab135d4e1412840cc52c7d400e71fbf121b985fe9eefbc86dfd38f7e91
|
data/README.md
CHANGED
@@ -471,6 +471,27 @@ Adds the following header to the request:
|
|
471
471
|
|
472
472
|
Assuming the method `access_token` responds on runtime of the request with `123456`.
|
473
473
|
|
474
|
+
###### Refresh Bearer Authentication
|
475
|
+
|
476
|
+
If you configure `expires_at` and `refresh` proc in addition to `bearer`, DHC will refresh the bearer token using the defined `refresh` proc in two cases:
|
477
|
+
|
478
|
+
1. before the request if `expires_at.call` < `DateTime.now + 1.minute`
|
479
|
+
2. if the request fails and `refresh(response)` responds to true
|
480
|
+
|
481
|
+
```ruby
|
482
|
+
refresh = ->(response = nil){
|
483
|
+
if response
|
484
|
+
if response.code == 401 && response.data && response.data.error_code == 'ACCESS_TOKEN_EXPIRED'
|
485
|
+
session[:access_token] = new_access_token
|
486
|
+
end
|
487
|
+
else
|
488
|
+
session[:access_token] = new_access_token
|
489
|
+
end
|
490
|
+
}
|
491
|
+
|
492
|
+
DHC.get('http://depay.fi', auth: { bearer: -> { session[:access_token] }, refresh: refresh, expires_at: -> { session[:expires_at] } })
|
493
|
+
```
|
494
|
+
|
474
495
|
##### Basic Authentication
|
475
496
|
|
476
497
|
```ruby
|
@@ -6,6 +6,7 @@ class DHC::Auth < DHC::Interceptor
|
|
6
6
|
|
7
7
|
def before_init
|
8
8
|
body_authentication! if auth_options[:body]
|
9
|
+
auth_options[:refresh].call if refresh_bearer?
|
9
10
|
end
|
10
11
|
|
11
12
|
def before_request
|
@@ -14,13 +15,24 @@ class DHC::Auth < DHC::Interceptor
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def after_response
|
17
|
-
|
18
|
-
|
19
|
-
reauthenticate!
|
18
|
+
reauthenticate! if configuration_correct? && reauthenticate?
|
19
|
+
retry_with_refreshed_token! if retry_with_refreshed_token?
|
20
20
|
end
|
21
21
|
|
22
22
|
private
|
23
23
|
|
24
|
+
def refresh_bearer?
|
25
|
+
auth_options[:bearer] &&
|
26
|
+
auth_options[:refresh].is_a?(Proc) &&
|
27
|
+
bearer_expired?(auth_options[:expires_at])
|
28
|
+
end
|
29
|
+
|
30
|
+
def bearer_expired?(expires_at)
|
31
|
+
expires_at = expires_at.call if expires_at.is_a?(Proc)
|
32
|
+
expires_at = DateTime.parse(expires_at) if expires_at.is_a?(String)
|
33
|
+
expires_at < DateTime.now + 1.minute
|
34
|
+
end
|
35
|
+
|
24
36
|
def body_authentication!
|
25
37
|
auth = auth_options[:body]
|
26
38
|
request.options[:body] = (request.options[:body] || {}).merge(auth)
|
@@ -65,6 +77,19 @@ class DHC::Auth < DHC::Interceptor
|
|
65
77
|
DHC::Error.find(response) == DHC::Unauthorized
|
66
78
|
end
|
67
79
|
|
80
|
+
def retry_with_refreshed_token!
|
81
|
+
bearer_authentication!
|
82
|
+
new_options = request.options.dup
|
83
|
+
new_options = new_options.merge(retry: { max: 1 })
|
84
|
+
request.options = new_options
|
85
|
+
end
|
86
|
+
|
87
|
+
def retry_with_refreshed_token?
|
88
|
+
auth_options[:bearer] &&
|
89
|
+
auth_options[:refresh].is_a?(Proc) &&
|
90
|
+
auth_options[:refresh].call(response)
|
91
|
+
end
|
92
|
+
|
68
93
|
def bearer_header_present?
|
69
94
|
@has_bearer_header ||= request.headers['Authorization'] =~ /^Bearer .+$/i
|
70
95
|
end
|
@@ -28,7 +28,7 @@ class DHC::Rollbar < DHC::Interceptor
|
|
28
28
|
}.merge additional_params
|
29
29
|
begin
|
30
30
|
Rollbar.warning("Status: #{response.code} URL: #{request.url}", data)
|
31
|
-
rescue
|
31
|
+
rescue JSON::GeneratorError
|
32
32
|
sanitized_data = data.deep_transform_values { |value| self.class.fix_invalid_encoding(value) }
|
33
33
|
Rollbar.warning("Status: #{response.code} URL: #{request.url}", sanitized_data)
|
34
34
|
end
|
data/lib/dhc/version.rb
CHANGED
data/spec/error/to_s_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe DHC::Error do
|
|
15
15
|
expect { "#{valid} #{invalid}" }.to raise_error Encoding::CompatibilityError
|
16
16
|
end
|
17
17
|
it 'to_json on an array raises an error' do
|
18
|
-
expect { [valid, invalid].to_json }.to raise_error
|
18
|
+
expect { [valid, invalid].to_json }.to raise_error JSON::GeneratorError
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'to_s on a hash does not raise an error' do
|
@@ -23,7 +23,7 @@ describe DHC::Error do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'to_json on a hash does raise an error' do
|
26
|
-
expect { { valid: valid, invalid: invalid }.to_json }.to raise_error
|
26
|
+
expect { { valid: valid, invalid: invalid }.to_json }.to raise_error JSON::GeneratorError
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -3,17 +3,61 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
describe DHC::Auth do
|
6
|
-
|
7
|
-
|
6
|
+
context 'simple bearer token authentication' do
|
7
|
+
before(:each) do
|
8
|
+
DHC.config.interceptors = [DHC::Auth]
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'adds the bearer token to every request' do
|
12
|
+
def bearer_token
|
13
|
+
'123456'
|
14
|
+
end
|
15
|
+
options = { bearer: -> { bearer_token } }
|
16
|
+
DHC.config.endpoint(:local, 'http://depay.fi', auth: options)
|
17
|
+
stub_request(:get, 'http://depay.fi').with(headers: { 'Authorization' => 'Bearer 123456' })
|
18
|
+
DHC.get(:local)
|
19
|
+
end
|
8
20
|
end
|
9
21
|
|
10
|
-
|
11
|
-
|
12
|
-
|
22
|
+
context 'refresh' do
|
23
|
+
before(:each) do
|
24
|
+
DHC.config.interceptors = [DHC::Auth, DHC::Retry]
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:first_access_token) { '1_ACCESS_TOKEN' }
|
28
|
+
let(:second_access_token) { '2_ACCESS_TOKEN' }
|
29
|
+
let(:third_access_token) { '3_ACCESS_TOKEN' }
|
30
|
+
|
31
|
+
let :session do
|
32
|
+
{
|
33
|
+
access_token: first_access_token
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
refresh = -> {}
|
38
|
+
|
39
|
+
before do
|
40
|
+
refresh = ->(response = nil) {
|
41
|
+
if response
|
42
|
+
if response.code == 401 && response.data && response.data.error_code == 'ACCESS_TOKEN_EXPIRED'
|
43
|
+
session[:access_token] = third_access_token
|
44
|
+
end
|
45
|
+
else
|
46
|
+
session[:access_token] = second_access_token
|
47
|
+
end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'refreshes the bearer if it expired' do
|
52
|
+
stub_request(:get, 'http://depay.fi/').with(headers: { 'Authorization' => 'Bearer 2_ACCESS_TOKEN' })
|
53
|
+
DHC.get('http://depay.fi', auth: { bearer: -> { session[:access_token] }, refresh: refresh, expires_at: -> { (DateTime.now - 1.minute).to_s } })
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'can evaluate response errors (like unauthorized) inside the refresh proc' do
|
57
|
+
stub_request(:get, 'http://depay.fi/').with(headers: { 'Authorization' => 'Bearer 2_ACCESS_TOKEN' })
|
58
|
+
.to_return(status: 401, body: { "error_code": 'ACCESS_TOKEN_EXPIRED' }.to_json)
|
59
|
+
stub_request(:get, 'http://depay.fi/').with(headers: { 'Authorization' => 'Bearer 3_ACCESS_TOKEN' })
|
60
|
+
DHC.get('http://depay.fi', auth: { bearer: -> { session[:access_token] }, refresh: refresh, expires_at: -> { (DateTime.now - 1.minute).to_s } })
|
13
61
|
end
|
14
|
-
options = { bearer: -> { bearer_token } }
|
15
|
-
DHC.config.endpoint(:local, 'http://depay.fi', auth: options)
|
16
|
-
stub_request(:get, 'http://depay.fi').with(headers: { 'Authorization' => 'Bearer 123456' })
|
17
|
-
DHC.get(:local)
|
18
62
|
end
|
19
63
|
end
|
@@ -14,7 +14,7 @@ describe DHC::Rollbar do
|
|
14
14
|
class Rollbar; end
|
15
15
|
::Rollbar.stub(:warning) do
|
16
16
|
call_counter += 1
|
17
|
-
raise
|
17
|
+
raise JSON::GeneratorError if call_counter == 1
|
18
18
|
end
|
19
19
|
|
20
20
|
# the response for the caller is still DHC::BadRequest
|
@@ -24,7 +24,7 @@ describe DHC::Rollbar do
|
|
24
24
|
let(:invalid) { (+"in\xc3lid").force_encoding('ASCII-8BIT') }
|
25
25
|
let(:valid) { described_class.fix_invalid_encoding(invalid) }
|
26
26
|
|
27
|
-
it 'calls fix_invalid_encoding incase a
|
27
|
+
it 'calls fix_invalid_encoding incase a JSON::GeneratorError was encountered' do
|
28
28
|
expect(described_class).to have_received(:fix_invalid_encoding).with(invalid)
|
29
29
|
end
|
30
30
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dhc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/DePayFi/dhc/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -399,7 +399,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
399
399
|
version: '0'
|
400
400
|
requirements:
|
401
401
|
- Ruby >= 2.0.0
|
402
|
-
rubygems_version: 3.
|
402
|
+
rubygems_version: 3.4.1
|
403
403
|
signing_key:
|
404
404
|
specification_version: 4
|
405
405
|
summary: Advanced HTTP Client for Ruby, fueled with interceptors
|