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

Dynamic client registration and management (RFC 7591 / RFC 7592), as conn-free
core.

Turns a parsed RFC 7591 §2 client-metadata document and `%AttestoPhoenix.Config{}`
into either an RFC 7591 §3.2.1 client information response (together with the
`:client_registered` audit event) or an `AttestoPhoenix.OAuthError`. Also
handles RFC 7592 §2.3 client deletion (`unregister/3`) given the client ID and
presented registration access token.

## North star

`AttestoPhoenix.Controller.RegistrationController` owns the HTTP framing only:
it enforces TLS (`require_https`), checks `Content-Type: application/json`,
extracts the parsed JSON body or `Bearer` registration access token off the
`Plug.Conn`, calls `register/2` or `unregister/3`, dispatches the returned
audit events, and renders the HTTP response. This module reads only data,
never touches a `Plug.Conn`, and returns events as data.

# `authorize_management`

```elixir
@spec authorize_management(AttestoPhoenix.Config.t(), String.t(), String.t() | nil) ::
  {:ok, term(), String.t()} | {:error, AttestoPhoenix.OAuthError.t()}
```

Authorize a request to a client's configuration endpoint (RFC 7592 §2):
registration is enabled, `token` is the registration access token issued
with `client_id`, and the client still exists.

The one check behind read, update and delete. Returns `{:ok, client,
token}` or the RFC 7592 §2 `401 invalid_token` error, which does not say
whether the client exists.

# `check_enabled`

```elixir
@spec check_enabled(AttestoPhoenix.Config.t()) ::
  :ok | {:error, AttestoPhoenix.OAuthError.t()}
```

Check whether dynamic client registration is enabled and wired on `config`.

# `read`

```elixir
@spec read(AttestoPhoenix.Config.t(), String.t(), String.t() | nil) ::
  {:ok, map()} | {:error, AttestoPhoenix.OAuthError.t()}
```

Read a client's current registration (RFC 7592 §2.1).

Returns `{:ok, client_information_response}`: the registered metadata from
the host's `:client_registration_metadata` callback, with `client_id`,
`registration_client_uri` and `registration_access_token`. The token is the
one presented; it is not rotated. `client_secret` is not returned, because
only its hash is stored.

Fails closed when `:client_registration_metadata` is not configured.

# `register`

```elixir
@spec register(
  AttestoPhoenix.Config.t(),
  AttestoPhoenix.AuthorizationServer.Registration.Request.t() | map()
) ::
  {:ok, map(), [AttestoPhoenix.Event.t()]}
  | {:error, AttestoPhoenix.OAuthError.t()}
```

Validate a client-metadata document, mint credentials, and persist via the
host's `:register_client` callback.

Returns `{:ok, client_information_response, events}` or
`{:error, %AttestoPhoenix.OAuthError{}}`.

# `unregister`

```elixir
@spec unregister(AttestoPhoenix.Config.t(), String.t(), String.t() | nil) ::
  :ok | {:error, AttestoPhoenix.OAuthError.t()}
```

Delete a previously registered client (RFC 7592 §2.3) after verifying its
registration access token.

# `update`

```elixir
@spec update(AttestoPhoenix.Config.t(), String.t(), String.t() | nil, term()) ::
  {:ok, map(), [AttestoPhoenix.Event.t()]}
  | {:error, AttestoPhoenix.OAuthError.t()}
```

Replace a client's registered metadata (RFC 7592 §2.2).

`metadata` is the request document. It is the client's complete new
metadata: a member it omits is reset to its default, exactly as in a
registration request, and it is validated by the same rules as one. Beyond
those rules, per RFC 7592 §2.2:

  * `client_id` MUST be present and equal the client being updated.
  * `registration_access_token`, `registration_client_uri`,
    `client_secret_expires_at` and `client_id_issued_at` MUST NOT be present.
  * `client_secret`, when present, MUST be the client's current secret. The
    client cannot choose a secret.

Three members cannot be changed by an update and are refused when they
differ from the registered value: `token_endpoint_auth_method` (it decides
whether the client has a secret, and this server does not issue or retire
one on update), `subject_type`, and, for a pairwise client, the host its
redirect URIs share (either change would alter every `sub` the client has
been given).

The registration access token and the client secret are not rotated.
Returns `{:ok, client_information_response, events}`. Fails closed when
`:client_registration_metadata` or `:update_client` is not configured.

---

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