AshVault.Macaroon.Caveat behaviour (AshVault v0.2.0)

Copy Markdown View Source

Behaviour for a caveat check: decides whether one caveat value admits the request.

caveat :ip, :string, check: MyApp.Caveats.Ip
caveat :ip, :string, check: {MyApp.Caveats.Ip, trust_proxy?: true}
caveat :ip, :string, check: fn ip, ctx -> ctx.context[:remote_ip] == ip end

check/3 returns :ok or true to admit, and anything else — false, {:error, reason}, nil — to refuse. Refusal is the default for every answer that is not an explicit yes.

Checks must only narrow

Every caveat a token carries must admit the request, so appending a caveat can only shrink what the token allows — provided each check depends only on its own value and the request. A check must never widen access because of its value (an admin: true caveat that grants more), and must never consult other caveats. That is what makes AshVault.Macaroon.attenuate/2 safe to hand to token holders.

A check that raises fails the request; it is not rescued.

Summary

Callbacks

Decide whether value admits the request described by context.

Callbacks

check(value, context, opts)

@callback check(
  value :: term(),
  context :: AshVault.Macaroon.CheckContext.t(),
  opts :: keyword()
) ::
  :ok | boolean() | {:error, term()}

Decide whether value admits the request described by context.