I don't know if I just have some kind of blind spot or what, but I've read the OAuth 2 spec many times over and perused the mailing list archives, and I have yet to find a good explanation of why the Implicit Grant flow for obtaining access tokens has been developed. Compared to the Authorization Code Grant, it seems to just give up on client authentication for no very compelling reason. How is this "optimized for clients implemented in a browser using a scripting language" (to quote the specification)?

这两个流程的起点是相同的(来源:https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-22):

客户端通过将资源所有者的用户代理定向到授权端点来启动流。 授权服务器对资源所有者进行身份验证(通过用户代理),并确定资源所有者是否授予或拒绝客户端的访问请求。 假设资源所有者授予访问权限,授权服务器使用前面提供的重定向URI(在请求中或在客户端注册期间)将用户代理重定向回客户端。

重定向URI包括一个授权代码(授权代码流) 重定向URI在URI片段中包含访问令牌(隐式流)

这里是流分裂的地方。在这两种情况下,此时重定向URI指向客户端托管的某个端点:

In the Authorization code flow, when the user agent hits that endpoint with the Authorization code in the URI, code at that endpoint exchanges the authorization code along with its client credentials for an access token which it can then use as needed. It could, for example, write it into a web page that a script on the page could access. The Implicit flow skips this client authentication step altogether and just loads up a web page with client script. There's a cute trick here with the URL fragment that keeps the access token from being passed around too much, but the end result is essentially the same: the client-hosted site serves up a page with some script in it that can grab the access token.

因此我的问题是:跳过客户端身份验证步骤可以获得什么?


当前回答

隐式授权允许使用GET从授权端点获取令牌。这意味着授权服务器不必支持CORS。

如果这不是一个问题,并且没有其他与授权服务器不灵活相关的问题(例如,出于某种原因,刷新令牌不是可选的),那么授权代码流是首选的,即使是对于公共客户端,根据最近的行业趋势和至少这个(当前)官方草案实例。

从历史上看,实现隐式流还有其他原因,但目前似乎它们被授权代码授予提供的安全优势所超越,包括:

为机密客户端通过反向通道交付和使用令牌的选项 没有在公共客户端的浏览器历史中公开令牌 在发布令牌之前中断未经授权的流-使用PKCE,用于“各种OAuth客户端”

其他回答

虽然隐式授权的设计是为了支持不能保护客户端机密的应用程序,包括客户端JavaScript应用程序,但一些提供商正在实现一种替代方案,使用没有客户端机密的授权代码。OAuth 2.0 IETF RFC-6749于2012年发布,目前的建议是2017年的一些最新讨论。

关于IETF OAuth邮件列表的2017年讨论可从以下实现者获得:

红帽:https://www.ietf.org/.../oauth/current/msg16966.html 德国电信:https://www.ietf.org/.../oauth/current/msg16968.html 智能健康IT: https://www.ietf.org/.../oauth/current/msg16967.html

点击此处阅读更多信息:

https://aaronparecki.com/oauth-2-simplified/ https://aaronparecki.com/oauth-2-simplified/#single-page-apps

Implicit was previously recommended for clients without a secret, but has been superseded by using the Authorization Code grant with no secret. ... Previously, it was recommended that browser-based apps use the "Implicit" flow, which returns an access token immediately and does not have a token exchange step. In the time since the spec was originally written, the industry best practice has changed to recommend that the authorization code flow be used without the client secret. This provides more opportunities to create a secure flow, such as using the state parameter. References: Redhat, Deutsche Telekom, Smart Health IT.

从隐式授权转移到没有客户端机密的认证代码也提到了这里的移动应用:

https://aaronparecki.com/oauth-2-simplified/#mobile-apps

通常的解释是,当您使用JavaScript客户端时,隐式授权更容易实现。但我认为这种看法是错误的。如果您使用的JavaScript客户端直接通过XMLHttpRequest请求受保护的资源,隐式授权是您唯一的选择,尽管它不太安全

