PubsubGrpc.Auth (PubsubGrpc v0.4.2)
View SourceAuthentication module for Google Cloud Pub/Sub.
Handles token retrieval with ETS-based caching using multiple methods:
- Goth library (if available and configured)
- gcloud CLI fallback
- Returns structured error if no auth available
Configuration
To use Goth for authentication, add it to your supervision tree:
children = [
{Goth, name: MyApp.Goth, source: {:service_account, credentials}},
# ... other children
]Then configure PubsubGrpc to use your Goth instance:
config :pubsub_grpc, :goth, MyApp.Goth
Summary
Functions
Clears the cached authentication token.
Gets an authentication token for Google Cloud API calls.
Gets request options including authentication metadata.
Functions
@spec clear_cache() :: :ok
Clears the cached authentication token.
Useful when you need to force a token refresh, for example after rotating credentials.
@spec get_token() :: {:ok, String.t()} | {:error, PubsubGrpc.Error.t()}
Gets an authentication token for Google Cloud API calls.
Returns a token string in the format "Bearer <token>". Tokens are cached in ETS and refreshed automatically when expired.
Returns
{:ok, "Bearer <token>"}- Token retrieved successfully{:error, %PubsubGrpc.Error{}}- Unable to get token
@spec request_opts() :: {:ok, keyword()} | {:error, PubsubGrpc.Error.t()}
Gets request options including authentication metadata.
In emulator mode, returns {:ok, []} (no auth needed).
In production, returns {:ok, [metadata: %{"authorization" => token}]}.
Returns
{:ok, keyword()}- Options to pass to gRPC stub functions{:error, %PubsubGrpc.Error{}}- Authentication failed