> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/maiagent-user-guide/maiagent-user-guide-en/org/sso/sso-keycloak.md).

# Keycloak Integration

Keycloak is a widely used open-source identity verification and access management platform. MaiAgent supports integration with Keycloak via the OpenID Connect protocol.

**Target audience**: Enterprises using Keycloak for unified identity management, or organizations that need to manage users by Realm

**Key advantages**:

* Supports Keycloak Realm isolation
* Automatically synthesizes virtual Email identities for accounts without email addresses
* Supports server-side Token revocation (Logout)
* Integrates with the Keycloak ecosystem features

## Setup Steps

### 1. Create a Client in Keycloak

1. Log in to the Keycloak Admin Console
2. Select the target Realm
3. Add a new Client under <mark style="color:blue;">Clients</mark>
4. Set Client Protocol to `openid-connect`
5. Set Access Type to `public` (or choose `confidential` based on security requirements)
6. Set Valid Redirect URIs to the MaiAgent frontend domain
7. Note down the Client ID

### 2. Fill in Keycloak Settings in the MaiAgent Backend

After selecting the <mark style="color:blue;">Keycloak</mark> authentication type, fill in the following fields:

| Field                      | Required | Description                                                                  |
| -------------------------- | -------- | ---------------------------------------------------------------------------- |
| Authentication Source Name | Yes      | Used as the Realm name mapping; using the Keycloak Realm name is recommended |
| Email Domain               | Yes      | The domain used for synthesizing virtual Emails, e.g., `company.com`         |
| Server URL                 | Yes      | Keycloak server URL, e.g., `https://keycloak.company.com/`                   |

After completing the fields, the system will automatically generate the **Login URL** in the following format:

```
https://{frontend-domain}/auth/keycloak?url={Server URL}&clientId={Client ID}&realm={Realm}
```

### 3. Save and Test

Click <mark style="color:blue;">Save</mark> in the MaiAgent backend, then test the login flow using the generated Login URL.

## Keycloak Virtual Email Mechanism

Since Keycloak users may not have email addresses, MaiAgent automatically synthesizes a virtual Email as the unique identifier:

```
Virtual Email = {preferred_username}@{email_domain}
```

For example, the user `john.doe` with an Email Domain of `company.com` would generate `john.doe@company.com` as their MaiAgent user Email.

{% hint style="info" %}
This virtual Email is used solely for internal system identification and will not be used for actual email delivery.
{% endhint %}

## Keycloak Logout

MaiAgent's Keycloak integration supports server-side logout. When a user logs out of MaiAgent, the system simultaneously sends a Token revocation request to the Keycloak server, ensuring the Token is also invalidated on the Keycloak side.

## Settings Quick Reference

| Field                      | Required | Format                                              | Example                         |
| -------------------------- | -------- | --------------------------------------------------- | ------------------------------- |
| Authentication Source Name | Yes      | Lowercase alphanumeric + hyphens, max 31 characters | `company-kc`                    |
| Email Domain               | Yes      | Domain format                                       | `company.com`                   |
| Server URL                 | Yes      | HTTPS URL                                           | `https://keycloak.company.com/` |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.maiagent.ai/maiagent-user-guide/maiagent-user-guide-en/org/sso/sso-keycloak.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
