AttestoPhoenix.Plug.CORS (AttestoPhoenix v5.0.0)

Copy Markdown View Source

Cross-Origin Resource Sharing (CORS) support for authorization-server and protected-resource endpoints (RFC 10017 §6.3.3.4).

Browser-based OAuth clients make cross-origin fetch() calls to metadata, JWKS, token, revocation, introspection, dynamic registration, UserInfo, and protected-resource endpoints, including OPTIONS preflight requests when sending Authorization, DPoP, or Content-Type: application/json headers.

Policy resolution order:

  1. The :origins option passed to the plug or helper.
  2. :cors_origins on conn.private[:attesto_phoenix_config].
  3. Default :* (wildcard Access-Control-Allow-Origin: *, never paired with Access-Control-Allow-Credentials).

Set cors_origins: false to disable CORS headers and refuse preflights through the router's normal missing-route handling, or configure an explicit list of allowed origins (["https://app.example"]) or a 1-arity predicate callback ((origin -> boolean())). When an allowlist or callback is used, responses vary by Origin (Vary: origin) and only echo a matching origin.

Summary

Functions

Applies CORS response headers to conn according to the active policy.

Answers an HTTP OPTIONS CORS preflight request with 204 No Content and halted connection state. When the effective policy is false, raises Phoenix.Router.NoRouteError with the connection and router. Outside a Phoenix router, sends an empty 404 response instead.

Functions

apply_headers(conn, opts \\ [])

@spec apply_headers(Plug.Conn.t(), keyword()) :: Plug.Conn.t()

Applies CORS response headers to conn according to the active policy.

Existing access-control-allow-origin headers set earlier in the pipeline are preserved.

preflight(conn, opts \\ [])

@spec preflight(Plug.Conn.t(), keyword()) :: Plug.Conn.t()

Answers an HTTP OPTIONS CORS preflight request with 204 No Content and halted connection state. When the effective policy is false, raises Phoenix.Router.NoRouteError with the connection and router. Outside a Phoenix router, sends an empty 404 response instead.