0%

做个笔记吧,OAuth1.0a

相关信息

OAuth 1.0a的登录流程共有三个步骤:

  1. 获取未授权的Request Token,与服务器交互。
    POST https://www.example.com/oauth/request_token
  2. 请求用户授权Request Token,客户端使用webview打开登录页面,用户登录授权。
    https://www.example.com/oauth/authorize
  3. 使用授权后的Request Token换取Access Token,与服务器交互。
    POST https://www.example.com/oauth/access_token

获取Request Token

示例来源:rfc5849
http请求示例:

1
2
3
4
5
6
7
8
9
POST /oauth/request_token HTTP/1.1
Host: www.example.com
Authorization: OAuth 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",
oauth_version="1.0a"

oauth_consumer_key是注册你的应用后获得。
oauth_signature_method=“HMAC-SHA1”,仅支持HMAC-SHA1。
oauth_timestamp是当前时间戳,以秒为单位。
oauth_nonce是随机字符串,只能包含字母与数字(不确定),与oauth_timestamp唯一对应。(不确定长度是否规定为8)
oauth_callback在注册你的应用时需要提供的回调URL,需要URLEncode。
oauth_signature签名结果,如何签名最后一节讨论,需要URLEncode。
oauth_version协议版本号。

http返回示例:

1
2
3
4
5
HTTP/1.1 200 OK
Content-Type: application/x-www-form-urlencoded

oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03&
oauth_callback_confirmed=true

oauth_token:未授权的token。
oauth_token_secret:参与第三步的签名。
oauth_callback_confirmed:对oauth_callback的确认信号 (true/false)。


请求用户授权,即用户登录

客户端使用webview打开链接:

1
https://www.example.com/oauth/authorize?oauth_token=hh5s93j4hdidpola

注意webview需要开启javascript

1
webView.getSettings().setJavaScriptEnabled(true);

用户登录授权成功后,

服务端返回重定向结果,重定向到第一步传递的callback地址,还包含了参数,oauth_token,oauth_verifier。
示例:

1
2
http://printer.example.com/ready?
oauth_token=hh5s93j4hdidpola&oauth_verifier=hfdp7dh39dks9884

oauth_token:与第一步返回的结果是一样的。
oauth_verifier:授权验证码。


获取Access Token

http请求示例:

1
2
3
4
5
6
7
8
9
POST /token HTTP/1.1
Host: photos.example.net
Authorization: OAuth 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"

与第一步类似,多了两个参数oauth_token,oauth_verifier
oauth_token:即request token,第一二步均有返回,都是一样的。
oauth_verifier:第二步返回的授权验证码。

http返回示例:

1
2
3
4
HTTP/1.1 200 OK
Content-Type: application/x-www-form-urlencoded

oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00

签名规则

rfc5849#section-3.4 Signature
签名的key略过,重点说说签名的内容。

签名内容:

  1. http request method in uppercase. For example: “GET”, “POST”, etc.
  2. An “&” character (ASCII code 38).
  3. The base string URI, after being encoded. Like https://www.example.com/oauth/request_token.
  4. An “&” character (ASCII code 38).
  5. The request parameters as normalized, after being encoded.

第5点内容的 request parameters 包括内容有:

  • OAuth1.0 协议的头部’Authorization’ 包含的内容。
  • http请求的查询参数,GET请求一般都会有。
  • http实体部分,POST请求一般都会有。需要满足几个条件才能包括该部分内容,如下:
  1. The entity-body is single-part.
  2. The entity-body follows the encoding requirements of the
    “application/x-www-form-urlencoded” content-type as defined by
    [W3C.REC-html40-19980424].
  3. The HTTP request entity-header includes the “Content-Type”
    header field set to “application/x-www-form-urlencoded”.

这三部分的内容都会被解析为键值对,组成一个列表,并且以key的字母表进行排序。以&符号相连所有键值对如oauth_consumer_key=<CONSUMER_KEY>, 拼接该部分内容前,需要对key/value都进行encode,注意http请求的查询参数、http实体部分的键值对可能已被encode,若已被encode,则该步骤就不需要再次encode。(拼接为一个字符串后还需要被encode后才能作为第5点的内容)


查阅了几次rfc5849,认识才慢慢完善了,更改了几次,最后还是需要仔仔细细的阅rfc5849。
推荐阅读JakeWharton大神关于OAuth1.0写的代码Oauth1SigningInterceptor.java