Class: StatelyDB::Common::Auth::Auth0TokenProvider

Inherits:
TokenProvider
  • Object
show all
Defined in:
lib/common/auth/auth0_token_provider.rb

Overview

Auth0TokenProvider is an implementation of the TokenProvider abstract base class which vends tokens from auth0 with the given client_id and client_secret. It will default to using the values of ‘STATELY_CLIENT_ID` and `STATELY_CLIENT_SECRET` if no credentials are explicitly passed and will throw an error if none are found.

Instance Method Summary collapse

Constructor Details

#initialize(auth_url: "https://oauth.stately.cloud", audience: "api.stately.cloud", client_secret: ENV.fetch("STATELY_CLIENT_SECRET"), client_id: ENV.fetch("STATELY_CLIENT_ID")) ⇒ Auth0TokenProvider

Returns a new instance of Auth0TokenProvider.

Parameters:

  • auth_url (String) (defaults to: "https://oauth.stately.cloud")

    The URL of the OAuth server

  • audience (String) (defaults to: "api.stately.cloud")

    The OAuth Audience for the token

  • client_secret (String) (defaults to: ENV.fetch("STATELY_CLIENT_SECRET"))

    The StatelyDB client secret credential

  • client_id (String) (defaults to: ENV.fetch("STATELY_CLIENT_ID"))

    The StatelyDB client ID credential



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/common/auth/auth0_token_provider.rb', line 28

def initialize(
  auth_url: "https://oauth.stately.cloud",
  audience: "api.stately.cloud",
  client_secret: ENV.fetch("STATELY_CLIENT_SECRET"),
  client_id: ENV.fetch("STATELY_CLIENT_ID")
)
  super()
  @client_id = client_id
  @client_secret = client_secret
  @audience = audience
  @auth_url = "#{auth_url}/oauth/token"
  @access_token = nil
  @pending_refresh = nil
  @timer = nil

  Async do |_task|
    refresh_token
  end

  # need a weak ref to ourself or the GC will never run the finalizer
  ObjectSpace.define_finalizer(WeakRef.new(self), finalize)
end

Instance Method Details

#access_tokenString

Get the current access token

Returns:

  • (String)

    The current access token



61
62
63
64
# File 'lib/common/auth/auth0_token_provider.rb', line 61

def access_token
  # TODO: - check whether or not the GIL is enough to make this threadsafe
  @access_token || refresh_token
end

#finalizeProc

finalizer kills the thread running the timer if one exists

Returns:

  • (Proc)

    The finalizer proc



53
54
55
56
57
# File 'lib/common/auth/auth0_token_provider.rb', line 53

def finalize
  proc {
    Thread.kill(@timer) unless @timer.nil?
  }
end