Skip to main content
The HERCULES SGI system uses Keycloak 13.0.0 as its authentication and authorization server. This guide covers Keycloak setup, realm configuration, and user management.

Keycloak Overview

Keycloak provides:
  • Single Sign-On (SSO) across all SGI services
  • OAuth2/OpenID Connect authentication
  • Role-Based Access Control (RBAC)
  • User Federation (LDAP, Active Directory)
  • Identity Brokering (SAML, Social Login)
SGI uses Keycloak 13.0.0 specifically. Using other versions may cause compatibility issues.

Keycloak Installation

The SGI authentication server extends the official Keycloak image:
# Build the custom Keycloak image
cd sgi-auth
mvn clean package -DskipTests
docker build -t sgi-auth:1.1.0 .

# Run Keycloak
docker run -d \
  --name sgi-auth \
  -p 8080:8080 \
  -e KEYCLOAK_USER=admin \
  -e KEYCLOAK_PASSWORD=admin \
  -e DB_VENDOR=postgres \
  -e DB_ADDR=postgres \
  -e DB_DATABASE=keycloak \
  -e DB_USER=keycloak \
  -e DB_PASSWORD=keycloak \
  sgi-auth:1.1.0

Standalone Installation

For non-containerized deployment:
1

Download Keycloak

Download Keycloak 13.0.0:
wget https://github.com/keycloak/keycloak/releases/download/13.0.0/keycloak-13.0.0.tar.gz
tar -xzf keycloak-13.0.0.tar.gz
cd keycloak-13.0.0
2

Configure Database

Configure Keycloak to use PostgreSQL (recommended for production):
# Edit standalone/configuration/standalone.xml
# Add PostgreSQL datasource configuration
Or use the H2 embedded database for development.
3

Install Custom Extensions

Install SGI’s custom SAML broker mappers:
cp sgi-auth/broker-saml-mappers/target/broker-saml-mappers-*.jar \
   keycloak-13.0.0/standalone/deployments/
4

Start Keycloak

cd keycloak-13.0.0/bin
./standalone.sh -Djboss.bind.address=0.0.0.0
Access the admin console at: http://localhost:8080/auth

SGI Realm Configuration

Importing the SGI Realm

SGI provides a pre-configured realm with all necessary clients, roles, and settings.
1

Locate the Realm File

The realm configuration is in sgi-auth/realm/sgi-realm.json.
Security Alert: The default realm file contains example credentials and secrets. Do not use in production without customization!
2

Import the Realm

Option 1: Admin Console (Recommended)
  1. Login to Keycloak admin console: http://localhost:8080/auth
  2. Hover over the realm dropdown (top-left)
  3. Click “Add realm”
  4. Select “Import” and choose sgi-realm.json
  5. Click “Create”
Option 2: Command Line
# Using Docker
docker exec -it sgi-auth \
  /opt/jboss/keycloak/bin/standalone.sh \
  -Djboss.socket.binding.port-offset=100 \
  -Dkeycloak.migration.action=import \
  -Dkeycloak.migration.provider=singleFile \
  -Dkeycloak.migration.file=/realm/sgi-realm.json \
  -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
3

Verify Realm Import

Check that the realm was imported successfully:
  1. Select the sgi realm from the dropdown
  2. Navigate to “Realm Settings”
  3. Verify the realm name is sgi
  4. Check “Clients” - you should see multiple clients (front, csp-service, eti-service, etc.)

Realm Settings

The SGI realm includes these important settings:
Realm
string
default:"sgi"
Realm identifier used in OAuth2 configuration.
Access Token Lifespan
integer
default:"300"
Access token validity in seconds (5 minutes).
SSO Session Idle
integer
default:"1800"
SSO session idle timeout in seconds (30 minutes).
SSO Session Max
integer
default:"36000"
Maximum SSO session lifespan in seconds (10 hours).
Offline Session Idle
integer
default:"2592000"
Offline session idle timeout in seconds (30 days).

Client Configuration

The SGI realm includes multiple OAuth2 clients:

Frontend Client (front)

Client ID: front
Client Protocol: openid-connect
Access Type: public
Valid Redirect URIs:
  • http://localhost:4200/* (development)
  • https://sgi.example.com/* (production)
Web Origins:
  • http://localhost:4200 (development)
  • https://sgi.example.com (production)
