使用 Microsoft Azure 作为 OAuth 2.0 认证提供商
- Tier: 免费版, 高级版, 终极版
- Offering: GitLab 自托管
你可以启用 Microsoft Azure OAuth 2.0 OmniAuth 提供商,并使用你的 Microsoft Azure 凭据登录 GitLab。
如果你是第一次将 GitLab 与 Azure/Entra ID 集成, 请配置 OpenID Connect 协议, 该协议使用 Microsoft identity platform (v2.0) 端点。
迁移到通用 OpenID Connect 配置
在 GitLab 17.0 及更高版本中,使用 azure_oauth2 的实例必须迁移到通用 OpenID Connect 配置。更多信息,请参阅 迁移到 OpenID Connect 协议。
注册 Azure 应用
要启用 Microsoft Azure OAuth 2.0 OmniAuth 提供商,你必须注册一个 Azure 应用并获取客户端 ID 和密钥。
-
登录 Azure 门户。
-
如果你有多个 Azure Active Directory 租户,请切换到所需的租户。记录租户 ID。
-
注册应用 并提供以下信息:
- 重定向 URI,需要你的 GitLab 安装的 Azure OAuth 回调 URL。
https://gitlab.example.com/users/auth/azure_activedirectory_v2/callback。 - 应用类型,必须设置为 Web。
- 重定向 URI,需要你的 GitLab 安装的 Azure OAuth 回调 URL。
-
保存客户端 ID 和客户端密钥。客户端密钥只显示一次。
如果需要,你可以 创建新的应用密钥。
client ID 和 client secret 是与 OAuth 2.0 相关的术语。
在某些 Microsoft 文档中,这些术语被称为 Application ID 和 Application Secret。
添加 API 权限(范围)
创建应用后,将其配置为暴露 Web API。 在 Microsoft Graph API 下添加以下委派权限:
emailopenidprofile
或者,添加 User.Read.All 应用权限。
在 GitLab 中启用 Microsoft OAuth
对于新项目,你应该使用 OpenID Connect 协议, 该协议使用 Microsoft identity platform (v2.0) 端点。
-
在你的 GitLab 服务器上,打开配置文件。
-
对于 Linux 包安装:
sudo editor /etc/gitlab/gitlab.rb -
对于自编译安装:
cd /home/git/gitlab sudo -u git -H editor config/gitlab.yml
-
-
配置 通用设置 以添加
azure_activedirectory_v2作为单点登录提供商。这将为没有现有 GitLab 账户的用户启用即时账户配置。 -
添加提供商配置。将
<client_id>、<client_secret>和<tenant_id>替换为你注册 Azure 应用时获得的值。-
对于 Linux 包安装:
gitlab_rails['omniauth_providers'] = [ { "name" => "azure_activedirectory_v2", "label" => "提供商名称", # 登录按钮的可选标签,默认为 "Azure AD v2" "args" => { "client_id" => "<client_id>", "client_secret" => "<client_secret>", "tenant_id" => "<tenant_id>", } } ] -
对于 替代 Azure 云, 在
args部分配置base_azure_url。例如,对于 Azure Government Community Cloud (GCC):gitlab_rails['omniauth_providers'] = [ { "name" => "azure_activedirectory_v2", "label" => "提供商名称", # 登录按钮的可选标签,默认为 "Azure AD v2" "args" => { "client_id" => "<client_id>", "client_secret" => "<client_secret>", "tenant_id" => "<tenant_id>", "base_azure_url" => "https://login.microsoftonline.us" } } ] -
对于自编译安装:
对于 v2.0 端点:
- { name: 'azure_activedirectory_v2', label: '提供商名称', # 登录按钮的可选标签,默认为 "Azure AD v2" args: { client_id: "<client_id>", client_secret: "<client_secret>", tenant_id: "<tenant_id>" } }对于 替代 Azure 云, 在
args部分配置base_azure_url。例如,对于 Azure Government Community Cloud (GCC):- { name: 'azure_activedirectory_v2', label: '提供商名称', # 登录按钮的可选标签,默认为 "Azure AD v2" args: { client_id: "<client_id>", client_secret: "<client_secret>", tenant_id: "<tenant_id>", base_azure_url: "https://login.microsoftonline.us" } }
你还可以选择将 OAuth 2.0 范围 参数的
scope添加到args部分。默认值为openid profile email。 -
-
保存配置文件。
-
如果你使用 Linux 包安装,请 重新配置 GitLab, 如果你自编译安装,请 重启 GitLab。
-
刷新 GitLab 登录页面。登录表单下方应显示 Microsoft 图标。
-
选择该图标。登录 Microsoft 并授权 GitLab 应用。
阅读 为现有用户启用 OmniAuth 了解现有 GitLab 用户如何连接到他们的新 Azure AD 账户。
故障排除
用户登录横幅消息:Extern UID 已被占用
登录时,你可能会收到一条错误消息,显示 Extern UID has already been taken。
要解决此问题,使用 Rails 控制台 检查是否有现有用户与该账户关联:
-
查找
extern_uid:id = Identity.where(extern_uid: '<extern_uid>') -
打印内容以查找与该
extern_uid关联的用户名:pp id
如果 extern_uid 与某个账户关联,你可以使用该用户名登录。
如果 extern_uid 未与任何用户名关联,这可能是因为删除错误导致出现幽灵记录。
运行以下命令删除身份以释放 extern uid:
Identity.find('<id>').delete