授权码授权提供了额外的安全性,但它仅在web服务器请求受保护资源时才有效。由于web服务器可以存储访问令牌,因此访问令牌暴露在Internet上的风险较小,并且可以发出持续较长时间的令牌。由于web服务器是受信任的,所以可以给它一个“刷新令牌”,所以当旧的访问令牌过期时,它可以获得一个新的访问令牌。

But -- and this is a point that's easy to miss -- the security of the Authorization code flow works only if the web server is protected with a session, which is established with user authentication (login). Without a session, an untrusted user could just make requests to the web server, using the client_id, and it would be the same as if the user had the access token. Adding a session means that only an authenticated user can access the protected resources. The client_id is just the "identity" of the JS webapp, not authentication of said webapp.

这还意味着您可以在OAuth令牌过期之前结束会话。没有使访问令牌失效的标准方法。但是如果你的会话过期了,访问令牌就没用了,因为除了web服务器没有人知道它。如果一个不受信任的用户获得了对会话密钥的访问权,那么他们只能在会话有效期间访问受保护的资源。

如果没有网络服务器,你必须使用隐式授权。但这意味着访问令牌暴露在Internet上。如果一个不受信任的用户获得了它的访问权限,他们可以使用它直到过期。这意味着他们将拥有比授权代码授予更长的访问时间。因此,您可能需要考虑让令牌提前过期,并避免授予对更敏感资源的访问权。

*EDIT: More recently, people are recommending that you avoid using the Implicit grant, even on web apps without a server. Instead you can use the Authorization Code grant configured with an empty secret, along with PKCE. The auth-code grant avoids storing the access token in your browser history, and PKCE avoids exposing it if someone hijacks the redirect URL to steal the auth code. In this case you would need the server to avoid returning a refresh token, since your client probably can't store it securely. And it should issue an access token with the same limitations mentioned above.

以下是我的想法:

授权代码流中的认证代码+令牌的目的是令牌和客户端秘密永远不会暴露给资源所有者,因为它们在服务器到服务器之间传播。

另一方面,隐式授权流适用于完全使用javascript实现并在资源所有者的浏览器中运行的客户端。使用此流不需要任何服务器端代码。然后,如果所有事情都发生在资源所有者的浏览器中,那么发布认证代码和客户端秘密就没有意义了,因为令牌和客户端秘密仍然会与资源所有者共享。包含认证代码和客户端机密只会使流程更加复杂,而不会增加任何真正的安全性。

因此,关于“我们获得了什么?”的答案是“简单”。

我想Will Cain回答了这个问题,他说:“出于同样的原因,客户凭证没有任何好处。(任何客户端都可以尝试使用这个流程。)”还要考虑隐式流的redirect_uri可能是“localhost”——没有从授权服务器对隐式流进行回调。由于无法预先信任客户端,用户必须批准用户声明的发布。

它的存在是出于安全考虑,而不是为了简单。

您应该考虑用户代理和客户端之间的区别:

用户代理是用户(“资源所有者”)与系统其他部分(身份验证服务器和资源服务器)通信的软件。

客户端是在资源服务器上访问用户资源的软件。

In the case of decoupled user-agent and client the Authorization Code Grant makes sense. E.g. the user uses a web-browser (user-agent) to login with his Facebook account on Kickstarter. In this case the client is one of the Kickstarter's servers, which handles the user logins. This server gets the access token and the refresh token from Facebook. Thus this type of client considered to be "secure", due to restricted access, the tokens can be saved and Kickstarter can access the users' resources and even refresh the access tokens without user interaction.

If the user-agent and the client are coupled (e.g. native mobile application, javascript application), the Implicit Authorization Workflow may be applied. It relies on the presence of the resource owner (for entering the credentials) and does not support refresh tokens. If this client stores the access token for later use, it will be a security issue, because the token can be easily extracted by other applications or users of the client. The absence of the refresh token is an additional hint, that this method is not designed for accessing the user resources in the absence of the user.