Class: StatelyDB::Common::Auth::AuthTokenProvider

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

Overview

AuthTokenProvider is an implementation of the TokenProvider abstract base class which vends tokens from the StatelyDB auth API. It will default to using the value of ‘STATELY_ACCESS_KEY` if no credentials are explicitly passed and will throw an error if no credentials are found.

Defined Under Namespace

Classes: Actor, TokenState

Instance Method Summary collapse

Constructor Details

#initialize(access_key: ENV.fetch("STATELY_ACCESS_KEY", nil), base_retry_backoff_secs: 1) ⇒ AuthTokenProvider

Returns a new instance of AuthTokenProvider.

Parameters:

  • access_key (String) (defaults to: ENV.fetch("STATELY_ACCESS_KEY", nil))

    The StatelyDB access key credential

  • base_retry_backoff_secs (Float) (defaults to: 1)

    The base retry backoff in seconds



29
30
31
32
33
34
35
36
# File 'lib/common/auth/auth_token_provider.rb', line 29

def initialize(
  access_key: ENV.fetch("STATELY_ACCESS_KEY", nil),
  base_retry_backoff_secs: 1
)
  super()
  @access_key = access_key
  @base_retry_backoff_secs = base_retry_backoff_secs
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the token provider and kill any background operations This just invokes the close method on the actor which should do the cleanup



55
56
57
58
# File 'lib/common/auth/auth_token_provider.rb', line 55

def close
  @actor.close unless @actor.nil?
  @actor = nil
end

#get_token(force: false) ⇒ String

Get the current access token

Returns:

  • (String)

    The current access token



62
63
64
65
66
67
68
69
70
71
# File 'lib/common/auth/auth_token_provider.rb', line 62

def get_token(force: false)
  if @actor.nil?
    raise StatelyDB::Error.new(
      "Token provider has not been started. Call start() before get_token().",
      code: GRPC::Core::StatusCodes::FAILED_PRECONDITION,
      stately_code: "FailedPrecondition"
    )
  end
  @actor.get_token(force: force)
end

#start(endpoint: "https://api.stately.cloud") ⇒ Object

Start the token provider. Starting multiple times is a no-op.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/common/auth/auth_token_provider.rb', line 39

def start(
  endpoint: "https://api.stately.cloud"
)
  # If the actor is already started, do nothing
  return unless @actor.nil?

  @actor = Async::Actor.new(Actor.new(endpoint:, access_key: @access_key,
                                      base_retry_backoff_secs: @base_retry_backoff_secs))
  # this initialization cannot happen in the constructor because it is async and must run on the event loop
  # which is not available in the constructor
  @actor.start
end