Class: Kameleoon::Network::AccessTokenSource

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/network/access_token_source.rb

Constant Summary collapse

TOKEN_EXPIRATION_GAP =

in seconds

60
TOKEN_OBSOLESCENCE_GAP =

in seconds

1800
JWT_ACCESS_TOKEN_FIELD =
'access_token'
JWT_EXPIRES_IN_FIELD =
'expires_in'

Instance Method Summary collapse

Constructor Details

#initialize(network_manager, client_id, client_secret) ⇒ AccessTokenSource

Returns a new instance of AccessTokenSource.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kameleoon/network/access_token_source.rb', line 17

def initialize(network_manager, client_id, client_secret)
  Logging::KameleoonLogger.debug(lambda {
    format("CALL: AccessTokenSource.new(network_manager, client_id: '%s', client_secret: '%s')",
           Utils::Strval.secret(client_id), Utils::Strval.secret(client_secret))
  })
  @network_manager = network_manager
  @client_id = client_id
  @client_secret = client_secret
  @fetching = false
  Logging::KameleoonLogger.debug(lambda {
    format("RETURN: AccessTokenSource.new(network_manager, client_id: '%s', client_secret: '%s')",
           Utils::Strval.secret(client_id), Utils::Strval.secret(client_secret))
  })
end

Instance Method Details

#discard_token(token) ⇒ Object



44
45
46
47
48
49
# File 'lib/kameleoon/network/access_token_source.rb', line 44

def discard_token(token)
  Logging::KameleoonLogger.debug("CALL: AccessTokenSource.discard_token(token: '%s')", token)
  @cached_token = nil if @cached_token&.value == token
  Logging::KameleoonLogger.debug("RETURN: AccessTokenSource.discard_token(token: '%s')", token)
  @cached_token
end

#get_token(timeout = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kameleoon/network/access_token_source.rb', line 32

def get_token(timeout = nil)
  Logging::KameleoonLogger.debug('CALL: AccessTokenSource.getToken(timeout: %s)', timeout)
  now = Time.new.to_i
  token = @cached_token
  return call_fetch_token(timeout) if token.nil? || token.expired?(now)

  run_fetch_token if !@fetching && token.obsolete?(now)
  Logging::KameleoonLogger.debug("RETURN: AccessTokenSource.getToken(timeout: %s) -> (token: '%s')",
                                 timeout, token.value)
  token.value
end