# 更新 discourse-plugin-office365-auth 以修复错误？

**URL:** https://meta.discourse.org/t/updated-discourse-plugin-office365-auth-due-to-bug/167484
**Category:** SSO
**Created:** [2020年十月17日 22:48 UTC](https://meta.discourse.org/t/updated-discourse-plugin-office365-auth-due-to-bug/167484 "2020-10-17T22:48:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![IT-Sumpfling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/it-sumpfling/32/197353_2.png) [@IT-Sumpfling](https://meta.discourse.org/u/IT-Sumpfling)
#### Post date: [2020年十月17日 22:48 UTC](https://meta.discourse.org/t/updated-discourse-plugin-office365-auth-due-to-bug/167484/1 "2020-10-17T22:48:53Z")

</div>

你好，

今天我尝试将我们论坛的认证扩展到 Azure AD / Office 365。（我已经配置了到内部 AD 的 SAML，但越来越多的用户迁移到了 Azure AD。）

我尝试了三个可以实现此功能的插件：

> **[GitHub - discourse/discourse-azure-ad](https://github.com/discourse/discourse-azure-ad)**
>
> Contribute to discourse/discourse-azure-ad development by creating an account on GitHub.

> **[GitHub - discourse/discourse-openid-connect: Allows an OpenID Connect provider to be used as an...](https://github.com/discourse/discourse-openid-connect)**
>
> Allows an OpenID Connect provider to be used as an authentication provider for Discourse

以及

> **[GitHub - discourse/discourse-microsoft-auth](https://github.com/discourse/discourse-microsoft-auth)**
>
> Contribute to discourse/discourse-microsoft-auth development by creating an account on GitHub.

遗憾的是，discourse-azure-ad 没有任何反应——它甚至在登录/注册屏幕上都没有显示按钮。

openid-connect 可以工作，但由于 Azure 的“所有租户”端点（[https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration）在](https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration%EF%BC%89%E5%9C%A8) URI 的“issuer”字段中返回了 …/{tenantid}/…，因此它仅适用于我们自己的 Azure AD/Office 365 租户中的用户（而不适用于其他租户的用户）。

因此，Office365-Auth 插件是剩下的最后一个选择。  
它确实可以工作（也适用于来自其他 Azure AD 租户的用户），但存在一个 bug：如果 Azure AD 登录的电子邮件不是（已存在的）Discourse 用户的主要电子邮件，则登录会失败：  
即：

 ![grafik](https://global.discourse-cdn.com/meta/original/3X/e/c/ec747b9672f13d1c7e10a67f557c007d6b62df21.png)

我需要添加第二个地址，以便在注册时将登录匹配到用户——但只要该地址是“次要”的，登录就会失败并报错（“您无权执行此操作”）（但注册可以成功！）。  
此外，该插件不允许我在用户屏幕上“关联”新的登录方式（这是一个很酷的新功能 🙂 ）。

我迅速通过这段代码识别出了电子邮件导致的登录问题：

> <https://github.com/discourse/discourse-microsoft-auth/blob/50e21994159574c29f66dfa272f643da451e37d2/plugin.rb#L21-L34>

我对 Ruby 和 Discourse 还不够熟悉，无法完全确定，但我认为这段代码会在任一电子邮件不匹配时尝试更新主要电子邮件（当存在多个电子邮件且主要电子邮件不是来自 O365 的那个时，这将失败）。

尽管如此，openid-connect 正确地处理了这部分，甚至提供了一个设置来更新电子邮件（或不更新），并允许在用户屏幕上关联登录。因此，我厚着脸皮将 openid-connect 的相关代码行复制到了 office365-auth 中，注释掉了 office365-auth 中的 “after\_authenticate”，现在实现了来自任何 Azure AD 租户的登录，并且只要电子邮件是用户拥有的任一电子邮件，就能进行关联。

相关更改已在我的 GitHub 仓库中，我将创建一个拉取请求。但还有一个问题未解决 ☹  
由于我添加了连接/撤销的功能，现在该提供商在用户个人资料中可见——但它的名称却缺失了？！:

 ![grafik](https://global.discourse-cdn.com/meta/original/3X/3/4/34c79e3f42657cca5a64dd0f007bfb2f50b8c684.png)

我该如何添加它？有关联的文档链接吗？仅仅将其输入到 client.en.yml 中似乎还不够……或者我遗漏了其他什么。  
在修复这个问题后，我很乐意为更新/修复后的提供商提交拉取请求……

---

<div class="post-metadata">

### Author: ![IT-Sumpfling](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/it-sumpfling/32/197353_2.png) [@IT-Sumpfling](https://meta.discourse.org/u/IT-Sumpfling)
#### Post date: [2020年十月24日 09:32 UTC](https://meta.discourse.org/t/updated-discourse-plugin-office365-auth-due-to-bug/167484/2 "2020-10-24T09:32:45Z")

</div>

> [@IT-Sumpfling](#):
>
> 相关更改已在我的 Github 仓库中，我将创建拉取请求。但还有一个问题：☹  
> 由于我添加了连接/撤销的功能，现在提供者显示在用户个人资料中，但缺少它的名称？!:
> 
> ![grafik](https://global.discourse-cdn.com/meta/original/3X/3/4/34c79e3f42657cca5a64dd0f007bfb2f50b8c684.png)
> 
> 我该如何添加这个？有关联文档的链接吗？仅仅将其输入到 client.en.yml 似乎不够……或者我漏掉了什么。  
> 解决这个问题后，我很乐意为更新/修复后的提供者提交拉取请求……

好的，算了。肯定是在最后一次（相关）更改后忘了执行 `./launcher rebuild app` ☹

不过，我在从 OAuth2Authenticator 切换到 ManagedAuthenticator 时肯定也犯了一些错误→所以，回到编程阶段。等真正修复后（关联已生效，但撤销不行）我会再次发帖。
