Skip to content

Commit cc59bb7

Browse files
committed
Replace RestClient with Faraday
1 parent 051901e commit cc59bb7

10 files changed

+169
-151
lines changed

Gemfile.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ PATH
33
specs:
44
auth0 (5.16.0)
55
addressable (~> 2.8)
6+
faraday (~> 2.9)
67
jwt (~> 2.7)
7-
rest-client (~> 2.1)
88
retryable (~> 3.0)
99
zache (~> 0.12)
1010

@@ -69,6 +69,10 @@ GEM
6969
erubi (1.12.0)
7070
faker (2.23.0)
7171
i18n (>= 1.8.11, < 2)
72+
faraday (2.9.0)
73+
faraday-net_http (>= 2.0, < 3.2)
74+
faraday-net_http (3.1.0)
75+
net-http
7276
ffi (1.16.3)
7377
formatador (1.1.0)
7478
fuubar (2.5.1)
@@ -116,6 +120,8 @@ GEM
116120
multi_json (1.15.0)
117121
mutex_m (0.2.0)
118122
nenv (0.3.0)
123+
net-http (0.4.1)
124+
uri
119125
netrc (0.11.0)
120126
nokogiri (1.16.2-aarch64-linux)
121127
racc (~> 1.4)
@@ -240,6 +246,7 @@ GEM
240246
unf_ext
241247
unf_ext (0.0.9)
242248
unicode-display_width (2.5.0)
249+
uri (0.13.0)
243250
vcr (6.2.0)
244251
webmock (3.20.0)
245252
addressable (>= 2.8.0)

auth0.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
1616
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
1717
s.require_paths = ['lib']
1818

19-
s.add_runtime_dependency 'rest-client', '~> 2.1'
19+
s.add_runtime_dependency 'faraday', '~> 2.9'
2020
s.add_runtime_dependency 'jwt', '~> 2.7'
2121
s.add_runtime_dependency 'zache', '~> 0.12'
2222
s.add_runtime_dependency 'addressable', '~> 2.8'

lib/auth0/mixins.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'base64'
2-
require 'rest-client'
32
require 'uri'
43

54
require 'auth0/mixins/access_token_struct'

lib/auth0/mixins/httpproxy.rb

+33-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
require "addressable/uri"
2+
require "faraday"
23
require "retryable"
34
require_relative "../exception.rb"
45

56
module Auth0
7+
# Shim for Faraday with interface similar to RestClient
8+
class HttpClient
9+
def self.execute(method:,url:,payload:,headers:,timeout:)
10+
params = headers.delete(:params)
11+
case method
12+
when :get
13+
Faraday.get(url, params, headers)
14+
when :post
15+
Faraday.post(url, payload, headers)
16+
when :patch
17+
Faraday.patch(url, payload, headers)
18+
when :put
19+
Faraday.put(url, payload, headers)
20+
when :delete
21+
Faraday.delete(url, params, headers)
22+
else
23+
raise 'Unsupported HTTP method'
24+
end
25+
end
26+
end
27+
628
module Mixins
729
# here's the proxy for Rest calls based on rest-client, we're building all request on that gem
830
# for now, if you want to feel free to use your own http client
@@ -95,33 +117,28 @@ def request(method, uri, body = {}, extra_headers = {})
95117
call(method, encode_uri(uri), timeout, headers, body.to_json)
96118
end
97119

98-
case result.code
120+
case result.status
99121
when 200...226 then safe_parse_json(result.body)
100-
when 400 then raise Auth0::BadRequest.new(result.body, code: result.code, headers: result.headers)
101-
when 401 then raise Auth0::Unauthorized.new(result.body, code: result.code, headers: result.headers)
102-
when 403 then raise Auth0::AccessDenied.new(result.body, code: result.code, headers: result.headers)
103-
when 404 then raise Auth0::NotFound.new(result.body, code: result.code, headers: result.headers)
104-
when 429 then raise Auth0::RateLimitEncountered.new(result.body, code: result.code, headers: result.headers)
105-
when 500 then raise Auth0::ServerError.new(result.body, code: result.code, headers: result.headers)
106-
else raise Auth0::Unsupported.new(result.body, code: result.code, headers: result.headers)
122+
when 400 then raise Auth0::BadRequest.new(result.body, code: result.status, headers: result.headers)
123+
when 401 then raise Auth0::Unauthorized.new(result.body, code: result.status, headers: result.headers)
124+
when 403 then raise Auth0::AccessDenied.new(result.body, code: result.status, headers: result.headers)
125+
when 404 then raise Auth0::NotFound.new(result.body, code: result.status, headers: result.headers)
126+
when 429 then raise Auth0::RateLimitEncountered.new(result.body, code: result.status, headers: result.headers)
127+
when 500 then raise Auth0::ServerError.new(result.body, code: result.status, headers: result.headers)
128+
else raise Auth0::Unsupported.new(result.body, code: result.status, headers: result.headers)
107129
end
108130
end
109131

