> ## Documentation Index
> Fetch the complete documentation index at: https://docs.linqra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Keycloak Configuration

> Identity and Access Management for Linqra

## Overview

Keycloak serves as the Identity and Access Management (IAM) solution for Linqra, providing robust authentication and authorization capabilities. It manages user identities, roles, and access policies while supporting various authentication protocols including OAuth 2.0 and OpenID Connect.

## Access Information

### Admin Console

```bash theme={null}
URL: http://localhost:8281/
Username: admin
Password: D!pme$Gateway77
```

<Note>
  For production environments, it's crucial to change the default admin credentials to ensure security.
</Note>

<Warning>
  The default credentials (admin/D!pme\$Gateway77) should only be used in development environments.
</Warning>

## Configuration Steps

### 1. Create New Realm

A realm in Keycloak manages a set of users, credentials, roles, and groups. Follow these steps to create a new realm:

1. Log in to the Keycloak Admin Console (admin, admin for dev)
2. Click on the dropdown in the top-left corner (default shows "master")
3. Click "Create Realm"
4. Enter "Linqra" as the Realm name
5. Ensure "Enabled" is switched ON
6. Click "Create"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_new_realm.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=49e19d517ab68e35345c1179fcf8b045" alt="Creating new Linqra realm in Keycloak" width="1874" height="1514" data-path="images/kc_create_new_realm.png" />
</Frame>

<Note>
  The master realm should only be used to create and manage other realms. Create a separate realm for your application.
</Note>

### 2. Create New Client

After creating the realm, you can either import the configuration or set it up manually.

#### Option 1: Import Configuration (Recommended)

You can skip the manual configuration by importing the client file.

1. Download the [Linqra Gateway Client Configuration](/images/linqra-gateway-client.json).
2. In the **Linqra** realm, click "Import client" and upload the file.

<Frame>
  <img src="https://mintcdn.com/linqra/oi7373amwE3JpJXe/images/kc_import_client.png?fit=max&auto=format&n=oi7373amwE3JpJXe&q=85&s=de415b09a3290898d49b447fa3521ee9" alt="Importing client configuration" width="1858" height="672" data-path="images/kc_import_client.png" />
</Frame>

#### Option 2: Manual Configuration

If you prefer to configure it manually, follow these steps:

##### General Settings

1. Navigate to "Clients" in the left sidebar
2. Click "Create client"
3. Set the following general settings:
   * Client ID: `linqra-gateway-client`
   * Name: `Linqra Gateway Client`
   * Description: `Linqra Gateway Client`
   * Always display in UI: `ON`
   * Click "Next"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_clientid_1.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=0da235de1986bc5a6d9d8b4504f6ed9d" alt="Creating new client - General Settings" width="3320" height="1828" data-path="images/kc_create_clientid_1.png" />
</Frame>

##### Access Settings

Configure the client's access settings:

* Root URL: `https://localhost:7777`
* Home URL: `https://localhost:7777`
* Valid redirect URIs: `http://localhost:3000/callback`
* Valid post logout redirect URIs: `http://localhost:3000`
* Web origins: `http://localhost:3000`
* Admin URL: `https://localhost:7777`

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_clientid_2.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=201b25d220a4985ff2218cd8253c69c3" alt="Creating new client - Access Settings" width="3318" height="1726" data-path="images/kc_create_clientid_2.png" />
</Frame>

##### Capability Config

Set up the authentication capabilities:

* Client authentication: `ON`
* Authorization: `OFF`
* Authentication flow:
  * Standard flow: `Enabled`
  * Service accounts roles: `Enabled`
  * All other flows: `Disabled`

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_clientid_3.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=30b37cf3c6c822d686c64741fdc41a9c" alt="Creating new client - Capability Config" width="3282" height="1938" data-path="images/kc_create_clientid_3.png" />
</Frame>

<Note>
  Make sure to adjust the URLs according to your deployment environment. The URLs shown here are for local development.
</Note>

### 3. Create Client Role

After setting up the client, you'll need to create a role for gateway authentication:

