OAuth2.0 protocol introduction

foreword

  For background development, it is inevitable to deal with third-party systems, which may be to call third-party services, or to require users to work in third-party systems .

  related information, etc. The former is mostly provided in the form of webservice , and the latter is provided by the OAuth protocol, which is also described below.

OAuth(Open Authorzation)介绍

  The official website has the following introduction to oAuth :

 An open protocol to allow secure API authorization in a simple and standard method from

   web, mobile and desktop applications. roughly means that OAUTH is an open protocol for desktop, mobile

   Or web applications provide an easy, standard way to access API services that require user authorization. The biggest benefit of this protocol is that it

   The difference from the previous authorization method is that OAuth authorization will not allow third parties to access the user 's account information (such as username and password),

   That is, a third party can apply for .

Introduction to terms related to OAuth protocol

   Third-party application : Third-party application

   HTTP service : service provider

   User Agent : User agent ( hereinafter referred to as browser )

 

   Authorization server : Authentication server ( a server that the service provider uses exclusively to handle authentication )

   Resource server : Resource server

  Objectives of the OAuth protocol: 1. The third-party application can only use the user's resources on the service provider's resource server user

          2. Resources are determined by the user

           3. Third-party applications should not touch the user's account information ( safe )

Introduction to OAuth Process

  
    

 1. After the user accesses the third-party application, the user is required to authorize.

 2. The user agrees to authorize the third party and specify the authorized resources.

 3. The third party uses the authorization obtained in the previous step to apply for a token from the authentication server.

 4. The third party uses the token to apply to the resource server to obtain user resources.

    5. After the resource server confirms the token, it opens the user resource to the third party.

 From the above process, we can see that the goal of the OAuth protocol is met. The most critical step in the above process occurs in step 2 , the user

 Authorization, the OAuth protocol specifies four authorization modes:

 authorization code model : authorization code model

 Implicit model : Simplified model

 resource owner password credentials model : password mode

    client credentials model : client mode 

Authorization mode

   Below we introduce these four authorization modes:

     1). Flowchart of authorization code mode:

        
        
       
Process introduction:

     1. The user accesses a third party, and the latter redirects the former to the authentication server ( applying for authentication ) .

        2. The user chooses whether to grant client authorization and resource permissions.

        3. If the user grants authorization, the authentication server directs the user to the "URI" specified in advance by the third party , and attaches an authorization code.

        4. The third party gets the authorization code, attaches the previous "URI" , and applies to the authentication server for a token.

     5.认证服务器核对了授权码和URI,通过后,返回给第三方访问令牌和更新令牌。

      下面介绍流程中,所涉及到的参数:

      第一步申请认证的URI包含参数:

       response_type:授权类型,此处的值固定为"code" ,必选项

       client_id:表示第三方的ID必选项

       redirect_uri:表示重定向URI可选项

       scope:表示申请的权限范围,可选项

       state:表示第三方的当前状态,任意值,认证服务器会原样地返回,可选项

         第三步认证服务器回应第三方的URI包含参数:

          code:授权码。有效期应很短,通常设为10分钟,且第三方只能使用该码一次,会被认

           证服务器拒绝(该码与第三方ID和重定向URI,存在对应关系)必选项

       state:如果第三方的请求中包含该参数,认证服务器的回应也必须包含这个参数。

      第四步申请令牌(http方式请求)包含参数:

       grant_type:使用的授权模式,该值固定为"authorization_code"必选项

       code:获得的授权码,必选项

       redirect_uri:重定向URI,且必须与A步骤中的该参数值保持一致,必选项

       client_id:第三方ID必选项

      第五步认证服务器返回的参数:

       access_token:访问令牌,必选项

       token_type:令牌类型(bearer/mac类型),该值大小写不敏感,必选项

       expires_in:过期时间()。如果省略该参数,必须其他方式设置过期时间。

       refresh_token:更新令牌,令牌过期后用来获取下一次的访问令牌,可选项

       scope:权限范围,如果与第三方申请的范围一致,此项可省略。

     下面通过案例来理解授权码模式的OAuth协议

      
            
 

          访问某网站首页,页面有qq登录和微博登录,我们选择qq登录

        
             

           浏览器会重定向到qq的认证服务页面,我们将location值拿出来看看https://graph.qq

          .com/oauth/show?which=Login&display=pc&response_type=code&client_id=101249010

        &state=30c504bb633f29857aadffed4c5d669a&scope=get_user_info%2Cget_info%2Ca

        dd_t%2Cdel_t%2Cadd_pic_t%2Cget_repost_list%2Cget_other_info%2Cget_fanslist

        %2Cget_idollist%2Cadd_idol%2Cdel_idol&redirect_uri=http%3A%2F%2Fwww.newbiefl

        y.com%2F%3Fconnect%3Dqq

         我们可以看到包含response_typeclient_idstatescoptredirec_url 5个参数,对应授

         权码模式的第一步--申请认证,这时用户指定资源权限后我们点击授权按钮。

         
        
