> ## Documentation Index
> Fetch the complete documentation index at: https://test-8ad8522e-feat-ai-sre.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Keycloak

> 通过 Keycloak 配置 SAML2.0 或 OIDC 协议实现单点登录

Keycloak 是一个开源的身份和访问管理解决方案，提供了一套全面的工具和功能，帮助开发人员快速实现安全的用户身份验证和授权机制。

<Note>
  本篇文章不涉及部署和讲解 Keycloak 相关内容，如需了解更多信息，请参考 [官方文档](https://www.keycloak.org/)。
</Note>

## 协议配置

<Tabs>
  <Tab title="SAML2.0 协议">
    ### 1. 获取 ACS 地址

    登录 Flashduty 控制台，获取 ACS 地址（后续步骤会用到）。

    路径：**访问控制 => 单点登录 => SAML2.0 协议 => Flashduty 服务提供商信息 => Assertion Consumer Service URL**

    ![获取ACS地址](https://docs-cdn.flashcat.cloud/images/png/437194.png)

    ### 2. 创建 Client

    登录 Keycloak 控制台，路径：**Clients => Create client**

    * **Client Type**：选择 SAML 协议
    * **Client ID**：填写 `flashcat.cloud`（固定值，不可更改）

    ![创建Client](https://docs-cdn.flashcat.cloud/images/png/437197.png)

    **Valid redirect URIs**：填写从 Flashduty 获取的 ACS 地址

    ![配置redirect](https://docs-cdn.flashcat.cloud/images/png/437029.png)

    ### 3. 配置 Client 相关信息

    **Name ID format** 更改为 email 类型：

    ![Name ID format](https://docs-cdn.flashcat.cloud/images/png/437031.png)

    **Client signature required** 设置为关闭状态：

    ![关闭签名](https://docs-cdn.flashcat.cloud/images/png/437195.png)

    **创建 Client scope**：

    <Warning>
      创建之前需要先删除之前 OpenID Connect 协议的用户，创建完成设置为 Default。
    </Warning>

    参考下图依次创建 email/phone/username 三种类型：

    ![创建scope](https://docs-cdn.flashcat.cloud/images/png/437033.png)

    创建完成的效果：

    ![scope效果](https://docs-cdn.flashcat.cloud/images/png/437034.png)

    **将添加的用户加入到 Client 中**：

    ![添加用户1](https://docs-cdn.flashcat.cloud/images/png/437037.png)

    ![添加用户2](https://docs-cdn.flashcat.cloud/images/png/437038.png)

    **配置 email/phone/username 映射器**（以 email 为例，其他按照相同步骤配置）：

    ![映射器1](https://docs-cdn.flashcat.cloud/images/png/437057.png)

    ![映射器2](https://docs-cdn.flashcat.cloud/images/png/437058.png)

    ![映射器3](https://docs-cdn.flashcat.cloud/images/png/437060.png)

    ### 4. 下载 XML 文件

    <Note>
      下载的文件是一个压缩包，在本地解压后会有两个 xml 文件，只需要 `idp-metadata.xml` 文件即可。
    </Note>

    在 **Client => Action** 中下载到本地：

    ![下载XML](https://docs-cdn.flashcat.cloud/images/png/437039.png)

    上传 XML 文件到 Flashduty 的单点登录配置中：

    ![上传XML](https://docs-cdn.flashcat.cloud/images/png/437040.png)

    ### 5. 创建用户并测试登录

    创建用户（一定要绑定一个邮箱地址）：

    ![创建用户](https://docs-cdn.flashcat.cloud/images/png/437041.png)

    **登录测试**：访问 `console.flashcat.cloud`，选择 SSO 登录，在域名处填写组织的登录域名（在 **平台管理 → 组织 → 组织信息 → 组织资料** 页面配置）；也可以直接访问 `{域名}.sso.flashcat.cloud` 发起单点登录。

    ![测试登录](https://docs-cdn.flashcat.cloud/images/png/437062.png)
  </Tab>

  <Tab title="OIDC 协议">
    ### 1. 获取 Redirect URL

    登录 Flashduty 控制台，获取 Redirect URL（后续步骤会用到）。

    路径：**访问控制 => 单点登录 => OIDC 协议 => Flashduty 服务提供商信息 => Redirect URL**

    ![获取Redirect URL](https://docs-cdn.flashcat.cloud/images/png/437183.png)

    ### 2. 创建 Client

    登录 Keycloak 控制台，创建新 Client：

    * **Client Type**：选择 OIDC 协议
    * **Client ID**：没有特殊要求

    ![创建Client](https://docs-cdn.flashcat.cloud/images/png/437179.png)

    **Client authentication**：保持开启状态

    ![Client authentication](https://docs-cdn.flashcat.cloud/images/png/437180.png)

    **Valid redirect URIs**：填写第 1 步获取的 Redirect URL 地址

    ![配置redirect](https://docs-cdn.flashcat.cloud/images/png/437184.png)

    ### 3. 获取 Client 相关信息

    * **Client ID**：创建 Client 时填写的 ID
    * **Client Secret**：在 **Client 详情 => Credentials** 卡片中查看

    ![Client Secret](https://docs-cdn.flashcat.cloud/images/png/437186.png)

    * **Issuer**：在 **Realm settings => Endpoints => OpenID Endpoint Configuration** 中查看

    ![Issuer](https://docs-cdn.flashcat.cloud/images/png/437187.png)

    ### 4. 配置 Flashduty 单点登录

    将上述信息填入 Flashduty 单点登录配置：

    ![Flashduty配置](https://docs-cdn.flashcat.cloud/images/png/437188.png)

    <Tip>
      配置完成后，登录测试参考 SAML2.0 协议的第 5 步即可。
    </Tip>
  </Tab>
</Tabs>
