碰巧遇到OAuth,在网上学了一学,在此记录以备忘。html
看了不少介绍文章,最后仍是以为RFC最能让你理解具体这个协议是怎么工做了,在此把RFC5849的1.2节翻译一下。安全
RFC5849服务器
1.2 例子app
Jane (resource owner) 已经上传了一些她的假期照片(protected resources) 到她的照片分享网站'photos.example.net' (server). 她但愿用 'printer.example.com' 网站(client) 来打印其中一张照片.和一般同样,Jane用她的用户名和密码登陆到 'photos.example.net' .
但Jane不想告诉'printer.example.com'她的用户名和密码,而为了打印而这个网站须要访问照片。为了给用户提供更好的服务,'printer.example.com' 已经事先取得了一组'photos.example.net'的客户端资格(client credentials): 客户端识别号码(Client Identifier):
dpf43f3p2l4k3l03 客户端共享密文(Client Shared-Secret): kd94hf93k423kf4 'printer.example.com' 网站也设置好了它的程序以便利用列举在 'photos.example.net' 的API文档里的协议终端接口,这些终端使用"HMAC-SHA1"签名方法:
临时资格请求 https://photos.example.net/initiate 资源主受权 URI:
https://photos.example.net/authorize Token请求URI:
https://photos.example.net/token 在'printer.example.com'能请求Jane受权它访问照片以前,它必须同'photos.example.net'创建一组临时资格以用来识别代理请求。 为了实现这个,客服端送出一下HTTPS [RFC2818] 请求到服务器端: POST /initiate HTTP/1.1 Host: photos.example.net Authorization: OAuth realm="Photos", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_signature_method="HMAC-SHA1", oauth_timestamp="137131200", oauth_nonce="wIjqoS", oauth_callback="http%3A%2F%2Fprinter.example.com%2Fready", oauth_signature="74KNZJeDHnMBp0EMJ9ZHt%2FXKycU%3D" 服务器端验证这个请求并在HTTP应答的正文里面回复一组临时资格(改行只是为了显示方便):
HTTP/1.1 200 OK Content-Type: application/x-www-form-urlencoded oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03& oauth_callback_confirmed=true 客户端转送Jane的user-agent到服务器的资源主受权端口以得到对于访问私有照片的Jane的批准: https://photos.example.net/authorize?oauth_token=hh5s93j4hdidpola 服务器请求Jane用她的用户名和密码登陆,若是登陆成功,服务器再要求她批准受权 'printer.example.com' 访问她私有的照片。Jane 批准这个请求而后她的user-agent就被转送到客服端在之前的请求里提供的callback URI: (改行只是为了显示方便):
http://printer.example.com/ready? oauth_token=hh5s93j4hdidpola&oauth_verifier=hfdp7dh39dks988 这个callback请求通知客户端Jane完成了受权处理。而后客户端用它的临时资格来要求另外一组token资格(在一个安全的Transport Layer Security (TLS) 通道上): POST /token HTTP/1.1 Host: photos.example.net Authorization: OAuth realm="Photos", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_token="hh5s93j4hdidpola", oauth_signature_method="HMAC-SHA1", oauth_timestamp="137131201", oauth_nonce="walatlh", oauth_verifier="hfdp7dh39dks9884", oauth_signature="gKgrFCywp7rO0OXSjdot%2FIHF7IU%3D" 服务器验证这个请求并在HTTP应答的正文里面回复一组token资格:
HTTP/1.1 200 OK Content-Type: application/x-www-form-urlencoded oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00
有了这组token资格,客户端作好了访问私有照片的准备:
GET /photos?file=vacation.jpg&size=original HTTP/1.1 Host: photos.example.net Authorization: OAuth realm="Photos", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_token="nnch734d00sl2jdk", oauth_signature_method="HMAC-SHA1", oauth_timestamp="137131202", oauth_nonce="chapoH", oauth_signature="MdpQcU8iPSUjWoN%2FUDMsK2sui9I%3D" 'photos.example.net'服务器验证这个请求并回复被请求访问的照片.在Jane的受权的存活期间,'printer.example.com'可以用同一个token资格 继续访问Jane的私有照片, 或者直到Jane让访问无效.
参考资料网站
http://tools.ietf.org/html/rfc5849ui
这里能看到一个很直观的user experience分析图url
http://hueniverse.com/oauth/.net
论述OAuth1.0a, WRAP( ) 以及 OAuth2.0的文章翻译
http://radar.oreilly.com/2010/01/whats-going-on-with-oauth.html代理