ProtoRune.Security.TokenStore behaviour (proto_rune v0.5.1)

Copy Markdown

Behaviour for pluggable token storage backends.

A backend persists opaque, already-encrypted blobs keyed by an account identifier (typically the account DID). Backends never see plaintext tokens: encryption and decryption happen in ProtoRune.Security before the blob reaches the store.

ProtoRune.Security.TokenStore.Dets is the default implementation. Implement this behaviour to store tokens elsewhere (a database, a system keyring, Redis, etc):

defmodule MyApp.DBTokenStore do
  @behaviour ProtoRune.Security.TokenStore

  @impl true
  def put(id, blob, _opts), do: MyApp.Repo.upsert_token(id, blob)

  @impl true
  def fetch(id, _opts), do: MyApp.Repo.get_token(id)

  @impl true
  def delete(id, _opts), do: MyApp.Repo.delete_token(id)
end

Then pass {MyApp.DBTokenStore, []} as the store argument to the ProtoRune.Security functions.

Summary

Types

A storage backend and its options.

Account identifier used as the storage key, typically a DID.

Backend-specific options.

Callbacks

Deletes the entry stored under id. Deleting a missing entry returns :ok.

Fetches the blob stored under id.

Persists the encrypted blob under id, overwriting any existing entry.

Types

backend()

@type backend() :: {module(), opts()}

A storage backend and its options.

id()

@type id() :: String.t()

Account identifier used as the storage key, typically a DID.

opts()

@type opts() :: keyword()

Backend-specific options.

Callbacks

delete(id, opts)

@callback delete(id(), opts()) :: :ok | {:error, term()}

Deletes the entry stored under id. Deleting a missing entry returns :ok.

fetch(id, opts)

@callback fetch(id(), opts()) :: {:ok, binary()} | {:error, :not_found | term()}

Fetches the blob stored under id.

Returns {:error, :not_found} when no entry exists for id.

put(id, blob, opts)

@callback put(id(), blob :: binary(), opts()) :: :ok | {:error, term()}

Persists the encrypted blob under id, overwriting any existing entry.