OAuth2.0详解(受权模式篇)

OAuth2.0有五种受权模式。json

(1)受权码模式(Authorization Code) 
(2)受权码简化模式(Implicit) 
(3)Pwd模式(Resource Owner Password Credentials) 
(4)Client模式(Client Credentials) 
(5)扩展模式(Extension)浏览器

注:文中的client,可理解为浏览器或APP。安全

但不论哪一种模式,都是为了从认证服务器获取Access Token,用来访问资源服务器。 
而申请Access Token,须要提交相应信息。例如,client_ID(我是谁),response_type或grant_typt(申请哪一种模式),scope(申请哪些权限,由受权服务器定义),redirect_uri(申请结果跳转至哪儿)等。固然不一样的模式,提交信息内容也不一样。服务器

咱们先从简单的模式开始。由于简单模式,申请流程短,安全级别较低,我的感受可用场景很少。微信

(一)Client模式
先上流程图。app

该模式下,并不存在对个体用户受权的行为,被受权的主体为client。所以,该模式可用于对某类用户进行集体受权。ide

申请该模式时,须要在HTTP request entity-body中提交如下信息。this

grant_type:
         REQUIRED.  Value MUST be set to "client_credentials".url

scope:
         OPTIONAL.  The scope of the access request
1
2
3
4
5
固然,能够根据受权服务器的实现,提交其它必要信息。 
若申请成功,服务器将返回access token和token有效时间。3d

附规范中的例子。 
Request:

     POST /token HTTP/1.1
     Host: server.example.com
     Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
     Content-Type: application/x-www-form-urlencoded

     grant_type=client_credentials
1
2
3
4
5
6
Response:

     HTTP/1.1 200 OK
     Content-Type: application/json;charset=UTF-8
     Cache-Control: no-store
     Pragma: no-cache

     {
       "access_token":"2YotnFZFEjr1zCsicMWpAA",
       "token_type":"example",
       "expires_in":3600,
       "example_parameter":"example_value"
     }
1
2
3
4
5
6
7
8
9
10
11
注:expires_in为token有效时长,单位为秒。

(二)Pwd模式


该模式下,须要用户将自身的account ID和password交由client,client将使用它们来申请access token,整个过程会将用户信息暴露。所以,除非client十分可靠(例如硬件设备,系统APP),不然,不建议使用该模式。

申请该模式时,须要在HTTP request entity-body中提交如下信息。

   grant_type:
         REQUIRED.  Value MUST be set to "password".

   username:
         REQUIRED.  The resource owner username.

   password:
         REQUIRED.  The resource owner password.

   scope:
         OPTIONAL.  The scope of the access request
1
2
3
4
5
6
7
8
9
10
11
申请成功后,受权服务器将返回access token和token有效时间,以及可选的refresh token,用于在access token过时时进行token更新。

附规范中的例子。 
Request:

     POST /token HTTP/1.1
     Host: server.example.com
     Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
     Content-Type: application/x-www-form-urlencoded

     grant_type=password&username=johndoe&password=A3ddj3w
1
2
3
4
5
6
Response:

     HTTP/1.1 200 OK
     Content-Type: application/json;charset=UTF-8
     Cache-Control: no-store
     Pragma: no-cache

     {
       "access_token":"2YotnFZFEjr1zCsicMWpAA",
       "token_type":"example",
       "expires_in":3600,
       "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
       "example_parameter":"example_value"
     }
1
2
3
4
5
6
7
8
9
10
11
12
(三)受权码模式


该模式是五种受权中最啰嗦的。从流程上看,申请分为两个阶段。首先,须要申请Authorization Code,以后,使用Authorization Code来申请Access Token。

咱们以优酷为例,讲述流程。 
(A)优酷向用户展现,“我”能够支持QQ、微信、支付宝等第三方式登陆。用户选择其中一种,例如QQ,则跳转至QQ界面(User-Agent),一般为WEB界面。此时,若用户未登陆,则要求用户登陆,若已登陆,则询问是否受权,以及展现受权后会得到哪些权限。 
(B)用户点击受权,触发申请。 
(C)假设受权经过,QQ认证服务器将用户导向优酷事先指定的”重定向URI”(redirection URI),同时附上一个Authorization Code。 
(D)优酷收到受权码,附上早先的”重定向URI”,向认证服务器申请Access Token。这一步是在优酷的后台的服务器上完成的,对用户不可见。 
(E)认证服务器核对了受权码和重定向URI,确认无误后,向优酷发送访问令牌(access token)和更新令牌(refresh token)。

Authorization Code只能使用一次,且有时间限制,规范建议为10分钟。

申请Authorization Code时,须要在URI的query component中附加如下信息

   response_type:
         REQUIRED.  Value MUST be set to "code".

   client_id:
         REQUIRED.  The client identifier

   redirect_uri:
         OPTIONAL.  

   scope:
         OPTIONAL.  The scope of the access request

   state:
         RECOMMENDED.  An opaque value used by the client to
         maintain state between the request and callback.  The
         redirecting the user-agent back to the client.  The 
         parameter SHOULD be used for preventing cross-site
         request forgery.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Authorization Code返回时,URI的query component中的附加信息以下

 code:
         REQUIRED.  The authorization code generated by the
         authorization server.  The authorization code MUST of 
         expire shortly after it is issued to mitigate the risk 
         of leaks.  A maximum authorization code lifetime of 10 
         minutes is RECOMMENDED.  The client MUST NOT use the 
         authorization code more than once.  If an authorization 
         code is used more than once, the authorization server 
         MUST deny the request and SHOULD revoke (when possible) 
         all tokens previously issued based on that authorization 
         code.  The authorization code is bound to the client 
         identifier and redirection URI.

   state:
         REQUIRED if the "state" parameter was present in the 
         client authorization request.  The exact value received 
         from the client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
