javascript - 使用 chrome.identity.launchWebAuthFlow 时如何防止弹出窗口提前关闭?

标签 javascript google-chrome-extension oauth azure-active-directory

我正在从 Chrome 扩展程序的后台脚本调用 chrome.identity.launchWebAuthFlow

这用于通过 https://login.microsoftonline.com 获得隐式用户身份验证

登录弹出窗口看起来很好,允许输入用户凭据。但是,提交凭据后,窗口不会重定向到我的回调重定向 uri。相反,它会重定向到不需要用户操作的另一个页面(这是 Microsoft Azure AD 中的某种公司登录页面)。

此时窗口关闭,我在 launchWebAuthFlow 回调函数中得到未定义响应。同时我收到控制台错误:Unchecked runtime.lastError: 无法加载授权页面。

相同的流程在浏览器窗口中运行良好:在提供凭据并重定向到公司登录页面后,它最终重定向到我的重定向网址(已准备好身份验证 token )。

据此我得出结论,launchWebAuthFlow 不会等到重定向完成到我的重定向页面,而是提前关闭弹出窗口。

有办法避免这种情况吗?

最佳答案

我在这方面花了太多时间,但找到了解决方法。

仍然不确定为什么 launchWebAuthFlow 会有这样的行为。但我决定以其他方式解决。

发生这种双重重定向是因为 Microsoft 身份验证页面首先需要输入纯电子邮件地址。这允许它决定重定向到哪个公司身份验证页面。即使它重用当前 session (cookie),也会发生这种情况。

因此,我寻找一种方法来缩短路线,并为第一个身份验证页面提供有关接下来应该转到哪个公司页面的提示。为此,您必须向 launchWebAuthFlow 使用的 URL 添加一个参数:

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize
?client_id={client-id}
&response_type=token
&<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc7c4ccc2c5f4c3c2c5df96ded8ced9ebcfc4c6cac2c585c8c4c6" rel="noreferrer noopener nofollow">[email protected]</a>
&prompt=login
&redirect_uri={redirect-uri}
&scope={some-scope}

请注意,我还必须添加prompt=login。它可以在没有浏览器的情况下工作(实际上,使用事件 session 时速度更快)。但是,如果从 chrome.identity.launchWebAuthFlow

调用,则需要这样做

希望这对遇到类似情况的人有所帮助。

更多详细信息请参见:Microsoft identity platform and Implicit grant flow

关于javascript - 使用 chrome.identity.launchWebAuthFlow 时如何防止弹出窗口提前关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60745780/

相关文章:

javascript - TypeScript接口(interface)自定义UUID

javascript - Javascript 中基于属性的搜索

jquery-ui - Chrome 扩展内容脚本中的 JQueryUI

javascript - 在 Chrome 扩展内容脚本中,我必须在处理文档之前等待 document.ready 吗?

oauth - 手动 Twitter REST API 身份验证

rest - 无法通过 OAuth 使用 Magento REST API

javascript - Safari 不读取 Jquery 表单提交上的提交按钮值

javascript - es6 如何在 if 语句中使用 console.log ?

javascript - 网络扩展 : Why does the browser add a trailing slash to the requested URL?

Android 谷歌 Oauth2 ContactService