HTTP Basic Authentication(二)—— BasicAuth,OAuth and XAuth 三种认证方式的区别

新浪微博于2011年6月1日全面停止BasicAuth认证,转向xAuth。说说他们的区别,并以新浪腾讯微博为例。

 BasicAuth 

 

BasicAuth required the developer of an application to store the username and password of the user, and transmit these along with each request.

Basic Authentication是一种通过HTTP头传递用户身份的授权方式。在非HTTPS方式下使用存在密码被窃听风险。 采用普通鉴权(Basic Authentication)时app_key(consumer key)通过请求参数直接传递,参数名为 source=consumer key,如

curl -u user:password -d "source=10001&status=api test" http://api.t.sina.com.cn/statuses/update.xml

Basic Auth编程也可以参看这篇文章 http://www.cnblogs.com/QLeelulu/archive/2009/11/22/1607898.html

 

 

OAuth

 

OAuth是一种国际通用的授权方式,它的特点是不需要用户在第三方应用输入用户名及密码。OAuth的技术说明可参看官方网站http://oauth.net (英文)。

微博系统中,OAuth的Access token不会过期,只有用户手工撤销授权或新浪收回您的app访问权限access token才会失效。

目前OAuth只支持授权读写访问,授权的应用可以获取用户数据及发表微博。目前暂时不支持只读权限授权。

 

OAuth  is an open standard, where the user is redirected to Twitter, fills in his username/password there (or is already logged in) and then grants clearance for the application to use his account. The application never sees the username/password.

To quote the twitter pages:

Basic Authentication is a liability. By storing logins and passwords, a developer takes on additional responsibilities for the secure storage of those credentials; the potential harm to users if login credentials are leaked or abused is very high. Because many users utilize the same password across many sites, the potential for damage does not necessarily stop with their Twitter account.

 

OAuth协议为用户资源的授权提供了一个安全的、开放而又简易的标准。同时,任何第三方都可以使用OAuth认证服务,任何服务提供商都可以实现自身的OAuth认证服务,因而OAuth是开放的。业界提供了OAuth的多种实现如PHP,JavaScript,Java,Ruby等各种语言开发包,大大节约了程序员的时间,因而OAuth是简易的。目前互联网很多服务如Open API,很多大头公司如Google,Yahoo,Microsoft等都提供了OAuth认证服务,这些都足以说明OAuth标准逐渐成为开放资源授权的标准。

在官方网站的首页,可以看到下面这段简介:

http://open.t.qq.com/resource.php?i=1,2 


An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.

大概意思是说OAuth是一种开放的协议,为桌面程序或者基于BS的web应用提供了一种简单的,标准的方式去访问需要用户授权的API服务。OAuth类似于Flickr Auth、Google's AuthSub[1]、Yahoo's BBAuth、 Facebook Auth等。

OAuth认证授权具有以下特点:

  • 1. 简单:不管是OAuth服务提供者还是应用开发者,都很容易于理解与使用;
  • 2. 安全:没有涉及到用户密钥等信息,更安全更灵活;
  • 3. 开放:任何服务提供商都可以实现OAuth,任何软件开发商都可以使用OAuth;

 

 

OAuth的原理认证流程及访问资源流程

图 1 授权流程

腾讯微博API通过以下四个步骤来完成认证授权并访问或修改受限资源的流程

  1. 1.获取未授权的Request Token(temporary credentials)
  2. 2.请求用户授权Request Token
  3. 3.使用授权后的Request Token换取Access Token(token credentials)
  4. 4.使用 Access Token 访问或修改受保护资源

其中1~3步使用https方式, 第4步使用http方式。

 

 

xAuth

xAuth认证实际上是OAuth认证的简化版。

 

使用xAuth认证方式,您仍然需要了解如何生成OAuth签名。

为了方便桌面应用和移动应用,特别是那些缺乏浏览器支持的应用,xAuth认证为这类应用提供了一种使用用户名和密码来获取OAuth的Access Token的方式。 采用xAuth认证的桌面应用和移动应用可以跳过oauth/request_token(获取Request Token)以及oauth/authorize(授权Request Token)两步,只要提供了username和password以后,即可直接通过oauth/access_token接口得到Access Token。

 

xAuth is a simplified version of OAuth. It removes several steps, so your app sends an OAuth-signed POST request with the username and password to Twitter's servers。 (usinghttps://api.twitter.com/oauth/access_token), which directly returns a consumer token and secret for use other requests.

You have to email the Twitter API team to enable xAuth for your app, after your app has OAuth access. 

 

 

 

 

OAuth库和资源

ActionScript/Flash
oauth-as3 http://code.google.com/p/oauth-as3/
A flex oauth client http://www.arcgis.com/home/item.html?id=ff6ffa302ad04a7194999f2ad08250d7
C/C++
QTweetLib http://github.com/minimoog/QTweetLib
libOAuth http://liboauth.sourceforge.net/
clojure
clj-oauth http://github.com/mattrepl/clj-oauth
.net
oauth-dot-net http://code.google.com/p/oauth-dot-net/
DotNetOpenAuth http://www.dotnetopenauth.net/
Erlang
erlang-oauth http://github.com/tim/erlang-oauth
java
Scrible http://github.com/fernandezpablo85/scribe-java
oauth-signpost http://code.google.com/p/oauth-signpost/
javascript
oauth in js http://oauth.googlecode.com/svn/code/javascript/
Objective-C/Cocoa & iPhone programming
OAuthCore http://bitbucket.org/atebits/oauthcore
MPOAuthConnection http://code.google.com/p/mpoauthconnection/
Objective-C OAuth http://oauth.googlecode.com/svn/code/obj-c/
Perl
Net::OAuth http://oauth.googlecode.com/svn/code/perl/
PHP
tmhOAuth http://github.com/themattharris/tmhOAuth
oauth-php http://code.google.com/p/oauth-php/
Python
python-oauth2 http://github.com/brosner/python-oauth2
Qt
qOauth http://github.com/ayoy/qoauth
Ruby
Oauth ruby gem http://oauth.rubyforge.org/
Scala
DataBinder Dispatch http://dispatch.databinder.net/About

  来自新浪微博:http://blog.sina.com.cn/s/blog_676032e60100s3zi.html

posted @ 2018-08-29 10:10  舞羊  阅读(2329)  评论(0编辑  收藏  举报