110132
def call(method, url, timeout, headers, body = nil)
111-
RestClient::Request.execute(
133+
Auth0::HttpClient.execute(
112134
method: method,
113135
url: url,
114136
timeout: timeout,
115137
headers: headers,
116138
payload: body
117139
)
118-
rescue RestClient::Exception => e
119-
case e
120-
when RestClient::RequestTimeout
121-
raise Auth0::RequestTimeout.new(e.message)
122-
else
123-
return e.response
124-
end
140+
rescue Faraday::RequestTimeoutError => e
141+
raise Auth0::RequestTimeout.new(e.message)
125142
end
126143
end
127144
end

spec/lib/auth0/api/authentication_endpoints_spec.rb

+24-24
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
context 'AuthenticationEndponts' do
5050
context 'api_token' do
5151
it 'requests a new token using client_secret' do
52-
expect(RestClient::Request).to receive(:execute).with(hash_including(
52+
expect(Auth0::HttpClient).to receive(:execute).with(hash_including(
5353
method: :post,
5454
url: 'https://samples.auth0.com/oauth/token',
5555
payload: {
@@ -76,7 +76,7 @@
7676
end
7777

7878
it 'requests a new token using organization' do
79-
expect(RestClient::Request).to receive(:execute).with(hash_including(
79+
expect(Auth0::HttpClient).to receive(:execute).with(hash_including(
8080
method: :post,
8181
url: 'https://samples.auth0.com/oauth/token',
8282
payload: {
@@ -103,7 +103,7 @@
103103
end
104104

105105
it 'requests a new token using client_assertion' do
106-
expect(RestClient::Request).to receive(:execute) do |arg|
106+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
107107
expect(arg).to match(
108108
include(
109109
method: :post,
@@ -135,7 +135,7 @@
135135

136136
context 'exchange_auth_code_for_tokens' do
137137
it 'requests a new token using client_secret' do
138-
expect(RestClient::Request).to receive(:execute) do |arg|
138+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
139139
expect(arg).to match(
140140
include(
141141
method: :post,
@@ -168,7 +168,7 @@
168168
end
169169

170170
it 'requests a new token using client_assertion' do
171-
expect(RestClient::Request).to receive(:execute) do |arg|
171+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
172172
expect(arg).to match(
173173
include(
174174
method: :post,
@@ -201,7 +201,7 @@
201201

202202
context 'exchange_refresh_token' do
203203
it 'exchanges the refresh token using a client secret' do
204-
expect(RestClient::Request).to receive(:execute) do |arg|
204+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
205205
expect(arg).to match(
206206
include(
207207
method: :post,
@@ -233,7 +233,7 @@
233233
end
234234

235235
it 'exchanges the refresh token using client_assertion' do
236-
expect(RestClient::Request).to receive(:execute) do |arg|
236+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
237237
expect(arg).to match(
238238
include(
239239
method: :post,
@@ -268,7 +268,7 @@
268268

269269
context 'exchange_sms_otp_for_tokens' do
270270
it 'requests the tokens using an OTP from SMS' do
271-
expect(RestClient::Request).to receive(:execute) do |arg|
271+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
272272
expect(arg).to match(
273273
include(
274274
method: :post,
@@ -304,7 +304,7 @@
304304
end
305305

306306
it 'requests the tokens using OTP from SMS, and overrides scope and audience' do
307-
expect(RestClient::Request).to receive(:execute) do |arg|
307+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
308308
expect(arg).to match(
309309
include(
310310
method: :post,
@@ -337,7 +337,7 @@
337337
end
338338

339339
it 'requests the tokens using an OTP from SMS using client assertion' do
340-
expect(RestClient::Request).to receive(:execute) do |arg|
340+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
341341
expect(arg).to match(
342342
include(
343343
method: :post,
@@ -366,7 +366,7 @@
366366

367367
context 'exchange_email_otp_for_tokens' do
368368
it 'requests the tokens using email OTP' do
369-
expect(RestClient::Request).to receive(:execute) do |arg|
369+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
370370
expect(arg).to match(
371371
include(
372372
method: :post,
@@ -402,7 +402,7 @@
402402
end
403403

404404
it 'requests the tokens using OTP from email, and overrides scope and audience' do
405-
expect(RestClient::Request).to receive(:execute) do |arg|
405+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
406406
expect(arg).to match(
407407
include(
408408
method: :post,
@@ -430,7 +430,7 @@
430430
end
431431

432432
it 'requests the tokens using OTP from email using client assertion' do
433-
expect(RestClient::Request).to receive(:execute) do |arg|
433+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
434434
expect(arg).to match(
435435
include(
436436
method: :post,
@@ -462,7 +462,7 @@
462462

463463
context 'login_with_resource_owner' do
464464
it 'logs in using a client secret' do
465-
expect(RestClient::Request).to receive(:execute) do |arg|
465+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
466466
expect(arg).to match(
467467
include(
468468
method: :post,
@@ -498,7 +498,7 @@
498498
end
499499

500500
it 'logs in using a client secret, realm and audience' do
501-
expect(RestClient::Request).to receive(:execute) do |arg|
501+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
502502
expect(arg).to match(
503503
include(
504504
method: :post,
@@ -534,7 +534,7 @@
534534
end
535535

536536
it 'logs in using client assertion' do
537-
expect(RestClient::Request).to receive(:execute) do |arg|
537+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
538538
expect(arg).to match(
539539
include(
540540
method: :post,
@@ -568,7 +568,7 @@
568568

569569
context 'start_passwordless_email_flow' do
570570
it 'starts passwordless flow using a client secret' do
571-
expect(RestClient::Request).to receive(:execute) do |arg|
571+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
572572
expect(arg).to match(
573573
include(
574574
method: :post,
@@ -592,7 +592,7 @@
592592
end
593593

594594
it 'starts passwordless email flow using client assertion' do
595-
expect(RestClient::Request).to receive(:execute) do |arg|
595+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
596596
expect(arg).to match(
597597
include(
598598
method: :post,
@@ -615,7 +615,7 @@
615615

616616
context 'start_passwordless_sms_flow' do
617617
it 'starts passwordless flow using a client secret' do
618-
expect(RestClient::Request).to receive(:execute) do |arg|
618+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
619619
expect(arg).to match(
620620
include(
621621
method: :post,
@@ -637,7 +637,7 @@
637637
end
638638

639639
it 'starts passwordless email flow using client assertion' do
640-
expect(RestClient::Request).to receive(:execute) do |arg|
640+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
641641
expect(arg).to match(
642642
include(
643643
method: :post,
@@ -675,7 +675,7 @@
675675

676676
context 'pushed_authorization_request' do
677677
it 'sends the request as a form post' do
678-
expect(RestClient::Request).to receive(:execute) do |arg|
678+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
679679
expect(arg[:url]).to eq('https://samples.auth0.com/oauth/par')
680680
expect(arg[:method]).to eq(:post)
681681

@@ -692,7 +692,7 @@
692692
end
693693

694694
it 'allows the RestClient to handle the correct header defaults' do
695-
expect(RestClient::Request).to receive(:execute) do |arg|
695+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
696696
expect(arg[:headers]).not_to have_key('Content-Type')
697697

698698
StubResponse.new({}, true, 200)
@@ -703,7 +703,7 @@
703703
end
704704

705705
it 'sends the request as a form post with all known overrides' do
706-
expect(RestClient::Request).to receive(:execute) do |arg|
706+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
707707
expect(arg[:url]).to eq('https://samples.auth0.com/oauth/par')
708708
expect(arg[:method]).to eq(:post)
709709

@@ -733,7 +733,7 @@
733733
end
734734

735735
it 'sends the request as a form post using client assertion' do
736-
expect(RestClient::Request).to receive(:execute) do |arg|
736+
expect(Auth0::HttpClient).to receive(:execute) do |arg|
737737
expect(arg[:url]).to eq('https://samples.auth0.com/oauth/par')
738738
expect(arg[:method]).to eq(:post)
739739
expect(arg[:payload][:client_secret]).to be_nil

0 commit comments

Comments
 (0)