# `ReqDPoP`
[🔗](https://github.com/XukuLLC/req_dpop/blob/v0.8.0/lib/req_dpop.ex#L1)

Req plugin for OAuth 2.0 DPoP client proof generation.

Attach the plugin to a `Req.Request` with `attach/2`:

    key = ReqDPoP.Key.generate(:es256)

    client =
      Req.new(base_url: "https://api.example.com")
      |> ReqDPoP.attach(key: key, access_token: access_token)

    Req.get!(client, url: "/resource")

To use proof-only mode for token endpoint requests, omit `:access_token`.

Every hop of a request gets its own proof. Req re-runs the request pipeline
for its retry and redirect steps, and the plugin signs a fresh proof for the
hop's method and URL with a new `jti`. On an untrusted cross-origin redirect
the plugin follows Req's rule for credentials: Req drops the `Authorization`
header, and the plugin drops the `DPoP` header, in proof-only mode as well.
The redirect target receives neither the token nor a proof unless Req's
`redirect_trusted: true` is set.

## Target URI override

RFC 9449 section 4.2 defines `htu` as the target URI of the request the proof
is attached to, and by default the plugin signs the URL Req sends the request
to. Those two differ when a client connects to one address for a request
whose target is another, for example an internal address with the public
authority in the `Host` header. The server reconstructs the public URI
(section 4.3) and rejects a proof signed for the internal one.

`:origin` covers that case by replacing scheme, host and port and keeping the
request path:

    Req.new(base_url: "http://api.internal:4000", headers: [host: "api.example.com"])
    |> ReqDPoP.attach(key: key, access_token: access_token, origin: "https://api.example.com")

`:htu` takes a function of the request for anything `:origin` cannot express,
such as a public path prefix:

    ReqDPoP.attach(request, key: key, htu: fn request -> "https://api.example.com/v1" <> request.url.path end)

An override is correct only when the request's target URI differs from the
URL it is sent to. A server that rejects a correct proof because it
reconstructs its own URI wrongly has a bug in its own configuration; signing
the URI that server expects hides the bug and binds the proof to a URI the
request was not made to.

The override applies to requests sent to the origin of the first hop,
including nonce retries, Req retries and same-origin redirects. A hop to any
other origin, which the plugin signs only under `redirect_trusted: true`,
signs its own URL.

# `alg`

```elixir
@type alg() :: :es256 | :rs256
```

# `attach_option`

```elixir
@type attach_option() ::
  proof_option()
  | {:htu, htu_fun()}
  | {:origin, binary()}
  | {:retry_on_nonce, boolean()}
  | {:max_nonce_retries, non_neg_integer()}
```

# `clock`

```elixir
@type clock() :: (-&gt; integer()) | (Req.Request.t() -&gt; integer())
```

# `htu_fun`

```elixir
@type htu_fun() :: (Req.Request.t() -&gt; binary() | URI.t())
```

# `jti`

```elixir
@type jti() :: (-&gt; binary()) | (Req.Request.t() -&gt; binary())
```

# `proof_option`

```elixir
@type proof_option() ::
  {:key, ReqDPoP.Key.t() | JOSE.JWK.t() | map()}
  | {:access_token,
     binary() | (-&gt; binary() | nil) | (Req.Request.t() -&gt; binary() | nil)}
  | {:nonce,
     binary() | (-&gt; binary() | nil) | (Req.Request.t() -&gt; binary() | nil)}
  | {:clock, clock()}
  | {:jti, jti()}
  | {:alg, alg()}
```

# `attach`

```elixir
@spec attach(Req.Request.t(), [attach_option()]) :: Req.Request.t()
```

Attaches DPoP proof generation to a `Req.Request`.

Calling `attach/2` again on the same request replaces the configuration
without adding a second set of steps.

Options:

  * `:key` - required DPoP private key. Accepts `ReqDPoP.Key`, `JOSE.JWK`,
    or a JWK map. P-256 EC keys sign with ES256 and RSA keys of at least
    2048 bits sign with RS256. See `ReqDPoP.Key.load!/1`.
  * `:access_token` - optional token string or function. When present, the
    plugin adds `Authorization: DPoP ...` and computes the proof `ath` claim.
    An empty string is treated as no token. The function runs once per
    request; retries and redirects reuse the token the first hop sent. Do
    not combine with Req's `:auth` option, which also sets `Authorization`.
  * `:nonce` - optional static nonce or function. A nonce received in a
    server challenge takes precedence for the origin that sent it.
  * `:retry_on_nonce` - retries once on DPoP nonce challenges by default.
  * `:max_nonce_retries` - defaults to `1`, counted per origin.
  * `:clock` - injectable Unix-second clock for tests.
  * `:jti` - injectable JTI generator for tests.
  * `:alg` - optional. Must match the key type: `:es256` for P-256 EC keys,
    `:rs256` for RSA keys.
  * `:origin` - optional `http` or `https` origin, such as
    `"https://api.example.com"`, signed into `htu` in place of the scheme,
    host and port of the URL the request is sent to. The request path is
    kept. See "Target URI override" in the module documentation.
  * `:htu` - optional one-argument function that receives the `Req.Request`
    and returns the `htu` to sign, as a string or `URI`. The result is
    normalized like any other `htu`: query and fragment are dropped. A
    static string is rejected, because it would be signed into every request
    the client makes whatever its path. Cannot be combined with `:origin`.

# `proof`

```elixir
@spec proof([proof_option() | {:htm, binary() | atom()} | {:htu, binary() | URI.t()}]) ::
  {:ok, binary()} | {:error, ReqDPoP.Error.t()}
```

Builds a compact DPoP proof JWT.

Required options are `:key`, `:htm`, and `:htu`. `:access_token` adds `ath`;
`:nonce` adds `nonce`. `:htu` must be an absolute `http` or `https` URI.

# `proof!`

```elixir
@spec proof!([
  proof_option() | {:htm, binary() | atom()} | {:htu, binary() | URI.t()}
]) :: binary()
```

Builds a compact DPoP proof JWT or raises `ReqDPoP.Error`.

---

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