Current section
Files
Jump to
Current section
Files
README.md
# ReqDPoP
[](https://hex.pm/packages/req_dpop)
[](https://hexdocs.pm/req_dpop)
[](LICENSE)
ReqDPoP is a small [Req](https://hex.pm/packages/req) plugin for OAuth 2.0
DPoP proof generation as defined by [RFC 9449](https://datatracker.ietf.org/doc/html/rfc9449).
It is not an OAuth provider, token store, or full OAuth client. It does not
fetch tokens for you. It only signs DPoP proofs for outgoing Req requests and,
when configured, attaches a DPoP-bound access token.
## Installation
```elixir
def deps do
[
{:req_dpop, "~> 0.10"}
]
end
```
## Resource Request
```elixir
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")
```
The plugin adds:
- `DPoP: <proof-jwt>`
- `Authorization: DPoP <access_token>`
- `ath` in the proof when an access token is present
`htu` and `htm` are derived from the final Req request URL and method after Req
has applied base URL and path options. Per RFC 9449, `htu` excludes the request
URL's query string and fragment.
### Target URI override
When a request is sent to one address and its target URI is another, for
example an internal address with the public authority in `Host`, the URL Req
sends to is not the URI the server checks the proof against (RFC 9449 sections
4.2 and 4.3). `:origin` signs a different scheme, host and port and keeps the
request path:
```elixir
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")
|> Req.get!(url: "/resource")
# htu: https://api.example.com/resource
```
`:htu` takes a one-argument function of the request and returns the `htu` to
sign, for a mapping `:origin` cannot express. A fixed string is rejected: it
would be signed into every request the client makes.
Use an override 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 configuration bug, and signing the URI it expects hides that
bug. The override applies to the origin of the first hop, including nonce
retries; a redirect hop to another origin signs its own URL.
## Token Endpoint Proof
Use proof-only mode by omitting `:access_token`:
```elixir
Req.new()
|> ReqDPoP.attach(key: key)
|> Req.post!(url: "https://auth.example.com/oauth/token", form: params)
```
## Nonce Retry
By default, ReqDPoP retries once when a response carries a single `DPoP-Nonce`
header with an RFC 9449 nonce value and is a nonce challenge in either form:
- authorization server (section 8): status `400` with a JSON body of
`{"error": "use_dpop_nonce"}`, on proof-only requests
- resource server (section 9): status `401` with a `WWW-Authenticate` header
whose `DPoP` challenge carries `error="use_dpop_nonce"`
The retry proof includes the server nonce. Nonces and the retry budget are
tracked per origin. Configure this behavior with `:retry_on_nonce` and
`:max_nonce_retries`.
A compressed challenge body is decoded only when the request was made with
Req's `compressed: true`, the same opt-in Req applies to every response body.
## Redirects and Retries
Each hop of a request gets its own proof for the hop's method and URL with a
fresh `jti`, including hops made by Req's `retry` and `redirect` steps.
On an untrusted cross-origin redirect, Req removes the `Authorization` header.
ReqDPoP then removes the `DPoP` header as well, in proof-only mode too, so the
redirect target receives neither the token nor a proof. Set Req's
`redirect_trusted: true` to send both to the new origin.
## Key Persistence
Generated keys are process-local values. Production clients should persist the
DPoP key if they need stable sender binding across restarts:
```elixir
key = ReqDPoP.Key.generate(:es256)
jwk = ReqDPoP.Key.export(key)
key = ReqDPoP.Key.load!(jwk)
```
When both peers advertise PS256, generate an RSA-3072 PS256 key instead:
```elixir
key = ReqDPoP.Key.generate(:ps256)
jwk = ReqDPoP.Key.export(key) # includes "alg": "PS256"
key = ReqDPoP.Key.load!(jwk)
```
When both endpoints advertise a SHA-384 algorithm, generate the matching key
directly:
```elixir
ec_key = ReqDPoP.Key.generate(:es384) # P-384
rsa_key = ReqDPoP.Key.generate(:ps384) # RSA-3072, includes "alg": "PS384"
```
## Signing Algorithms and Upgrading to 0.9.x
Supported DPoP proof signing algorithms are `:es256` (P-256/SHA-256), `:es384`
(P-384/SHA-384), `:ps256` (RSA-PSS/SHA-256), `:ps384`
(RSA-PSS/SHA-384), and `:rs256` (RSASSA-PKCS1-v1_5/SHA-256):
- **Prefer a stronger negotiated classical option when peers advertise it.**
`ReqDPoP.Key.generate(:es384)` creates a P-384 key, and
`ReqDPoP.Key.generate(:ps256)` or `generate(:ps384)` creates an RSA-3072 key.
ES384 is the direct elliptic-curve hedge for deployments concerned about
256-bit curves; RSA-PSS is an independent classical alternative. None is
post-quantum protection. Keep `:es256` when peer support or an existing
registration requires it; `generate(:rs256)` remains RSA-2048 for
compatibility.
- **Select from server-advertised algorithms.** Choose an algorithm supported
by both the authorization server (`dpop_signing_alg_values_supported` in OAuth
2.0 server metadata, RFC 9449 section 5.1) and resource servers (the `algs`
parameter in `WWW-Authenticate: DPoP` challenges, RFC 9449 section 7.1). Not
every DPoP server accepts ES384, PS256, PS384, or RSA. The package does not
automatically switch existing keys or infer a preference from a challenge.
- **SHA-384 options are additive.** Existing ES256 and unlabeled-RSA defaults
do not change. Use ES384 or PS384 only after both endpoints advertise it;
generate a new key instead of relabeling an existing key.
- **Classical, not post-quantum.** ES256, ES384, PS256, PS384, and RS256 are all
classical signature algorithms and do not provide post-quantum security.
See `SECURITY.md` in the repository for deployment guidance.
When upgrading from 0.8 to 0.9.x:
- **Declared `"PS256"` begins signing PS256.** Before 0.9, a JWK's `"alg"`
member was ignored and every RSA key signed RS256. Starting in 0.9, an RSA
JWK carrying `"alg": "PS256"` loads as `:ps256` and signs PS256 proofs. If
you load persisted RSA JWKs with `"alg": "PS256"`, verify that your
authorization and resource servers advertise `"PS256"` in
`dpop_signing_alg_values_supported` and `WWW-Authenticate` `algs` before
upgrading.
- **Mismatched or unsupported declared `"alg"` values are rejected.** A JWK
whose `"alg"` member is unsupported or incompatible with its key type (for
example `"ES384"` on a P-256 key, `"ES256"` on a P-384 key, `"RS256"` on an
EC key, `"ES256"` or `"HS256"` on an RSA key, or a non-string/`nil` `"alg"`)
raises `ReqDPoP.Error` with reason
`:unsupported_key` at load time. A caller `:alg` option or
`%ReqDPoP.Key{alg: ...}` struct cannot override a JWK's declared `"alg"`
(`{:alg_mismatch, requested, declared}`).
- **Compatibility for unlabeled keys and direct structs.** Unlabeled RSA JWKs
continue to default to `:rs256` and allow opting into `alg: :ps256` or
`alg: :ps384`. Version 0.9.0 accidentally rejected a directly constructed
`%ReqDPoP.Key{jwk: jwk, alg: nil}`; 0.9.1 restores inference from the JWK while
enforcing any declared JWK `"alg"`. The RS256 default in a key returned by
`generate(:rs256)` or `load!(unlabeled_rsa_jwk)` remains selectable with
either RSA-PSS algorithm. A hand-built struct with a nondefault algorithm is
an explicit selection: it must agree with any declared JWK algorithm, and a
conflicting caller `alg:` option is rejected.
- **Preserve or pin an RSA algorithm deliberately.** A PS256 or PS384 choice
survives an export/load round trip only while the matching JWK `"alg"`
member is retained. Selecting RSA-PSS only through a hand-built
`%ReqDPoP.Key{jwk: unlabeled_jwk, alg: ...}` does not add that member when
`ReqDPoP.Key.export/1` runs. Formats such as PEM do not retain it either, so
the resulting unlabeled RSA key loads as RS256. For durable persistence,
store a JWK whose own `"alg"` is `"PS256"` or `"PS384"`; both RSA-PSS
generators do this. An intentionally unlabeled key must select the algorithm
again after every reload. To keep RS256 when importing a labeled RSA-PSS JWK,
remove or replace its `"alg"` member before loading instead of trying to
override the declaration.
## Security Notes
- Do not log access tokens or private JWKs.
- Do not store tokens globally.
- `ath` is SHA-256 over the ASCII access token, encoded as unpadded base64url.
- `htu` is the wire form of the target URI: lowercase scheme and host, `/` for
an empty path, and no userinfo, query, or fragment.
- The proof `jwk` header carries only the RFC 7638 thumbprint members of the
key. Other members of a loaded JWK map, such as `kid`, stay out of the proof.
- Keys are validated on load: a complete private P-256 or P-384 EC key, or a
private RSA key of at least 2048 bits. EC coordinates and private values must
have the exact curve width. A JWK's `alg`, when present, must agree with its
key type and curve. `inspect/1` of a key shows the algorithm only.
- A plain-string `:access_token` is kept out of `inspect/1` output for the
request. Passing a zero-arity function works as well.
- Req's `:auth` option and `:access_token` both set `Authorization`; setting
both raises.
- Supported signing algorithms are ES256/P-256, ES384/P-384, PS256 or
PS384/RSA-PSS, and RS256/RSA. When peers advertise them, ES384 with a newly
generated P-384 key and PS256 or PS384 with a newly generated RSA-3072 key
are stronger classical migration options. `generate(:rs256)` remains
RSA-2048 for compatibility. All are classical, not post-quantum. Choose an
algorithm advertised in
`dpop_signing_alg_values_supported` and `WWW-Authenticate: DPoP algs="..."`
challenges, retain ES256 when interoperability requires it, and treat ES384
and PS384 as additive options rather than automatic migrations.
## Development
The test suite includes server-side interop checks against Attesto's DPoP
verifier as a test-only dependency; Attesto is not required at runtime.
```sh
mix format --check-formatted
mix compile --warnings-as-errors
mix credo --strict
mix test
mix docs
mix hex.build
```
## License
MIT. See [LICENSE](LICENSE).