# Auth Module

Source: https://docs.formance.com/v4.0/deploy/self-hosted/modules/auth

Stack: v4.0

## Requirements

Formance Auth requires:

- **PostgreSQL**: See configuration guide [here](https://docs.formance.com/v4.0/deploy/self-hosted/infrastructure/postgresql).

## Auth Object

You can find all the available parameters in [the comprehensive CRD documentation](https://docs.formance.com/v4.0/deploy/self-hosted/config/api-reference).

```yaml
apiVersion: formance.com/v1beta1
kind: Auth
metadata:
  name: formance-dev
spec:
  stack: formance-dev
```

## Signing keys

Auth uses an RSA private key to sign JWTs. Choose how the key is managed:

| Key management     | Auth configuration                   | Use when                                                                   |
| ------------------ | ------------------------------------ | -------------------------------------------------------------------------- |
| Operator-managed   | Leave both signing-key fields unset. | You want the Operator to provision and retain a unique key for each stack. |
| Externally managed | Set `spec.signingKeyFromSecret`.     | Your secret-management process controls the key and its rotation.          |

Ordinary upgrades retain the key in either case. To replace it, follow [Rotate a signing key](#rotate-a-signing-key).

### Use an Operator-managed key

Leave both `spec.signingKey` and `spec.signingKeyFromSecret` unset to let the Operator provision the key. It stores the key in the `auth-signing-key` Kubernetes Secret in the stack's namespace, under `signing-key`, and injects it into Auth.

Automatic key provisioning requires Operator `3.18.0` or later. Use the latest compatible stable release; see [Upgrading the Operator](https://docs.formance.com/v4.0/manage/upgrade/from-operator).

### Use an externally managed key

Use `signingKeyFromSecret` when you manage key ownership or rotation externally. Create a Kubernetes Secret containing a PEM-encoded PKCS#1 RSA private key in the stack's namespace, then reference its name and data key:

```yaml
apiVersion: formance.com/v1beta1
kind: Auth
metadata:
  name: formance-dev
spec:
  stack: formance-dev
  signingKeyFromSecret:
    name: auth-custom-signing-key
    key: signing-key
```

Use only one signing-key field. `spec.signingKey` also accepts a private key directly, but a Secret reference keeps private key material out of the Auth manifest.

### Rotate a signing key

Replacing the signing key invalidates existing tokens once services refresh their verification keys. Plan a change window for token renewal and user sign-ins.

1. Prepare a new PEM-encoded PKCS#1 RSA private key using your secret-management process.
2. Replace the key in the Secret and roll out Auth according to the table below.
3. Refresh cached verification keys in services that validate Auth JWTs, restarting those services if necessary.
4. Obtain fresh tokens. Verify that they work and that tokens signed with the previous key are rejected.

| Key management     | Replace and roll out                                                                                                                                               |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Operator-managed   | Replace `signing-key` in the stack's `auth-signing-key` Secret, preserving its name and data field. The Operator watches this Secret and rolls Auth automatically. |
| Externally managed | Update the Secret referenced by `spec.signingKeyFromSecret`, then restart all Auth replicas to load it.                                                            |

If you use an inline `spec.signingKey`, update that field and verify that every Auth replica has rolled out with the new key before continuing.

## Define OAuth2 clients

You can define oAuth2 clients to secure your stack. The Auth will then be able to authenticate the incoming requests and forward them to the right service.

Here, you can replace `YOUR_ID` and `YOUR_SECRET` with your own arbitrary string values.

```yaml
apiVersion: formance.com/v1beta1
kind: AuthClient
metadata:
  name: formance-dev-clients
spec:
  id: YOUR_ID
  secret: YOUR_SECRET
  scopes:
    - ledger:read
    - ledger:write
    - payments:read
    - payments:write
  stack: formance-dev
```
