Class: Justa::TokenManager

Inherits:
Object show all
Defined in:
lib/justa/token_manager.rb

Overview

Class to manage Tokens with singleton structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokenManager

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

#authenticatorsObject (readonly)

Returns the value of attribute authenticators.



6
7
8
# File 'lib/justa/token_manager.rb', line 6

def authenticators
  @authenticators
end

#mutexObject (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

Examples:

Ads a new client to be used in calls to Justa Api

Justa::TokenManager.add_client Client.new(client_id: <CLIENT_KEY>, key: :<CLIENT_ALIAS>)

Parameters:

  • client (Justa::Client)

    Client instance to be registered in TokenManager

Returns:

  • (Array)

    Authenticators array

Raises:



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

Parameters:

  • key (Symbol) (defaults to: Justa.default_client_key)

    Client Key to be found in Authenticators Array ( Defaults to Justa.default_client_key)

Returns:

  • (Justa::Client)

    Client instance registed with the key passed



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

Parameters:

  • key (Symbol) (defaults to: Justa.default_client_key)

    Client Key to be found in Authenticators Array ( Defaults to Justa.default_client_key)

Returns:

  • (Symbol)

    Return the cleint type ( :pdv or :e_commerce) ( Defaults to :pdv if not found)



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

.instanceObject



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

Parameters:

  • key (Symbol) (defaults to: Justa.default_client_key)

    Client Key to be found in Authenticators Array

Returns:

Raises:



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

Parameters:

  • tokens (Array)

    Array of tokens to be registered as clients

Returns:

  • (Array)

    Authenticators array



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