# Signed request objects

> The app signs its sign-in request with its own key, can encrypt it to the realm, and can have the answer posted back instead of placed in the address bar.

A signed request object is the app's sign-in request as a JWT signed with the app's own key. The realm reads only
what is inside the signed part, so no one handling the URL can add to or change what the app asked for, even when the
request travels through the browser. The app can also encrypt the object to the realm, so nothing that carries it
can read it, and can have the code posted back to it rather than placed in the address bar.

- **Sign** when the app must prove the request is exactly its own, even if it travels through the browser.
- **Encrypt** when the request carries details no one on the way should read, and it travels through the browser.
  Inside a [pushed request](../pushed-authorization-requests/) it already travels only between the app and the
  realm, so encryption adds little there.
- **Form post** when the app would rather not have the code in browser history or server logs.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) registers the client's algorithms and keys;
  `RealmViewer` can read them. The app signs each request itself.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the app holds a signing key whose public half is in its `jwks` or at its `jwksUri`.

## How it works

The app puts the sign-in parameters in the claims of a JWT, beside the claims that say who sent it, to whom, and for
how long:

```json
{
  "iss": "team-wiki",
  "aud": "https://foobar.cvyuh.com/oauth2",
  "client_id": "team-wiki",
  "nbf": <unix time>,
  "exp": <nbf + 300>,
  "jti": "4d6f1c2e-8b3a-4f9e-a1c7-5e2b9d0f7a31",
  "response_type": "code",
  "redirect_uri": "https://wiki.example.com/callback",
  "scope": "openid profile",
  "state": "af0ifjsldkj",
  "code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
  "code_challenge_method": "S256"
}
```

It signs the JWT with its own key, under the algorithm it registered, and sends it as `request`. The `client_id`
outside the object is still needed, because it tells the realm whose keys to check the signature with:

```http
GET /oauth2/authorize?client_id=team-wiki&request=eyJhbGciOiJFUzI1NiIsImtpZCI6Indpa2ktMSJ9.eyJpc3MiOi…
Host: foobar.cvyuh.com
```

The object can travel in the browser like this, or inside a [pushed request](../pushed-authorization-requests/).

**Encryption.** An encrypted request object is a signed one the app has also encrypted to the realm's own key. The
realm opens it first and then checks it exactly as a signed one; encryption never stands in for the signature. Every
realm that signs tokens holds an encryption key of its own, never its signing key. It is published in the realm's
key set (`/oauth2/jwks`) with `"use": "enc"` and `"alg": "ECDH-ES"`.

**Form post.** To have the code posted back, the app sends `response_mode=form_post`, or puts
`"response_mode": "form_post"` among the object's claims. Instead of a redirect with the code in the address bar, the
browser posts the code to the app's registered address. It needs no setting.

## Set it up

Set these on the client (`https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`; see [Call the API](../../../../develop/api/)):

| Where | Setting | What it does |
|---|---|---|
| Client | `requestObjectSigningAlg` | `ES256`, `RS256` or `PS256`. A client with `securityProfile` `fapi2` may name only `ES256` or `PS256`. Without it, signed requests are refused |
| Client | `jwks` or `jwksUri` | The client's public keys, to check the signature |
| Client | `requestObjectEncryptionAlg` | `ECDH-ES`, the one key agreement taken. Needs `requestObjectSigningAlg` |
| Client | `requestObjectEncryptionEnc` | `A128GCM`, `A256GCM` or `A128CBC-HS256`. Left out, `A128CBC-HS256`, as OpenID Connect Registration sets. Named only beside the key agreement |

## What the user sees

Nothing different with a good object. A refused one shows the realm's error page, with `invalid_request_object`;
the user goes back to the app and tries again. With form post, the browser shows a brief page that posts the code
to the app.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `requestObjectSigningAlg` | none: signed requests refused | `ES256`, `RS256`, `PS256` (`ES256`, `PS256` under `fapi2`) |
| `requestObjectEncryptionAlg` | none | `ECDH-ES` |
| `requestObjectEncryptionEnc` | `A128CBC-HS256` | `A128GCM`, `A256GCM`, `A128CBC-HS256` |
| Between `nbf` and `exp` | | at most an hour |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `request_not_supported` | The client registered no `requestObjectSigningAlg` | Register the algorithm the app signs with |
| `invalid_request_object` | `aud` isn't the issuer alone, `nbf` or `exp` is missing or over an hour apart, the key isn't registered, or a `jti` was reused | Check those claims; register the key |
| `invalid_request` | No `client_id` outside the object | Send `client_id` as a parameter too |

## Audit record

A request object writes no record of its own; the sign-in it starts writes `LOGIN` and its steps, and its code a
`TOKEN_ISSUED` record. Changes to the client's algorithms are in **Audit → Config**.

## Good to know

- **The object must be signed** with the algorithm the client registered, with the client's own key.
- **It must name the client as its sender and the realm as its only receiver.** `iss` is the client; `aud` is the
  realm's issuer as a string, or an array holding the issuer alone, never the issuer beside another audience.
- **It must carry both a start (`nbf`) and an expiry (`exp`)**, no more than an hour apart.
- **`client_id` goes outside the object as well.** A `client_id` inside the object that differs from it is refused.
- **An object that carries a `jti` is accepted once.**
- **An encrypted object is taken only from a client registered for encryption**, under the one content encryption it
  registered, and must hold a signed object. An unsigned one inside is refused. A client registered for encryption
  may still send its object signed only.
- **Every refused encrypted object gets the same answer**, `invalid_request_object`, whatever went wrong, so the
  answer says nothing about the key.
- **A form post goes only to an address the client registered.**
- **The realm's encryption key is made when the realm is created**; it is not rotated or revoked yet. The discovery
  document lists `request_object_encryption_alg_values_supported` and
  `request_object_encryption_enc_values_supported` only while the realm holds one and takes the authorization code
  grant.
- **RSA key encryption (`RSA-OAEP`) is not offered.**

## Reference

### Errors

At `/authorize`, a refused object is shown on the realm's error page. It is never sent to the app's return address,
since nothing in an object that failed its checks can be trusted. Inside a pushed request, the same error is the
push's answer.

| Error | When |
|---|---|
| `request_not_supported` | The client registered no `requestObjectSigningAlg` |
| `invalid_request_object` | The signature, a claim or the encryption doesn't check out; the `jti` was used before; or the inner `client_id` differs |
| `invalid_request` | The request carries no `client_id` outside the object |

## Related

- [Make sign-in requests tamper-proof](../../../guides/sign-in-to-apps/tighten-sign-in-requests/): signing and
  pushing requests, start to finish.
- [Pushed authorization requests](../pushed-authorization-requests/): keep the request out of the browser entirely.
- [Security profiles](../security-profiles/): which algorithms a FAPI 2.0 client may sign with.
- [Discovery document](../../registration/discovery/): where the realm's encryption key is published.
