# Authentication

The token endpoint of the ID Check authorisation server gives the API client an access token for the Handover API.

```http
POST https://auth.prod.idcheck.bankid.no/oauth/token
```

## Request

The API client authenticates with a client assertion: a JSON Web Token (JWT) that it signs with its own private key ([RFC 7523](https://www.rfc-editor.org/rfc/rfc7523)). To create one, see [Client assertion](https://docs.stoe.no/id-check/client-assertion.md).

Send the parameters in an `application/x-www-form-urlencoded` body. Send each parameter once.

| Parameter               | Type   | Required | Description                                                                                                                          |
| ----------------------- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `grant_type`            | string | Yes      | `client_credentials` ([RFC 6749](https://www.rfc-editor.org/rfc/rfc6749#section-4.4)).                                               |
| `client_assertion_type` | string | Yes      | `urn:ietf:params:oauth:client-assertion-type:jwt-bearer`.                                                                            |
| `client_assertion`      | string | Yes      | The client assertion. Create a new one for each request.                                                                             |
| `scope`                 | string | Yes      | The scopes, separated by spaces. With `handover:create`, the API client can create sessions. With `handover:read`, it can read them. |
| `client_id`             | string | No       | The client ID. If present, it must be the same as the `iss` and the `sub` of the client assertion.                                   |

For example:

```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
```

## Response

```http
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
    "access_token":"<<ACCESS_TOKEN>>",
    "token_type":"Bearer",
    "expires_in":900,
    "scope":"handover:create handover:read"
}
```

| Field          | Type   | Description                                                                                                                                                 |
| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `access_token` | string | The access token.                                                                                                                                           |
| `token_type`   | string | `Bearer`.                                                                                                                                                   |
| `expires_in`   | number | The lifetime of the access token, in seconds. The default is 900. Read it from each response, because Stø can set a different lifetime for each API client. |
| `scope`        | string | The scopes of the access token, separated by spaces.                                                                                                        |

Keep the access token, and use it until shortly before it expires. Leave a margin, for example 60 seconds, and then request a new access token with a new client assertion. If a Handover API request returns `401 Unauthorized`, discard the access token: an access token can stop working before it expires, for example when Stø revokes it. Get a new access token, and send the request again once. The token endpoint does not issue refresh tokens.

Send the access token in the `Authorization` header of each Handover API request:

```http
Authorization: Bearer <<ACCESS_TOKEN>>
```

> **Do not depend on the token format**
>
> The access token is currently a JWT, but its format can change. Do not decode, inspect or verify it. Send it to the Handover API unchanged.

## Errors

An error response has a JSON body with an `error` code and, usually, an `error_description`, as [RFC 6749](https://www.rfc-editor.org/rfc/rfc6749#section-5.2) describes. The `error_description` is for people. Do not use it to make programmatic decisions, because its text can change.

| `error`                  | Status     | Cause                                                                                                                                                                                                                                                                                                                                                                                           |
| ------------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `invalid_request`        | 400        | The `Content-Type` of the request is not `application/x-www-form-urlencoded`, or the request has a parameter more than once. The request also fails if it has a `client_secret` parameter and an `Authorization` header.                                                                                                                                                                        |
| `invalid_client`         | 400 or 401 | Client authentication failed. For example, the authorisation server cannot find the public key, the signature or a claim is not correct, or the client assertion was used before. A request that uses a client assertion and another authentication method also fails. The status is 401, with a `WWW-Authenticate` header, if the request has an `Authorization` header. Otherwise, it is 400. |
| `unauthorized_client`    | 400        | The API client cannot use this grant type, or its set-up is not complete. [Contact Stø](https://docs.stoe.no/id-check.md#support).                                                                                                                                                                                                                                                              |
| `unsupported_grant_type` | 400        | The request has no `grant_type`, or the authorisation server does not support its value. Use `client_credentials`.                                                                                                                                                                                                                                                                              |
| `invalid_scope`          | 400        | The request has no `scope`, or it asks for a scope that the API client cannot use.                                                                                                                                                                                                                                                                                                              |
| `server_error`           | 500        | An error occurred in the authorisation server. Try again later, with a new client assertion.                                                                                                                                                                                                                                                                                                    |

For example, a request that sends a client assertion a second time gets this response:

```http
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
    "error":"invalid_client",
    "error_description":"client_assertion jti has already been used"
}
```

## Authorisation server metadata

The authorisation server metadata ([RFC 8414](https://www.rfc-editor.org/rfc/rfc8414)) gives the token endpoint, the client authentication methods, the signing algorithms and the scopes. Its `issuer` is the `aud` of each client assertion.

The metadata is at <https://prod.idcheck.bankid.no/.well-known/openid-configuration>. The same document is also at <https://prod.idcheck.bankid.no/.well-known/oauth-authorization-server>.

## Client secret

An API client that already has a client secret can still use it instead of a client assertion. Send the client ID and the client secret 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_id=<<CLIENT_ID>>&client_secret=<<CLIENT_SECRET>>&scope=handover:create+handover:read
```

Alternatively, send them in an HTTP Basic `Authorization` header, as [RFC 6749](https://www.rfc-editor.org/rfc/rfc6749#section-2.3.1) describes. Do not send them in both places: a request with both fails with `invalid_request`.

## Next steps

- [Client assertion](https://docs.stoe.no/id-check/client-assertion.md): create a key pair and sign client assertions.
- [Create a session](https://docs.stoe.no/id-check/create-session.md): use the access token to start an ID Check session.
- [Fetch a session](https://docs.stoe.no/id-check/fetch-session.md): use the access token to read the state of a session.
