OAuth2.0初体验

410 阅读3分钟

问就是学习令人开心

###Open Authorization

  • 是——标准授权协议

  • 解决——以前使用的授权方法容易造成信息泄露,不安全,需要不向第三方泄漏数据,允许第三方访问服务器中的数据

  • 做什么——给第三方客户端有时效的token,第三方拿token令牌访问资源

  • 常用于——第三方登录

  • 传统授权方式:

    ​ 复制资源拥有者的凭据直接使用

    ​ 向用户索要凭据

    ​ 开发者密钥,万能钥匙

    ​ 特殊密码令牌

角色

1.Client 客户端 (使用受保护资源的API)

2.Resource Owner资源拥有者

3.Authorization Server 授权服务器

4.Resource Server 资源服务器

5.User Agent 用户代理,浏览器

我去银行ATM机取钱为例子

我就是resource owner

银行的金库就是resource server

ATM机子是client

我输入密码之后验证我账号密码对不对的服务器是authorization server

ATM机内嵌的系统是user agent

授权流程

     +--------+                               +---------------+
     |        |--(A)- Authorization Request ->|   Resource    |
     |        |                               |     Owner     |
     |        |<-(B)-- Authorization Grant ---|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(C)-- Authorization Grant -->| Authorization |
     | Client |                               |     Server    |
     |        |<-(D)----- Access Token -------|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(E)----- Access Token ------>|    Resource   |
     |        |                               |     Server    |
     |        |<-(F)--- Protected Resource ---|               |
     +--------+                               +---------------+

授权模式

  • Authorization Code Flow授权码模式

    服务器与客户端配合

    1.用户访问客户端,选择第三方登录

    2.客户端重定向到授权服务器,并携带client_id

    GET {Authorization Endpoint}
      ?response_type=code             // - Required
      &client_id={Client ID}          // - Required
      &redirect_uri={Redirect URI}    // - Conditionally required
      &scope={Scopes}                 // - Optional
      &state={Arbitrary String}       // - Recommended
      &code_challenge={Challenge}     // - Optional
      &code_challenge_method={Method} // - Optional
      HTTP/1.1
    HOST: {Authorization Server}
    

    示例:

    https://your_domain/login?
    response_type=code&             // response_type为code时表示是授权码请求
    client_id=your_app_client_id&   //客户端id
    redirect_uri=your_callback_url   //通过客户端注册的重定向 URI
    

    3.用户确认授权(Authentication),客户端被重定向到给定的URI,并携带code

    HTTP/1.1 302 Found
    Location: {Redirect URI}
      ?code={Authorization Code}  // - Always included
      &state={Arbitrary String}   // - Included if the authorization
                                  //   request included 'state'.
    

    示例:

    https://www.example.com/?code=95157e76-7f22-40b7-81f6-77b27be91f19
    

    4.重定向过程中,客户端拿到code,client_id,client_serect请求令牌,如果成功,去资源服务器获取资源

    token request:

    POST {Token Endpoint} HTTP/1.1
    Host: {Authorization Server}
    Content-Type: application/x-www-form-urlencoded
    grant_type=authorization_code  // - Required
    &code={Authorization Code}     // - Required
    &redirect_uri={Redirect URI}   // - Required if the authorization
                                   //   request included 'redirect_uri'.
    &code_verifier={Verifier}      // - Required if the authorization
                                   //   request included
                                   //   'code_challenge'.
    

    5.客户端拿到token后,就可以请求资源了

    token response:

    HTTP/1.1 200 OK
    Content-Type: application/json;charset=UTF-8
    Cache-Control: no-store
    Pragma: no-cache
    {
      "access_token": "{Access Token}",    // - Always included
      "token_type": "{Token Type}",        // - Always included
      "expires_in": {Lifetime In Seconds}, // - Optional
      "refresh_token": "{Refresh Token}",  // - Optional
      "scope": "{Scopes}"                  // - Mandatory if the granted
                                           //   scopes differ from the
                                           //   requested ones.
    }
    
  • Implicit Flow 隐式模式

    用于移动应用程序或者Web应用程序

    直接在浏览器中请求token,没有授权码

    要求用户授权应用程序,然后授权服务器将访问令牌传回给用户代理,用户代理将其传递给客户端

    1.用户访问客户端,选择第三方登录

    2.客户端重定向到授权服务器,发起请求

    GET {Authorization Endpoint}
      ?response_type=token          // - Required
      &client_id={Client ID}        // - Required
      &redirect_uri={Redirect URI}  // - Conditionally required
      &scope={Scopes}               // - Optional
      &state={Arbitrary String}     // - Recommended
      HTTP/1.1
    HOST: {Authorization Server}
    
    https://your_domain/login?response_type=token&client_id=your_app_client_id&redirect_uri=your_callback_url
    

    3.授权服务器响应给用户代理,其中包含access_token

    HTTP/1.1 302 Found
    Location: {Redirect URI}
      #access_token={Access Token}       // - Always included
      &token_type={Token Type}           // - Always included
      &expires_in={Lifetime In Seconds}  // - Optional
      &state={Arbitrary String}          // - Included if the request
                                         //   included 'state'.
      &scope={Scopes}                    // - Mandatory if the granted
                                         //   scopes differ from the
                                         //   requested ones.
    

    4.用户代理提取转发令牌token给客户端

    5.客户端拿到token请求资源信息

  • Resource Owner Password credentials Flow 密码模式

    用户把账号密码直接提供给客户端

    客户端不能存储密码,对客户端高度信任的情况下,其他授权模式无法使用的情况下

    https://oauth.example.com/token?grant_type=password&
    username=USERNAME&
    password=PASSWORD&
    client_id=CLIENT_ID
    
    
    { 
      "access_token"  : "",
      "token_type"    : "",
      "expires_in"    : "",
      "refresh_token" : "",
    }
    
  • Client Credentials 客户端模式。 适用于后端API的操作

    这种模式只需要提供 client_idclient_secret 即可获取授权。一般用于后端 API 的相关操作。

    https://oauth.example.com/token?
    grant_type=client_credentials&
    client_id=CLIENT_ID&
    client_secret=CLIENT_SECRET
    
    
  • Refresh Token Flow

    访问时token过期,则需要更新令牌获取新的令牌

    客户端发出更新令牌的HTTP请求,包含以下参数:

    • granttype:表示使用的授权模式,固定为"refresh*token"
    • refresh_token:表示早前收到的更新令牌
    • scope:表示申请的授权范围,不可以超出上一次申请的范围,如果省略,则表示与上一次一致。

学习链接

OAuth2: www.oauth.com/oauth2-serv… www.ory.sh/oauth2-for-… medium.com/@darutk/dia… medium.com/@darutk/ful…