closes 24 - Now compatible with node v0.3.7
This commit is contained in:
35
lib/oauth.js
35
lib/oauth.js
@ -1,6 +1,7 @@
|
|||||||
var crypto= require('crypto'),
|
var crypto= require('crypto'),
|
||||||
sha1= require('./sha1'),
|
sha1= require('./sha1'),
|
||||||
http= require('http'),
|
http= require('http'),
|
||||||
|
https= require('https'),
|
||||||
URL= require('url'),
|
URL= require('url'),
|
||||||
querystring= require('querystring');
|
querystring= require('querystring');
|
||||||
|
|
||||||
@ -193,8 +194,21 @@ exports.OAuth.prototype._getNonce= function(nonceSize) {
|
|||||||
return result.join('');
|
return result.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._createClient= function( port, hostname, sshEnabled, credentials ) {
|
exports.OAuth.prototype._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return http.createClient(port, hostname, sshEnabled, credentials);
|
var options = {
|
||||||
|
host: hostname,
|
||||||
|
port: port,
|
||||||
|
path: path,
|
||||||
|
method: method,
|
||||||
|
headers: headers
|
||||||
|
};
|
||||||
|
var httpModel;
|
||||||
|
if( sshEnabled ) {
|
||||||
|
httpModel= https;
|
||||||
|
} else {
|
||||||
|
httpModel= http;
|
||||||
|
}
|
||||||
|
return httpModel.request(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
|
exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
|
||||||
@ -248,14 +262,6 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80;
|
if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80;
|
||||||
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
|
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
|
||||||
|
|
||||||
var oauthProvider;
|
|
||||||
if( parsedUrl.protocol == "https:" ) {
|
|
||||||
oauthProvider= this._createClient(parsedUrl.port, parsedUrl.hostname, true, crypto.createCredentials({}));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
oauthProvider= this._createClient(parsedUrl.port, parsedUrl.hostname);
|
|
||||||
}
|
|
||||||
|
|
||||||
var headers= {};
|
var headers= {};
|
||||||
headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters);
|
headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters);
|
||||||
headers["Host"] = parsedUrl.host
|
headers["Host"] = parsedUrl.host
|
||||||
@ -285,7 +291,14 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
|
|||||||
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
|
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
|
||||||
else path= parsedUrl.pathname;
|
else path= parsedUrl.pathname;
|
||||||
|
|
||||||
var request = oauthProvider.request(method, path , headers);
|
var request;
|
||||||
|
if( parsedUrl.protocol == "https:" ) {
|
||||||
|
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers, true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
request= this._createClient(parsedUrl.port, parsedUrl.hostname, method, path, headers);
|
||||||
|
}
|
||||||
|
|
||||||
if( callback ) {
|
if( callback ) {
|
||||||
var data="";
|
var data="";
|
||||||
var self= this;
|
var self= this;
|
||||||
|
256
tests/oauth.js
256
tests/oauth.js
@ -126,23 +126,21 @@ vows.describe('OAuth').addBatch({
|
|||||||
'When non standard ports are used': {
|
'When non standard ports are used': {
|
||||||
topic: function() {
|
topic: function() {
|
||||||
var oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
|
var oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"),
|
||||||
mockProvider= {};
|
mockProvider= {};
|
||||||
|
|
||||||
mockProvider.request= function(method, path, headers) {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
assert.equal(headers.Host, "somehost.com:8080");
|
assert.equal(headers.Host, "somehost.com:8080");
|
||||||
return result= {addListener:function(){},
|
assert.equal(hostname, "somehost.com");
|
||||||
end:function(){},
|
assert.equal(port, "8080");
|
||||||
socket: {addListener: function(){}}};
|
return {
|
||||||
}
|
on: function() {},
|
||||||
oa._createClient= function(port, host) {
|
end: function() {}
|
||||||
assert.equal(port, '8080');
|
};
|
||||||
assert.equal(host, 'somehost.com');
|
|
||||||
return mockProvider;
|
|
||||||
}
|
}
|
||||||
return oa;
|
return oa;
|
||||||
},
|
},
|
||||||
'getProtectedResrouce should correctly define the host headers': function(oa) {
|
'getProtectedResource should correctly define the host headers': function(oa) {
|
||||||
oa.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){require('sys').p('dddd')})
|
oa.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'When building the OAuth Authorization header': {
|
'When building the OAuth Authorization header': {
|
||||||
@ -194,20 +192,13 @@ vows.describe('OAuth').addBatch({
|
|||||||
var post_body_written= false;
|
var post_body_written= false;
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(post_body){
|
||||||
return {
|
post_body_written= true;
|
||||||
write: function(post_body){
|
assert.equal(post_body,"scope=foobar%2C1%2C2");
|
||||||
post_body_written= true;
|
|
||||||
assert.equal(post_body,"scope=foobar%2C1%2C2");
|
|
||||||
},
|
|
||||||
socket: {addListener: function(){}},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"});
|
oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"});
|
||||||
assert.equal(post_body_written, true);
|
assert.equal(post_body_written, true);
|
||||||
@ -237,19 +228,14 @@ vows.describe('OAuth').addBatch({
|
|||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(){},
|
||||||
return {
|
on: function() {},
|
||||||
write: function(){},
|
end: function() {
|
||||||
socket: {addListener: function(){}},
|
callbackCalled= true;
|
||||||
addListener: function() {},
|
|
||||||
end: function() {
|
|
||||||
callbackCalled= true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.post("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){})
|
var request= oa.post("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -265,21 +251,17 @@ vows.describe('OAuth').addBatch({
|
|||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
|
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded")
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(data){
|
||||||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded")
|
callbackCalled= true;
|
||||||
return {
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
socket: {addListener: function(){}},
|
},
|
||||||
write: function(data) {
|
on: function() {},
|
||||||
callbackCalled= true;
|
end: function() {
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
|
||||||
},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.post("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"})
|
var request= oa.post("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -295,22 +277,18 @@ vows.describe('OAuth').addBatch({
|
|||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded");
|
||||||
request: function(method, path, headers) {
|
assert.equal(headers["Content-length"], 23);
|
||||||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded");
|
return {
|
||||||
assert.equal(headers["Content-length"], 23);
|
write: function(data){
|
||||||
return {
|
callbackCalled= true;
|
||||||
socket: {addListener: function(){}},
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
write: function(data) {
|
},
|
||||||
callbackCalled= true;
|
on: function() {},
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
end: function() {
|
||||||
},
|
}
|
||||||
addListener: function() {},
|
};
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2")
|
var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2")
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -325,23 +303,19 @@ vows.describe('OAuth').addBatch({
|
|||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
|
assert.equal(headers["Content-Type"], "unicorn/encoded");
|
||||||
|
assert.equal(headers["Content-length"], 23);
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(data){
|
||||||
assert.equal(headers["Content-Type"], "unicorn/encoded");
|
callbackCalled= true;
|
||||||
assert.equal(headers["Content-length"], 23);
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
return {
|
},
|
||||||
socket: {addListener: function(){}},
|
on: function() {},
|
||||||
write: function(data) {
|
end: function() {
|
||||||
callbackCalled= true;
|
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
|
||||||
},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded")
|
var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded")
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
}
|
}
|
||||||
@ -366,18 +340,13 @@ vows.describe('OAuth').addBatch({
|
|||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
on: function() {},
|
||||||
return {
|
end: function() {
|
||||||
socket: {addListener: function(){}},
|
callbackCalled= true;
|
||||||
addListener: function() {},
|
|
||||||
end: function() {
|
|
||||||
callbackCalled= true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.get("http://foo.com/blah", "token", "token_secret", function(e,d) {})
|
var request= oa.get("http://foo.com/blah", "token", "token_secret", function(e,d) {})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -400,25 +369,22 @@ vows.describe('OAuth').addBatch({
|
|||||||
},
|
},
|
||||||
'if a callback is passed' : {
|
'if a callback is passed' : {
|
||||||
"it should call the internal request's end method and return nothing": function(oa) {
|
"it should call the internal request's end method and return nothing": function(oa) {
|
||||||
var callbackCalled= false;
|
var callbackCalled= 0;
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
on: function() {},
|
||||||
return {
|
write: function(data) {
|
||||||
write: function(){},
|
callbackCalled++;
|
||||||
socket: {addListener: function(){}},
|
},
|
||||||
addListener: function() {},
|
end: function() {
|
||||||
end: function() {
|
callbackCalled++;
|
||||||
callbackCalled= true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.put("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){})
|
var request= oa.put("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, 2);
|
||||||
assert.isUndefined(request);
|
assert.isUndefined(request);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
@ -431,21 +397,14 @@ vows.describe('OAuth').addBatch({
|
|||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
|
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded")
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(data) {
|
||||||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded")
|
callbackCalled= true;
|
||||||
return {
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
socket: {addListener: function(){}},
|
|
||||||
write: function(data) {
|
|
||||||
callbackCalled= true;
|
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
|
||||||
},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.put("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"})
|
var request= oa.put("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -458,25 +417,18 @@ vows.describe('OAuth').addBatch({
|
|||||||
'if the post_body is a string' : {
|
'if the post_body is a string' : {
|
||||||
"and no post_content_type is specified" : {
|
"and no post_content_type is specified" : {
|
||||||
"It should be written as is, with a content length specified, and the encoding should be set to be x-www-form-urlencoded" : function(oa) {
|
"It should be written as is, with a content length specified, and the encoding should be set to be x-www-form-urlencoded" : function(oa) {
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
|
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded");
|
||||||
|
assert.equal(headers["Content-length"], 23);
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(data) {
|
||||||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded");
|
callbackCalled= true;
|
||||||
assert.equal(headers["Content-length"], 23);
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
return {
|
|
||||||
socket: {addListener: function(){}},
|
|
||||||
write: function(data) {
|
|
||||||
callbackCalled= true;
|
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
|
||||||
},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.put("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2")
|
var request= oa.put("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2")
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -491,22 +443,15 @@ vows.describe('OAuth').addBatch({
|
|||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
|
assert.equal(headers["Content-Type"], "unicorn/encoded");
|
||||||
|
assert.equal(headers["Content-length"], 23);
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
write: function(data) {
|
||||||
assert.equal(headers["Content-Type"], "unicorn/encoded");
|
callbackCalled= true;
|
||||||
assert.equal(headers["Content-length"], 23);
|
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
||||||
return {
|
}
|
||||||
socket: {addListener: function(){}},
|
};
|
||||||
write: function(data) {
|
|
||||||
callbackCalled= true;
|
|
||||||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2");
|
|
||||||
},
|
|
||||||
addListener: function() {},
|
|
||||||
end: function() {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var request= oa.put("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded")
|
var request= oa.put("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded")
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
@ -532,18 +477,13 @@ vows.describe('OAuth').addBatch({
|
|||||||
var callbackCalled= false;
|
var callbackCalled= false;
|
||||||
var op= oa._createClient;
|
var op= oa._createClient;
|
||||||
try {
|
try {
|
||||||
oa._createClient= function() {
|
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) {
|
||||||
return {
|
return {
|
||||||
request: function(method, path, headers) {
|
on: function() {},
|
||||||
return {
|
end: function() {
|
||||||
socket: {addListener: function(){}},
|
callbackCalled= true;
|
||||||
addListener: function() {},
|
|
||||||
end: function() {
|
|
||||||
callbackCalled= true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
var request= oa.delete("http://foo.com/blah", "token", "token_secret", function(e,d) {})
|
var request= oa.delete("http://foo.com/blah", "token", "token_secret", function(e,d) {})
|
||||||
assert.equal(callbackCalled, true);
|
assert.equal(callbackCalled, true);
|
||||||
|
Reference in New Issue
Block a user