Configuration steps:
  1. Navigate to Clientsfront
  2. Update Valid Redirect URIs for your domain
  3. Update Web Origins to match your frontend URL
  4. Save changes
{
  "clientId": "front",
  "enabled": true,
  "protocol": "openid-connect",
  "publicClient": true,
  "redirectUris": [
    "https://sgi.example.com/*"
  ],
  "webOrigins": [
    "https://sgi.example.com"
  ]
}

Backend Service Clients

Each backend service has its own client for service-to-service authentication:
Client ID: csp-service
Access Type: confidential
Service Accounts Enabled: true
Authorization Enabled: true
Change the client secret before production deployment!The default secret in the realm file is: cf693c2b-bdaf-4f98-87db-e7996b917b0a
To generate a new client secret:
  1. Navigate to Clientscsp-service
  2. Go to Credentials tab
  3. Click Regenerate Secret
  4. Copy the new secret
  5. Update the secret in your service’s application.yml:
spring:
  security:
    oauth2:
      client:
        registration:
          csp-service:
            client-secret: YOUR_NEW_SECRET_HERE
Similar configuration for other services:
  • eti-service - Ethics Committee service
  • pii-service - Intellectual Property service
  • cnf-service - Configuration service
  • com-service - Communications service
  • tp-service - Third Parties service
  • usr-service - User management service
  • rel-service - Relations service
  • rep-service - Reports service
  • prc-service - Processing service
  • eer-service - External Evaluation Reports service
Each client requires:
  1. Unique client secret (regenerate for production)
  2. Service account roles configured
  3. Scope mappings to access other services

Role Configuration

SGI uses role-based access control with module-specific roles.

Role Naming Convention

Roles follow the pattern: [MODULE]-[PERMISSION] Examples from the CSP (Calls, Solicitudes, Proyectos) module:
  • CSP-ROLE-R - Read permission
  • CSP-ROLE-E - Edit permission
  • CSP-ROLE-C - Create permission
  • CSP-ROLE-B - Delete (Borrar) permission

Module Roles

The SGI realm includes roles for these modules:
  • CSP-ROLE-R - View calls, applications, and projects
  • CSP-ROLE-E - Edit existing records
  • CSP-ROLE-C - Create new records
  • CSP-ROLE-B - Delete records
  • CSP-ROLS-R - View funding requests
  • CSP-ROLS-E - Edit funding requests
  • CSP-ROLS-C - Create funding requests
  • CSP-ROLS-B - Delete funding requests
  • ETI-ACT-V - View committee acts
  • ETI-ACT-E - Edit committee acts
  • ETI-EVR-V - View evaluations
  • ETI-EVR-EVALR - Evaluate as evaluator
  • ETI-MEM-V - View memories
  • ETI-MEM-E - Edit memories
  • PII-INV-V - View inventions
  • PII-INV-E - Edit inventions
  • PII-INV-C - Create inventions
  • PII-INV-B - Delete inventions
  • ADMINISTRADOR-CSP - CSP module administrator
  • ADMINISTRADOR-ETI - ETI module administrator
  • ADMINISTRADOR-PII - PII module administrator
  • SYSADM-CSP - CSP system administrator
  • UNIDAD-GESTION - Management unit access

Assigning Roles to Users

1

Navigate to User

In Keycloak admin console:
  1. Select the sgi realm
  2. Click Users in the left menu
  3. Find and select the user
2

Assign Realm Roles

  1. Go to Role Mappings tab
  2. Under Available Roles, select roles to assign
  3. Click Add selected
  4. Assigned roles appear in Assigned Roles section
3

Verify Role Assignment

Test the user can access appropriate SGI modules:
# Get access token
curl -X POST "http://localhost:8080/auth/realms/sgi/protocol/openid-connect/token" \
  -d "client_id=front" \
  -d "username=testuser" \
  -d "password=password" \
  -d "grant_type=password"

# Decode the JWT to verify roles

User Management

Creating Users

1

Add New User

  1. Navigate to UsersAdd user
  2. Fill in user details:
    • Username (required)
    • Email
    • First Name
    • Last Name
  3. Set Email Verified to ON
  4. Click Save
2

Set Password

  1. Go to Credentials tab
  2. Set password
  3. Set Temporary to OFF for permanent password
  4. Click Set Password
3

Configure User Attributes

SGI requires the user_ref_id attribute:
  1. Go to Attributes tab
  2. Add attribute:
    • Key: user_ref_id
    • Value: External user reference ID (e.g., employee ID)
  3. Click Save
The user_ref_id is used to link Keycloak users with external systems (HR, LDAP, etc.).
4