1. Navigate to the "Roles" tab in your client settings
2. Click "Create role"
3. Configure the role:
   * Role name: `gateway_admin`
   * Description: `Gateway admin role for the client id: linqra-gateway-client`
4. Click "Save"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_client_role.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=eac4e4dea21bfd4d8529caebd71f1a48" alt="Creating gateway_admin client role" width="3072" height="1322" data-path="images/kc_create_client_role.png" />
</Frame>

<Note>
  The `gateway_admin` role is used by the API Gateway's security system to verify that requests are coming from a valid Linqra Gateway application. This is a system-level role for application authentication, not for user administration.
</Note>

### 4. Create Realm Role

Next, you'll need to create a realm-level role to identify Linqra realm belongings:

1. Navigate to "Realm roles" in the left sidebar
2. Click "Create role"
3. Configure the role:
   * Role name: `gateway_admin_realm`
   * Description: `Role to identify the belongings of the Linqra realm`
4. Click "Save"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_realm_role.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=393d2064bff4243ce4f32668b96f4135" alt="Creating gateway_admin_realm role" width="2670" height="1132" data-path="images/kc_create_realm_role.png" />
</Frame>

<Note>
  The `gateway_admin_realm` role is used at the realm level to identify and manage resources that belong to the Linqra realm. This is distinct from the client-specific `gateway_admin` role created earlier.
</Note>

### 5. Assign Service Account Roles

Now you need to assign both the client and realm roles to the service account:

1. Navigate to your client's "Service accounts roles" tab
2. You'll see the service account username: `service-account-linqra-gateway-client`
3. Assign the roles:

   First, assign the client role:

   * Click "Assign role"
   * Select "Filter by client" and choose "linqra-gateway-client"
   * Select `gateway_admin`
   * Click "Assign"

   Then, assign the realm role:

   * Click "Assign role" again
   * Select "Filter by realm roles"
   * Select `gateway_admin_realm`
   * Click "Assign"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_assign_service_account_roles.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=b0c0f798bbd13c6f923fb62629ad8ef6" alt="Assigning roles to service account" width="2710" height="1348" data-path="images/kc_assign_service_account_roles.png" />
</Frame>

<Note>
  The service account needs both roles to function properly:

  * `gateway_admin`: For client-level authentication
  * `gateway_admin_realm`: For realm-level resource access
</Note>

<Warning>
  Make sure to assign both roles as they are essential for the gateway's proper operation. Missing role assignments can lead to authentication failures.
</Warning>

### 6. Create Client Scopes

Next, create the necessary client scopes for gateway and team-based access control.

#### a. Gateway Read Scope

1. Navigate to "Client scopes" in the left sidebar
2. Click "Create client scope"
3. Configure the scope:
   * Name: `gateway.read`
   * Description: `To read the gateway end points`
   * Type: `Default`
   * Display on consent screen: `ON`
   * Include in token scope: `ON`
4. Click "Save"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_create_client_scope.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=1ac31d59aa13e269d2fc6d6e92c6c782" alt="Creating gateway.read client scope" width="2166" height="1794" data-path="images/kc_create_client_scope.png" />
</Frame>

<Note>
  Client scopes help in defining sets of protocol mappers and role scope mappings that can be shared between multiple clients. The `gateway.read` scope provides the necessary permissions for reading gateway endpoints.
</Note>

#### b. Team Scope

1. In the "Client scopes" section, click "Create client scope" again.
2. Configure the scope:
   * Name: `team.scope`
   * Description: `Restricts access to specific teams`
   * Type: `Default`
   * Display on consent screen: `ON`
   * Include in token scope: `ON`
3. Click "Save"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_teamsscope.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=0671e2b6ab745549dabf8b8f219a14fa" alt="Creating team.scope client scope" width="1360" height="1568" data-path="images/kc_teamsscope.png" />
</Frame>

Now, add a hardcoded claim mapper to this scope:

