GcpCompute.TokenProvider behaviour (GcpCompute v0.2.0)

Copy Markdown View Source

Behaviour for minting the OAuth2 access tokens used to authenticate Compute API requests.

The library never hard-depends on a particular auth mechanism. A GcpCompute.Config holds a {module, arg} pair; before each request the configured module.fetch_token(arg) is invoked.

Built-in implementations:

Writing your own

defmodule MyApp.WorkloadIdentity do
  @behaviour GcpCompute.TokenProvider

  @impl true
  def fetch_token(_arg) do
    # ... fetch from the metadata server, Vault, etc.
    {:ok, %{token: token, expires: unix_seconds}}
  end
end

then GcpCompute.Config.new(token_provider: {MyApp.WorkloadIdentity, []}, ...).

Summary

Types

A minted token. Only :token is required; :type and :expires are advisory.

Callbacks

Return a valid access token, or an error.

Types

token()

@type token() :: %{
  :token => String.t(),
  optional(:type) => String.t(),
  optional(:expires) => non_neg_integer()
}

A minted token. Only :token is required; :type and :expires are advisory.

Callbacks

fetch_token(arg)

@callback fetch_token(arg :: term()) :: {:ok, token()} | {:error, term()}

Return a valid access token, or an error.

Implementations are expected to cache/refresh internally (as Goth does) — fetch_token/1 is called on every request and must be cheap on the hot path.