Assign Roles

Follow the role assignment steps in the previous section.

User Federation

Connect Keycloak to LDAP or Active Directory:
  1. Navigate to User FederationAdd providerldap
  2. Configure LDAP settings:
Edit Mode
string
default:"READ_ONLY"
How users are edited. Use READ_ONLY for authoritative LDAP.
Vendor
string
LDAP server type (Active Directory, Red Hat Directory Server, etc.).
Connection URL
string
required
LDAP connection string (e.g., ldap://ldap.example.com:389).
Users DN
string
required
Base DN for users (e.g., ou=users,dc=example,dc=com).
Bind DN
string
required
Service account DN for binding to LDAP.
Bind Credential
string
required
Password for the bind DN.
  1. Configure attribute mappings to match SGI requirements
  2. Test connection and authentication
  3. Synchronize users

Security Considerations

Critical Security Tasks Before Production:
  1. Change all client secrets - Regenerate secrets for all service clients
  2. Update admin credentials - Change default admin username/password
  3. Enable HTTPS - Configure TLS certificates for Keycloak
  4. Configure CORS - Restrict valid redirect URIs and web origins
  5. Enable brute force protection - In Realm Settings → Security Defenses
  6. Review token lifespans - Adjust based on security requirements
  7. Audit roles and permissions - Remove unnecessary roles from realm
  8. Backup realm configuration - Export realm regularly

Rotating Client Secrets

Refer to the official guide in the source repository: sgi-auth/docs/keycloak-secrets-rotation.md
1

Generate New Secret

For each service client:
  1. Navigate to Clients → [client-name] → Credentials
  2. Click Regenerate Secret
  3. Copy the new secret
2

Update Service Configuration

Update each service’s application.yml or environment variables:
# Using environment variable
export SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_CSP_SERVICE_CLIENT_SECRET="new_secret"

# Or in application.yml
spring:
  security:
    oauth2:
      client:
        registration:
          csp-service:
            client-secret: new_secret
3

Restart Services

Restart each service to apply the new secret:
# Docker
docker restart sgi-csp-service

# Systemd
systemctl restart sgi-csp-service
4

Verify Connectivity

Test that services can still authenticate:
# Check service health endpoints
curl http://localhost:4281/actuator/health

CVE-2020-27838 Mitigation

SGI 1.0.0 updates Keycloak to version 13.0.0 to address CVE-2020-27838, a security vulnerability in earlier Keycloak versions.Ensure you’re using Keycloak 13.0.0 or later.

Sensitive Information in Realm Export

The default sgi-realm.json contains sensitive information including:
  • Example user credentials
  • Client secrets
  • Sample role assignments
Action Required: Before production deployment:
  1. Import the realm
  2. Change all credentials and secrets
  3. Remove example users
  4. Export a clean realm configuration for your environment

Token Configuration

Configure JWT tokens for optimal security and performance:

Access Token Settings

  1. Navigate to Realm SettingsTokens
  2. Configure token lifespans:
Access Token Lifespan
duration
default:"5 minutes"
How long access tokens are valid. Shorter is more secure but requires more token refreshes.
Client Login Timeout
duration
default:"1 minute"
Timeout for OAuth2 authorization code flow.
Refresh Token Max Reuse
integer
default:"0"
How many times a refresh token can be reused (0 = single use).

Token Claims

SGI uses these custom claims:
  • user_ref_id - External user reference ID
  • authorities - User roles and permissions
  • sub - Subject (Keycloak user ID)
Claims are automatically included in JWT tokens.

Troubleshooting

Realm Import Fails

Problem: Realm import fails with validation errors. Solution:
# Check realm file validity
jq . sgi-auth/realm/sgi-realm.json

# Import with partial import strategy
-Dkeycloak.migration.strategy=IGNORE_EXISTING

Invalid Redirect URI

Problem: Login fails with “Invalid redirect URI” error. Solution: Update the front client’s Valid Redirect URIs to match your frontend URL.

Client Secret Mismatch

Problem: Services can’t authenticate with “invalid_client” error. Solution: Verify client secret matches between Keycloak and service configuration.

User Attribute Missing

Problem: JWT token missing user_ref_id claim. Solution:
  1. Add user_ref_id attribute to user
  2. Configure client mapper:
    • ClientsfrontMappersCreate
    • Mapper Type: User Attribute
    • User Attribute: user_ref_id
    • Token Claim Name: user_ref_id

Next Steps

Build docs developers (and LLMs) love