OAuth2 與 JWT:如何設(shè)計安全的API

導(dǎo)讀:本文會詳細(xì)描述兩種通用的保證API安全性的方法:OAuth2和JSON Web Token (JWT)。
你已經(jīng)或者正在實現(xiàn)API; 你正在考慮選擇一個合適的方法保證API的安全性;
JWT和OAuth2之比較
JWT是一種認(rèn)證協(xié)議 JWT提供了一種用于發(fā)布接入令牌(Access Token),并對發(fā)布的簽名接入令牌進(jìn)行驗證的方法。令牌(Token)本身包含了一系列聲明,應(yīng)用程序可以根據(jù)這些聲明限制用戶對資源的訪問。 OAuth2是一種授權(quán)框架 另一方面,OAuth2是一種授權(quán)框架,提供了一套詳細(xì)的授權(quán)機(jī)制(指導(dǎo))。用戶或應(yīng)用可以通過公開的或私有的設(shè)置,授權(quán)第三方應(yīng)用訪問特定資源。既然JWT和OAuth2沒有可比性,為什么還要把這兩個放在一起說呢?實際中確實會有很多人拿JWT和OAuth2作比較。標(biāo)題里把這兩個放在一起,確實有誤導(dǎo)的意思。很多情況下,在討論OAuth2的實現(xiàn)時,會把JSON Web Token作為一種認(rèn)證機(jī)制使用。這也是為什么他們會經(jīng)常一起出現(xiàn)。
JSON Web Token (JWT)

JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is digitally signed using JSON Web Signature (JWS).
-RFC7519 https://tools.ietf.org/html/rfc7519
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ一個token包含三部分: header.claims.signature了安全的在url中使用,所有部分都 base64 URL-safe進(jìn)行編碼處理。
{"alg" : "AES256","typ" : "JWT"}
一個簡單的聲明(claim)的例子:
{"sub": "1234567890","name": "John Doe","admin": true}
OAuth2是什么?
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.
-RFC6749 https://tools.ietf.org/html/rfc6749
資源擁有者
資源服務(wù)器
客戶端應(yīng)用
認(rèn)證服務(wù)器
私有的
公開的
Web應(yīng)用
用戶代理
原聲應(yīng)用
授權(quán)碼
隱式授權(quán)
資源擁有者密碼證書
客戶端證書
Endpoints終端
認(rèn)證終端
Token終端
重定向終端
使用HTTPS保護(hù)用戶密碼
????????安全地傳輸用戶提供的私密信息,在任何一個安全的系統(tǒng)里都是必要的。否則任何人都可以通過侵入私人wifi,在用戶登錄的時候竊取用戶的用戶名和密碼等信息。
結(jié)論
JWT使用場景
無狀態(tài)的分布式API
快速開發(fā)
不需要cookie
JSON在移動端的廣泛應(yīng)用
不依賴于社交登錄
相對簡單的概念理解
Token有長度限制
Token不能撤銷
需要token有失效時間限制(exp)
OAuth2使用場景
在作者看來兩種比較有必要使用OAuth2的場景:
外包認(rèn)證服務(wù)器
快速開發(fā)
實施代碼量小
維護(hù)工作減少
大型企業(yè)解決方案
To be clear, OAuth 2.0 at the hand of a developer with deep understanding of web security will likely result is a secure implementation. However, at the hands of most developers – as has been the experience from the past two years – 2.0 is likely to produce insecure implementations.
hueniverse - OAuth 2.0 and the Road to Hell
靈活的實現(xiàn)方式
可以和JWT同時使用
可針對不同應(yīng)用擴(kuò)展
進(jìn)一步
http://jwt.io - JWT官方網(wǎng)站,也可以查看到使用不同語言實現(xiàn)的庫的狀態(tài)。
http://oauth.net/2/ OAuth2官方網(wǎng)站, 也也可以查看到使用不同語言實現(xiàn)的庫的狀態(tài)。
OAuth 2 tutorials - Useful overview of how OAuth 2 works
Oauth2 Spec issues Eran Hammer’s (推進(jìn)OAuth標(biāo)準(zhǔn)的作者) views on what went wrong with the OAuth 2 spec process. Whatever your own opinion, good to get some framing by someone who understand’s key aspects of what make a security standard successful.
Thoery and implemnetation: with Laravel and Angular Really informative guide to JWT in theory and in practice for Laravel and Angular.
版權(quán)申明:內(nèi)容來源網(wǎng)絡(luò),版權(quán)歸原創(chuàng)者所有。除非無法確認(rèn),我們都會標(biāo)明作者及出處,如有侵權(quán)煩請告知,我們會立即刪除并表示歉意。謝謝!

