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, log_func) ⇒ AccessTokenSource

Returns a new instance of AccessTokenSource.



15
16
17
18
19
20
21
# File 'lib/kameleoon/network/access_token_source.rb', line 15

def initialize(network_manager, client_id, client_secret, log_func)
  @network_manager = network_manager
  @client_id = client_id
  @client_secret = client_secret
  @fetching = false
  @log_func = log_func
end

Instance Method Details

#discard_token(token) ⇒ Object



32
33
34
# File 'lib/kameleoon/network/access_token_source.rb', line 32

def discard_token(token)
  @cached_token = nil if @cached_token&.value == token
end

#get_token(timeout = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/kameleoon/network/access_token_source.rb', line 23

def get_token(timeout = nil)
  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)
  token.value
end