簡單易懂的 OAuth 2.0.pdf

上传人:李主任 文档编号:3334918 上传时间:2019-08-13 格式:PDF 页数:303 大小:2.38MB
返回 下载 相关 举报
簡單易懂的 OAuth 2.0.pdf_第1页
第1页 / 共303页
簡單易懂的 OAuth 2.0.pdf_第2页
第2页 / 共303页
簡單易懂的 OAuth 2.0.pdf_第3页
第3页 / 共303页
簡單易懂的 OAuth 2.0.pdf_第4页
第4页 / 共303页
簡單易懂的 OAuth 2.0.pdf_第5页
第5页 / 共303页
点击查看更多>>
资源描述

《簡單易懂的 OAuth 2.0.pdf》由会员分享,可在线阅读,更多相关《簡單易懂的 OAuth 2.0.pdf(303页珍藏版)》请在三一文库上搜索。

1、簡單易懂的 OAuth 2 鴨七 2013/11/26, Ruby Tuesday Taipei #27 (?) ! 鴨七(莊育承) Rails Developer at KKBOX, working at the KKTIX team “ chitsaou, # yorkxin, $ blog.yorkxin.org Todays Target 你會知道 OAuth 2 Protocol 怎麼跑 你會看得懂 99% 基於 OAuth 2 的第三方 API 文件 你會知道怎麼用 OAuth 2 鎖你的 API Agenda OAuth 2 是甚麼?可以拿來幹嘛? OAuth 2 通訊協定怎麼跑

2、 製造 OAuth 2 Provider 的方法 第一次用 Rails + Grape API 整合 OAuth 2 就上手 OAuth 2 是甚麼?好吃嗎? OAuth 2.0 全名 “The OAuth 2.0 Authorization Framework” Authorization (n.) 授權 人 透過 授權給 程式 說你可以去跟母站拿我的資料 人 透過 授權給 程式 說你可以去跟母站拿我的資料 User 人 透過 授權給 程式 說你可以去跟母站拿我的資料 User App 人 透過 授權給 程式 說你可以去跟母站拿我的資料 User Website App 人 透過 授權給 程

3、式 說你可以去跟母站拿我的資料 User Website App API 人 透過 授權給 程式 說你可以去跟母站拿我的資料 User Website App API Sounds Familiar? You Facebook ? API 人 透過 授權給 程式 說你可以去跟母站拿我的資料 Sounds Familiar? The Old-School Way App 要你輸入帳號密碼,還聲明不會亂搞 你要信任 App 不會拿你的密碼亂搞 就算 App 很乖,被幹走還是沒救 不管你信不信,我反正是不信 OAuth 如何解決這個問題 網站提供官方界面,讓你可以授權程式使用資料 程式存的是 Tok

4、en 而非帳號密碼 也用 Token 去打 API Token 可以規定存取範圍(scopes, 如:好友列表) Token 有期限而且可以隨時撤銷(不怕被幹走) OAuth 2 裡面的角色 Resource Owner - 資料擁有者,通常是人類 (User) Client - 即 App ,要存取 User 的資料的程式 Authorization Server - 總管一切授權事務 Resource Server - Client 從這裡拿資料,即 API 人 透過 授權給 程式 說你可以去跟母站拿我的資料 You Facebook ? API 人 透過 授權給 程式 說你可以去跟母站拿

5、我的資料 You Facebook ? API Resource Owner Client Resource Server Authorization Server Resource Owner Resource Owner = User 就是你網站的 User API 拉到的是跟 User 有關的資料 例如好友名單、信件內容 授權必須經由本人親自確認 Resource Owner = Client 這種情況沒有人的存在。 例:爬公開資料 Twitter 稱為 App-Only Authorization Facebook 稱為 App Access Token Client Client U

6、ser 授權給第三方程式,這個程式就是 Client 例:Facebook 上的遊戲、手機上的 App 官方程式也算(例如手機 App 、桌面 App) Client 必須事先註冊 “Client Registration” Client ID Client Secret (視為密碼) Redirect URI 這個很重要 Client ID Client Secret Redirect URI Redirect URI User 本人親自確認之後,返回結果到 Client 要事先指定,若不一致則不可 Redirect 過去 可以指定多組,但通常是一組,或是開頭一致 最好是 HTTPS (不強

7、制,例如手機 App 就辦不到) Redirect URI https:/ http:/kktix.dev/users/auth/facebook/callback kktix-app:oauth2/callback Public v.s. Confidential Client 根據能不能保護資料來區分 Confidential - Server-Side Application Public - 手機 App / 桌面程式 / JavaScript App / Browser Extension Client Authorization (認證) 出示 Client ID + Secret

