Keycloak and Hasura Setup Guide
This guide provides step-by-step instructions to set up Keycloak for the UBI beneficiary backend system based on the realm configuration.
Prerequisites
A running PostgreSQL database (uba_beneficiary_mw)
Docker and Docker Compose installed
Environment variables configured
Keycloak Setup
Keycloak provides identity and access management for the UBI Beneficiary system, handling user authentication, authorization, and session management.
1. Starting Keycloak
The docker-compose.yml file located in the root of the repository provisions the Keycloak services.
Run the following command:
docker-compose up -d keycloakThis will start Keycloak on: 👉 http://localhost:8080
Login credentials:
Use the username and password defined in the Docker environment variables
2. Initial Keycloak Configuration
You need to manually configure the realm with users, groups, clients, and roles based on the realm configuration.
Steps:
Login to Keycloak Admin Console
Navigate to: 👉 http://localhost:8080/admin
Use admin credentials from environment variables
Create a New Realm
Top left → Dropdown → Create Realm
Realm name:
<your-realm-name>Display name:
<your-display-name>Display name HTML:
<div class="kc-logo-text"><span>your-display-name SSO</span></div>Click "Create"
Configure Clients
A. Create Admin CLI Client:
Go to Clients → Create client
Client ID:
admin-cliClient authentication:
OnAuthorization:
OnService accounts roles:
OnStandard flow:
OffDirect access grants:
OnService accounts:
OnAuthorization services:
OnPublic client:
OffClick "Save"
Configure Client Secret:
Go to Credentials tab
Copy the Secret value
Update your
.envfile with this secret
B. Create Beneficiary App Client:
Go to Clients → Create client
Client ID:
<your-client-name>Client authentication:
Off(Public client)Standard flow:
OnDirect access grants:
OnPublic client:
OnFront channel logout:
OnFull scope allowed:
OnClick "Save"
Configure Redirect URIs:
Go to Settings tab
Valid redirect URIs:
/*Web origins:
/*Valid post logout redirect URIs:
/*Click "Save"
Configure Roles
A. Create Client Roles for :
Go to Clients → → Roles
Create role:
adminCreate role:
beneficiary
B. Assign Roles to Admin CLI Service Account
Go to Clients → admin-cli → Service account role tab 3.To manage detail and group mappings, click on the username service-account-admin-cli
Role mapping tab
Client roles:
realm-managementAssign roles->Filter by clients
manage-usersquery-users`view-users
Configure Groups
A. Create Groups:
Go to Groups → Create group
Name:
adminPath:
/adminClick "Save"
Go to Groups → Create group
Name:
beneficiaryPath:
/beneficiaryClick "Save"
B. Assign Client Roles to Groups:
Admin Group:
Go to Groups → admin → Role mapping
Client roles:
<your-client-name>Assign:
adminrole
Beneficiary Group:
Go to Groups → beneficiary → Role mapping
Client roles:
<your-client-name>Assign:
beneficiaryrole
Realm Settings Configuration
A. General Settings:
Go to Realm settings → General -2. Realm name:
<your-realm-name>Display name:
<your dispaly name>Display name HTML:
<div class="kc-logo-text"><span>your dispaly name SSO</span></div>Default signature algorithm:
RS256Revoke refresh token:
OffAccess token lifespan:
82800(23 hours)SSO session idle timeout:
86400(24 hours)SSO session max lifespan:
86400(24 hours)
B. Login Settings:
Go to Realm settings → Login
User registration:
OffLogin with email:
OnDuplicate emails:
OffReset password:
OffEdit username:
OnBrute force detection:
Off
C. Security Defenses:
Go to Realm settings → Security defenses
Brute force detection:
OffSSL required:
External
Create Test Users
A. Create Admin User:
Go to Users → Create new user
Username:
test-adminEmail:
admin@test.comFirst name:
TestLast name:
AdminEmail verified:
OnEnabled:
OnClick "Save"
Set password:
Go to Credentials tab
Set password:
test123Temporary:
Off
Assign to group:
Go to Groups tab
Join group:
admin
B. Create Beneficiary User:
Go to Users → Create new user
Username:
test-beneficiaryEmail:
beneficiary@test.comFirst name:
TestLast name:
BeneficiaryEmail verified:
OnEnabled:
OnClick "Save"
Set password:
Go to Credentials tab
Set password:
test123Temporary:
Off
Assign to group:
Go to Groups tab
Join group:
beneficiary
8. Test Authentication
Admin CLI Token:
User Token:
9. Start Your Application
Update your application's environment variables
Verify integration by testing API endpoints with Keycloak tokens
Keycloak Setup Complete
At this point:
✅ PostgreSQL is running
✅ Keycloak is up (via docker-compose)
✅ Realm is configured successfully
✅ Users, groups, clients, and roles are created
✅ Authentication flows are configured
Hasura Setup
Hasura GraphQL Engine exposes GraphQL APIs on top of the PostgreSQL database, providing a powerful and flexible way to interact with your data.
1. Starting Hasura
The docker-compose.yml file located in the root of the repository provisions the Hasura services.
Run the following command:
This will start Hasura on: 👉 http://localhost:8081
2. Connect Hasura to the Database
Open the Hasura Console in your browser: 👉 http://localhost:8081/console
Navigate to: Data → Manage Databases → default → Edit
Hasura supports 4 ways to connect your database:
A. Connection Parameters Enter parameters individually in Hasura Console:
B. Connection String (Direct URL)
C. Connection String from Environment Variable In docker-compose.yml, set:
Then in Hasura Console → Add Database → select Environment Variable → use:
✅ Recommended for production
D. From Connection Template Use Hasura's Template Gallery → Select Postgres template → Fill in DB details
3. Track Tables
In the Hasura Console:
Go to Data → default → Untracked tables/views
Click Track All to expose all tables in GraphQL
If tables don't appear:
Navigate to Settings → Metadata Actions → Reload
4. Verify GraphQL Endpoint
Test GraphQL API at: 👉 http://localhost:8081/v1/graphql
You can test with a simple query:
5. Populate Benefit Data via Content API
Your backend provides a Content API that populates benefit data into the system.
Example Endpoint:
Run with curl:
This ensures that benefit schemes and related data are inserted into the database.
Verification Checklist
Keycloak Verification
Hasura Verification
Troubleshooting
Common Issues
Keycloak not starting
Check Docker logs:
docker-compose logs keycloakVerify PostgreSQL is running
Check port 8080 is not in use
Hasura connection issues
Verify database credentials in docker-compose.yml
Check PostgreSQL is accessible from Hasura container
Review Hasura logs:
docker-compose logs hasura
Tables not appearing in Hasura
Reload metadata in Hasura Console
Check database permissions
Verify table names and schema
Realm configuration issues
Verify all required clients are created
Check service account permissions
Ensure proper role assignments
Test admin token generation
User registration fails
Check admin CLI service account has
manage-usersroleVerify realm name in environment variables
Ensure client secret is correct
Test with manual user creation in admin console
Last updated