使用Authorization Code申请Access Token时,须要在HTTP request entity-body中提交如下信息。

   grant_type:
         REQUIRED.  Value MUST be set to "authorization_code".

   code:
         REQUIRED.  The authorization code received from the
         authorization server.

   redirect_uri:
         REQUIRED, if the "redirect_uri" parameter was included 
         in the authorization request, and their values MUST be 
         identical.

   client_id:
         REQUIRED, if the client is not authenticating with the
         authorization server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
申请成功后,受权服务器将返回access token和token有效时间,以及可选的refresh token。

附规范中的例子。 
Request Authorization Code:

GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
1
2
Authorization Code Response:

HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz
1
2
Request Access Token:

     POST /token HTTP/1.1
     Host: server.example.com
     Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
     Content-Type: application/x-www-form-urlencoded

     grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
     &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
1
2
3
4
5
6
7
Access Token Response:

     HTTP/1.1 200 OK
     Content-Type: application/json;charset=UTF-8
     Cache-Control: no-store
     Pragma: no-cache

     {
       "access_token":"2YotnFZFEjr1zCsicMWpAA",
       "token_type":"example",
       "expires_in":3600,
       "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
       "example_parameter":"example_value"
     }
1
2
3
4
5
6
7
8
9
10
11
12
(四)受权码简化模式


在受权码模式中,Authorization Code和Access Token都由受权服务器生成和验证,而最终只用到Access Token,这让Authorization Code显得无足轻重。所以,受权码简化模式,去掉了Authorization Code的申请流程,从而经过User-Agent(Browser)直接申请Access Token。

咱们还以优酷为例,讲述流程。 
(A)优酷向用户展现,“我”能够支持QQ、微信、支付宝等第三方式登陆。用户选择其中一种,例如QQ,则跳转至QQ界面(User-Agent),一般为WEB界面。此时,若用户未登陆,则要求用户登陆,若已登陆,则询问是否受权,以及展现受权后会得到哪些权限。 
(B)用户点击受权,触发申请。 
(C)假设受权经过,QQ认证服务器将用户导向优酷事先指定的”重定向URI”(redirection URI),同时附上Access Token。 
(D)QQ界面(User-Agent)收到重定向响应后,向优酷服务器提出请求,表示想提取URI中的Access Token。 
(E)优酷服务器返回带有解析脚本的页面,用于解析重定向URI fragment中的Access Token。 
(F)User-Agent使用解析脚本,获取Access Token。 
(G)User-Agent将Access Token转交给优酷。

申请Access Token时,须要在URI的query component中附加如下信息

   response_type:
         REQUIRED.  Value MUST be set to "token".

   client_id:
         REQUIRED.  The client identifier.

   redirect_uri:
         OPTIONAL.

   scope:
         OPTIONAL.  The scope of the access request.

   state:
         RECOMMENDED.  An opaque value used by the client to 
         maintain state between the request and callback.  The 
         authorization server includes this value when 
         redirecting the user-agent back to the client.  The 
         parameter SHOULD be used for preventing cross-site 
         request forgery.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Access Token返回时,URI的query component中的附加信息以下

   access_token:
         REQUIRED.  The access token issued by the authorization 
         server.

   token_type:
         REQUIRED.  The type of the token issued.  Value is case 
         insensitive.

   expires_in:
         RECOMMENDED.  The lifetime in seconds of the access 
         token.  For example, the value "3600" denotes that the 
         access token will expire in one hour from the time the 
         response was generated. If omitted, the authorization 
         server SHOULD provide the expiration time via other 
         means or document the default value.

   scope:
         OPTIONAL, if identical to the scope requested by the 
         client; otherwise, REQUIRED.  The scope of the access 
         token.

   state:
         REQUIRED if the "state" parameter was present in the 
         client authorization request.  The exact value received 
         from the client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
注:受权码简化模式,不生成refresh token。

附规范例子。 
Access Token Request:

GET /authorize?response_type=token&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
1
2
Access Token Response:

HTTP/1.1 302 Found
Location: http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA
               &state=xyz&token_type=example&expires_in=3600
1
2
3
注:此处存在质疑。规范例子中URI不一致,我的认为Response中的URI应为:https://client.example.com/cb。若同窗有其余看法,望赐教。

(五)扩展模式
扩展模式,实际上是一种自定义模式。规范中仅对“grant type”参数提出了须为URI的要求。对于其余申请数据,能够根据需求进行自定义。

附规范例子。

     POST /token HTTP/1.1
     Host: server.example.com
     Content-Type: application/x-www-form-urlencoded

     grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Asaml2-      bearer&assertion=PEFzc2VydGlvbiBJc3N1ZUluc3RhbnQ9IjIwMTEtMDU      [...omitted for brevity...]aG5TdGF0ZW1lbnQ-PC9Bc3NlcnRpb24-  

相关文章
相关标签/搜索