OAuth2.0 Authorization flows and grant types

来源:互联网 发布:大数据风控平台价格 编辑:程序博客网 时间:2024/06/10 08:04

         项目需要走OAuth2.0,于是找了一些开源实现,最终确定Thinktecture IdentityServer。通过github的samples,能够容易的跑通,但是在使用的时候总感觉差点什么,有些方式可以直接返回access_token,有些返回的access_token每次刷新都会变化,细看,可能是由于授权方式不一样,但是有不知道各种方式的区别和场景。

        网上查询相关资料,结果没有太多,好不容易找到了2篇英文版的,就仔细看了下,写下自己的理解

首先解释下授权

   授权,是一种凭证,是访问资源所有者的受保护的资源的凭证,客户端使用这个凭证来获取access_token,这种授权有一下四中授权类型:authorization code, implicit, resource owner password credentials, and client credentials,当然,可能还有其他自定义的类型。

 1、Authorization Code

Authorization Code 是client和resource owner之间的授权服务器(authorization server)颁发的。client不会直接从resource owner请求授权,而是将resource owner 导向到授权服务器(通过 [RFC2616]定义的user-agent),授权完成后,将resource owner返回到client,并同时将authorization code返回到client。

       在将resource owner 和authorization code返回client之前,authorization server验证resource owner的合法有效性,并获得授权。因此,resource owner并没有对client授权,而是对authorization server授权。

authorization code 只提供了一些很少的重要有用的安全信息,比如对client授权、不经过resource owner的user-agent而直接对client授权,也就不用暴露到外面去,比如resource owner。

2、implicit

这种授权方式,是这对浏览器端(比如JavaScript)的一种简单的授权方式。这种方式,不会对client授予authorization code,而是直接对client返回access_token,这个access_token其实就是resource owner对client的一种授权。这种授权方式就没有使用中间凭证,直接一步到位。

当使用implicat授权access_token时,authorization server 其实没有权限给client,从某种程度上说,client identity 可以通过将access_token 发送到redirection URI来验证。这个access_token可能会暴露给resource owner或者其他对resource owner的user-agent有访问权限的程序。

implicit 授权由于减少了请求access_token需要发出请求的次数,从而提高了一些client的响应速度和效率。但是,使用这种方式必须权衡下安全与方便性,特别是当可以使用authorization code方式授权时。

3、resource owner password credentials

这种方式(比如用户名、密码)可以直接获取access_token。所以,这种获取凭证的方式,必须建立在resource owner和client高度信任的基础之上,想想也是,不然谁会把账号密码暴露给你。还有一种情况下可以使用这种授权(没有其他授权方式   !==)。

尽管这种授权方式可以直接向resource owner请求权限,但是,resource owner的凭证也只能是针对一个请求和交换access_token。这种方式的一个便利之处就是,客户端不需要存储resource owner的凭证。其他方式可能会需要存储,以便换取一个更长过期时间的access_token或者refresh token。

4、Client Credentials

当授权范围(authorization scope)仅限于client受保护的资源,或者为了显示范围为authorization server配置的资源时,可以使用这种授权方式。

开源实现列表:http://oauth.net/2/

QQ OAuth2.0:http://wiki.open.qq.com/wiki/website/%E4%BD%BF%E7%94%A8Authorization_Code%E8%8E%B7%E5%8F%96Access_Token

 Authorization flows and grant types: https://www.drupal.org/node/1958718、http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-1.2

0 0
原创粉丝点击