1. Go to the `team.scope` client scope you just created.
2. Navigate to the "Mappers" tab.
3. Click "Create" to add a new mapper.
4. Configure the mapper:
   * Name: `teams`
   * Category: `Token mapper`
   * Mapper Type: `Hardcoded claim`
   * Claim name: `teams`
   * Claim value: `["67d0aeb17172416c411d419e"]`
   * Claim JSON type: `JSON`
   * Add to ID token: `ON`
   * Add to access token: `ON`
   * Add to userinfo: `ON`
5. Click "Save"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_teamsscopemapper.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=25338ba40e7115a4d4f9a08a51d9317e" alt="Creating teams hardcoded claim mapper in team.scope" width="1170" height="1664" data-path="images/kc_teamsscopemapper.png" />
</Frame>

<Note>
  You can add more team IDs in the claim value as a comma-separated list, e.g. `["teamid1","teamid2"]`.\
  This provides an extra layer of security, ensuring that only users belonging to the specified teams can access the application.
</Note>

### 7. Assign Roles to Client Scope

After creating the client scope, you need to assign both client and realm roles to it:

1. Navigate to the `gateway.read` client scope
2. Go to the "Scope" tab
3. Assign the roles:

   First, assign the client role:

   * Click "Assign role"
   * Select "Filter by client" and choose "linqra-gateway-client"
   * Select `gateway_admin`
   * Click "Assign"

   Then, assign the realm role:

   * Click "Assign role" again
   * Select "Filter by realm roles"
   * Select `gateway_admin_realm`
   * Click "Assign"

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_assign_roles_to_client_scope.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=4d24d280f4a5aa087157a6c4c562fc5c" alt="Assigning roles to gateway.read client scope" width="3112" height="1302" data-path="images/kc_assign_roles_to_client_scope.png" />
</Frame>

<Note>
  These role assignments determine what permissions are included when the `gateway.read` scope is used. Users must have at least one of these roles to use this client scope.
</Note>

<Warning>
  If no role scope mapping is defined, any user can use this client scope. Adding role mappings restricts the scope to users with the specified roles.
</Warning>

### 8. Add Client Scopes to Client

Finally, you need to add both the `gateway.read` and `team.scope` scopes to the `linqra-gateway-client`:

1. Navigate to the `linqra-gateway-client` settings
2. Go to the "Client scopes" tab
3. Click "Add client scope"
4. In the popup:
   * Find and select `gateway.read`
   * Make sure it's set as "Default"
   * Click "Add"
5. Repeat the process to add `team.scope` as a "Default" client scope as well

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_add_client_scope_to_the_client.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=75db50aed1d8c1568f599a7c38e10a7a" alt="Adding gateway.read and team.scope to linqra-gateway-client" width="2854" height="1516" data-path="images/kc_add_client_scope_to_the_client.png" />
</Frame>

<Note>
  Setting both scopes as "Default" ensures they will be automatically included in every token request for this client, providing both endpoint and team-based access control.
</Note>

<Warning>
  This is a crucial final step that links the client scope to the client. Without this connection, the client won't receive the necessary permissions in its tokens.
</Warning>

### 9. Frontend URL Configuration

The Frontend URL configuration varies depending on your deployment environment:

#### Standalone Environment

If running Keycloak standalone:

```bash theme={null}
Frontend URL: http://localhost:8281
```

#### Containerized Environment

If running in Docker/containers:

```bash theme={null}
Frontend URL: http://keycloak-service:8080
```

<Frame>
  <img src="https://mintcdn.com/linqra/S2P68q9lpseV7vU7/images/kc_frontend_url_standalone.png?fit=max&auto=format&n=S2P68q9lpseV7vU7&q=85&s=60cb2c1e943305916803e5a0e343f503" alt="Setting Frontend URL" width="2746" height="1938" data-path="images/kc_frontend_url_standalone.png" />
</Frame>

<Warning>
  Make sure to use the correct URL based on your deployment:

  * Use `http://localhost:8281` for standalone deployment
  * Use `http://keycloak-service:8080` when running in containers with Docker Compose
    Using the wrong URL will cause authentication flows to fail.
</Warning>

<Note>
  The Frontend URL setting is crucial for proper redirection in authentication flows. It tells Keycloak which base URL to use when generating redirect URLs and other authentication-related endpoints.
</Note>
