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. 参与者:

    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 流程图

接口

  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 流程图

OAuth2.0 流程

OAuth 2.0定义了四种授权方式。

  1. 授权码模式(authorization code)(本文只讲解该授权方式)
  2. 简化模式(implicit)
  3. 密码模式(resource owner password credentials)
  4. 客户端模式(client credentials)

接口

  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参数是否是该用户所对应的重定向地址

参考文献

  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)

最新文章

  1. 打造自己的html5视频播放器
  2. STL迭代器之一:偏特化
  3. Codeforces Beta Round #3
  4. 函数式编程Map()&Reduce()
  5. Compile Sources 和 Copy Bundle Resources的区别
  6. Azure杂七杂八系列(二) - 如何在Azure上重新配置VM
  7. UVaLive 7370 Classy (排序,比较)
  8. [JIT_APP]Android SQLite简介
  9. Java反射获取类和对象信息全解析
  10. MFC_Office
  11. Tomcat 安装与配置
  12. PHP 微信分享页面(图文)
  13. Linux Shell 命令--rename
  14. 安装VirtualBox后 不能选择64bit的系统
  15. 通过GUID确保winform运行唯一实例
  16. 【学习】数据处理基础知识(汇总和计算描述统计)【pandas】
  17. css学习_写法规范、选择器
  18. 20145326蔡馨熤《网络对抗》—— Web基础
  19. Win7如何解决telnet不是内部或外部命令的方案!
  20. 使用Koa2搭建web项目

热门文章

  1. Jmeter连接Mysql出现Cannot create PoolableConnectionFactory (Could not create connection to database server.)错误
  2. 基础篇四:Ngnix安装
  3. python之处理json字符串
  4. EXAM-2018-7-27
  5. King of the Waves
  6. python关于文件操作
  7. android应用市场、社区客户端、漫画App、TensorFlow Demo、歌词显示、动画效果等源码
  8. YII框架开发一个项目的通用目录结构:
  9. iOS路由详解
  10. RHCSA考试(Linux7)