8、 向 Auth. Server 認證自己 也就是說 Client 要登入到 Auth. Server 只適用 Confidential Client Client 有 ID / Secret 用於認證 用 Redirect URI 確保瀏覽器轉址到正確的 Client Public / Confidential 有各自適用的授權流程 Endpoints Endpoints Authorization Endpoint - 用來給 User 本人確認授權 Token Endpoint - 用來讓 Client 取得真正的 Token Redirection Endpoint - Client 用

9、來收資料用 Authorization Endpoint 給 User 本人確認授權 是一個網頁 拿到的是 “Grant” (授權狀)而不是 Token User 答覆之後,會轉回 Client 的 Redirect URI Token Endpoint 給 Client 取得真正的 Token JSON API 機械化界面,無網頁 Redirection Endpoint 用來從瀏覽器接收 Auth. Server 來的資料 開在 Client 而不是 Auth. Server Client 註冊時要寫的 “Callback URL” Auth. Server 會驗證 Redirect UR

10、I 是否相符才轉址 SSL! SSL! SSL! Auth. Server 上面的 Endpoints 必須上 HTTPS Client 必須驗證 SSL Certificate 是否合格 Client Redirection Endpoint 不強制,但能上最好上 網站 最好上 手機 / 桌面 App 沒辦法可以不要上 Resource Server Resource Server Client 必須出示 Token 才能進去拿資料 Client 只要出示 Token 就能進去拿資料 可以用 Scope 限制 Token 能夠取用的資料範圍 Resource Server Password-

11、Free API Login via YourWebsite OAuth 2 Protocol 怎麼跑 簡單啊,讀 Spec 啊* *代誌不是本人所想的這麼簡單 憨 人 RFC 6749: The OAuth 2.0 Authorization Framework 77 pages (PDF) RFC 6750: The OAuth 2.0 Authorization Framework: Bearer Token Usage 19 pages (PDF) tl;dr 用去 3 個禮拜 寫了 13 篇筆記 /me 已讀。 OAuth 2.0 Spec(s) RFC 6749 - 定義與 Tok

12、en 核發有關的 Protocol RFC 6750 - 定義Bearer Token怎麼用 RFC 6749: OAuth 2 Protocol 定義了 OAuth 2 的角色、互動方式 定義了資料有哪些、怎麼跑 定義了核發、換發 Token 等的 Protocol 規畫了 4 種常見適合使用 OAuth 2 的流程 RFC 6750: Bearer Token RFC 6749 只規定授權方式,沒規定 API 怎麼擋 RFC 6750 定義一種叫做 “Bearer” 的 Token Type 各種 type 有不同擋法, Bearer 是直接 = 檢查 一句話解釋:直接拿來就能用,不用簽來

13、簽去 Parameters charset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1zCsicMWpAA“, “token_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Prag

14、ma: no-cache “access_token“:“2YotnFZFEjr1zCsicMWpAA“, “token_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Access Token HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1zC

15、sicMWpAA“, “token_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Access Token 表示這個 Token 是 Bearer Token HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1zCsicMWpAA“, “token

16、_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Access Token 表示這個 Token 是 Bearer Token 3600 秒之後過期 HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1zCsicMWpAA“, “token_type“

17、:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Access Token 表示這個 Token 是 Bearer Token 3600 秒之後過期 Access Token 對應的 Refresh Token HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1

18、zCsicMWpAA“, “token_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Access Token 表示這個 Token 是 Bearer Token 3600 秒之後過期 Access Token 對應的 Refresh Token 若 scope 與申請時 不一致則要附上 手機 App / 桌面 App 視為 Public ,Client Authentication 不可信 Token Request 要附上 Client ID 確保

19、Token 發給正確的 Client App 聽固定 URI ,如 kktix-app:oauth2-callback What about JavaScript App “User-Agent-Based Client” Public Client Redirect Endpoint 也沒辦法聽自訂 Protocol Auth. Code Grant Flow 不可用,怎麼辦? “Implicit Grant Flow” Implicit Grant Flow 專門給 Public Client 用 Grant 不傳到 Client ,直接發 Token 不經過 Token Endpoint

20、 發的 Token 通常是短效期,降低被偷的風險 不給 Refresh Token,過期重新跑申請流程 Resource Owner Client (JS) ! Authorization Server 抄自 spec Resource Owner Browser Client (JS) ! Authorization Server 抄自 spec Resource Owner Browser Client (JS) ! Authorization Server 抄自 spec Client (Web Server) Resource Owner Browser Client (JS) ! A

