# `AttestoPhoenix.AuthorizationServer.Continuation`
[🔗](https://github.com/XukuLLC/attesto_phoenix/blob/v5.0.0/lib/attesto_phoenix/authorization_server/continuation.ex#L1)

Runs a host continuation wrapper and verifies what it returns, as conn-free
core.

Three host hooks share one shape, `(context, continuation -> result)`:
`:authorization_code_issuance`, `:authorization_code_completion` and
`:refresh_completion`. The host receives a secret-free `context` and a
zero-arity `continuation` that performs the library's work, and may run it
inside its own database transaction and locks. This module is the one
implementation of the rules all three follow:

  * the continuation runs at most once, only in the process that received
    it, and only while the callback is running;
  * the callback must return the continuation's result unchanged. A digest
    and outcome of that result are recorded, without keeping the result
    itself in process state, so a callback that never ran the continuation
    cannot pass off a success it assembled, and a substituted success is
    refused;
  * the single `{:ok, _}` wrapper `Repo.transaction/1` puts around a
    committed return is accepted and unwrapped;
  * a host `{:error, %AttestoPhoenix.OAuthError{}}` is honored; any other
    substituted return becomes the hook's fixed refusal;
  * nothing from the exception, the context or the result is ever logged,
    because any of them may hold host secrets.

A caller describes its hook with a `t:profile/0` (the names used in log
lines, what counts as success, which results are well formed, and the
refusal to answer with) and translates the normalized result. No protocol
decision about codes or tokens lives here.

# `profile`

```elixir
@type profile() :: %{
  callback: String.t(),
  continuation: String.t(),
  work: String.t(),
  not_done: String.t(),
  no_success: String.t(),
  no_response: String.t(),
  after_success: String.t(),
  state_key: {module(), atom()},
  succeeded?: (term() -&gt; boolean()),
  well_formed?: (term() -&gt; boolean()),
  refusal: term()
}
```

What distinguishes one hook from another.

  * `:callback` - the hook's name in log lines, e.g.
    `"authorization code completion callback"`.
  * `:continuation` - the continuation's name in log lines.
  * `:work` - the continuation's work as a noun, e.g.
    `"authorization code completion"`.
  * `:not_done` - what did not happen when the hook is refused before any
    work, e.g. `"no token was issued"`.
  * `:no_success` - what was withheld when a success cannot be trusted, e.g.
    `"no successful token response was returned"`.
  * `:no_response` - what was withheld after a failed continuation, e.g.
    `"completion did not succeed and no token response was returned"`.
  * `:after_success` - the consequence of the callback failing after the
    continuation succeeded, completing "...after the continuation
    succeeded; ".
  * `:state_key` - `{module, name}` naming the hook's private
    process-dictionary entry.
  * `:succeeded?` - whether a continuation result is a success.
  * `:well_formed?` - whether a result is one the continuation may produce.
  * `:refusal` - the fixed result returned for a contract violation.

# `run`

```elixir
@spec run(profile(), (list() -&gt; term()), term(), (-&gt; term())) :: term()
```

Invoke `callback` with `context` and a guarded continuation around
`work`, and return the normalized result.

`invoke` applies the host callback to `[context, continuation]`; the caller
passes its own callback invoker so this module needs no knowledge of
callback forms. Exceptions, throws and exits from the callback propagate
after a secret-free log line when the continuation had already started.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
