Merge branch 'master' of http://github.com/coopernurse/node-oauth into merge_coopernurse
Conflicts: lib/oauth.js
This commit is contained in:
commit
bfaa9c4e11
48
lib/oauth.js
48
lib/oauth.js
@ -2,7 +2,7 @@ var crypto= require('crypto'),
|
|||||||
sha1= require('./sha1'),
|
sha1= require('./sha1'),
|
||||||
http= require('http'),
|
http= require('http'),
|
||||||
URL= require('url'),
|
URL= require('url'),
|
||||||
querystring= require('querystring');
|
querystring= require('querystring');
|
||||||
|
|
||||||
exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
|
exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
|
||||||
this._requestUrl= requestUrl;
|
this._requestUrl= requestUrl;
|
||||||
@ -51,14 +51,14 @@ exports.OAuth.prototype._decodeData= function(toDecode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) {
|
exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) {
|
||||||
var signatureBase= this._createSignatureBase(method, url, parameters);
|
var signatureBase= this._createSignatureBase(method, url, parameters);
|
||||||
return this._createSignature( signatureBase, tokenSecret );
|
return this._createSignature( signatureBase, tokenSecret );
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._normalizeUrl= function(url) {
|
exports.OAuth.prototype._normalizeUrl= function(url) {
|
||||||
var parsedUrl= URL.parse(url, true)
|
var parsedUrl= URL.parse(url, true)
|
||||||
var port ="";
|
var port ="";
|
||||||
if( parsedUrl.port ) {
|
if( parsedUrl.port ) {
|
||||||
if( (parsedUrl.protocol == "http:" && parsedUrl.port != "80" ) ||
|
if( (parsedUrl.protocol == "http:" && parsedUrl.port != "80" ) ||
|
||||||
(parsedUrl.protocol == "https:" && parsedUrl.port != "443") ) {
|
(parsedUrl.protocol == "https:" && parsedUrl.port != "443") ) {
|
||||||
port= ":" + parsedUrl.port;
|
port= ":" + parsedUrl.port;
|
||||||
@ -66,7 +66,7 @@ exports.OAuth.prototype._normalizeUrl= function(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
|
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
|
||||||
|
|
||||||
return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname;
|
return parsedUrl.protocol + "//" + parsedUrl.hostname + port + parsedUrl.pathname;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters)
|
|||||||
// Whilst the all the parameters should be included within the signature, only the oauth_ arguments
|
// Whilst the all the parameters should be included within the signature, only the oauth_ arguments
|
||||||
// should appear within the authorization header.
|
// should appear within the authorization header.
|
||||||
var m = orderedParameters[i][0].match('^oauth_');
|
var m = orderedParameters[i][0].match('^oauth_');
|
||||||
if( m && m[0] === "oauth_") {
|
if( m && ( m[0] === "oauth_" || m[0] === 'scope' ) ) {
|
||||||
authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
|
authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,15 +88,15 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters)
|
|||||||
// Takes a literal in, then returns a sorted array
|
// Takes a literal in, then returns a sorted array
|
||||||
exports.OAuth.prototype._sortRequestParams= function(argumentsHash) {
|
exports.OAuth.prototype._sortRequestParams= function(argumentsHash) {
|
||||||
var argument_pairs= [];
|
var argument_pairs= [];
|
||||||
for(var key in argumentsHash ) {
|
for(var key in argumentsHash ) {
|
||||||
argument_pairs[argument_pairs.length]= [key, argumentsHash[key]];
|
argument_pairs[argument_pairs.length]= [key, argumentsHash[key]];
|
||||||
}
|
}
|
||||||
// Sort by name, then value.
|
// Sort by name, then value.
|
||||||
argument_pairs.sort(function(a,b) {
|
argument_pairs.sort(function(a,b) {
|
||||||
if ( a[0]== b[0] ) {
|
if ( a[0]== b[0] ) {
|
||||||
return a[1] < b[1] ? -1 : 1;
|
return a[1] < b[1] ? -1 : 1;
|
||||||
}
|
}
|
||||||
else return a[0] < b[0] ? -1 : 1;
|
else return a[0] < b[0] ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return argument_pairs;
|
return argument_pairs;
|
||||||
@ -110,19 +110,19 @@ exports.OAuth.prototype._normaliseRequestParams= function(arguments) {
|
|||||||
args+= "="
|
args+= "="
|
||||||
args+= this._encodeData( argument_pairs[i][1] );
|
args+= this._encodeData( argument_pairs[i][1] );
|
||||||
if( i < argument_pairs.length-1 ) args+= "&";
|
if( i < argument_pairs.length-1 ) args+= "&";
|
||||||
}
|
}
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._createSignatureBase= function(method, url, parameters) {
|
exports.OAuth.prototype._createSignatureBase= function(method, url, parameters) {
|
||||||
url= this._encodeData( this._normalizeUrl(url) );
|
url= this._encodeData( this._normalizeUrl(url) );
|
||||||
parameters= this._encodeData( parameters );
|
parameters= this._encodeData( parameters );
|
||||||
return method.toUpperCase() + "&" + url + "&" + parameters;
|
return method.toUpperCase() + "&" + url + "&" + parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
|
exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
|
||||||
if( tokenSecret === undefined ) var tokenSecret= "";
|
if( tokenSecret === undefined ) var tokenSecret= "";
|
||||||
else tokenSecret= this._encodeData( tokenSecret );
|
else tokenSecret= this._encodeData( tokenSecret );
|
||||||
// consumerSecret is already encoded
|
// consumerSecret is already encoded
|
||||||
var key= this._consumerSecret + "&" + tokenSecret;
|
var key= this._consumerSecret + "&" + tokenSecret;
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ exports.OAuth.prototype._getNonce= function(nonceSize) {
|
|||||||
var chars= this.NONCE_CHARS;
|
var chars= this.NONCE_CHARS;
|
||||||
var char_pos;
|
var char_pos;
|
||||||
var nonce_chars_length= chars.length;
|
var nonce_chars_length= chars.length;
|
||||||
|
|
||||||
for (var i = 0; i < nonceSize; i++) {
|
for (var i = 0; i < nonceSize; i++) {
|
||||||
char_pos= Math.floor(Math.random() * nonce_chars_length);
|
char_pos= Math.floor(Math.random() * nonce_chars_length);
|
||||||
result[i]= chars[char_pos];
|
result[i]= chars[char_pos];
|
||||||
@ -194,8 +194,8 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
|
var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
|
||||||
var orderedParameters= this._sortRequestParams( oauthParameters );
|
var orderedParameters= this._sortRequestParams( oauthParameters );
|
||||||
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
|
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
|
||||||
|
|
||||||
var query="";
|
var query="";
|
||||||
for( var i= 0 ; i < orderedParameters.length; i++) {
|
for( var i= 0 ; i < orderedParameters.length; i++) {
|
||||||
query+= this._encodeData(orderedParameters[i][0])+"="+ this._encodeData(orderedParameters[i][1]) + "&";
|
query+= this._encodeData(orderedParameters[i][0])+"="+ this._encodeData(orderedParameters[i][1]) + "&";
|
||||||
}
|
}
|
||||||
@ -222,7 +222,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
|
|
||||||
headers["Content-length"]= post_body ? post_body.length : 0; //Probably going to fail if not posting ascii
|
headers["Content-length"]= post_body ? post_body.length : 0; //Probably going to fail if not posting ascii
|
||||||
headers["Content-Type"]= post_content_type;
|
headers["Content-Type"]= post_content_type;
|
||||||
|
|
||||||
var path;
|
var path;
|
||||||
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
|
if( !parsedUrl.pathname || parsedUrl.pathname == "" ) parsedUrl.pathname ="/";
|
||||||
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
|
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
|
||||||
@ -230,7 +230,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
|
|
||||||
var request = oauthProvider.request(method, path , headers);
|
var request = oauthProvider.request(method, path , headers);
|
||||||
if( callback ) {
|
if( callback ) {
|
||||||
var data="";
|
var data="";
|
||||||
var self= this;
|
var self= this;
|
||||||
request.addListener('response', function (response) {
|
request.addListener('response', function (response) {
|
||||||
response.setEncoding('utf8');
|
response.setEncoding('utf8');
|
||||||
@ -245,7 +245,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
request.socket.addListener("error",callback);
|
request.socket.addListener("error",callback);
|
||||||
if( method == "POST" && post_body != null && post_body != "" ) {
|
if( method == "POST" && post_body != null && post_body != "" ) {
|
||||||
request.write(post_body);
|
request.write(post_body);
|
||||||
@ -258,7 +258,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
}
|
}
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,7 +269,7 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
|
|||||||
} else {
|
} else {
|
||||||
extraParams.oauth_verifier= oauth_verifier;
|
extraParams.oauth_verifier= oauth_verifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", null, function(error, data, response) {
|
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, "", null, function(error, data, response) {
|
||||||
if( error ) callback(error);
|
if( error ) callback(error);
|
||||||
else {
|
else {
|
||||||
@ -305,14 +305,14 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
|
|||||||
}
|
}
|
||||||
return this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback );
|
return this._performSecureRequest( oauth_token, oauth_token_secret, "POST", url, extra_params, post_body, post_content_type, callback );
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) {
|
exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) {
|
||||||
if( typeof extraParams == "function" ){
|
if( typeof extraParams == "function" ){
|
||||||
callback = extraParams;
|
callback = extraParams;
|
||||||
extraParams = {};
|
extraParams = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callbacks are 1.0A related
|
// Callbacks are 1.0A related
|
||||||
if( this._authorize_callback ) {
|
if( this._authorize_callback ) {
|
||||||
extraParams["oauth_callback"]= this._authorize_callback;
|
extraParams["oauth_callback"]= this._authorize_callback;
|
||||||
}
|
}
|
||||||
@ -361,12 +361,12 @@ exports.OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret,
|
|||||||
var orderedParameters= this._sortRequestParams( collectedParameters );
|
var orderedParameters= this._sortRequestParams( collectedParameters );
|
||||||
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
|
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
|
||||||
|
|
||||||
var query="";
|
var query="";
|
||||||
for( var i= 0 ; i < orderedParameters.length; i++) {
|
for( var i= 0 ; i < orderedParameters.length; i++) {
|
||||||
query+= orderedParameters[i][0]+"="+ this._encodeData(orderedParameters[i][1]) + "&";
|
query+= orderedParameters[i][0]+"="+ this._encodeData(orderedParameters[i][1]) + "&";
|
||||||
}
|
}
|
||||||
query= query.substring(0, query.length-1);
|
query= query.substring(0, query.length-1);
|
||||||
|
|
||||||
return parsedUrl.protocol + "//"+ parsedUrl.host + parsedUrl.pathname + "?" + query;
|
return parsedUrl.protocol + "//"+ parsedUrl.host + parsedUrl.pathname + "?" + query;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user