Class: Justa::TokenManager
Overview
Class to manage Tokens with singleton structure
Instance Attribute Summary collapse
-
#authenticators ⇒ Object
readonly
Returns the value of attribute authenticators.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Class Method Summary collapse
-
.add_client(client) ⇒ Array
Registers a new client to be used.
-
.client_for(key = Justa.default_client_key) ⇒ Justa::Client
Find a Client for a specific Key.
-
.client_type_for(key = Justa.default_client_key) ⇒ Symbol
Find a Client Type for a specific Key.
- .instance ⇒ Object
-
.token_for(key = Justa.default_client_key) ⇒ String
Find a token for a specific Client Key.
Instance Method Summary collapse
-
#initialize ⇒ TokenManager
constructor
Initializes TokenManager.
-
#setup_autenticators(tokens) ⇒ Array
Sets authenticators based on tokens passed by constructor.
Constructor Details
#initialize ⇒ TokenManager
Initializes TokenManager
This class builds authentication array with a mutex to share tokens
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/justa/token_manager.rb', line 14 def initialize tokens = nil if Justa.credentials case Justa.credentials when Array tokens = Justa.credentials when Hash tokens = [Justa.credentials] end else tokens = [{ username: Justa.username, password: Justa.password, client_id: Justa.client_id, client_secret: Justa.client_secret, integrator_id: Justa.integrator_id, document: Justa.document, key: :default, default: true }] end @mutex = Mutex.new @authenticators = nil setup_autenticators tokens end |
Instance Attribute Details
#authenticators ⇒ Object (readonly)
Returns the value of attribute authenticators.
6 7 8 |
# File 'lib/justa/token_manager.rb', line 6 def authenticators @authenticators end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
6 7 8 |
# File 'lib/justa/token_manager.rb', line 6 def mutex @mutex end |
Class Method Details
.add_client(client) ⇒ Array
Registers a new client to be used
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/justa/token_manager.rb', line 89 def self.add_client(client) instance unless @instance client = (client.is_a? Justa::Client) ? client : Justa::Client.new(**client) raise ParamError.new("Client key '#{client.key}' already exists", "Key", "") if client_for client.key @instance.mutex.synchronize do @instance.authenticators << Authenticator.new(client) end end |
.client_for(key = Justa.default_client_key) ⇒ Justa::Client
Find a Client for a specific Key
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/justa/token_manager.rb', line 107 def self.client_for(key = Justa.default_client_key) k = Justa::Util.to_sym(key) instance unless @instance return nil unless @instance.authenticators.present? @instance.mutex.synchronize do auth = @instance.authenticators.find { |obj| obj.key == k } auth&.client end end |
.client_type_for(key = Justa.default_client_key) ⇒ Symbol
Find a Client Type for a specific Key
125 126 127 |
# File 'lib/justa/token_manager.rb', line 125 def self.client_type_for(key = Justa.default_client_key) client_for(key)&.type || :pdv end |
.instance ⇒ Object
129 130 131 132 133 |
# File 'lib/justa/token_manager.rb', line 129 def self.instance return @instance if @instance @instance = TokenManager.new end |
.token_for(key = Justa.default_client_key) ⇒ String
Find a token for a specific Client Key
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/justa/token_manager.rb', line 67 def self.token_for(key = Justa.default_client_key) instance unless @instance k = Justa::Util.to_sym(key) raise MissingCredentialsError, "Missing credentials for key: '#{key}'" unless @instance.authenticators @instance.mutex.synchronize do auth = @instance.authenticators.find { |obj| obj.key == k } raise MissingCredentialsError, "Missing credentials for key: '#{key}'" if auth.blank? auth.token end end |
Instance Method Details
#setup_autenticators(tokens) ⇒ Array
Sets authenticators based on tokens passed by constructor
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/justa/token_manager.rb', line 48 def setup_autenticators(tokens) return @authenticators if @authenticators tokens = tokens.map { |t| Justa::Client.new(**t) } @mutex.synchronize do @authenticators = [] tokens.each do |client| @authenticators << Authenticator.new(client) end end end |