Skip to content

Commit 853b7b5

Browse files
committed
refac: add authorization to the connection headers
1 parent 74aafbd commit 853b7b5

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Initialize the sdk with the basics:
3030

3131
```ruby
3232
Zaig.configure do |config|
33-
config.access_token = "xxxxx-xxxxx-xxxxx-xxxx" # optional
33+
config.jwt_secret = "xxxxx-xxxxx-xxxxx-xxxx" # optional
34+
config.jwt_algorithm = "HS256" # optional
35+
config.jwt_user = "qcedente" # optional if jwt_secret is not defined
3436
config.base_url = "https://example.com"
3537
config.registration_endpoint = "zaig/consulta_de_credito" # optional
3638
end

lib/zaig.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ def self.configure
4848

4949
# Basic configuration settings
5050
class Configuration
51-
attr_accessor :access_token, :base_url, :registration_endpoint
51+
attr_accessor :base_url, :jwt_secret, :jwt_user
52+
attr_writer :jwt_algorithm, :jwt_exp_time, :registration_endpoint
53+
54+
def jwt_algorithm
55+
@jwt_algorithm ||= "HS256"
56+
end
57+
58+
def jwt_exp_time
59+
@jwt_exp_time ||= 1_658_439_475
60+
end
61+
62+
def registration_endpoint
63+
@registration_endpoint ||= "zaig/consulta_de_credito"
64+
end
5265
end
5366
end

lib/zaig/connection.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
module Zaig
44
# Class to instance a authenticated connection object.
55
class Connection < Flash::Integration::Connection
6-
def initialize(request_class: Faraday, base_url: Zaig.configuration.base_url, access_token: Zaig.configuration.access_token)
7-
@access_token = access_token
6+
def initialize(request_class: Faraday, base_url: Zaig.configuration.base_url)
7+
@jwt_algorithm = Zaig.configuration.jwt_algorithm
8+
@jwt_exp_time = Zaig.configuration.jwt_exp_time
9+
@jwt_secret = Zaig.configuration.jwt_secret
10+
@jwt_user = Zaig.configuration.jwt_user
811

912
super(request_class: request_class, base_url: base_url)
1013
end
1114

1215
def default_headers
13-
{
16+
headers = {
1417
"Content-Type": "application/json",
15-
Accept: "application/json",
16-
"x-api-key": @access_token
18+
Accept: "application/json"
1719
}
20+
21+
return headers if @jwt_secret.nil? || @jwt_secret.empty?
22+
23+
headers[:Authorization] = "Bearer #{access_token}"
24+
headers
1825
end
26+
27+
private
28+
def access_token
29+
JWT.encode({ exp: @jwt_exp_time, user: @jwt_user }, @jwt_secret, @jwt_algorithm)
30+
end
1931
end
2032
end

lib/zaig/registration.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def initialize(connection: Zaig::Connection.new, registration_payload: Zaig::Reg
1717
def call(obj)
1818
payload = @registration_payload.call(obj)
1919

20-
req_endpoint = Zaig.configuration.registration_endpoint
21-
22-
res = @connection.post(url: (req_endpoint.nil? ? ENDPOINT : req_endpoint), body: payload.to_json)
20+
res = @connection.post(url: Zaig.configuration.registration_endpoint, body: payload.to_json)
2321

2422
verify_response(res)
2523

zaig.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
1717

1818
s.add_dependency "cpf_cnpj"
1919
s.add_dependency "flash_integration"
20+
s.add_dependency "jwt"
2021
s.add_dependency "singleton"
2122

2223
s.add_development_dependency "bundler", "~> 2.3", ">= 2.3.0"

0 commit comments

Comments
 (0)