brl_auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd354f62a92d35682bdafa06eebf3ae5995dca34b53aed0e21154a6c5e754fbd
4
+ data.tar.gz: 7790bdf84e4a7efae9d15eace2464cb69b7e56f9dcce2d1ace7a3ab152156f91
5
+ SHA512:
6
+ metadata.gz: 5c989bfb885e1f24376b54014fdab445ef1e18dfbeff725783ab2d50e5324d4852c5870f5ac82a38a05299e6bcfe11ba6f6b8e5cc0dbc1a2e031001e6730c85b
7
+ data.tar.gz: 3d22b1bfadb636b35167c133a07fb0324f7f1ad34199391515b042e1a659ee38a7e1cf6b39bc73c7272027475a404a7d8f8291baa2ce3cb2c499041138bb2cab
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## v0.1.0
4
+
5
+ * Initial release of BRL Auth
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2021 Quasar Flash
2
+
3
+ MIT LICENSE
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # brl_auth_ruby
2
+
3
+ Módulo de autenticação para API da BRL para projetos em Ruby
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/brl_auth.svg)](https://badge.fury.io/rb/brl_auth)
6
+ [![brl_auth](https://github.com/Quasar-Flash/brl_auth_ruby/actions/workflows/ruby.yml/badge.svg)](https://github.com/Quasar-Flash/brl_auth_ruby/actions/workflows/ruby.yml)
7
+
8
+ ## Dev Requirements
9
+
10
+ - Ruby: Any version
11
+ - Bundler
12
+
13
+ ## Global Installation
14
+
15
+ ```ruby
16
+ gem install brl_auth
17
+ ```
18
+
19
+ ## Installation for Rails
20
+
21
+ ```ruby
22
+ # Add to the Gemfile
23
+ gem "brl_auth", "~> 0.1.0"
24
+ ```
25
+
26
+ ## Setting the BRL credentials - Rails Project
27
+
28
+ Create the config/initializers/brl.rb file and define:
29
+
30
+ ```ruby
31
+ # Set the env variables
32
+ BRL.configure do |config|
33
+ config.handshake = "XXXXXX-XXXXX-XXXX"
34
+ config.secret_key = "xxxxxxxxx"
35
+ end
36
+ ```
37
+
38
+ ## Retrieving token
39
+
40
+ ```ruby
41
+ require "brl_auth"
42
+
43
+ BRL::Auth::TokenService.new.retrieve
44
+ ```
45
+
46
+ ## Result Example
47
+
48
+ ```ruby
49
+ #<BRL::Auth::Token:0x00005606d4e9ae40
50
+ @access_token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
51
+ @created_at=2021-08-30 21:19:21.428047024 UTC,
52
+ @expires_in=86400,
53
+ @refresh_token="xxxxxxxxxxxxxxxxxxxxxx",
54
+ @scope="refresh_token",
55
+ @token_type="Bearer">
56
+ ```
57
+
58
+ ## Problems?
59
+
60
+ **Please do not directly email any committers with questions or problems.** A
61
+ community is best served when discussions are held in public.
62
+
63
+ Searching the [issues](https://github.com/Quasar-Flash/brl_auth_ruby/issues)
64
+ for your problem is also a good idea.
65
+
66
+ ## Contributing
67
+
68
+ - Check out the latest master to make sure the feature hasn't been implemented
69
+ or the bug hasn't been fixed yet;
70
+ - Check out the issue tracker to make sure someone already hasn't requested it
71
+ and/or contributed it;
72
+ - Fork the project;
73
+ - Start a feature/bugfix branch;
74
+ - Commit and push until you are happy with your contribution;
75
+ - Make sure to add tests for it. This is important so I don't break it in a
76
+ future version unintentionally.;
77
+ - Please try not to mess with the Rakefile, version, or history. If you want to
78
+ have your own version, or is otherwise necessary, that is fine, but please
79
+ isolate to its own commit so I can cherry-pick around it.
80
+
81
+ ## License
82
+
83
+ Please see [LICENSE](https://github.com/Quasar-Flash/brl_auth_ruby/blob/master/LICENSE.txt)
84
+ for licensing details.
85
+
86
+ ## Authors
87
+
88
+ Danilo Carolino, [@danilogco](https://github.com/danilogco) / [@Quasar-Flash](https://github.com/Quasar-Flash)
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
4
+ begin
5
+ require "bundler/setup"
6
+ rescue LoadError
7
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
8
+ end
9
+
10
+ begin
11
+ require "rdoc/task"
12
+ rescue LoadError
13
+ require "rdoc/rdoc"
14
+ require "rake/rdoctask"
15
+
16
+ RDoc::Task = Rake::RDocTask
17
+ end
18
+
19
+ RDoc::Task.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = "rdoc"
21
+ rdoc.title = "brl_auth"
22
+ rdoc.options << "--line-numbers"
23
+ rdoc.rdoc_files.include("README.rdoc")
24
+ rdoc.rdoc_files.include("lib/**/*.rb")
25
+ end
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ begin
30
+ require "rake/testtask"
31
+ require "rubocop/rake_task"
32
+
33
+ RuboCop::RakeTask.new(:rubocop) do |t|
34
+ t.options = ["--display-cop-names"]
35
+ end
36
+ rescue LoadError
37
+ # no rspec available
38
+ end
39
+
40
+ begin
41
+ require "rspec/core/rake_task"
42
+
43
+ RSpec::Core::RakeTask.new(:spec)
44
+ rescue LoadError
45
+ # no rspec available
46
+ end
47
+
48
+ task default: %i[rubocop spec]
@@ -0,0 +1,4 @@
1
+ BRL.configure do |config|
2
+ config.handshake = "D317F1AA-8D36-468F-9"
3
+ config.secret_key = "f89f8d0e735a91c5269ab08d72fa27670d000e7561698d6e664e7b603f5c4e40"
4
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class AuthenticatedConnection < Connection
6
+ COOKIE_CACHE_KEY = "BRL_AUTH_COOKIE_CACHE_KEY"
7
+ CONTENT_TYPE = "application/json"
8
+
9
+ def initialize(token_service: BRL::Auth::TokenService.new,
10
+ cache: defined?(Rails) ? Rails.cache : nil,
11
+ request_class: Faraday,
12
+ base_url: BRL::BASE_URL)
13
+ super(request_class: request_class, base_url: base_url)
14
+ @token_service = token_service
15
+ @cache = cache
16
+ end
17
+
18
+ def default_headers
19
+ {
20
+ "Accept": CONTENT_TYPE,
21
+ "Authorization": (@cache ? cached_token : auth_token),
22
+ "Content-Type": CONTENT_TYPE
23
+ }
24
+ end
25
+
26
+ private
27
+ def auth_token
28
+ token = @token_service.retrieve
29
+
30
+ "Bearer #{token.access_token}"
31
+ end
32
+
33
+ def cached_token
34
+ @cache.fetch(COOKIE_CACHE_KEY, expires_in: BRL::Auth::TOKEN_EXPIRATION) { auth_token }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class AuthenticatedResource
6
+ def initialize(connection: AuthenticatedConnection.new)
7
+ @connection = connection
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class AuthenticationError < BaseError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class BadRequestError < BaseError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class BaseError < StandardError
6
+ def initialize(message = self.class.default_message)
7
+ super
8
+ end
9
+
10
+ def self.default_message
11
+ name = self.name.dup
12
+
13
+ I18n.t("errors.#{underscore(name)}")
14
+ end
15
+
16
+ def self.underscore(str)
17
+ str.gsub(/::/, ".")
18
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
19
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
20
+ .tr("-", "_")
21
+ .downcase
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class Connection < Flash::Integration::Connection
6
+ ACCEPT = "application/json"
7
+ CONTENT_TYPE = "application/x-www-form-urlencoded"
8
+
9
+ def initialize(request_class: Faraday, base_url: BRL::Auth::BASE_URL)
10
+ super(request_class: request_class, base_url: base_url)
11
+ end
12
+
13
+ def default_headers
14
+ {
15
+ "Accept": "application/json",
16
+ "Authorization": authorization_string,
17
+ "Content-Type": CONTENT_TYPE
18
+ }
19
+ end
20
+
21
+ protected
22
+ def authorization_string
23
+ handshake = BRL.configuration.handshake.strip
24
+ secret_key = BRL.configuration.secret_key.strip
25
+ auth_string = ::Base64.strict_encode64("#{handshake}:#{secret_key}")
26
+
27
+ "basic #{auth_string}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class InvalidCredentialsError < BaseError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class ServerError < BaseError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class Token
6
+ attr_reader :access_token, :created_at, :expires_in, :refresh_token, :scope, :token_type
7
+
8
+ def initialize(access_token: nil, created_at: Time.now.zone, expires_in: nil, refresh_token: nil, scope: nil, token_type: nil)
9
+ @access_token = access_token
10
+ @created_at = created_at
11
+ @expires_in = expires_in
12
+ @refresh_token = refresh_token
13
+ @scope = scope
14
+ @token_type = token_type
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class TokenService
6
+ def initialize(connection: Connection.new)
7
+ @connection = connection
8
+ end
9
+
10
+ def retrieve
11
+ res = @connection.post(url: BRL::Auth::TOKEN_ENDPOINT, body: BRL::Auth::TokenService.login_body)
12
+
13
+ return parse_response(res.body) if res.status == 200
14
+
15
+ raise BRL::Auth::BadRequestError if res.status == 400
16
+ raise BRL::Auth::InvalidCredentialsError if res.status == 401
17
+ raise BRL::Auth::ServerError if res.status == 500
18
+ raise BRL::Auth::UnexpectedError
19
+ end
20
+
21
+ private
22
+ def self.login_body
23
+ URI.encode_www_form({ grant_type: "client_credentials", scope: "refresh_token" })
24
+ end
25
+
26
+ def parse_response(res)
27
+ res = JSON.parse(res)
28
+
29
+ BRL::Auth::Token.new(
30
+ access_token: res["access_token"],
31
+ created_at: Time.now.utc,
32
+ expires_in: res["expires_in"],
33
+ refresh_token: res["refresh_token"],
34
+ scope: res["scope"],
35
+ token_type: res["token_type"]
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ class UnexpectedError < BaseError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRL
4
+ module Auth
5
+ # When updating version, keep in mind Semantic Versioning http://semver.org/
6
+ # TL;DR; (Major.Minor.Patch)
7
+ # Releases before 1.0.0 are in active development and can change anytime
8
+ # 1.0.0 and up is indication and declaration of a stable public API
9
+ # Major - Incremented for incompatible changes with previous release (or big enough new features)
10
+ # Minor - Incremented for new backwards-compatible features + deprecations
11
+ # Patch - Incremented for backwards-compatible bug fixes
12
+ VERSION = "0.1.0"
13
+ end
14
+ end
data/lib/brl_auth.rb ADDED
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "flash_integration"
5
+ require "i18n"
6
+ require "json"
7
+
8
+ require "brl/auth/version"
9
+
10
+ require "brl/auth/connection"
11
+ require "brl/auth/token"
12
+ require "brl/auth/token_service"
13
+ require "brl/auth/authenticated_resource"
14
+ require "brl/auth/authenticated_connection"
15
+
16
+ require "brl/auth/base_error"
17
+ require "brl/auth/authentication_error"
18
+ require "brl/auth/bad_request_error"
19
+ require "brl/auth/invalid_credentials_error"
20
+ require "brl/auth/server_error"
21
+ require "brl/auth/unexpected_error"
22
+
23
+ I18n.load_path += Dir[File.join(__dir__, "locales", "**/*.yml")]
24
+ I18n.reload! if I18n.backend.initialized?
25
+
26
+ module BRL
27
+ BASE_URL = "https://hom.api.acesso.hmra.com.br/autorizacao/token"
28
+
29
+ class << self
30
+ attr_writer :configuration
31
+
32
+ def configuration
33
+ @configuration ||= Configuration.new
34
+ end
35
+ end
36
+
37
+ def self.configure
38
+ self.configuration ||= Configuration.new
39
+
40
+ yield(configuration)
41
+ end
42
+
43
+ class Configuration
44
+ attr_writer :auth_base_url, :handshake, :secret_key
45
+
46
+ def auth_base_url
47
+ @auth_base_url ||= ENV["BRL_AUTH_BASE_URL"]
48
+ end
49
+
50
+ def handshake
51
+ @handshake ||= ENV["BRL_HANDSHAKE"]
52
+ end
53
+
54
+ def secret_key
55
+ @secret_key ||= ENV["BRL_SECRET_KEY"]
56
+ end
57
+ end
58
+
59
+ module Auth
60
+ BASE_URL = "https://hom.api.acesso.hmra.com.br"
61
+ TOKEN_ENDPOINT = "autorizacao/token"
62
+ TOKEN_EXPIRATION = 86_400
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ en:
2
+ errors:
3
+ brl:
4
+ auth:
5
+ authentication_error: An error ocurred during the authentication
6
+ bad_request_error: The request body or format is invalid
7
+ invalid_credentials_error: The credentials informed are invalid
8
+ server_error: An internal server error ocurred
9
+ unexpected_error: An unexpected error ocurred
@@ -0,0 +1,9 @@
1
+ 'pt-BR':
2
+ errors:
3
+ brl:
4
+ auth:
5
+ authentication_error: Ocorreu um erro durante a autenticação
6
+ bad_request_error: O corpo ou formato da requisição é inválido
7
+ invalid_credentials_error: As credenciais informadas são inválidas
8
+ server_error: Ocorreu um erro inesperado no servidor
9
+ unexpected_error: Ocorreu um erro inesperado
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe BRL::Auth::AuthenticatedConnection do
6
+ describe "#default_headers" do
7
+ let(:token_service) { instance_double(BRL::Auth::TokenService) }
8
+ let(:token) { BRL::Auth::Token.new(access_token: access_token) }
9
+
10
+ subject { described_class.new(token_service: token_service, cache: cache).default_headers }
11
+
12
+ context "when token is cached" do
13
+ let(:access_token) { "Bearer access_token" }
14
+ let(:cache) { double("cache") }
15
+
16
+ before do
17
+ allow(token_service).to receive(:retrieve).and_return(token)
18
+ allow(cache).to receive(:fetch).with(described_class::COOKIE_CACHE_KEY, expires_in: BRL::Auth::TOKEN_EXPIRATION).and_return(access_token)
19
+ end
20
+
21
+ it { expect(token_service).not_to have_received(:retrieve) }
22
+
23
+ it { expect(subject).to eq({
24
+ Accept: described_class::CONTENT_TYPE,
25
+ Authorization: access_token,
26
+ "Content-Type": described_class::CONTENT_TYPE
27
+ }) }
28
+ end
29
+
30
+ context "when token is not cached" do
31
+ let(:access_token) { "access_token" }
32
+ let(:cache) { nil }
33
+
34
+ before do
35
+ allow(token_service).to receive(:retrieve).and_return(token)
36
+ end
37
+
38
+ it { subject; expect(token_service).to have_received(:retrieve).once }
39
+
40
+ it { expect(subject).to eq({
41
+ Accept: described_class::CONTENT_TYPE,
42
+ Authorization: "Bearer #{access_token}",
43
+ "Content-Type": described_class::CONTENT_TYPE
44
+ }) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe BRL::Auth::AuthenticatedResource do
6
+ describe "#initialize" do
7
+ let(:connection) { instance_double(BRL::Auth::AuthenticatedConnection) }
8
+
9
+ subject { described_class.new(connection: connection) }
10
+
11
+ it { expect(subject).to be_kind_of(BRL::Auth::AuthenticatedResource) }
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe BRL::Auth::Connection do
6
+ let(:auth_string) { "aGFuZHNoYWtlX3ZhbHVlOnBhc3N3b3JkX3ZhbHVl" }
7
+
8
+ before do
9
+ BRL.configure do |config|
10
+ config.auth_base_url = "auth_url_value"
11
+ config.handshake = "handshake_value"
12
+ config.secret_key = "password_value"
13
+ end
14
+ end
15
+
16
+ describe "#default_headers" do
17
+ subject { described_class.new.default_headers }
18
+
19
+ it { expect(subject).to eq({
20
+ "Content-Type": described_class::CONTENT_TYPE,
21
+ Accept: described_class::ACCEPT,
22
+ Authorization: "basic #{auth_string}"
23
+ }) }
24
+ end
25
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe BRL::Auth::TokenService do
6
+ let(:connection) { instance_double(BRL::Auth::Connection) }
7
+
8
+ describe "#retrieve" do
9
+ let(:req_body) { "grant_type=client_credentials&scope=refresh_token" }
10
+ let(:result) { double("result") }
11
+ let(:access_token) { "access_token" }
12
+ let(:expires_in) { 19000 }
13
+ let(:refresh_token) { "refresh_token" }
14
+ let(:scope) { "scope" }
15
+ let(:token_type) { "token_type" }
16
+
17
+ subject { described_class.new(connection: connection).retrieve }
18
+
19
+ before do
20
+ allow(connection).to receive(:post).with(url: BRL::Auth::TOKEN_ENDPOINT, body: req_body).and_return(result)
21
+ end
22
+
23
+ context "when it was requested successfully" do
24
+ before do
25
+ allow(result).to receive(:body).and_return({
26
+ access_token: access_token,
27
+ expires_in: expires_in,
28
+ refresh_token: refresh_token,
29
+ scope: scope,
30
+ token_type: token_type
31
+ }.to_json)
32
+ allow(result).to receive(:status).and_return(200)
33
+ end
34
+
35
+ it { expect(subject.access_token).to eq(access_token) }
36
+ it { expect(subject.expires_in).to eq(expires_in) }
37
+ it { expect(subject.refresh_token).to eq(refresh_token) }
38
+ it { expect(subject.scope).to eq(scope) }
39
+ it { expect(subject.token_type).to eq(token_type) }
40
+ end
41
+
42
+ context "when it received a bad request error" do
43
+ before { allow(result).to receive(:status).and_return(400) }
44
+
45
+ it { expect { subject }.to raise_error(BRL::Auth::BadRequestError) }
46
+ end
47
+
48
+ context "when it received an unauthorized error" do
49
+ before { allow(result).to receive(:status).and_return(401) }
50
+
51
+ it { expect { subject }.to raise_error(BRL::Auth::InvalidCredentialsError) }
52
+ end
53
+
54
+ context "when it received an internal server error" do
55
+ before { allow(result).to receive(:status).and_return(500) }
56
+
57
+ it { expect { subject }.to raise_error(BRL::Auth::ServerError) }
58
+ end
59
+
60
+ context "when it received an unexpected error" do
61
+ before { allow(result).to receive(:status).and_return(550) }
62
+
63
+ it { expect { subject }.to raise_error(BRL::Auth::UnexpectedError) }
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe BRL::Auth do
6
+ describe "BASE_URL" do
7
+ subject { defined? BRL::Auth::BASE_URL }
8
+
9
+ it { expect(subject).to be_truthy }
10
+ end
11
+
12
+ describe "TOKEN_ENDPOINT" do
13
+ subject { defined? BRL::Auth::TOKEN_ENDPOINT }
14
+
15
+ it { expect(subject).to be_truthy }
16
+ end
17
+
18
+ describe "TOKEN_EXPIRATION" do
19
+ subject { defined? BRL::Auth::TOKEN_EXPIRATION }
20
+
21
+ it { expect(subject).to be_truthy }
22
+ end
23
+
24
+ describe ".configure" do
25
+ before do
26
+ BRL.configuration = nil
27
+ ENV.clear
28
+ end
29
+
30
+ subject { BRL.configuration }
31
+
32
+ context "when configuration is defined" do
33
+ before do
34
+ BRL.configure do |config|
35
+ config.auth_base_url = "auth_url_value"
36
+ config.handshake = "handshake_value"
37
+ config.secret_key = "password_value"
38
+ end
39
+ end
40
+
41
+ it { expect(subject).not_to be_nil }
42
+
43
+ it { expect(subject.auth_base_url).to eq("auth_url_value") }
44
+
45
+ it { expect(subject.handshake).to eq("handshake_value") }
46
+
47
+ it { expect(subject.secret_key).to eq("password_value") }
48
+ end
49
+
50
+ context "when configuration is not defined" do
51
+ it { expect(subject).not_to be_nil }
52
+
53
+ it { expect(subject.auth_base_url).to be_nil }
54
+
55
+ it { expect(subject.handshake).to be_nil }
56
+
57
+ it { expect(subject.secret_key).to be_nil }
58
+ end
59
+
60
+ context "when its configured by envs" do
61
+ before do
62
+ ENV["BRL_AUTH_BASE_URL"] = "auth_url_value"
63
+ ENV["BRL_HANDSHAKE"] = "handshake_value"
64
+ ENV["BRL_SECRET_KEY"] = "password_value"
65
+ end
66
+
67
+ it { expect(subject).not_to be_nil }
68
+
69
+ it { expect(subject.auth_base_url).to eq("auth_url_value") }
70
+
71
+ it { expect(subject.handshake).to eq("handshake_value") }
72
+
73
+ it { expect(subject.secret_key).to eq("password_value") }
74
+ end
75
+
76
+ context "when its envs configured and configs setted" do
77
+ before do
78
+ ENV["BRL_AUTH_BASE_URL"] = "auth_url_value"
79
+ ENV["BRL_HANDSHAKE"] = "handshake_value"
80
+ ENV["BRL_SECRET_KEY"] = "password_value"
81
+
82
+ BRL.configure do |config|
83
+ config.auth_base_url = "auth_url_value2"
84
+ config.handshake = "handshake_value2"
85
+ config.secret_key = "password_value2"
86
+ end
87
+ end
88
+
89
+ it { expect(subject).not_to be_nil }
90
+
91
+ it { expect(subject.auth_base_url).to eq("auth_url_value2") }
92
+
93
+ it { expect(subject.handshake).to eq("handshake_value2") }
94
+
95
+ it { expect(subject.secret_key).to eq("password_value2") }
96
+ end
97
+
98
+ describe "I18n calls" do
99
+ it { expect(I18n.default_locale).to eq(:en) }
100
+
101
+ it { expect(I18n.config.available_locales).to contain_exactly(:en, :'pt-BR') }
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "simplecov"
4
+
5
+ SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter]
6
+ SimpleCov.minimum_coverage 70.0
7
+
8
+ SimpleCov.start do
9
+ add_filter "/spec/"
10
+ minimum_coverage 70
11
+ minimum_coverage_by_file 40
12
+ end
13
+
14
+ require "rubygems"
15
+ require "bundler"
16
+ require "brl_auth"
17
+ require "pry"
18
+
19
+ begin
20
+ Bundler.setup(:default, :development, :test)
21
+ rescue Bundler::BundlerError => e
22
+ warn e.message
23
+ warn "Run `bundle install` to install missing gems"
24
+
25
+ exit e.status_code
26
+ end
27
+
28
+ RSpec.configure do |config|
29
+ # Enable flags like --only-failures and --next-failure
30
+ config.example_status_persistence_file_path = ".rspec_status"
31
+
32
+ config.expect_with :rspec do |c|
33
+ c.syntax = :expect
34
+ end
35
+ end
36
+
37
+ # minitest/mock # Uncomment me to use minitest mocks
metadata ADDED
@@ -0,0 +1,298 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brl_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danilo Carolino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: base64
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: flash_integration
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.0
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '1.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: 1.8.0
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: json
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 2.5.1
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '2.0'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: 2.5.1
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '2.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: bundler
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: 2.2.25
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '2.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 2.2.25
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '2.0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: factory_bot
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 6.2.0
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 6.2.0
115
+ - !ruby/object:Gem::Dependency
116
+ name: gemsurance
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '0.10'
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '0.10'
129
+ - !ruby/object:Gem::Dependency
130
+ name: pry
131
+ requirement: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: 0.14.1
136
+ type: :development
137
+ prerelease: false
138
+ version_requirements: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - "~>"
141
+ - !ruby/object:Gem::Version
142
+ version: 0.14.1
143
+ - !ruby/object:Gem::Dependency
144
+ name: rake
145
+ requirement: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 13.0.6
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 10.0.0
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 13.0.6
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 10.0.0
163
+ - !ruby/object:Gem::Dependency
164
+ name: rspec
165
+ requirement: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: 3.10.0
170
+ type: :development
171
+ prerelease: false
172
+ version_requirements: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - "~>"
175
+ - !ruby/object:Gem::Version
176
+ version: 3.10.0
177
+ - !ruby/object:Gem::Dependency
178
+ name: rubocop
179
+ requirement: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - "~>"
182
+ - !ruby/object:Gem::Version
183
+ version: '1.20'
184
+ type: :development
185
+ prerelease: false
186
+ version_requirements: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - "~>"
189
+ - !ruby/object:Gem::Version
190
+ version: '1.20'
191
+ - !ruby/object:Gem::Dependency
192
+ name: rubocop-packaging
193
+ requirement: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - "~>"
196
+ - !ruby/object:Gem::Version
197
+ version: 0.5.1
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - "~>"
203
+ - !ruby/object:Gem::Version
204
+ version: 0.5.1
205
+ - !ruby/object:Gem::Dependency
206
+ name: rubocop-performance
207
+ requirement: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - "~>"
210
+ - !ruby/object:Gem::Version
211
+ version: 1.11.5
212
+ type: :development
213
+ prerelease: false
214
+ version_requirements: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - "~>"
217
+ - !ruby/object:Gem::Version
218
+ version: 1.11.5
219
+ - !ruby/object:Gem::Dependency
220
+ name: simplecov
221
+ requirement: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - "~>"
224
+ - !ruby/object:Gem::Version
225
+ version: 0.21.2
226
+ type: :development
227
+ prerelease: false
228
+ version_requirements: !ruby/object:Gem::Requirement
229
+ requirements:
230
+ - - "~>"
231
+ - !ruby/object:Gem::Version
232
+ version: 0.21.2
233
+ description: Módulo de autenticação para API da BRL para projetos em Ruby
234
+ email:
235
+ - danilo.carolino@qflash.com.br
236
+ executables: []
237
+ extensions: []
238
+ extra_rdoc_files: []
239
+ files:
240
+ - CHANGELOG.md
241
+ - LICENSE.txt
242
+ - README.md
243
+ - Rakefile
244
+ - lib/brl/auth/BRL.configure do |config|.arb
245
+ - lib/brl/auth/authenticated_connection.rb
246
+ - lib/brl/auth/authenticated_resource.rb
247
+ - lib/brl/auth/authentication_error.rb
248
+ - lib/brl/auth/bad_request_error.rb
249
+ - lib/brl/auth/base_error.rb
250
+ - lib/brl/auth/connection.rb
251
+ - lib/brl/auth/invalid_credentials_error.rb
252
+ - lib/brl/auth/server_error.rb
253
+ - lib/brl/auth/token.rb
254
+ - lib/brl/auth/token_service.rb
255
+ - lib/brl/auth/unexpected_error.rb
256
+ - lib/brl/auth/version.rb
257
+ - lib/brl_auth.rb
258
+ - lib/locales/en.yml
259
+ - lib/locales/pt-BR.yml
260
+ - spec/brl/auth/authenticated_connection_spec.rb
261
+ - spec/brl/auth/authenticated_resource_spec.rb
262
+ - spec/brl/auth/connection_spec.rb
263
+ - spec/brl/auth/token_service_spec.rb
264
+ - spec/brl_auth_spec.rb
265
+ - spec/spec_helper.rb
266
+ homepage: https://github.com/Quasar-Flash/brl_auth_ruby
267
+ licenses:
268
+ - MIT
269
+ metadata:
270
+ changelog_uri: https://github.com/Quasar-Flash/brl_auth_ruby/blob/master/CHANGELOG.md
271
+ source_code_uri: https://github.com/Quasar-Flash/brl_auth_ruby
272
+ bug_tracker_uri: https://github.com/Quasar-Flash/brl_auth_ruby/issues
273
+ post_install_message:
274
+ rdoc_options: []
275
+ require_paths:
276
+ - lib
277
+ required_ruby_version: !ruby/object:Gem::Requirement
278
+ requirements:
279
+ - - ">="
280
+ - !ruby/object:Gem::Version
281
+ version: '2.5'
282
+ required_rubygems_version: !ruby/object:Gem::Requirement
283
+ requirements:
284
+ - - ">="
285
+ - !ruby/object:Gem::Version
286
+ version: '0'
287
+ requirements: []
288
+ rubygems_version: 3.2.22
289
+ signing_key:
290
+ specification_version: 4
291
+ summary: BRL Auth Library
292
+ test_files:
293
+ - spec/brl/auth/authenticated_connection_spec.rb
294
+ - spec/brl/auth/authenticated_resource_spec.rb
295
+ - spec/brl/auth/connection_spec.rb
296
+ - spec/brl/auth/token_service_spec.rb
297
+ - spec/brl_auth_spec.rb
298
+ - spec/spec_helper.rb