WardrikCore.Credentials behaviour (wardrik_core v0.1.0-rc.3)

Copy Markdown

Gives tools the tokens of the person a turn acts for. For your own API keys, see WardrikCore.Keys.

defmodule MyApp.Vault do
  @behaviour WardrikCore.Credentials

  @impl true
  def resolve(%{principal: nil}, _opts), do: {:error, "There's no one to act for."}

  def resolve(%{name: name, principal: %{id: id}}, _opts) do
    case MyApp.Tokens.get(id, name) do
      nil -> {:authorize, "https://app.example.com/connect/#{name}"}
      token -> {:ok, token}
    end
  end
end

Give it to an agent as credentials: {MyApp.Vault, opts}. A tool fetches what it needs:

with {:ok, token} <- WardrikCore.Credentials.fetch(context, "github") do
  GitHub.create_issue(token, title)
end
  • {:authorize, url} parks the call on a WardrikCore.Authorization.
  • Call refused/2 when a service rejects a token, then fetch again.
  • Tokens are never saved or emitted. Keep them out of private data and attributes.

See the Credentials guide.

Summary

Callbacks

Called when a service refused the credential the request names. Optional.

Returns the credential the request names: {:ok, secret}, {:authorize, url} for a sign-in first, or {:error, reason}.

Functions

Fetches the credential name for whoever the turn acts for.

Tells the credentials module that a service refused the credential name.

Callbacks

refused(t, opts)

(optional)
@callback refused(WardrikCore.Credentials.Request.t(), opts :: term()) :: :ok

Called when a service refused the credential the request names. Optional.

resolve(t, opts)

@callback resolve(WardrikCore.Credentials.Request.t(), opts :: term()) ::
  {:ok, secret :: term()} | {:authorize, url :: String.t()} | {:error, term()}

Returns the credential the request names: {:ok, secret}, {:authorize, url} for a sign-in first, or {:error, reason}.

Functions

fetch(context, name)

@spec fetch(WardrikCore.Context.t(), String.t()) ::
  {:ok, term()} | {:park, WardrikCore.Authorization.t()} | {:error, term()}

Fetches the credential name for whoever the turn acts for.

Returns {:park, authorization} if the person must sign in first, and {:error, reason} if the agent has no credentials module.

refused(context, name)

@spec refused(WardrikCore.Context.t(), String.t()) :: :ok

Tells the credentials module that a service refused the credential name.