# Authentication


The MoneyMonk API uses **OAuth 2** with the authorization code flow and **PKCE** (S256). Every call to a resource endpoint must be made using an access token obtained through this flow.

We do not support the implicit flow, the resource owner password credentials flow, or the client credentials flow.


## Registering a client

We provide client registration on a case-by-case basis for selected partners and integrations. See [Requesting access](./requesting-access) for how to apply.

Clients are configured with:

- A **client ID** — a public identifier for your application.
- An optional **client secret** — a confidential value used to authenticate your application at the token endpoint. Treat this as a password and never embed it in code that is distributed to end users (e.g. mobile or single-page apps). A client secret is mandatory for applications that have a dedicated backend. Applications that do not have a dedicated backend should not use a client secret, since they run on user devices and cannot securely store a secret.
- A **redirect URI** — exact URL we will redirect the user to after they grant or deny consent. Redirect URIs are matched exactly, including scheme, host, port, and path. Only a single redirect URI can be used.


## Discovery

A machine-readable description of the authorization server is available at:

```
GET https://api.moneymonk.nl/.well-known/oauth-authorization-server
```

This document follows [RFC 8414](https://datatracker.ietf.org/doc/html/rfc8414) and lists the authorization endpoint, the token endpoint, the supported scopes, the supported grant types and response types, the supported PKCE methods, and the supported client authentication methods at the token endpoint.


## The authorization code flow

### 1. Generate a PKCE code verifier, code challenge, and state

Generate a high-entropy random string of 43–128 characters (the **code verifier**) and store it in your application's session. Derive the **code challenge** by SHA-256 hashing the verifier and base64url-encoding the result without padding.

PKCE is mandatory for all clients, including confidential ones, and only the `S256` method is supported.

To generate a code verifier and code challenge in Python, you can refer to the following example code. A code verifier is a cryptographically secure 48-byte random value that is Base64URL-encoded, resulting in an encoded length of 64 characters.
```
code_verifier = secrets.token_urlsafe(48)
code_verifier_sha256 = hashlib.sha256(code_verifier.encode("UTF-8")).digest()
code_challenge = base64.urlsafe_b64encode(code_verifier_sha256).decode("UTF-8").rstrip("=")
```

It is also recommended to generate a cryptographically secure random value for the state, which can be done in a similar way. To generate a Base64URL-encoded value from 32 bytes:
```
state = secrets.token_urlsafe(32)
```


### 2. Redirect the user to the authorization endpoint

Send the user's browser to:

```
GET https://api.moneymonk.nl/oauth2/authorize
    ?client_id={client_id}
    &response_type=code
    &code_challenge_method=S256
    &code_challenge={code_challenge}
    &state={state}
```

Parameters:

| Parameter | Required | Description |
|-----------|----------|-------------|
| `client_id` | yes | Your registered client ID. |
| `response_type` | yes | Must be `code`. |
| `code_challenge_method` | yes | Must be `S256`. |
| `code_challenge` | yes | PKCE challenge derived from the verifier. |
| `state` | recommended | Opaque value you use to maintain state between request and callback, and to defend against CSRF. We will return it unchanged on the redirect. |
| `scope` | no | Space-separated list of scopes you are requesting (see [Scopes](./introduction#scopes)). If omitted, the full set of scopes registered for the client is requested. |
| `redirect_uri` | no | The redirect URI registered for your client. If provided, you must also send the same redirect URI to the token endpoint. There is no need to use this parameter, since we only support one redirect URI per client, but it is supported to be compliant with the OAuth 2.0 standard. |

MoneyMonk shows the user a consent screen where they select the administration to grant access to and see which scopes are being requested. Consent covers a single administration, and the resulting tokens are valid for that administration only. Only the primary user of an administration can grant consent; other users with access to the administration cannot authorize an integration. After the user grants consent, we redirect them back to your `redirect_uri` with a `code` and the `state` you supplied:

```
<redirect_uri>?code=<authorization_code>&state=<state>
```

If the user denies consent or an error occurs, we redirect to your `redirect_uri` with `error` and `error_description` parameters as defined in [RFC 6749 section 4.1.2.1](https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1).

Authorization codes are single-use and expire after **two minutes**.


### 3. Exchange the code for tokens

From your server, POST to the token endpoint with an `application/x-www-form-urlencoded` body:

```
POST https://api.moneymonk.nl/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=<authorization_code>
&client_id=<your_client_id>
&redirect_uri=<the redirect_uri used in step 2, if any>
&code_verifier=<the PKCE verifier from step 1>
```

Confidential clients need to provide their client credentials by either
1. Adding a `client_secret` parameter in the body (with their client secret as value)
2. Using HTTP Basic Auth: The client_id and client_secret are encoded using application/x-www-form-urlencoded rules. They are then concatenated as `<client_id>:<client_secret>`, base64-encoded. The resulting value is sent in the `Authorization` header as `Authorization: Basic <base64-encoded-value>`.

A successful response is of the form:

```json
{
  "access_token": <access_token>,
  "expires_in": 14400,
  "refresh_token": <refresh_token>,
  "refresh_token_expires_in": 5184000,
  "scope": "product:read product:write",
  "token_type": "Bearer"
}
```

- The **access token** is valid for **4 hours** (`expires_in`, in seconds).
- The **refresh token** is valid for **60 days** (`refresh_token_expires_in`, in seconds).
- The `scope` field lists the scopes that were granted. These are always the scopes that were requested.

When the last refresh token expires, there is no way to get an access token, and the entire flow needs to be restarted, which requires user interaction to grant permission to the application.


## Refreshing an access token

When an access token is close to expiring (or has expired), use the refresh token to obtain a new pair:

```
POST https://api.moneymonk.nl/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
&refresh_token=<refresh_token>
&client_id=<your_client_id>
&client_secret=<your_client_secret>
```

For public clients, the `client_secret` should be omitted from the request.

The response has the same shape as the authorization code response. Refresh tokens are rotated on each use; the previous refresh token becomes invalid as soon as the new one is issued. Store the new refresh token immediately.


## Revoking a token

To proactively invalidate an access or refresh token (for example because the user has logged out, or disconnected your integration), call the revocation endpoint:

```
POST https://api.moneymonk.nl/oauth2/revoke
Content-Type: application/x-www-form-urlencoded

token=<access_token or refresh_token>
&client_id=<your_client_id>
&client_secret=<your_client_secret>
```

For public clients, the `client_secret` should be omitted from the request.
