OAuth 流程與發展總結 (1.0 => 1.0a => 2.0)

OAuth 流程與發展 (1.0 => 1.0a => 2.0)

概述

  1. 概述: 開放受權協議
  2. 做用: 容許第三方應用訪問服務提供方中註冊的終端用戶的部分資源
    下面是官方描述: [OAuth描述] The OAuth 2.0 authorization framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf.
  3. 參與者:php

    1. Client (Consumer) => 第三方應用
    2. Resource Owner(User) => 用戶
    3. Resource Server(Service Provider) => 資源服務提供方(OAuth2.0將服務提供方拆分爲兩部分)
    4. Authorization Server(Service Provider) => 受權服務提供方(OAuth2.0將服務提供方拆分爲兩部分)
  4. OpenID (WHO) 和 OAuth (WHAT)
    => OpenID 更加關注 '我是誰' 的問題
    => OAuth 更加關注 '我能得到哪些權限的問題'

OAuth1.0 流程圖

clipboard.png

接口

  1. (步驟 A) Consumer申請Request Token(/oauth/1.0/request_token):
    oauth_consumer_key
    oauth_signature_method
    oauth_signature
    oauth_timestamp
    oauth_nonce
    oauth_version
  2. (步驟 B) Service Provider返回Request Token:
    oauth_token
    oauth_token_secret
  3. (步驟 C) Consumer重定向User到Service Provider(/oauth/1.0/authorize):
    oauth_token
    oauth_callback
  4. (步驟 D) Service Provider在用戶受權後重定向User到Consumer:
    oauth_token
  5. (步驟 E) Consumer申請Access Token(/oauth/1.0/access_token):
    oauth_consumer_key
    oauth_token
    oauth_signature_method
    oauth_signature
    oauth_timestamp
    oauth_nonceoauth_version
  6. (步驟 F) Service Provider返回Access Token:
    oauth_token
    oauth_token_secret

OAuth1.0 漏洞

兩種Token,分別是Request Token和Access Token,其中Request Token又涉及兩種狀態,分別是未受權和已受權。

攻擊者會先申請攻擊者本身的Request Token,而後誘使用戶受權這個攻擊者的Request Token,接着針對回調地址的使用,又存在如下幾種攻擊手段:

  1. 若是Service Provider沒有限制回調地址(應用設置沒有限定根域名一致),那麼攻擊者能夠把oauth_callback設置成成本身的URL。
  2. 若是Consumer不使用回調地址(桌面或手機程序),而是經過User手動拷貝粘貼Request Token完成受權的話,那麼只要攻擊者在在User前面發起請求,就能拿到User的Access Token。

OAuth1.0 改進

  1. 步驟 B 傳遞 callback_url參數 (而不是 獲取已受權 request_token 步驟) Consumer申請Request Token時,必須傳遞oauth_callback, 讓callback參與簽名, 避免攻擊者假冒callback。
  2. 驗證完未受權 request_token 返回新的參數 oauth_verifier 驗證完成後會返回驗證碼(oauth_verifier)在沒有callback的時候, 服務提供方顯示給用戶,而後用戶能夠在第三方應用的設備上輸入,標示本身已經受權(和未受權的用戶分開),而後第三方應用必須加上該驗證碼去獲取

OAuth1.0a 流程圖

clipboard.png

OAuth2.0 流程

OAuth 2.0定義了四種受權方式。html

  1. 受權碼模式(authorization code)(本文只講解該受權方式)
  2. 簡化模式(implicit)
  3. 密碼模式(resource owner password credentials)
  4. 客戶端模式(client credentials)

clipboard.png

clipboard.png

接口

  1. (步驟 A) Client 向Authorization Server發出申請(/oauth/2.0/authorize):
    response_type = code
client_id
redirect_uri
scope
state
  2. (步驟 B) Authorization Server 在Resource Owner受權後給Client返回
    code
state
  3. (步驟 C) Client向Authorization Server發出申請(/oauth/2.0/token):
    grant_type = authorization_code
code
client_id
client_secrect
redirect_uri
  4. (步驟 E) Server在Resource Owner受權後給Client返回Access Token:
    access_token
token_type
expires_in
refresh_token

關於 state 參數

[RFC 對state參數的解釋]
The authorization server SHOULD require the client to provide the complete redirection URI (the client MAY use the "state" request parameter to achieve per-request customization). If requiring the registration of the complete redirection URI is not possible, the authorization server SHOULD require the registration of the URIscheme, authority, and path (allowing the client to dynamically vary only the query component of the redirection URI when requesting authorization).
咱們假設出現下面的場景:
(1)用戶甲到第三方網站A登陸後,到了綁定頁面。此時還沒綁定微博。
(2)綁定頁面提供一個按鈕:「綁定微博」(地址a:http://aaa.com/index.php?m=us...
(3)用戶甲點擊地址a,程序生成以下地址b:
https://api.weibo.com/oauth2/...【9999999】&redirect_uri=【http://aaa.comindex.php】&response_type=【code】
(4)用戶甲瀏覽器定向到地址b,受權該應用。
(5)受權服務器根據傳遞的redirect_uri參數,組合認證參數code生成地址c:
http://aaa.comindex.php&code=【809ui0asduve】
(6)用戶甲瀏覽器返回到地址c,完成綁定
此時即便咱們交換兩個用戶的地址c,則會出現綁定錯誤的狀況,避免出現該狀況的辦法就是對state參數進行驗證,來判斷該state參數是不是該用戶所對應的重定向地址api

參考文獻

  1. https://huoding.com/2010/10/10/8 《OAuth那些事兒》
  2. https://huoding.com/2011/11/0... 《OAuth的改變》
  3. https://www.zhihu.com/questio...《Oauth 1.0 1.0a 和 2.0 的之間的區別有哪些?》
  4. http://www.ruanyifeng.com/blo... 《理解 OAuth2.0》
  5. http://blog.sina.com.cn/s/blo... 《小議OAuth 2.0的state參數——從開發角度也說《互聯網最大規模賬號劫持漏洞即將引爆》》
  6. https://tools.ietf.org/html/r... 《RFC 6749》

結語

這篇文章是參考網上的資料的總結, 若是有錯誤歡迎批評指正, 若是侵權, 請做者聯繫我刪除文章, 謝謝
有興趣的同窗也能夠參照 [微博OAuth] API, 申請一個第三方應用, 參照微博API去實現一個獲取 access_token的測試用例 (微博會給予申請的第三方應用 client_id, client_secret)瀏覽器

相關文章
相關標籤/搜索