# Client assertion

A client assertion is a JSON Web Token (JWT) that the API client signs with its own private key (`private_key_jwt`, [RFC 7523](https://www.rfc-editor.org/rfc/rfc7523)). This guide shows how to set up client assertions, so that the API client can get access tokens from the ID Check authorisation server.

## Prerequisites

- An API client, with the client ID from Stø. See [Get access](https://docs.stoe.no/id-check.md#get-access).
- A JSON Object Signing and Encryption (JOSE) or JWT library that can sign a JWT with one of the algorithms in step 1.

## 1. Create a key pair

Create the key pair on the merchant's own systems, and keep the private key secret. The authorisation server supports these algorithms:

| Algorithm          | Key                              |
| ------------------ | -------------------------------- |
| `ES256`            | An EC key on the P-256 curve     |
| `PS256` or `RS256` | An RSA key of at least 2048 bits |
| `Ed25519`          | An OKP key on the Ed25519 curve  |

## 2. Register the public key

The authorisation server uses the public key to verify the signature of each client assertion. Give Stø the public key in one of these forms:

- **A public JSON Web Key (JWK)** ([RFC 7517](https://www.rfc-editor.org/rfc/rfc7517)). Send it to Stø, as [Support](https://docs.stoe.no/id-check.md#support) describes. Stø enrols it for the API client.
- **The URL of a JSON Web Key Set (JWKS).** Publish the public keys as a JWKS at an `https` URL that the merchant controls, and send the URL to Stø. The URL must use the default port, 443. It must return the JWKS with status 200, without a redirect. When Stø sets up the JWKS URL, it allows the host of the URL in the ID Check firewall. After that, the merchant can add and remove keys without contacting Stø.

Never send the private key to Stø.

Give each key a `kid`. If a key has an `alg` member, it must be the algorithm that the API client signs with. If a key has a `use` member, it must be `sig`.

The authorisation server caches the JWKS for 5 minutes. When a client assertion names a key that is not in the cached JWKS, the authorisation server fetches the JWKS again, but not within 30 seconds of the last fetch. If it cannot fetch the JWKS, the token request fails. It tries to fetch the JWKS again on the next token request.

## 3. Create a client assertion

The JWT has these header parameters and claims. Send each one that the table does not mark as optional:

| Member | Value                                                                                                                                                                                                            |
| ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `alg`  | `ES256`, `PS256`, `RS256` or `Ed25519`. For an Ed25519 key, use `Ed25519`, not `EdDSA`.                                                                                                                          |
| `kid`  | The `kid` of the key. Always send it, so that the authorisation server can find the key when there is more than one.                                                                                             |
| `typ`  | Optional. `client-authentication+jwt` (recommended) or `JWT`.                                                                                                                                                    |
| `iss`  | The client ID.                                                                                                                                                                                                   |
| `sub`  | The client ID.                                                                                                                                                                                                   |
| `aud`  | The `issuer` in the [authorisation server metadata](https://docs.stoe.no/id-check/authentication.md#authorisation-server-metadata), as a single string. In production, this is `https://prod.idcheck.bankid.no`. |
| `exp`  | The expiry time. Not more than 60 seconds in the future.                                                                                                                                                         |
| `iat`  | Optional. The time of issue. Not in the future, and not more than 60 seconds in the past.                                                                                                                        |
| `nbf`  | Optional. If present, it must not be in the future.                                                                                                                                                              |
| `jti`  | A unique ID, for example a random UUID.                                                                                                                                                                          |

Each client assertion is valid for one use only. Create a new client assertion, with a new `jti`, for each token request. Do this also for a retry after a failure. A second request with the same client assertion fails, also before the client assertion expires.

## 4. Request a token

Send the client assertion in the request body:

```http
POST /oauth/token HTTP/1.1
Host: auth.prod.idcheck.bankid.no
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&client_assertion=<<CLIENT_ASSERTION>>&scope=handover:create+handover:read
```

The `client_id` parameter is optional. If present, it must be the same as the `iss` and the `sub` of the client assertion.

The `scope` parameter gives the scopes.

The response contains the access token. See [Authentication](https://docs.stoe.no/id-check/authentication.md#response).

## Rotate keys

To replace a key without failed token requests:

1. **Add the new key.** Give it a new `kid`. With a JWKS URL, publish the new key next to the old key, and wait at least 30 seconds. With a public JWK, send the new key to Stø, and wait until Stø confirms that it enrolled the key.
2. **Sign with the new key.** Put the new `kid` in the header of each client assertion.
3. **Remove the old key.** With a JWKS URL, remove the old key from the JWKS. The authorisation server can still accept the old key for up to 5 minutes. With a public JWK, ask Stø to remove the old key.

## Test the integration

Stø gives access to the preproduction environment of ID Check for tests. Stø gives the preproduction details during the set-up of access. Preproduction has its own host names, so its token endpoint and its `aud` value are different. Register a public key for preproduction too, as in step 2.

1. **Request a token in preproduction.** The response has status 200 and an `access_token`.
2. **Create a session.** Use the access token to [create a session](https://docs.stoe.no/id-check/create-session.md) in preproduction.
3. **Fetch the session.** Use the access token to [fetch the session](https://docs.stoe.no/id-check/fetch-session.md) in preproduction.

## Next steps

- [Authentication](https://docs.stoe.no/id-check/authentication.md): the token request, the response and the errors.
- [Create a session](https://docs.stoe.no/id-check/create-session.md): use the access token to start an ID Check session.
