|
| 1 | +--- |
| 2 | +sidebar_position: 12 |
| 3 | +title: Service Account Authentication |
| 4 | +--- |
| 5 | + |
| 6 | +import Disclaimer from '../_disclaimer.mdx'; |
| 7 | + |
| 8 | +<Disclaimer /> |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The Reference Implementation APIs support two authentication methods: |
| 13 | + |
| 14 | +1. **Session-based authentication** - For interactive users via the web UI (uses OAuth2 authorization code flow) |
| 15 | +2. **Service account authentication** - For automated integrations and machine-to-machine (M2M) access |
| 16 | + |
| 17 | +This guide covers how to configure and use service account authentication for programmatic API access. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- Keycloak identity provider running and configured |
| 22 | +- A service account client configured in Keycloak (see [Keycloak Configuration](#keycloak-configuration)) |
| 23 | + |
| 24 | +## Keycloak Configuration |
| 25 | + |
| 26 | +The default Keycloak realm includes a pre-configured service account client: |
| 27 | + |
| 28 | +| Property | Value | |
| 29 | +|----------|-------| |
| 30 | +| Client ID | `ri-service-account` | |
| 31 | +| Client Secret | `service-account-secret` | |
| 32 | +| Grant Type | Client Credentials | |
| 33 | + |
| 34 | +:::warning Production Usage |
| 35 | +For production deployments, you should: |
| 36 | +1. Change the default client secret to a secure, randomly generated value |
| 37 | +2. Configure appropriate client scopes and role mappings |
| 38 | +::: |
| 39 | + |
| 40 | +### Creating a Custom Service Account Client |
| 41 | + |
| 42 | +To create a new service account client in Keycloak: |
| 43 | + |
| 44 | +1. Navigate to your Keycloak Admin Console |
| 45 | +2. Select your realm (e.g., `untp-reference-implementation`) |
| 46 | +3. Go to **Clients** → **Create client** |
| 47 | +4. Configure the client: |
| 48 | + - **Client ID**: Your desired client ID (e.g., `my-integration`) |
| 49 | + - **Client authentication**: ON |
| 50 | + - **Authorization**: OFF |
| 51 | + - **Authentication flow**: Check only "Service accounts roles" |
| 52 | +5. Save the client and note the generated client secret from the **Credentials** tab |
| 53 | + |
| 54 | +## Obtaining an Access Token |
| 55 | + |
| 56 | +Use the OAuth2 client credentials grant to obtain an access token from Keycloak. |
| 57 | + |
| 58 | +### Using cURL |
| 59 | + |
| 60 | +```bash |
| 61 | +# Set your configuration |
| 62 | +KEYCLOAK_URL="http://localhost:8080" |
| 63 | +REALM="untp-reference-implementation" |
| 64 | +CLIENT_ID="ri-service-account" |
| 65 | +CLIENT_SECRET="service-account-secret" |
| 66 | + |
| 67 | +# Request an access token |
| 68 | +curl -X POST "${KEYCLOAK_URL}/realms/${REALM}/protocol/openid-connect/token" \ |
| 69 | + -H "Content-Type: application/x-www-form-urlencoded" \ |
| 70 | + -d "grant_type=client_credentials" \ |
| 71 | + -d "client_id=${CLIENT_ID}" \ |
| 72 | + -d "client_secret=${CLIENT_SECRET}" |
| 73 | +``` |
| 74 | + |
| 75 | +### Response |
| 76 | + |
| 77 | +```json |
| 78 | +{ |
| 79 | + "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI...", |
| 80 | + "expires_in": 300, |
| 81 | + "refresh_expires_in": 0, |
| 82 | + "token_type": "Bearer", |
| 83 | + "not-before-policy": 0, |
| 84 | + "scope": "profile email roles" |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Calling the API with a Bearer Token |
| 89 | + |
| 90 | +Once you have an access token, include it in the `Authorization` header of your API requests. |
| 91 | + |
| 92 | +### Using cURL |
| 93 | + |
| 94 | +```bash |
| 95 | +# Using the token obtained above |
| 96 | +ACCESS_TOKEN="eyJhbGciOiJSUzI1NiIsInR5cCI..." |
| 97 | + |
| 98 | +# Call the credentials API |
| 99 | +curl -X POST "http://localhost:3003/api/v1/credentials" \ |
| 100 | + -H "Authorization: Bearer ${ACCESS_TOKEN}" \ |
| 101 | + -H "Content-Type: application/json" \ |
| 102 | + -d '{}' |
| 103 | +``` |
| 104 | + |
| 105 | +## Environment Variables |
| 106 | + |
| 107 | +Configure the following environment variables for service account support: |
| 108 | + |
| 109 | +| Variable | Description | Default | |
| 110 | +|----------|-------------|---------| |
| 111 | +| `AUTH_KEYCLOAK_ISSUER` | Keycloak realm issuer URL | Required | |
| 112 | +| `AUTH_KEYCLOAK_SERVICE_ACCOUNT_CLIENT_ID` | Service account client ID | `ri-service-account` | |
| 113 | +| `AUTH_KEYCLOAK_SERVICE_ACCOUNT_CLIENT_SECRET` | Service account client secret | `service-account-secret` | |
| 114 | +| `AUTH_KEYCLOAK_SERVICE_ACCOUNT_AUDIENCE` | Expected token audience (optional) | - | |
0 commit comments