SlideShare une entreprise Scribd logo
1  sur  28
OAuth2.0 协议介绍
冯炜
什么是OAuth协议
OAuth协议为用户资源的授权提供了一个安全的、开放而又
简易的标准。与以往的授权方式不同之处是OAuth的授权不
会使第三方触及到用户的帐号信息(如用户名与密码),
即第三方无需使用用户的用户名与密码就可以申请获得该
用户资源的授权。
目前的版本是2.0
OAuth2.0的使用
应用场景
第三方登录 分享
开放API 应用接入
微信服务号
应用场景
第三方登录 分享
开放API 应用接入
微信服务号
名词定义(Roles)
• Resource Owner:资源所有者,用户
• Client:客户端,第三方应用程序
• User Agent:用户代理,一般指浏览器
• Authorization server:认证服务器,即服务提供商专门用
来处理认证的服务器
• Resource server:资源服务器,即服务提供商存放用户生
成的资源的服务器。它与认证服务器,可以是同一台服
务器,也可以是不同的服务器
协议流程(Protocol Flow)
认证授权模式(Authorization Grant)
• 授权码模式(Authorization Code)
• 隐式模式(Implicit)
• 资源所有者密码凭据模式(Resource Owner Password
Credentials)
• 客户端凭据模式(Client Credentials)
授权码模式授权码模式(Authorization Code)
授权码模式(Authorization Code)
步骤A,用户访问客户端,客户端将用户导向认证服务器
客户端申请认证的URI示例:
GET
/authorize?response_type=code&client_id=s6BhdRkqXtXEF3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fc
b&scope=user_info%20friend_info&state=xyz HTTP/1.1
Host: server.example.com
其中参数response_type和client_id是必选项
步骤B,用户在认证服务器登录
授权码模式(Authorization Code)
步骤C,假设用户给予授权,认证服务器将用户导向客户
端事先指定的“重定向URI”(redirection URI),同时附上一
个授权码,服务器回应客户端的URI:
HTTP/1.1 302 Found
Location:
https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSb
IA &state=xyz
code表示授权码,必选项。有效期应很短,通常设为10分
钟,客户端只能使用该码一次,否则会被认证服务器拒绝
授权码与client_id和redirect_uri 是一一对应关系
授权码模式(Authorization Code)
步骤D,客户端收到授权码,附上"重定向URI",向认证服
务器申请令牌。这一步是在客户端的后台的服务器上完成
的,对用户不可见。
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2
Fcb
授权码模式(Authorization Code)
步骤E,认证服务器核对了授权码和重定向URI,确认无误
后,向客户端发送访问令牌(access token)和更新令牌
(refresh token),一个成功响应:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
授权码模式(Authorization Code)
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600,
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
"example_parameter":"example_value"
}
返回JSON格式,不使用缓存
token_type表示令牌类型,该值大小写不敏感,必选项,
可以是bearer类型或mac类型
授权码模式(Authorization Code)
• 适用场景
• 第三方应用是Web服务器
• 需要长时间访问资源(refresh_token)
• 避免访问令牌经过浏览器,减少泄漏可能(相对于隐
式模式)
简化模式(Implicit)隐式模式(Implicit)
隐式模式(Implicit)
步骤A,用户访问客户端,客户端将用户导向认证服务器
客户端申请认证的URI示例:
GET
/authorize?response_type=token&client_id=s6BhdRkqXtXEF3
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2
Fcb&scope=&state=xyz HTTP/1.1
Host: server.example.com
其中参数response_type和client_id是必选项
步骤B,用户在认证服务器登录
隐式模式(Implicit)
步骤C,假设用户给予授权,认证服务器将用户导向客户端
指定的"重定向URI",并在URI的Hash部分包含了访问令牌:
HTTP/1.1 302 Found
Location:
http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpA
A&state=xyz&token_type=example&expires_in=3600
隐式模式(Implicit)
• 适用场景
• 只需要临时访问保护资源
• 第三方应用(客户端)运行在浏览器上(JavaScript, Flash …)
• 第三方应用和浏览器是高可信的
• 与授权码模式的区别
• 不使用二次交换,直接发放访问令牌
• 访问令牌在浏览器传输,安全性较低
• 为安全考虑,不提供刷新令牌
资源所有者密码凭据模式(Resource Owner
Password Credentials)
资源所有者密码凭据模式(Resource Owner
Password Credentials)
• 适用场景
• 仅限官方应用
• 刷新令牌要慎用,等同于在第三方应用保存了一份用
户名和密码
• 一般辅助以更高安全保护,如SSL,数字证书、U盾等
客户端凭据模式(Client Credentials)
客户端凭据模式(Client Credentials)
• 使用场景
• 第三方应用自己的资源
• 内部应用,如本身是资源服务器
• 其他流程已经保障了资源安全性和授权
其他
更新令牌(refresh_token)
错误返回
Client注册与认证
HTTP + TLS HTTPS
安全性问题
OAuth2.0的实现
平台 完全符合协议 支持的认证授权模式
新浪微博 ✕ Authorization Code
QQ ✕ Authorization Code,Implicit
微信 ✕ Authorization Code
豆瓣 ✕ Authorization Code, Implicit
Facebook ✕ Authorization Code, Implicit, Client Credentials
Amazon ◯ Authorization Code, Implicit
GitHub ✕ Authorization Code, Password (自制)
新浪微博OAuth2.0授权机制
1. 引导需要授权的用户到如下地址:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&r
esponse_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
2. 如果用户同意授权,页面跳转至
YOUR_REGISTERED_REDIRECT_URI/?code=CODE
3. 换取Access Token
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_I
D&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_co
de&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
4.使用获得的Access Token调用API
参考文档
RFC 6749
RFC 6749中文版
oauth.net
OAuth 2.0:通往地狱之路
理解OAuth 2.0
OAuth 2.0 筆記
谢谢

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

