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

使用 Generic OAuth2 gem 作为 OAuth 2.0 认证提供程序

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab Self-Managed

如果您的提供程序支持 OpenID 规范,您应该使用 omniauth-openid-connect 作为您的认证提供程序。

omniauth-oauth2-generic gem 允许在 GitLab 和您的 OAuth 2.0 提供程序之间实现单点登录(SSO),或者与此 gem 兼容的任何 OAuth 2.0 提供程序。

此策略允许配置此 OmniAuth SSO 过程:

  1. 策略将客户端引导至您的授权 URL(可配置),并使用指定的 ID 和密钥。
  2. OAuth 2.0 提供程序处理请求、用户身份验证(可选)以及访问用户资料的授权。
  3. OAuth 2.0 提供程序将客户端引导回 GitLab,策略在此处获取访问令牌。
  4. 策略使用访问令牌从 可配置 的"用户资料"URL 请求用户信息。
  5. 策略使用 可配置 的格式解析响应中的用户信息。
  6. GitLab 查找或创建返回的用户并为其登录。

此策略:

  • 只能用于单点登录,不提供任何 OAuth 2.0 提供程序授予的其他访问权限。例如,导入项目或用户。
  • 仅支持授权码流程(Authorization Grant flow),这是 GitLab 等客户端-服务器应用程序最常见的流程。
  • 无法从多个 URL 获取用户信息。
  • 无法从 JWT 格式的访问令牌中获取用户信息。
  • 除 JSON 外,尚未测试其他用户信息格式。

配置 OAuth 2.0 提供程序

要配置提供程序:

  1. 在您想要认证的 OAuth 2.0 提供程序中注册您的应用程序。

    注册应用程序时提供的重定向 URI 应该是:

    http://your-gitlab.host.com/users/auth/oauth2_generic/callback

    您现在应该能够获取客户端 ID 和客户端密钥。这些信息在不同提供程序中出现的位置不同。这也可能被称为应用程序 ID 和应用程序密钥。

  2. 在您的 GitLab 服务器上,完成以下步骤。

    1. 配置 通用设置 以添加 oauth2_generic 作为单点登录提供程序。这为没有现有 GitLab 账户的用户启用即时账户配置。

    2. 编辑 /etc/gitlab/gitlab.rb 以添加您提供程序的配置。例如:

      gitlab_rails['omniauth_providers'] = [
        {
          name: "oauth2_generic",
          label: "提供程序名称", # 登录按钮的可选标签,默认为 "Oauth2 Generic"
          app_id: "<your_app_client_id>",
          app_secret: "<your_app_client_secret>",
          args: {
            client_options: {
              site: "<your_auth_server_url>",
              user_info_url: "/oauth2/v1/userinfo",
              authorize_url: "/oauth2/v1/authorize",
              token_url: "/oauth2/v1/token"
            },
            user_response_structure: {
              root_path: [],
              id_path: ["sub"],
              attributes: {
                email: "email",
                name: "name"
              }
            },
            authorize_params: {
              scope: "openid profile email"
            },
            strategy_class: "OmniAuth::Strategies::OAuth2Generic"
          }
        }
      ]
    3. 保存文件并重新配置 GitLab:

      sudo gitlab-ctl reconfigure
    1. 配置 通用设置 以添加 oauth2_generic 作为单点登录提供程序。这为没有现有 GitLab 账户的用户启用即时账户配置。

    2. 导出 Helm 值:

      helm get values gitlab > gitlab_values.yaml
    3. 将以下内容放入名为 oauth2_generic.yaml 的文件中,用作 Kubernetes Secret

      name: "oauth2_generic"
      label: "Provider name" # 登录按钮的可选标签,默认为 "Oauth2 Generic"
      app_id: "<your_app_client_id>"
      app_secret: "<your_app_client_secret>"
      args:
        client_options:
          site: "<your_auth_server_url>"
          user_info_url: "/oauth2/v1/userinfo"
          authorize_url: "/oauth2/v1/authorize"
          token_url: "/oauth2/v1/token"
        user_response_structure:
          root_path: []
          id_path: ["sub"]
          attributes:
            email: "email"
            name: "name"
        authorize_params:
          scope: "openid profile email"
        strategy_class: "OmniAuth::Strategies::OAuth2Generic"
    4. 创建 Kubernetes Secret:

      kubectl create secret generic -n <namespace> gitlab-oauth2-generic --from-file=provider=oauth2_generic.yaml
    5. 编辑 gitlab_values.yaml 并添加提供程序配置:

      global:
        appConfig:
          omniauth:
            providers:
              - secret: gitlab-oauth2-generic
    6. 保存文件并应用新值:

      helm upgrade -f gitlab_values.yaml gitlab gitlab/gitlab
    1. 配置 通用设置 以添加 oauth2_generic 作为单点登录提供程序。这为没有现有 GitLab 账户的用户启用即时账户配置。

    2. 编辑 /home/git/gitlab/config/gitlab.yml

      production: &base
        omniauth:
          providers:
            - { name: "oauth2_generic",
                label: "Provider name", # 登录按钮的可选标签,默认为 "Oauth2 Generic"
                app_id: "<your_app_client_id>",
                app_secret: "<your_app_client_secret>",
                args: {
                  client_options: {
                    site: "<your_auth_server_url>",
                    user_info_url: "/oauth2/v1/userinfo",
                    authorize_url: "/oauth2/v1/authorize",
                    token_url: "/oauth2/v1/token"
                  },
                  user_response_structure: {
                    root_path: [],
                    id_path: ["sub"],
                    attributes: {
                      email: "email",
                      name: "name"
                    }
                  },
                  authorize_params: {
                    scope: "openid profile email"
                  },
                  strategy_class: "OmniAuth::Strategies::OAuth2Generic"
                }
              }
    3. 保存文件并重启 GitLab:

      # 对于运行 systemd 的系统
      sudo systemctl restart gitlab.target
      
      # 对于运行 SysV init 的系统
      sudo service gitlab restart

在登录页面上,常规登录表单下方现在应该会出现一个新的图标。选择该图标开始您提供程序的认证过程。这会将浏览器引导至您的 OAuth 2.0 提供程序的认证页面。如果一切顺利,您将被返回到您的 GitLab 实例并登录。