这时就登录进来了,可以看到是qq认证服务器重定向了浏览器,提取location值如下:

           http://www.newbiefly.com/?connect=qq&code=C3EF94F8DDCA92FB23FE726B0BF4CC

    8D&state=30c504bb633f29857aadffed4c5d669a,可以看出认证服务器回应该申请网站code

    以及state,对应对应授权码模式的第三步。

    第四,五步是后台通过代码模拟http请求调用的,用户是不可见的。

     以上是基于授权码模式的OAuth协议流程。

    2).简化模式流程图

      
       
    
流程介绍:

     1.用户访问第三方,后者将前者重定向到认证服务器(申请认证)

        2.用户选择是否给予客户端授权以及资源权限。

     3.若用户给予授权,认证服务器将用户导向第三方事先指定的"URI",并以URIhash部分

      访问令牌等信息。

        4.浏览器向资源服务器发出请求。

        5.资源服务器返回一个网友,其中js脚步可以获取Hash值的令牌。

        6.提取令牌后,浏览器访问第三方(包含令牌信息)。

      下面介绍流程中,所涉及到的参数:

       第一步申请认证的URI包含参数:

         response_type:授权类型,此处的值固定为"code" ,必选项

         client_id:表示第三方的ID必选项

         redirect_uri:表示重定向URI可选项

         scope:表示申请的权限范围,可选项

         state:表示第三方的当前状态,任意值,认证服务器会原样地返回,可选项

           注意:response_type的值为code

       第三步认证服务器回应第三方的URI包含参数:

         access_token:访问令牌,必选项。

         token_type:令牌类型,该值大小写不敏感,必选项。

         expires_in:过期时间()。如果省略该参数,必须其他方式设置过期时间。

         scope:权限范围,如果与第三方申请的范围一致,此项可省略。

         state:如果第三方的请求中包含该参数,认证服务器的回应也必须包含这个参数。

        注意:此时浏览器得到http头的location参数值为:URI#access_token=..&token_type=

             这种形式;URI的值为资源服务器上的。

      第五步资源服务器返回的页面会执行js代码用于获取Hash值的access_token

        token_type等参数,再将数据包含在访问第三方URI的请求中。

     以上是基于简化模式的OAuth2.0协议的流程。

  3).密码模式流程图

     
       
      
用户向第三方提供自己的用户名和密码,客户端使用这些信息,向"服务商提供商"索要授权(客户

    端不得储存密码),该模式基本不使用(也违背OAuth协议的初衷,只有在其他授权模式无法执行

    的情况下,才考虑)

   上图中B为第三方发出http请求包含的参数:

     grant_type:授权类型,此处的值固定为"password"必选项

     username:用户名,必选项

     password:用户的密码,必选项

     scope:表示权限范围,可选项

     上图C为响应第三方的http请求包含的参数:查看授权码模式流程图的第五步。

              以上是基于密码模式的OAuth2.0模式的流程。

           客户端模式流程图

       
       
      
该模式是以第三方的名义向认证服务器进行认证授权

    上图中A为第三方发出http请求包含的参数:

      granttype:授权类型,此处的值固定为"clientcredentials"必选项

      scope:权限范围,可选项

     上图B为响应第三方的http请求包含的参数:查看授权码模式流程图的第五步。

   以上是基于客户端模式的OAuth2.0模式的流程。

 

参考:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326393720&siteId=291194637