Module: Auth0::Mixins::TokenManagement

Included in:
Auth0::Mixins
Defined in:
lib/auth0/mixins/token_management.rb

Instance Method Summary collapse

Instance Method Details

#get_tokenString

Note:

This method may perform a network request to refresh an expired token. It is not thread-safe.

Get the Client's api token (or generate a new one if it has expired).

Returns:

  • (String)

    the api token



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/auth0/mixins/token_management.rb', line 9

def get_token
  has_expired = @token && @token_expires_at ? @token_expires_at < (Time.now.to_i + 10) : false

  if (@token.nil? || has_expired) && @token_provider
    @token = @token_provider.call
    @token_expires_at = nil
    @token
  elsif (@token.nil? || has_expired) && @client_id && (@client_secret || @client_assertion_signing_key)
    response = api_token(audience: @audience)
    @token = response.token
    @token_expires_at = response.expires_in ? Time.now.to_i + response.expires_in : nil

    @token
  else
    @token
  end
end