21、uthorization Server 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint Resource Owner Browser Client (JS) ! Au

22、thorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner (C) Token (in Frag.), S

23、cope, State 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner (C) Token (in Frag.), Scope, State 抄自 spec Authorization Endpoint Client (Web Server) Redirect

24、ion Endpoint (D) GET Redirect URI (no Token) Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner (C) Token (in Frag.), Scope, State (E) JavaScript (to Decode Token) 抄自 spec Authorization Endpoint Client (Web Server) Redirection En

25、dpoint (D) GET Redirect URI (no Token) Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner (C) Token (in Frag.), Scope, State (E) JavaScript (to Decode Token) 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint

26、 (D) GET Redirect URI (no Token) (F) Resource Owner Browser Client (JS) ! Authorization Server (A) (A) ID, Redirect URI, Scope, State (B) (B) 問 Resource Owner (C) Token (in Frag.), Scope, State (E) JavaScript (to Decode Token) 抄自 spec Authorization Endpoint Client (Web Server) Redirection Endpoint (

27、D) GET Redirect URI (no Token) (G) Assign Access Token (F) (C) (G) 花生省魔術 (C) Auth. Server 叫 Browser 轉回 Redirect URI https:/ (D) Browser 會去 GET 該 URL ,但不含 # (天性) Token 自動保留在 Browser 的 Fragment Part 裡面 (C) (G) 花生省魔術 (E) 再丟回一個 JavaScript 用來解出 # 裡面的 Token (F) Browser 自動 run 這個 JavaScript 解出 Token (G) 把解

28、出來的 Token 丟給 JavaScript App 所以為什麼需要 Client 分身 JavaScript 不能開 Redirection Endpoint 所以用 Web Server 開 裡面就是一個 JavaScript code 用來解出 Token 並傳給 JavaScript App (真 Client) https:/ response_type=token / store token in local storage or cookie Implicit Grant Flow Usage Facebook JavaScript SDK 手機 App 、桌面 App 也適用

29、 Facebook: WebView 監聽固定 Redirect URI 抓出 #access_token 特別注意事項 為降低 Token 被偷風險,通常會發短效期的 Token 因為可能偽造 Token 餵給 Client ,所以必須驗證之 Facebook: “Token Debug Endpoint” What if 自產自用? 自己寫的 script 想要用 Token 去打 API Server App 已經存了帳號密碼,想改成 OAuth 2 “Resource Owner Password Credentials Grant Flow” Resource Owner Clien

30、t ! Authorization Server 抄自 spec Resource Owner Client ! Authorization Server 抄自 spec Token Endpoint Resource Owner Client ! Authorization Server 抄自 spec Token Endpoint (A) Username, Password Resource Owner Client ! Authorization Server (B) Client Auth, Username, Password, Scopes 抄自 spec Token Endpo

31、int (A) Username, Password Resource Owner Client ! Authorization Server (B) Client Auth, Username, Password, Scopes (C) Token 抄自 spec Token Endpoint (A) Username, Password Resource Owner Password Credentials Grant Flow 需要是 Resource Owner 高度信賴 Client 作業系統內建的應用程式(OS X 的 Twitter 整合) 官方應用程式(GitHub.app)

32、且其他別的流程都不適用 Resource Owner Password Credentials Grant Flow No “state” 因為不經過 Browser No “Redirect URI” 因為沒有 redirection 直接 POST 去 Token Endpoint 拿 Token POST /token HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- urlencoded grant_type=password charset=UTF-8 WWW-

33、Authenticate: Basic Cache-Control: no-store Pragma: no-cache “error“:“invalid_client“, “error_description“:“Client Authentication Failed“, “error_uri“:“https:/ HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 WWW-Authenticate: Basic Cache-Control: no-store Pragma: no-cache “err

34、or“:“invalid_client“, “error_description“:“Client Authentication Failed“, “error_uri“:“https:/ 各有建議的 Response Code HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 WWW-Authenticate: Basic Cache-Control: no-store Pragma: no-cache “error“:“invalid_client“, “error_description“:“Cl

35、ient Authentication Failed“, “error_uri“:“https:/ 各有建議的 Response Code Client 透過 Basic Auth 認證失敗則塞這個 header HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 WWW-Authenticate: Basic Cache-Control: no-store Pragma: no-cache “error“:“invalid_client“, “error_description“:“Client Aut