OAuth2.0协议介绍

Notes de l'éditeur

  1. 主要参照RFC6749 http://tools.ietf.org/html/rfc6749
  2. 互联,开放
  3. 主要根据OAuth2.0协议定义RFC6749来进行介绍
  4. (A)用户打开客户端以后,客户端要求用户给予授权。 (B)用户同意给予客户端授权。 (C)客户端使用上一步获得的授权,向认证服务器申请令牌。 (D)认证服务器对客户端进行认证以后,确认无误,同意发放令牌。 (E)客户端使用令牌,向资源服务器申请获取资源。 (F)资源服务器确认令牌无误,同意向客户端开放资源。
  5. 认证授权模式实际上就是获取access_token的模式 常用的是前两种
  6. (A)用户访问客户端,后者将前者导向认证服务器。 (B)用户选择是否给予客户端授权。 (C)假设用户给予授权,认证服务器将用户导向客户端事先指定的"重定向URI"(redirection URI),同时附上一个授权码。 (D)客户端收到授权码,附上早先的"重定向URI",向认证服务器申请令牌。这一步是在客户端的后台的服务器上完成的,对用户不可见。 (E)认证服务器核对了授权码和重定向URI,确认无误后,向客户端发送访问令牌(access token)和更新令牌(refresh token)。
  7. A步骤中,客户端申请认证的URI,包含以下参数: response_type:表示授权类型,必选项,此处的值固定为"code" client_id:表示客户端的ID,必选项 redirect_uri:表示重定向URI,可选项 scope:表示申请的权限范围,用空格分隔,可选项 state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值,用于第三方应用防止CSRF(跨站请求伪造)攻击(http://drops.wooyun.org/papers/155)。
  8. C步骤中,服务器回应客户端的URI,包含以下参数: code:表示授权码,必选项。该码的有效期应该很短,通常设为10分钟,客户端只能使用该码一次,否则会被授权服务器拒绝。该码与client_id和redirect_uri ,是一一对应关系。 state:如果客户端的请求中包含这个参数,认证服务器的回应也必须一模一样包含这个参数。
  9. D步骤中,客户端向认证服务器申请令牌的HTTP请求,包含以下参数: grant_type:表示使用的授权模式,必选项,此处的值固定为"authorization_code"。 code:表示上一步获得的授权码,必选项。 redirect_uri:表示重定向URI,必选项,且必须与A步骤中的该参数值保持一致。 client_id:表示客户端ID,必选项。
  10. E步骤中,认证服务器发送的HTTP回复,包含以下参数: access_token:表示访问令牌,必选项。 token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。 expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。 refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。 scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。
  11. E步骤中,认证服务器发送的HTTP回复,包含以下参数: access_token:表示访问令牌,必选项。 token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。 expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。 refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。 scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。
  12. E步骤中,认证服务器发送的HTTP回复,包含以下参数: access_token:表示访问令牌,必选项。 token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。 expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。 refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。 scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。
  13. (A)客户端将用户导向认证服务器。 (B)用户决定是否给于客户端授权。 (C)假设用户给予授权,认证服务器将用户导向客户端指定的"重定向URI",并在URI的Hash部分包含了访问令牌。 (D)浏览器向资源服务器发出请求,其中不包括上一步收到的Hash值。 (E)资源服务器返回一个网页,其中包含的代码可以获取Hash值中的令牌。 (F)浏览器执行上一步获得的脚本,提取出令牌。 (G)浏览器将令牌发给客户端。
  14. window.location.hash
  15. (A)用户向客户端提供用户名和密码。 (B)客户端将用户名和密码发给认证服务器,向后者请求令牌。 (C)认证服务器确认无误后,向客户端提供访问令牌。
  16. 其实不属于Oauth范围,访问的资源不属于个人。如多个应用访问一个服务 (A)客户端向认证服务器进行身份认证,并要求一个访问令牌。 (B)认证服务器确认无误后,向客户端提供访问令牌。
  17. 内网 防火墙