OAuth 2.0 筆記 (4.3) Resource Owner Password Credentials Grant Flow 細節
在 Resource Owner Password Credentials Grant Flow 流程裡, Resource Owner 自己的帳號密碼會直接用來當做 Authorization Grant ,並傳遞給 Authorization Server 來取得 Access Token 。這種流程只有在以下情況才能使用:
- Resource Owner 高度信賴 Client ,例如作業系統內建的應用程式(好比說 OS X 的 Twitter 整合)或是官方應用程式。
- 其他別的流程都不適用。
而就算 Client 可以直接拿到 Resource Owner 的帳號密碼,也只會使用一次,用來取得 Access Token 。Spec 裡面定義的流程,會要求 Client 不儲存帳號密碼,而是隨後以長時效的 Access Token 或 Refresh Token 取代之。
Authorization Server 應該要特別小心開放這種流程,並且要在別的流程都行不通的時候才使用這種。
這種流程適用於可以取得 Resource Owner 帳號密碼的 Client (通常是透過一個輸入框)。也可以用來把以前的帳號密碼認證,遷移到 OAuth 認證。
最後拿到的除了 Access Token 之外,還會拿到 Refresh Token (Authorization Server 有支援的話)。
這是 OAuth 2.0 內建的四個流程之一。本文整理自 Section 4.3。
流程圖
+----------+
| Resource |
| Owner |
| |
+----------+
v
| Resource Owner
(A) Password Credentials
|
v
+---------+ +---------------+
| |>--(B)---- Resource Owner ------->| |
| | Password Credentials | Authorization |
| Client | | Server |
| |<--(C)---- Access Token ---------<| |
| | (w/ Optional Refresh Token) | |
+---------+ +---------------+
Figure 5: Resource Owner Password Credentials Flow
(A) Resource Owner 向 Client 提供真正的帳號密碼。
(B) Client 用 Resource Owner 的帳號密碼,向 Authorization Server 的 Token Endpoint 申請 Access Token。這個時候 Client 還要向 Authorization Server 認證自己。
(C) Authorization Server 認證 Client 、驗證 Resource Owner 的帳號密碼,如果正確的話,核發 Access Token。
(A) Authorization Request & Response
在這個流程裡面, Authorization Grant 就是 Resource Owner 的帳號密碼,所以在 Step (A) 裡面直接向 Resource Onwer 索取,沒有經過網路來取得 Authorization。
Spec 不規定 Client 要怎麼拿到帳號密碼,但是 Client 取得 Access Token 之後,必須把 Resource Owner 的帳號密碼給銷毀掉。
(B) Access Token Request
【Client】POST ▶ 【Token Endpoint】
參數
grant_type | 必 | password |
username | 必 | Resource Owner 的帳號 |
password | 必 | Resource Owner 的密碼 |
scope | 選 | 申請的存取範圍 |
Authorization Server 的處理程序
這個 Request 進來的時候, Authorization Server 要做這些事:
- 要求 Client 認證自己(如果是 Confidential Client 或有拿到 Client Credentials)
- 如果 Client 有出示認證資料,就認證它,細節見系列文第 2 篇
- 驗證 Resource Owner 的帳號密碼(以既有的驗證方式)
慎防暴力破解
因為牽涉到帳號密碼,所以 Authorization Server 要可以防 Endpoint 被暴力破解,具體實施的方法像是 Rate Limiting 或是發出警告。
範例
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
(C) Access Token Response
(同 Authorization Code Grant Flow。)
【Client】 ◀ 【Token Endpoint】
若 Access Token Request 合法且有經過授權,則核發 Access Token,同時可以核發 Refresh Token (非必備)。如果 Client 認證失敗,或 Request 不合法,則依照 Section 5.2 的規定回覆錯誤。
詳細核發 Access Token 的細節寫在系列文第 5 篇。
範例
發給 Access Token:
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"
}
安全性問題 (Section 10.7)
帳號密碼外洩
Resource Owner Password Credentials Grant Type 通常是用在老舊 Client ,或是遷移舊的認證機制到 OAuth。雖然這種流程降低了在 Client 裡面儲存帳號密碼所引來的風險,但是沒有消除把帳號密碼給 Client 看的必要性。(編按:第一步還是需要 Resonrce Owner 提供帳號密碼)
這個流程的風險比起其他流程還要高,因為它保留了使用密碼的 anti-pattern,而這個卻是 OAuth spec 致力避免的。Client 可能會濫用密碼,或是密碼會不經意地洩漏給壞人(例如 Log 或是其他 Client 保存的記錄)。
Resource Owner 無法控制授權權限與存取範圍
此外,因為 Resource Owner 沒辦法控制授權的流程(Resource Owner 只參與到輸入帳號密碼),Client 可以取得比 Resource Owner 期望的權限 (scopes) 還要更多的權限。Authorization Server 在透過這種流程合法 Access Token 的時候應該要慎重考慮 scope 和時效的問題。
Authorization Server 和 Client 應該要儘量不使用這種流程,改用其他流程。
转自:https://blog.yorkxin.org/2013/09/30/oauth2-4-3-resource-owner-credentials-grant-flow
相关推荐
在服务端,基于 Owin OAuth, 针对 Resource Owner Password Credentials Grant 的授权方式,只需重载 OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials() 方法即可
Spring Security OAuth2.0学习笔记 什么是认证、授权、会话。 Java Servlet为支持http会话做了哪些事儿。 基于session认证机制的运作流程。 基于token认证机制的运作流程。 理解Spring Security的工作原理,Spring ...
OAuth2.0是OAuth协议的延续版本,但不向前兼容OAuth 1.0(即完全废止了OAuth1.0)。 OAuth 2.0关注客户端开发者的简易性。要么通过组织在资源拥有者和HTTP服务商之间的被批准的交互动作代表用户,要么允许第三方应用...
springboot 集成oauth2.0服务器,基于oauth2.0授权码形式集成
完整Oauth 2.0 代码实现,包含数据库脚本,使用说明,导入数据库脚本,修改数据库配置可直接运行。
通过点击viewbutton获取用户openid,实现方式oauth2.0认证
OAUTH2.0+OpenLDAP技术框架,及适用场景,综合价值等PPT文档,可修改!!!
基于Owin中间件的OAuth2.0身份认证,文章位置https://blog.csdn.net/u013938578/article/details/82956188
cas3.5.0集成oauth2.0协议,模拟cas3.5.0通过oauth2.0协议与集成了coauth2.0的cas进行模拟通信。可以实现 与新浪微博等第三方身份验证平台对接,实现单点登录。
基于Django2.1.2的OAuth2.0授权登录 大学生课程设计 基于Django2.1.2的OAuth2.0授权登录的课程设计 自己大二写的课程设计
Oauth2.0,第三方登录鉴权验证教程。
OAuth2.0协议原理与实现
OAuth2.0协议中文版,方便了解OAuth2.0协议
spring security oauth2.0 需要的基础 sql 文件
Oauth2.0协议 整合php框架 客户端 服务端授权码模式小demo
OWIN OAuth 2.0 Authorization ...- Resource Owner Password Credentials Grant - Client Credentials Grant - Creating a resource server which is protected by an access token. - Creating OAuth 2.0 clients.
Spring-security-oauth2.0实现
OAuth 2.0 in Action OAuth 2.0 in Action OAuth 2.0 in Action OAuth 2.0 in Action OAuth 2.0 in Action