|
| 1 | +# Authentication for OneGround ZGW APIs |
| 2 | + |
| 3 | +This guide explains how to authenticate against the ZGW APIs during local development and demos. |
| 4 | + |
| 5 | +## Supported token types |
| 6 | + |
| 7 | +- **OAuth2 access tokens (recommended)** |
| 8 | + - Issued via standard flows (e.g. client credentials as in this guide). |
| 9 | + - Tokens are verified by checking the signature with the Identity Provider's public keys (JWKS), enabling offline validation and reducing network calls. |
| 10 | + - Tokens are issued in one place (the Identity Provider), so you can centrally enforce short expiration times, rotate keys, and revoke compromised clients. |
| 11 | + - Any standards‑compliant OAuth2 Identity Provider can be used to issue access tokens for these APIs. Keycloak is used in this repository only as an example. |
| 12 | + - Each access token must include an `rsin` claim that contains the organization's RSIN. APIs use this claim for tenant/organization context. In this local Keycloak setup, `rsin` claim is added via hardcoded claim mapper on the api client. When using a different Identity Provider, configure an equivalent claim/attribute mapping so that issued access tokens contain the `rsin` claim. |
| 13 | + |
| 14 | +- **ZGW standard tokens (legacy/backwards compatibility)** |
| 15 | + - Self‑issued JWTs signed with HS256 (HMAC‑SHA256) using the Keycloak client's secret. |
| 16 | + - Must include a `client_id` claim that matches a client in the Keycloak realm. |
| 17 | + - Supported only with Keycloak together with the custom token introspection plugin. See the project for details: [Keycloak-ZGW-Token-Introspection](https://github.com/OneGround/Keycloak-ZGW-Token-Introspection). |
| 18 | + - Use short token lifetimes. Tokens without an `exp` claim are treated as active, but it's not recommended to use non expiring access tokens. |
| 19 | + |
| 20 | +## API Authentication using OAuth2 access tokens |
| 21 | + |
| 22 | +#### Get the Client Secret from Keycloak |
| 23 | + |
| 24 | +1. Navigate to the Keycloak admin console: [http://localhost:8080/admin/master/console/#/OneGround/](http://localhost:8080/admin/master/console/#/OneGround/) |
| 25 | +2. Log in using the credentials: |
| 26 | + - **Username**: `admin` |
| 27 | + - **Password**: `admin` |
| 28 | +3. From the navigation on the left, select **Clients**. |
| 29 | +4. Select the `oneground-000000000` client from the list. |
| 30 | + > **Note on the Default Client:** This local setup is configured with a single default client, `oneground-000000000`, which has full administrative access to all APIs. If you wish to add more clients with specific permissions, you must first create them in Keycloak by following the [Keycloak Setup Guide](./localdev/keycloak/KeycloakSetup/README.md). After creating a new client, you must also configure its permissions using the Autorisaties API or by updating the [autorisaties service's seed data](./localdev/oneground-services-data/ac-data/applicaties.json). |
| 31 | +5. Go to the **Credentials** tab. |
| 32 | +6. Copy the value from the **Client Secret** field. This is your `<oneground-client-secret>`. |
| 33 | + |
| 34 | +#### Request an Access Token |
| 35 | + |
| 36 | +Now you can exchange the client credentials for a temporary access token. Use the command for your operating system, replacing `<oneground-client-secret>` with your actual secret. The default client ID is `oneground-000000000`. |
| 37 | + |
| 38 | +##### For Windows (PowerShell) |
| 39 | + |
| 40 | +- Open Windows PowerShell and execute this command: |
| 41 | + |
| 42 | +```powershell |
| 43 | +$response = Invoke-WebRequest ` |
| 44 | + -Uri "http://localhost:8080/realms/OneGround/protocol/openid-connect/token" ` |
| 45 | + -Method POST ` |
| 46 | + -Headers @{"Content-Type" = "application/x-www-form-urlencoded"} ` |
| 47 | + -Body "grant_type=client_credentials&client_id=oneground-000000000&client_secret=<oneground-client-secret>" |
| 48 | +``` |
| 49 | + |
| 50 | +- Then take an access token from `$response`: |
| 51 | + |
| 52 | +```powershell |
| 53 | +$response.Content |
| 54 | +``` |
| 55 | + |
| 56 | +##### For Linux, macOS, or WSL (cURL) |
| 57 | + |
| 58 | +- Open terminal and execute this command: |
| 59 | + |
| 60 | +```bash |
| 61 | +curl --location --request POST 'http://localhost:8080/realms/OneGround/protocol/openid-connect/token' \ |
| 62 | +--header 'Content-Type: application/x-www-form-urlencoded' \ |
| 63 | +--data-urlencode 'grant_type=client_credentials' \ |
| 64 | +--data-urlencode 'client_id=oneground-000000000' \ |
| 65 | +--data-urlencode 'client_secret=<oneground-client-secret>' |
| 66 | +``` |
| 67 | + |
| 68 | +You will receive a JSON response containing the `access_token`. You can now use this token as a `Bearer` token to authorize your API requests. |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAi......", |
| 73 | + "expires_in": 300, |
| 74 | + "refresh_expires_in": 0, |
| 75 | + "token_type": "Bearer" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +> **Tip: How to Increase Token Expiration Time (For Testing Only)** |
| 80 | +> |
| 81 | +> **Warning:** Extending access token lifespans reduces security. Long-lived tokens are easier to steal and misuse and increase the impact of any leak because they remain valid for longer. Only increase token lifespans for local testing in non-production environments. |
| 82 | +> By default, the access token expires in 5 minutes (300 seconds). To increase this time: |
| 83 | +> |
| 84 | +> 1. Navigate directly to the **Tokens** settings page in Keycloak: [http://localhost:8080/admin/master/console/#/OneGround/realm-settings/tokens](http://localhost:8080/admin/master/console/#/OneGround/realm-settings/tokens). |
| 85 | +> 2. In the `Access Token Lifespan` field, set a longer duration (e.g., `30 minutes` or `1 hour`). |
| 86 | +> 3. Click **Save**. |
| 87 | +> |
| 88 | +> You will need to request a new token for this change to take effect. |
0 commit comments