oauth-1.0a
Advanced tools
Comparing version 0.0.8 to 0.1.0
{ | ||
"name": "oauth-1.0a", | ||
"main": "oauth-1.0a.js", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/ddo/oauth-1.0a", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -5,3 +5,3 @@ { | ||
"description": "OAuth 1.0a Request Authorization for Node and Browser", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "oauth", |
@@ -15,5 +15,22 @@ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') { | ||
this.consumer = opts.consumer; | ||
this.signature_method = opts.signature_method || 'HMAC-SHA1'; | ||
if(!opts) { | ||
opts = {}; | ||
} | ||
if(!opts.consumer) { | ||
throw new Error('consumer option is required'); | ||
} | ||
this.consumer = opts.consumer; | ||
this.signature_method = opts.signature_method || 'HMAC-SHA1'; | ||
this.nonce_length = opts.nonce_length || 32; | ||
this.version = opts.version || '1.0'; | ||
this.parameter_seperator = opts.parameter_seperator || ', '; | ||
if(typeof opts.last_ampersand === 'undefined') { | ||
this.last_ampersand = true; | ||
} else { | ||
this.last_ampersand = opts.last_ampersand; | ||
} | ||
switch (this.signature_method) { | ||
@@ -32,6 +49,4 @@ case 'HMAC-SHA1': | ||
throw new Error('oauth-1.0a does not support this signature method right now. Coming Soon...'); | ||
break; | ||
default: | ||
throw new Error('The OAuth 1.0a protocol defines three signature methods: HMAC-SHA1, RSA-SHA1, and PLAINTEXT only'); | ||
break; | ||
} | ||
@@ -57,10 +72,12 @@ } | ||
oauth_timestamp: this.getTimeStamp(), | ||
oauth_version: '1.0' | ||
oauth_version: this.version | ||
}; | ||
if (!token) | ||
if(!token) { | ||
token = {}; | ||
} | ||
if (token.public) | ||
if(token.public) { | ||
oauth_data.oauth_token = token.public; | ||
} | ||
@@ -109,3 +126,3 @@ oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data); | ||
//base_string_data to string | ||
for (var key in base_string_data) { | ||
for(var key in base_string_data) { | ||
data_str += key + '=' + base_string_data[key] + '&'; | ||
@@ -126,2 +143,7 @@ } | ||
token_secret = token_secret || ''; | ||
if(!this.last_ampersand && !token_secret) { | ||
return this.percentEncode(this.consumer.secret); | ||
} | ||
return this.percentEncode(this.consumer.secret) + '&' + this.percentEncode(token_secret); | ||
@@ -148,3 +170,3 @@ }; | ||
for (var i = 0; i < arr.length; i++) { | ||
for(var i = 0; i < arr.length; i++) { | ||
var item = arr[i].split('='); | ||
@@ -192,3 +214,3 @@ data[item[0]] = item[1]; | ||
for (var key in data) { | ||
for(var key in data) { | ||
result[this.percentEncode(key)] = this.percentEncode(data[key]); | ||
@@ -210,10 +232,10 @@ } | ||
for (var key in oauth_data) { | ||
for(var key in oauth_data) { | ||
if (key.indexOf('oauth_') === -1) | ||
continue; | ||
header_value += this.percentEncode(key) + '="' + this.percentEncode(oauth_data[key]) + '", '; | ||
header_value += this.percentEncode(key) + '="' + this.percentEncode(oauth_data[key]) + '"' + this.parameter_seperator; | ||
} | ||
return { | ||
Authorization: header_value.substr(0, header_value.length - 2) //cut the last 2 chars | ||
Authorization: header_value.substr(0, header_value.length - this.parameter_seperator.length) //cut the last chars | ||
}; | ||
@@ -224,12 +246,9 @@ }; | ||
* Create a random word characters string with input length | ||
* @param {Int} length (Default: 32) | ||
* @return {String} a random word characters string | ||
*/ | ||
OAuth.prototype.getNonce = function(length) { | ||
length = length || 32; | ||
OAuth.prototype.getNonce = function() { | ||
var word_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | ||
var result = ''; | ||
for (var i = 0; i < length; i++) { | ||
for(var i = 0; i < this.nonce_length; i++) { | ||
result += word_characters[parseInt(Math.random() * word_characters.length, 10)]; | ||
@@ -259,3 +278,3 @@ } | ||
var merged_obj = obj1; | ||
for (var key in obj2) { | ||
for(var key in obj2) { | ||
merged_obj[key] = obj2[key]; | ||
@@ -277,3 +296,3 @@ } | ||
for (var i = 0; i < keys.length; i++) { | ||
for(var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
@@ -280,0 +299,0 @@ result[key] = data[key]; |
{ | ||
"name": "oauth-1.0a", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "OAuth 1.0a Request Authorization for Node and Browser.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -27,8 +27,7 @@ oauth-1.0a | ||
```js | ||
var oauth = new OAuth({ | ||
var oauth = OAuth({ | ||
consumer: { | ||
public: '<your consumer key>', | ||
secret: '<your consumer secret>' | ||
}, | ||
signature_method: '<signature method>' //HMAC-SHA1 or PLAINTEXT ... | ||
} | ||
}); | ||
@@ -54,3 +53,3 @@ ``` | ||
###Browser | ||
Download oauth-1.0a.js [here](https://github.com/ddo/oauth-1.0a/blob/0.0.8/oauth-1.0a.js) | ||
Download oauth-1.0a.js [here](https://github.com/ddo/oauth-1.0a/blob/0.1.0/oauth-1.0a.js) | ||
@@ -74,3 +73,3 @@ <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script> | ||
```js | ||
var oauth = new OAuth({ | ||
var oauth = OAuth({ | ||
consumer: { | ||
@@ -134,3 +133,3 @@ public: 'xvz1evFS4wEEPTGEFPHBog', | ||
```js | ||
var oauth = new OAuth({ | ||
var oauth = OAuth({ | ||
consumer: { | ||
@@ -187,2 +186,25 @@ public: 'xvz1evFS4wEEPTGEFPHBog', | ||
``` | ||
##Options | ||
```js | ||
var oauth = OAuth(/* options */); | ||
``` | ||
* ``consumer``: ``Object`` ``Required`` your consumer keys | ||
```js | ||
{ | ||
public: <your consumer key>, | ||
secret: <your consumer secret> | ||
} | ||
``` | ||
* ``signature_method``: ``String`` default ``'HMAC-SHA1'`` | ||
* ``nonce_length``: ``Int`` default ``32`` | ||
* ``version``: ``String`` default ``'1.0'`` | ||
* ``parameter_seperator``: ``String`` for header only, default ``', '``. Note that there is a space after ``,`` | ||
* ``last_ampersand``: ``Bool`` default ``true``. For some services if there is no Token Secret then no need ``&`` at the end. Check [oauth doc](http://oauth.net/core/1.0a/#anchor22) for more information | ||
> oauth_signature is set to the concatenated encoded values of the Consumer Secret and Token Secret, separated by a '&' character (ASCII code 38), even if either secret is empty | ||
##Notes | ||
@@ -189,0 +211,0 @@ |
634875
39
19730
269