单页应用程序 (SPA) 的单点登录 (SSO) solution/architecture

Single Sign On (SSO) solution/architecture for Single Page App (SPA)

我研究 SPA 的 SSO 解决方案已有一段时间了。有很多解决方案之间存在细微差别,同时我还发现并不是每个人都对 SSO 有相同的理解,而且 SPA 的 SSO 既定模式也不多。因此,我不是要详细的 design/architecture,只是想看看是否有关于这个主题的任何常见做法。

SSO 是什么意思?

  1. 我们有几个新的 SPA 正在开发中(也可能是移动和平板电脑应用程序),它们将部署在不同的服务器和不同的域中。
  2. 我们还有一个中央 IdP (authServer),所有用户身份都将存储在其中。
  3. 一旦我登录到 SPA1 并单击一个按钮,它会将我带到 SPA2(或 SPA3, SPA4, 可能), 我不必输入用户凭据并且会自动登录。

SPA有什么区别? (相对于常规网络应用程序)

我看过一些解决方案,甚至是像 SAML 这样的旧解决方案(只是想了解一下 SSO..)。我目前的候选人是 OpenId Connect,但后来我意识到 SPA 的不同之处,如果我的理解是正确的:与常规网络应用程序不同,SPA 通常没有(或者我们尽量没有)后端服务器。 SPA 所拥有的只是一个服务于静态页面以及脚本、样式表和图像的服务器。

问题来了:

OpenId Connect 基于 OAuth2 授权码 授权类型,这意味着:

  1. 如果我想让每个 SPA 工作,我需要一个类似后端代理的模块。
  2. 我使用不同的解决方案来进行客户端 SSO,例如 the one auth0 provides
  3. 我还没有找到其他 solution/examples

我的问题:

对于以上第1点,我的理解正确吗?最好不要让 SPA 像普通 Web 应用程序那样拥有后端代码吗?

对于第 2 点,这听起来像是一个解决方案,但这与 OAuth2 Implicit 授权类型有何本质区别?

还有其他我应该知道但尚未探索的解决方案(框架、协议等)吗?

除了使用授权码授权的基本客户端配置文件外,OpenID Connect 还具有基于 OAuth 2.0 隐式授权的隐式客户端配置文件。此配置文件允许将令牌直接交付给 in-browser/Javascript 客户端,而无需涉及后端。

如今,代码流与跨源资源共享 (CORS) and Proof Key of Code Exchange (PKCE) 相结合,也可用于单页应用程序。

"OAuth 2.0 for Browser-Based Apps"-Draft describes in detail the current steps you should use to implement OAuth/OpenID Connect for SPAs in a secure way. In Sec. 4, "Overview"当前最佳实践总结:

In recent years, widespread adoption of Cross-Origin Resource Sharing (CORS), which enables exceptions to the same-origin policy, allows browser-based apps to use the OAuth 2.0 authorization code flow and make a POST request to exchange the authorization code for an access token at the token endpoint. In this flow, the access token is never exposed in the less secure front-channel. Furthermore, adding PKCE to the flow ensures that even if an authorization code is intercepted, it is unusable by an attacker.

For this reason, and from other lessons learned, the current best practice for browser-based applications is to use the OAuth 2.0 authorization code flow with PKCE.

草案恢复了以下要点,您在为 SPA(或其他 public 客户)实施 OAuth/OpenID 时应考虑:

Browser-based applications:

  • MUST use the OAuth 2.0 authorization code flow with the PKCE extension when obtaining an access token

  • MUST Protect themselves against CSRF attacks by either:

    • ensuring the authorization server supports PKCE, or

    • by using the OAuth 2.0 "state" parameter or the OpenID Connect "nonce" parameter to carry one-time use CSRF tokens

  • MUST Register one or more redirect URIs, and use only exact registered redirect URIs in authorization requests