36、hentication Failed“, “error_uri“:“https:/ 各有建議的 Response Code Client 透過 Basic Auth 認證失敗則塞這個 header 錯誤碼 HTTP/1.1 401 Unauthorized Content-Type: application/json;charset=UTF-8 WWW-Authenticate: Basic Cache-Control: no-store Pragma: no-cache “error“:“invalid_client“, “error_description“:“Client Authent

37、ication Failed“, “error_uri“:“https:/ 各有建議的 Response Code Client 透過 Basic Auth 認證失敗則塞這個 header 錯誤碼 簡述錯誤是什麼 error =Meaning invalid_clientClient 認證失敗 Authorization Server 認不出 Client 要回 401 Unauthorized 如果是用 Basic Auth 送出 Client Auth ,則要附上 WWW-Authenticate: Basic error =Meaning invalid_grant 提出的 Grant

38、或是 Refresh Token 不正確、 過期、被撤銷,或 Redirection URI 不符, 或根本就不是給你這個 Client。 授權狀不正確 error =Meaning invalid_request 沒給必要參數、給了不正確的參數、 重複給參數、或其他原因導致無法解讀。 unauthorized_client Client 不准用這種 Response Type 來取得 Authorization Code。 unsupported_grant_type Authorization Server 不支援使用這種 Grant Type。 invalid_scope 所要求的 sc

39、ope 不正確、 不明、無法解讀。 Client 造成的錯誤 error =Meaning Authorization Server Internal Server Error error =Meaning Q: 為什麼沒有? Authorization Server Internal Server Error A: 因為是 Client 直接發 Request 到 Server,直接噴 500 就行 拿到 Token 了,如何打 API RFC 6750 “Bearer Token Usage” HTTP/1.1 200 OK Content-Type: application/json;c

40、harset=UTF-8 Cache-Control: no-store Pragma: no-cache “access_token“:“2YotnFZFEjr1zCsicMWpAA“, “token_type“:“Bearer“, “expires_in“:3600, “refresh_token“:“tGzv3JOkF0XG5Qx2TlKWIA“, “scopes“:“email“ Bearer Token RFC 6750 定義,一種 OAuth 2.0 的 Token 用法最簡單:出示 Token 給 Resource Server Token 內容亂碼就行 Spec 只規定出示 T

41、oken 的方式 charset=UTF-8 Cache-Control: private(or no-store if not 200) 發生錯誤時的回應方式 Bearer Token Error 1. 參數傳錯 2. Token 不正確 3. Scope 權限不足 回應方式 一律用 WWW-Authenticate header 回錯誤 有出示 Token ,才能附上其他 Error Parameters 沒規定不能再用 JSON 回 error =StatusMeaning invalid_request400 沒給必要參數、給了不正確的參數 重複給參數、重複出示 Access Toke

42、n 或其他原因導致無法解讀。 invalid_token401 Access Token 過期、被撤回、無法解讀。 Client 可以重新申請一個 Token 再試。 insufficient_scope403 Token 的 scope 權限不足。 Error 可以附上 scope= 來提示所需權限。 HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer realm=“The API“ error=“invalid_token“ error_description=“Token is not usable.“ HTTP/1.1 403 Forb

43、idden WWW-Authenticate: Bearer realm=“The API“, error=“insufficient_scope“, error_description=“Scope not sufficient.“, scope=“friend_list photo“ 完全沒出示 Access Token HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer realm=“The API“ GET /api/articles HTTP/1.1 Host: Token 過期 換發 Token Refresh Token Ref

44、resh 拿 Access Token 核發時的 Refresh Token 去換發 原本沒有發 Refresh Token ,就不能換發 POST 到 Token Endpoint POST /token HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- urlencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2TlKWIA &scope=search_timeline POST /token

45、HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- urlencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2TlKWIA &scope=search_timeline Token Endpoint POST /token HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- ur

46、lencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2TlKWIA &scope=search_timeline Token Endpoint Client Authentication POST /token HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- urlencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2Tl

47、KWIA &scope=search_timeline Token Endpoint 表示我 Client 要換發新的 Token Client Authentication POST /token HTTP/1.1 Host: Authorization: Basic YWJjOjEyMw= Content-Type: application/x-www-form- urlencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2TlKWIA &scope=search_timeline Token Endpoint 表示我 Client 要換發新的 Token Client Authentication scope 可省略,不可大於 Access Token 申請過的權限 製造 OAuth 2 Provider 的方法 = 造 Authorization Server Service spec 相容 支援的 Grant Typesscope 分隔 Refresh Token Client 認證 FacebookAuth Code, Implicit, Client Cma (自製)GET Gi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/环境 > 装饰装潢


经营许可证编号:宁ICP备18001539号-1