Class: StatelyDB::Common::Auth::Auth0TokenProvider
- Inherits:
-
TokenProvider
- Object
- TokenProvider
- StatelyDB::Common::Auth::Auth0TokenProvider
- 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
-
#access_token ⇒ String
Get the current access token.
-
#finalize ⇒ Proc
finalizer kills the thread running the timer if one exists.
-
#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
constructor
A new instance of Auth0TokenProvider.
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.
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_token ⇒ String
Get 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 |
#finalize ⇒ Proc
finalizer kills the thread running the timer if one exists
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 |