OAuth 2.0协议中为什么要引入授权码

时间:2021/10/22

一.问题引入

OAuth 2.0作为一个web端的授权协议,考虑到不同的应用场景,它提供了四种授权模式,其中安全性最高的是授权码模式,如下图所示:

 

对于另一种模式简化模式而言,它省略了其中授权码的部分,在用户授权之后直接传给客户端令牌,如下图所示:

这引发了一个疑问,授权码的作用究竟是什么,为什么不直接返回令牌?

二.解释

1.参考文献中给出的解释

由于在简化模式的步骤C中令牌是通过重定向url传递给用户代理的,所以该用户代理上的所有应用其实都能看到该令牌,这就会引发安全问题。而在授权码模式中,由于授权码具有只能使用一次的特性(即使用完后就作废),而且使用时还需要客户端的秘密,所以可以很好的保证安全性,这也是引入授权码的原因。

2.官方文档(RFC 6819)中给出的解释

Authorization "codes" are sentto the client’s redirect URI instead of tokens for two purposes:

  • Browser-based flows expose protocol parameters to potential attackers via URI query parameters (HTTP referrer), the browser cache, or log file entries, and could be replayed. In order to reduce this threat, short-lived authorization "codes" are passed instead of tokens and exchanged for tokens over a more secure direct connection between the client and the authorization server.
  • It is much simpler to authenticate clients during the direct request between the client and the authorization server than in the context of the indirect authorization request. The latter would require digital signatures.

参考博文:https://www.jianshu.com/p/f2aebfcf8cb4?utm_campaign=haruki

努力,向上,自律
原文地址:https://www.cnblogs.com/machi12/p/15440053.html