bolt-lightningQuickstart: Keycloak

Integrate GL IAM with Keycloak for enterprise-grade identity management.

circle-info

When to use Keycloak: Choose Keycloak for enterprise environments with existing Keycloak infrastructure, advanced identity features (SSO, MFA, federation), or requirements for OIDC/SAML protocol support.

circle-info

What you'll build: A FastAPI application that authenticates users via Keycloak tokens and enforces role-based access control using GL IAM's unified interface.

chevron-rightPrerequisiteshashtag

This example requires completion of all setup steps listed on the Prerequisites page. To summarize:

  • Python 3.11+

  • Docker and Docker Compose

  • Access to the GDP Labs' Gen AI SDK repository (request via formarrow-up-right or ticket@gdplabs.id)

  • gcloud CLI: Installarrow-up-right, then run gcloud auth login

  • uvarrow-up-right — Install with: curl -LsSf https://astral.sh/uv/install.sh | sh

No external Keycloak server needed — we'll run it locally with Docker.

Setup Keycloak

Create docker-compose.yml:

version: '3.8'

services:
  keycloak:
    image: quay.io/keycloak/keycloak:24.0
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
    ports:
      - "8080:8080"
    command:
      - start-dev
      - --import-realm
    volumes:
      - ./realm-export.json:/opt/keycloak/data/import/realm-export.json
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health/ready"]
      interval: 10s
      timeout: 5s
      retries: 5

Create realm-export.json with pre-configured users:

Start Keycloak:

Wait for Keycloak to be healthy (30-60 seconds on first start):

Admin console: http://localhost:8080/adminarrow-up-right (credentials: admin / admin)

Demo Users

The pre-configured realm includes these test users:

Email
Password
Roles

user@example.com

user123

member

admin@example.com

admin123

admin, member

Installation

Install GL IAM from the internal Google Artifact Registry (latest version).

5-Line Core

The essential code to validate Keycloak tokens with GL IAM:

Step-by-Step

1

Configure Environment

Create .env file:

2

Setup Provider

3

Add Protected Endpoints

4

Run the Server

Output:

5

Test the API

Get a token using the Resource Owner Password Grant:

6

Test Admin Access

circle-check

Complete Example

Create main.py:

Run it:

Keycloak Role Mapping

Keycloak realm roles map to GL IAM standard roles:

Keycloak Role
GL IAM Standard Role
Access Level

admin

ORG_ADMIN

Admin endpoints

member

ORG_MEMBER

Member endpoints

Common Pitfalls

Pitfall
Solution

Keycloak not starting

Wait 30-60 seconds on first start; check docker-compose logs keycloak

Token validation fails

Ensure client_secret matches the Keycloak client configuration

User has no roles

Assign realm roles to the user in Keycloak admin console

Port 8080 in use

Change the port mapping in docker-compose.yml

Resource Owner Password Grant disabled

Enable "Direct Access Grants" on the Keycloak client

Cleanup

Stop and remove the Keycloak container:

Production Notes

circle-exclamation

For production deployments:

  • Use HTTPS for the Keycloak server URL

  • Configure proper SSL/TLS certificates

  • Set up realm roles and client scopes according to your security requirements

  • Consider using Keycloak's built-in user federation for LDAP/Active Directory integration

Last updated

Was this helpful?