AttestoPhoenix.Controller.RegistrationController (AttestoPhoenix v5.0.0)

Copy Markdown View Source

OAuth 2.0 Dynamic Client Registration endpoint (RFC 7591 §3).

Handles POST /oauth/register and, at the client configuration endpoint /oauth/register/:client_id (RFC 7592 §2), GET (read), PUT (update) and DELETE. This module owns the HTTP and protocol-framing concerns only: it enforces TLS (require_https), checks Content-Type: application/json, extracts the parsed JSON request body or Bearer registration access token off the Plug.Conn, delegates metadata validation, credential minting, and host persistence to the conn-free AttestoPhoenix.AuthorizationServer.Registration core, dispatches the returned :client_registered audit event, and renders the RFC 7591 §3.2.1 client information response or the RFC 7591 §3.2.2 error body.

Disabled by default

Dynamic registration is an open door: a successful request mints a new client from an otherwise unauthenticated POST. The library therefore mounts this endpoint only when the host opts in (AttestoPhoenix.Router's :registration option) AND supplies a :register_client callback (AttestoPhoenix.Config raises at boot otherwise). Any admission control the host wants in front of registration - a registration access token (RFC 7591 §3), an allowlist, rate limiting - lives in the host pipeline ahead of this action; the library does not assume one.

Wire contract

POST /oauth/register with application/json: the request body is a JSON client-metadata document (RFC 7591 §3.1). Any other Content-Type is rejected as invalid_client_metadata rather than parsed through an unintended path. A metadata document carries nested arrays (redirect_uris, grant_types) that have no canonical form-encoded representation, so no form encoding is offered here.

Recognised metadata members (RFC 7591 §2) include redirect_uris, grant_types, token_endpoint_auth_method, and a space-delimited scope string. The request is validated member by member against the server's policy inputs - the scope catalog (AttestoPhoenix.Config's :scopes_supported), the supported grant types, and the supported token-endpoint auth methods - and the first failure is returned.

An omitted application_type uses the host's :registration_default_application_type profile, which defaults to "web". Explicit client values are preserved. Native HTTP loopback registrations honor the configured :native_apps loopback and localhost policies.

Human-readable client metadata remains untrusted after registration. It is stored verbatim after size, UTF-8, and control-character checks; a host consent or administration UI must HTML-escape it at the rendering sink. Logout callback URIs use an intentional HTTPS-only server profile even where an OpenID logout specification permits a confidential deployment to allow plain HTTP.

Issued credentials

AttestoPhoenix.AuthorizationServer.Registration mints the client_id and (for client_secret_basic or client_secret_post) a high-entropy client_secret via Attesto.Secret (RFC 6749 §2.3.1 high-entropy secret). The plaintext secret appears in the RFC 7591 §3.2.1 response exactly once, accompanied by the REQUIRED client_secret_expires_at (0, non-expiring); only its one-way hash is handed to the host for persistence, so a leaked client store yields no usable secret.

Responses

Success renders HTTP 201 with the RFC 7591 §3.2.1 client information response (the registered metadata plus the synthesised client_id, the optional client_secret with its REQUIRED client_secret_expires_at, and client_id_issued_at). Failure renders the RFC 7591 §3.2.2 error body ({"error": code, "error_description": ...}) with the RFC 7591 §3.2.2 codes invalid_redirect_uri and invalid_client_metadata. A host store rejection surfaces as invalid_client_metadata (the request named a client the store would not accept) rather than a 500, unless the host chose the answer: a 4xx AttestoPhoenix.OAuthError, or {:unavailable, retry_after_seconds} for 503 with Retry-After. The metadata in a success response is read back from the stored client when :client_registration_metadata is configured. Both success and error responses carry no-store cache headers (RFC 7234 §5.2) because the body can carry a freshly minted credential.

An authenticated GET or PUT without the required management callbacks returns 405 Method Not Allowed with an empty body and an Allow header (RFC 9110 §15.5.6, §10.2.1). The header lists configured, mounted GET, PUT and DELETE methods for this client resource. POST belongs to the collection endpoint and is not included. Missing or invalid registration access tokens still return 401 invalid_token (RFC 7592 §2). Hosts can omit read and update entirely with the router's registration_read: false and registration_update: false options.

Event

A successful registration emits a :client_registered event (RFC 7591) through AttestoPhoenix.Event carrying the issued client_id. The plaintext secret is never placed on the event.

Summary

Functions

Dynamic client registration action (RFC 7591 §3.1).

Dynamic client registration management delete action (RFC 7592 §2).

Client read action (RFC 7592 §2.1): GET /oauth/register/:client_id with the registration access token. Renders the client's current registration.

Client update action (RFC 7592 §2.2): PUT /oauth/register/:client_id with the registration access token and the client's complete new metadata as application/json. Renders the updated registration.

Functions

create(conn, params)

@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()

Dynamic client registration action (RFC 7591 §3.1).

Validates the client-metadata document, mints the client's credentials, persists via the host callback, and renders either the RFC 7591 §3.2.1 client information response or an RFC 7591 §3.2.2 error. Every response carries no-store cache headers (RFC 7234 §5.2).

delete(conn, map)

@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t()

Dynamic client registration management delete action (RFC 7592 §2).

Deletes a previously registered client at its client configuration endpoint (RFC 7592 §2.3). A host must wire both :client_registration_access_token_hash and :unregister_client; absent either callback, the endpoint fails closed.

show(conn, map)

@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()

Client read action (RFC 7592 §2.1): GET /oauth/register/:client_id with the registration access token. Renders the client's current registration.

A host must wire :client_registration_access_token_hash and :client_registration_metadata. Without the token callback authentication fails; without the metadata callback an authenticated request receives 405.

update(conn, map)

@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()

Client update action (RFC 7592 §2.2): PUT /oauth/register/:client_id with the registration access token and the client's complete new metadata as application/json. Renders the updated registration.

A host must wire :client_registration_access_token_hash, :client_registration_metadata and :update_client. Without the token callback authentication fails; without either management callback an authenticated request receives 405 before its metadata is inspected.