Help us learn about your current experience with the documentation. Take the survey.

使用 AWS Cognito 作为 OAuth 2.0 身份验证提供商

  • 版本:免费、高级、旗舰
  • 产品:GitLab 自管版

Amazon Web Services (AWS) Cognito 让您可以为您的 GitLab 实例添加用户注册、登录和访问控制功能。 本文档将介绍如何将 AWS Cognito 启用为 OAuth 2.0 提供商。

配置 AWS Cognito

要启用 AWS Cognito OAuth 2.0 OmniAuth 提供商,您需要在 Cognito 中注册您的应用程序。此过程会为您的应用程序生成 Client ID 和 Client Secret。 要将 AWS Cognito 启用为身份验证提供商,请完成以下步骤。您之后可以随时修改这些配置。

  1. 登录 AWS 控制台

  2. 服务 菜单中,选择 Cognito

  3. 选择 管理用户池,然后在右上角选择 创建用户池

  4. 输入用户池名称,然后选择 逐步设置

  5. 您希望最终用户如何登录? 下,选择 电子邮件地址或电话号码允许电子邮件地址

  6. 您需要哪些标准属性? 下,选择 email

  7. 根据您的需求配置其余设置。在基本设置中,这些设置不会影响 GitLab 的配置。

  8. App clients 设置中:

    1. 选择 添加 App client
    2. 添加 App client 名称
    3. 选中 启用基于用户名密码的身份验证 复选框。
  9. 选择 创建 App client

  10. 设置用于发送电子邮件的 AWS Lambda 函数,并完成用户池的创建。

  11. 创建用户池后,转到 App client 设置 并提供所需信息:

    • 启用的身份提供商 - 全选
    • 回调 URL - https://<your_gitlab_instance_url>/users/auth/cognito/callback
    • 允许的 OAuth 流程 - 授权码授予
    • 允许的 OAuth 2.0 范围 - emailopenidprofile
  12. 保存 App client 设置的更改。

  13. 域名 下,为您的 AWS Cognito 应用程序添加 AWS 域名。

  14. App Clients 下,找到您的 App client ID。选择 显示详情 以显示 App client 密钥。这些值分别对应 OAuth 2.0 的 Client ID 和 Client Secret。请保存这些值。

配置 GitLab

  1. 配置通用设置,将 cognito 添加为单点登录 (SSO) 提供商。这将为没有现有 GitLab 账户的用户启用即时 (Just-In-Time) 账户配置功能。

  2. 在您的 GitLab 服务器上,打开配置文件。对于 Linux 软件包安装:

    sudo editor /etc/gitlab/gitlab.rb
  3. 在以下代码块中,将您的 AWS Cognito 应用程序信息填入以下参数:

    • app_id:您的客户端 ID。
    • app_secret:您的客户端密钥。
    • site:您的 Amazon 域和区域。

    将以下代码块包含在 /etc/gitlab/gitlab.rb 文件中:

    gitlab_rails['omniauth_allow_single_sign_on'] = ['cognito']
    gitlab_rails['omniauth_providers'] = [
      {
        name: "cognito",
        label: "Provider name", # 登录按钮的可选标签,默认为 "Cognito"
        icon: nil,   # 可选的图标 URL
        app_id: "<client_id>",
        app_secret: "<client_secret>",
        args: {
          scope: "openid profile email",
          client_options: {
            site: "https://<your_domain>.auth.<your_region>.amazoncognito.com",
            authorize_url: "/oauth2/authorize",
            token_url: "/oauth2/token",
            user_info_url: "/oauth2/userInfo"
          },
          user_response_structure: {
            root_path: [],
            id_path: ["sub"],
            attributes: { nickname: "email", name: "email", email: "email" }
          },
          name: "cognito",
          strategy_class: "OmniAuth::Strategies::OAuth2Generic"
        }
      }
    ]
  4. 保存配置文件。

  5. 保存文件并重新配置 GitLab 以使更改生效。

现在,您的登录页面应该在常规登录表单下方显示一个 Cognito 选项。 选择此选项以开始身份验证流程。 然后,AWS Cognito 会要求您登录并授权 GitLab 应用程序。 如果授权成功,您将被重定向并登录到您的 GitLab 实例。

更多信息,请参阅配置通用设置