Comparing version 0.0.4 to 0.0.5
@@ -24,3 +24,3 @@ var http = require("http"); | ||
if (path == "/" || path.match(/^\/user\/?$/)) { | ||
if (path == "/" || path.match(/^\/user\/?$/)) { | ||
@@ -41,9 +41,9 @@ // redirect to github if there is no access token | ||
user.show(function(err, user) { | ||
if (err) { | ||
res.writeHead(err.status); | ||
res.end(JSON.stringify(err)); | ||
return; | ||
} | ||
res.writeHead(200); | ||
res.end(JSON.stringify(user)) | ||
if (err) { | ||
res.writeHead(err.status); | ||
res.end(JSON.stringify(err)); | ||
return; | ||
} | ||
res.writeHead(200); | ||
res.end(JSON.stringify(user)); | ||
}); | ||
@@ -56,13 +56,20 @@ return; | ||
oauth.getOAuthAccessToken(query.code, {}, function (err, access_token, refresh_token) { | ||
accessToken = access_token; | ||
// authenticate github API | ||
github.authenticateOAuth(accessToken); | ||
//redirect back | ||
res.writeHead(303, { | ||
Location: "/" | ||
}); | ||
res.end(); | ||
}) | ||
if (err) { | ||
console.log(err); | ||
res.writeHead(500); | ||
res.end(err + ""); | ||
return; | ||
} | ||
accessToken = access_token; | ||
// authenticate github API | ||
github.authenticateOAuth(accessToken); | ||
//redirect back | ||
res.writeHead(303, { | ||
Location: "/" | ||
}); | ||
res.end(); | ||
}); | ||
return; | ||
@@ -69,0 +76,0 @@ } |
@@ -45,3 +45,3 @@ /** | ||
{ | ||
var options = options || {}; | ||
options = options || {}; | ||
this.$options = {}; | ||
@@ -78,3 +78,3 @@ for (var key in this.$defaults) { | ||
{ | ||
var defaultValue = defaultValue === undefined ? null : defaultValue; | ||
defaultValue = defaultValue === undefined ? null : defaultValue; | ||
return this.$options[name] ? this.$options[name] : defaultValue; | ||
@@ -110,3 +110,3 @@ }; | ||
{ | ||
var httpMethod = httpMethod || "GET"; | ||
httpMethod = httpMethod || "GET"; | ||
if(options) | ||
@@ -125,7 +125,7 @@ { | ||
var response = self.decodeResponse(response); | ||
response = self.decodeResponse(response); | ||
if (initialOptions) { | ||
self.options = initialOptions; | ||
}; | ||
} | ||
callback && callback(null, response); | ||
@@ -144,13 +144,6 @@ }); | ||
{ | ||
var httpMethod = httpMethod.toUpperCase(); | ||
httpMethod = httpMethod.toUpperCase(); | ||
var host = this.$options.proxy_host ? this.$options.proxy_host : this.$options.hostname; | ||
var port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443; | ||
if (this.$options.protocol == "https") { | ||
var creds = crypto.createCredentials({ }); | ||
var client = http.createClient(port, host, true, creds); | ||
} | ||
else | ||
var client = http.createClient(port, host); | ||
var headers = { | ||
@@ -196,10 +189,12 @@ 'Host':'github.com', | ||
var request = client.request(httpMethod, this.$options.protocol + "://" + this.$options.hostname + path, headers); | ||
if (httpMethod == "POST") | ||
request.write(postQuery); | ||
var getOptions = { | ||
host: host, | ||
post: port, | ||
path: path, | ||
method: httpMethod, | ||
headers: headers | ||
}; | ||
this.$debug('send ' + httpMethod + ' request: ' + path); | ||
request.on('response', function (response) { | ||
var request = require(this.$options.protocol).request(getOptions, function(response) { | ||
response.setEncoding('utf8'); | ||
@@ -212,18 +207,23 @@ | ||
response.addListener('end', function () { | ||
body = body.join(""); | ||
var msg; | ||
body = body.join(""); | ||
if (response.statusCode > 200) { | ||
if (response.headers["content-type"].indexOf("application/json") == 0) { | ||
var msg = JSON.parse(body) | ||
} else { | ||
msg = body; | ||
if (response.headers["content-type"].indexOf("application/json") === 0) { | ||
msg = JSON.parse(body); | ||
} else { | ||
msg = body; | ||
} | ||
callback({status: response.statusCode, msg: msg}); | ||
return; | ||
} | ||
callback({status: response.statusCode, msg: msg}); | ||
return; | ||
} | ||
callback(null, body); | ||
}); | ||
}); | ||
if (httpMethod == "POST") | ||
request.write(postQuery); | ||
request.end(); | ||
}, | ||
}; | ||
@@ -236,4 +236,3 @@ | ||
{ | ||
if(this.$options['format'] === "text") | ||
{ | ||
if(this.$options['format'] === "text") { | ||
return response; | ||
@@ -240,0 +239,0 @@ } |
{ | ||
"name" : "github", | ||
"version" : "0.0.4", | ||
"version" : "0.0.5", | ||
"description" : "Wrapper for the GitHub API", | ||
@@ -12,3 +12,3 @@ "author": "Fabian Jakobs <fabian.jakobs@web.de>", | ||
}, | ||
"engine" : ["node >=0.2.0"], | ||
"engine" : ["node >=0.4.0"], | ||
"licenses": [{ | ||
@@ -15,0 +15,0 @@ "type": "The MIT License", |
@@ -26,3 +26,3 @@ /** | ||
assert.equal(response.users.length, 1, "Found one user"); | ||
assert.equal(response.users[0].name, "diem-project", "Found one user"); | ||
assert.equal(response.users[0].login, "diem-project", "Found one user"); | ||
finished(); | ||
@@ -29,0 +29,0 @@ }); |
@@ -28,4 +28,4 @@ /** | ||
assert.equal(err, null); | ||
assert.equal(users.length, 1); | ||
assert.equal(users[0].name, username); | ||
assert.equal(users.length, 1); | ||
assert.equal(users[0].login, username); | ||
finished(); | ||
@@ -32,0 +32,0 @@ }); |
@@ -5,28 +5,31 @@ var sys= require('sys') | ||
var oa= new OAuth("http://term.ie/oauth/example/request_token.php?foo=bar", | ||
"http://term.ie/oauth/example/access_token.php", | ||
"key", | ||
"secret", | ||
"1.0", | ||
null, | ||
"PLAINTEXT") | ||
var oa = new OAuth("http://term.ie/oauth/example/request_token.php?foo=bar", | ||
"http://term.ie/oauth/example/access_token.php", | ||
"key", | ||
"secret", | ||
"1.0", | ||
null, | ||
"PLAINTEXT") | ||
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){ | ||
if(error) sys.puts('error :' + error) | ||
else { | ||
sys.puts('oauth_token :' + oauth_token) | ||
sys.puts('oauth_token_secret :' + oauth_token_secret) | ||
sys.puts('requestoken results :' + sys.inspect(results)) | ||
sys.puts("Requesting access token") | ||
oa.getOAuthAccessToken(oauth_token, oauth_token_secret, function(error, oauth_access_token, oauth_access_token_secret, results2) { | ||
sys.puts('oauth_access_token :' + oauth_access_token) | ||
sys.puts('oauth_token_secret :' + oauth_access_token_secret) | ||
sys.puts('accesstoken results :' + sys.inspect(results2)) | ||
sys.puts("Requesting access token") | ||
var data= ""; | ||
oa.getProtectedResource("http://term.ie/oauth/example/echo_api.php?foo=bar&too=roo", "GET", oauth_access_token, oauth_access_token_secret, function (error, data, response) { | ||
sys.puts(data); | ||
}); | ||
if (error) return console.log('error :' + error) | ||
console.log('oauth_token :' + oauth_token) | ||
console.log('oauth_token_secret :' + oauth_token_secret) | ||
console.log('requestoken results :', results) | ||
console.log("Requesting access token") | ||
oa.getOAuthAccessToken(oauth_token, oauth_token_secret, | ||
function(error, oauth_access_token, | ||
oauth_access_token_secret, results2) { | ||
console.log('oauth_access_token :' + oauth_access_token) | ||
console.log('oauth_token_secret :' + oauth_access_token_secret) | ||
console.log('accesstoken results :', results2) | ||
console.log("Requesting access token") | ||
var data= ""; | ||
oa.getProtectedResource( | ||
"http://term.ie/oauth/example/echo_api.php?foo=bar&too=roo", "GET", | ||
oauth_access_token, oauth_access_token_secret, | ||
function (error, data, response) { | ||
console.log(data); | ||
}); | ||
} | ||
}); | ||
}) |
var crypto= require('crypto'), | ||
sha1= require('./sha1'), | ||
http= require('http'), | ||
https= require('https'), | ||
URL= require('url'), | ||
@@ -98,8 +99,22 @@ querystring= require('querystring'); | ||
// Takes a literal in, then returns a sorted array | ||
exports.OAuth.prototype._sortRequestParams= function(argumentsHash) { | ||
// Takes an object literal that represents the arguments, and returns an array | ||
// of argument/value pairs. | ||
exports.OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) { | ||
var argument_pairs= []; | ||
for(var key in argumentsHash ) { | ||
argument_pairs[argument_pairs.length]= [key, argumentsHash[key]]; | ||
for(var key in argumentsHash ) { | ||
var value= argumentsHash[key]; | ||
if( Array.isArray(value) ) { | ||
for(var i=0;i<value.length;i++) { | ||
argument_pairs[argument_pairs.length]= [key, value[i]]; | ||
} | ||
} | ||
else { | ||
argument_pairs[argument_pairs.length]= [key, value]; | ||
} | ||
} | ||
return argument_pairs; | ||
} | ||
// Sorts the encoded key value pairs by encoded name, then encoded value | ||
exports.OAuth.prototype._sortRequestParams= function(argument_pairs) { | ||
// Sort by name, then value. | ||
@@ -117,8 +132,18 @@ argument_pairs.sort(function(a,b) { | ||
exports.OAuth.prototype._normaliseRequestParams= function(arguments) { | ||
var argument_pairs= this._sortRequestParams( arguments ); | ||
var argument_pairs= this._makeArrayOfArgumentsHash(arguments); | ||
// First encode them #3.4.1.3.2 .1 | ||
for(var i=0;i<argument_pairs.length;i++) { | ||
argument_pairs[i][0]= this._encodeData( argument_pairs[i][0] ); | ||
argument_pairs[i][1]= this._encodeData( argument_pairs[i][1] ); | ||
} | ||
// Then sort them #3.4.1.3.2 .2 | ||
argument_pairs= this._sortRequestParams( argument_pairs ); | ||
// Then concatenate together #3.4.1.3.2 .3 & .4 | ||
var args= ""; | ||
for(var i=0;i<argument_pairs.length;i++) { | ||
args+= this._encodeData( argument_pairs[i][0] ); | ||
args+= argument_pairs[i][0]; | ||
args+= "=" | ||
args+= this._encodeData( argument_pairs[i][1] ); | ||
args+= argument_pairs[i][1]; | ||
if( i < argument_pairs.length-1 ) args+= "&"; | ||
@@ -146,6 +171,10 @@ } | ||
else { | ||
hash= sha1.HMACSHA1(key, signatureBase); | ||
if( crypto.Hmac ) { | ||
hash = crypto.createHmac("sha1", key).update(signatureBase).digest("base64"); | ||
} | ||
else { | ||
hash= sha1.HMACSHA1(key, signatureBase); | ||
} | ||
} | ||
return hash; | ||
return hash; | ||
} | ||
@@ -171,4 +200,17 @@ exports.OAuth.prototype.NONCE_CHARS= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n', | ||
exports.OAuth.prototype._createClient= function( port, hostname, sshEnabled, credentials ) { | ||
return http.createClient(port, hostname, sshEnabled, credentials); | ||
exports.OAuth.prototype._createClient= function( port, hostname, method, path, headers, sslEnabled ) { | ||
var options = { | ||
host: hostname, | ||
port: port, | ||
path: path, | ||
method: method, | ||
headers: headers | ||
}; | ||
var httpModel; | ||
if( sslEnabled ) { | ||
httpModel= https; | ||
} else { | ||
httpModel= http; | ||
} | ||
return httpModel.request(options); | ||
} | ||
@@ -196,10 +238,19 @@ | ||
if( parsedUrl.query ) { | ||
var extraParameters= querystring.parse(parsedUrl.query); | ||
for(var key in extraParameters ) { | ||
oauthParameters[key]= extraParameters[key]; | ||
} | ||
var key2; | ||
var extraParameters= querystring.parse(parsedUrl.query); | ||
for(var key in extraParameters ) { | ||
var value= extraParameters[key]; | ||
if( typeof value == "object" ){ | ||
// TODO: This probably should be recursive | ||
for(key2 in value){ | ||
oauthParameters[key + "[" + key2 + "]"] = value[key2]; | ||
} | ||
} else { | ||
oauthParameters[key]= value; | ||
} | ||
} | ||
} | ||
var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret); | ||
var orderedParameters= this._sortRequestParams( oauthParameters ); | ||
var orderedParameters= this._sortRequestParams( this._makeArrayOfArgumentsHash(oauthParameters) ); | ||
orderedParameters[orderedParameters.length]= ["oauth_signature", sig]; | ||
@@ -219,10 +270,2 @@ return orderedParameters; | ||
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= {}; | ||
@@ -245,3 +288,3 @@ headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters); | ||
if( method == "POST" && ( post_body == null && extra_params != null) ) { | ||
if( (method == "POST" || method == "PUT") && ( post_body == null && extra_params != null) ) { | ||
post_body= querystring.stringify(extra_params); | ||
@@ -258,12 +301,19 @@ } | ||
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 ) { | ||
var data=""; | ||
var self= this; | ||
request.addListener('response', function (response) { | ||
request.on('response', function (response) { | ||
response.setEncoding('utf8'); | ||
response.addListener('data', function (chunk) { | ||
response.on('data', function (chunk) { | ||
data+=chunk; | ||
}); | ||
response.addListener('end', function () { | ||
response.on('end', function () { | ||
if( response.statusCode != 200 ) { | ||
@@ -277,4 +327,5 @@ callback({ statusCode: response.statusCode, data: data }); | ||
request.socket.addListener("error",callback); | ||
if( method == "POST" && post_body != null && post_body != "" ) { | ||
request.on("error", callback); | ||
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) { | ||
request.write(post_body); | ||
@@ -285,3 +336,3 @@ } | ||
else { | ||
if( method == "POST" && post_body != null && post_body != "" ) { | ||
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) { | ||
request.write(post_body); | ||
@@ -303,3 +354,3 @@ } | ||
this._performSecureRequest( oauth_token, oauth_token_secret, "GET", this._accessUrl, extraParams, null, null, function(error, data, response) { | ||
this._performSecureRequest( oauth_token, oauth_token_secret, "POST", this._accessUrl, extraParams, null, null, function(error, data, response) { | ||
if( error ) callback(error); | ||
@@ -322,2 +373,6 @@ else { | ||
exports.OAuth.prototype.delete= function(url, oauth_token, oauth_token_secret, callback) { | ||
return this._performSecureRequest( oauth_token, oauth_token_secret, "DELETE", url, null, "", null, callback ); | ||
} | ||
exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) { | ||
@@ -327,3 +382,3 @@ return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback ); | ||
exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) { | ||
exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) { | ||
var extra_params= null; | ||
@@ -339,5 +394,14 @@ if( typeof post_content_type == "function" ) { | ||
} | ||
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, method, url, extra_params, post_body, post_content_type, callback ); | ||
} | ||
exports.OAuth.prototype.put= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) { | ||
return this._putOrPost("PUT", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback); | ||
} | ||
exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) { | ||
return this._putOrPost("POST", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback); | ||
} | ||
exports.OAuth.prototype.getOAuthRequestToken= function(extraParams, callback) { | ||
@@ -344,0 +408,0 @@ if( typeof extraParams == "function" ){ |
var querystring= require('querystring'), | ||
crypto= require('crypto'), | ||
http= require('http'), | ||
https= require('https'), | ||
URL= require('url'); | ||
var sys= require('sys'); | ||
exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) { | ||
@@ -32,3 +30,2 @@ this._clientId= clientId; | ||
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443; | ||
var httpClient = http.createClient(parsedUrl.port, parsedUrl.hostname, true, creds); | ||
@@ -50,13 +47,13 @@ var realHeaders= {}; | ||
var request = httpClient.request(method, parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query), realHeaders ); | ||
var result= ""; | ||
httpClient.addListener("secure", function () { | ||
/* // disable verification for now. | ||
var options = { | ||
host:parsedUrl.hostname, | ||
port: parsedUrl.port, | ||
path: parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query), | ||
method: method, | ||
headers: realHeaders | ||
}; | ||
var verified = httpClient.verifyPeer(); | ||
if(!verified) this.end(); */ | ||
}); | ||
var result= ""; | ||
request.addListener('response', function (response) { | ||
request = https.request(options, function (response) { | ||
response.addListener("data", function (chunk) { | ||
@@ -74,2 +71,6 @@ result+= chunk | ||
request.on('error', function(e) { | ||
callback(e); | ||
}); | ||
request.end(); | ||
@@ -93,3 +94,15 @@ } | ||
else { | ||
var results= querystring.parse(data); | ||
var results; | ||
try { | ||
// As of http://tools.ietf.org/html/draft-ietf-oauth-v2-07 | ||
// responses should be in JSON | ||
results= JSON.parse( data ); | ||
} | ||
catch(e) { | ||
// .... However both Facebook + Github currently use rev05 of the spec | ||
// and neither seem to specify a content-type correctly in their response headers :( | ||
// clients of these services will suffer a *minor* performance cost of the exception | ||
// being thrown | ||
results= querystring.parse( data ); | ||
} | ||
var access_token= results["access_token"]; | ||
@@ -96,0 +109,0 @@ var refresh_token= results["refresh_token"]; |
{ "name" : "oauth" | ||
, "description" : "Library for interacting with OAuth 1.0, 1.0A and 2. Provides simplified client access and allows for construction of more complex apis and OAuth providers." | ||
, "version" : "0.8.2" | ||
, "version" : "0.8.4" | ||
, "directories" : { "lib" : "./lib" } | ||
@@ -5,0 +5,0 @@ , "main" : "index.js" |
@@ -10,5 +10,13 @@ node-oauth | ||
If you're running a node.js version more recent than 0.4 then you will need to use a version of node.js greater than or equal to 0.9.0. | ||
If you're running a node.js version in the 0.2x stable branc, then you will need to use version 0.8.4. | ||
Please be aware that when moving from 0.8.x to 0.9.0 there are no major API changes your, I've bumped the semi-major version element | ||
so that I can release fixes to the 0.8.x stream if problems come out. | ||
Change History | ||
============== | ||
* 0.9.0 - Compatibility fixes to bring node-oauth up to speed with node.js 0.4x [thanks to Rasmus Andersson for starting the work ] | ||
* 0.8.4 - Fixed issue #14 (Parameter ordering ignored encodings). Added support for repeated parameter names. Implements issue #15 (Use native SHA1 if available, 10x speed improvement!). Fixed issue #16 (Should use POST when requesting access tokens.). Fixed Issue #17 (OAuth2 spec compliance). Implemented enhancement #13 (Adds support for PUT & DELETE http verbs). Fixes issue #18 (Complex/Composite url arguments [thanks novemberborn]) | ||
* 0.8.3 - Fixed an issue where the auth header code depended on the Array's toString method (Yohei Sasaki) Updated the getOAuthRequestToken method so we can access google's OAuth secured methods. Also re-implemented and fleshed out the test suite. | ||
* 0.8.2 - The request returning methods will now write the POST body if provided (Chris Anderson), the code responsible for manipulating the headers is a bit safe now when working with other code (Paul McKellar) and tweaked the package.json to use index.js instead of main.js | ||
@@ -15,0 +23,0 @@ * 0.8.1 - Added mechanism to get hold of a signed Node Request object, ready for attaching response listeners etc. (Perfect for streaming APIs) |
@@ -27,2 +27,73 @@ var vows = require('vows'), | ||
}, | ||
'When making an array out of the arguments hash' : { | ||
topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), | ||
'flatten out arguments that are arrays' : function(oa) { | ||
var parameters= {"z": "a", | ||
"a": ["1", "2"], | ||
"1": "c" }; | ||
var parameterResults= oa._makeArrayOfArgumentsHash(parameters); | ||
assert.equal(parameterResults.length, 4); | ||
assert.equal(parameterResults[0][0], "1"); | ||
assert.equal(parameterResults[1][0], "z"); | ||
assert.equal(parameterResults[2][0], "a"); | ||
assert.equal(parameterResults[3][0], "a"); | ||
} | ||
}, | ||
'When ordering the request parameters' : { | ||
topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), | ||
'Order them by name' : function(oa) { | ||
var parameters= {"z": "a", | ||
"a": "b", | ||
"1": "c" }; | ||
var parameterResults= oa._sortRequestParams(oa._makeArrayOfArgumentsHash(parameters)) | ||
assert.equal(parameterResults[0][0], "1"); | ||
assert.equal(parameterResults[1][0], "a"); | ||
assert.equal(parameterResults[2][0], "z"); | ||
}, | ||
'If two parameter names are the same then order by the value': function(oa) { | ||
var parameters= {"z": "a", | ||
"a": ["z", "b", "b", "a", "y"], | ||
"1": "c" }; | ||
var parameterResults= oa._sortRequestParams(oa._makeArrayOfArgumentsHash(parameters)) | ||
assert.equal(parameterResults[0][0], "1"); | ||
assert.equal(parameterResults[1][0], "a"); | ||
assert.equal(parameterResults[1][1], "a"); | ||
assert.equal(parameterResults[2][0], "a"); | ||
assert.equal(parameterResults[2][1], "b"); | ||
assert.equal(parameterResults[3][0], "a"); | ||
assert.equal(parameterResults[3][1], "b"); | ||
assert.equal(parameterResults[4][0], "a"); | ||
assert.equal(parameterResults[4][1], "y"); | ||
assert.equal(parameterResults[5][0], "a"); | ||
assert.equal(parameterResults[5][1], "z"); | ||
assert.equal(parameterResults[6][0], "z"); | ||
} | ||
}, | ||
'When normalising the request parameters': { | ||
topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), | ||
'the resulting parameters should be encoded and ordered as per http://tools.ietf.org/html/rfc5849#section-3.1 (3.4.1.3.2)' : function(oa) { | ||
var parameters= {"b5" : "=%3D", | ||
"a3": ["a", "2 q"], | ||
"c@": "", | ||
"a2": "r b", | ||
"oauth_consumer_key": "9djdj82h48djs9d2", | ||
"oauth_token":"kkk9d7dh3k39sjv7", | ||
"oauth_signature_method": "HMAC-SHA1", | ||
"oauth_timestamp": "137131201", | ||
"oauth_nonce": "7d8f3e4a", | ||
"c2" : ""}; | ||
var normalisedParameterString= oa._normaliseRequestParams(parameters); | ||
assert.equal(normalisedParameterString, "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"); | ||
} | ||
}, | ||
'When preparing the parameters for use in signing': { | ||
topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), | ||
'We need to be wary of node\'s auto object creation from foo[bar] style url parameters' : function(oa) { | ||
var result= oa._prepareParameters( "", "", "", "http://foo.com?foo[bar]=xxx&bar[foo]=yyy", {} ); | ||
assert.equal( result[0][0], "bar[foo]") | ||
assert.equal( result[0][1], "yyy") | ||
assert.equal( result[1][0], "foo[bar]") | ||
assert.equal( result[1][1], "xxx") | ||
} | ||
}, | ||
'When signing a url': { | ||
@@ -59,19 +130,17 @@ topic: function() { | ||
var oa= new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), | ||
mockProvider= {}; | ||
mockProvider.request= function(method, path, headers) { | ||
mockProvider= {}; | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
assert.equal(headers.Host, "somehost.com:8080"); | ||
return result= {addListener:function(){}, | ||
end:function(){}, | ||
socket: {addListener: function(){}}}; | ||
assert.equal(hostname, "somehost.com"); | ||
assert.equal(port, "8080"); | ||
return { | ||
on: function() {}, | ||
end: function() {} | ||
}; | ||
} | ||
oa._createClient= function(port, host) { | ||
assert.equal(port, '8080'); | ||
assert.equal(host, 'somehost.com'); | ||
return mockProvider; | ||
} | ||
return oa; | ||
}, | ||
'getProtectedResrouce should correctly define the host headers': function(oa) { | ||
oa.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){require('sys').p('dddd')}) | ||
'getProtectedResource should correctly define the host headers': function(oa) { | ||
oa.getProtectedResource("http://somehost.com:8080", "GET", "oauth_token", null, function(){}) | ||
} | ||
@@ -128,16 +197,9 @@ }, | ||
try { | ||
oa._createClient= function() { | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
return { | ||
request: function(method, path, headers) { | ||
return { | ||
write: function(post_body){ | ||
post_body_written= true; | ||
assert.equal(post_body,"scope=foobar%2C1%2C2"); | ||
}, | ||
socket: {addListener: function(){}}, | ||
addListener: function() {}, | ||
end: function() {} | ||
} | ||
write: function(post_body){ | ||
post_body_written= true; | ||
assert.equal(post_body,"scope=foobar%2C1%2C2"); | ||
} | ||
} | ||
}; | ||
} | ||
@@ -172,15 +234,10 @@ oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"}); | ||
try { | ||
oa._createClient= function() { | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
return { | ||
request: function(method, path, headers) { | ||
return { | ||
write: function(){}, | ||
socket: {addListener: function(){}}, | ||
addListener: function() {}, | ||
end: function() { | ||
callbackCalled= true; | ||
} | ||
} | ||
write: function(){}, | ||
on: function() {}, | ||
end: function() { | ||
callbackCalled= true; | ||
} | ||
} | ||
}; | ||
} | ||
@@ -201,17 +258,13 @@ var request= oa.post("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){}) | ||
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 { | ||
request: function(method, path, headers) { | ||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded") | ||
return { | ||
socket: {addListener: function(){}}, | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
addListener: function() {}, | ||
end: function() {} | ||
} | ||
write: function(data){ | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
on: function() {}, | ||
end: function() { | ||
} | ||
} | ||
}; | ||
} | ||
@@ -232,18 +285,14 @@ var request= oa.post("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"}) | ||
var callbackCalled= false; | ||
oa._createClient= function() { | ||
return { | ||
request: function(method, path, headers) { | ||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded"); | ||
assert.equal(headers["Content-length"], 23); | ||
return { | ||
socket: {addListener: function(){}}, | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
addListener: function() {}, | ||
end: 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 { | ||
write: function(data){ | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
on: function() {}, | ||
end: function() { | ||
} | ||
}; | ||
} | ||
@@ -263,19 +312,15 @@ var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2") | ||
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 { | ||
request: function(method, path, headers) { | ||
assert.equal(headers["Content-Type"], "unicorn/encoded"); | ||
assert.equal(headers["Content-length"], 23); | ||
return { | ||
socket: {addListener: function(){}}, | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
addListener: function() {}, | ||
end: function() {} | ||
} | ||
write: function(data){ | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
}, | ||
on: function() {}, | ||
end: function() { | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
var request= oa.post("http://foo.com/blah", "token", "token_secret", "foo=1%2C2%2C3&bar=1%2B2", "unicorn/encoded") | ||
@@ -305,16 +350,148 @@ assert.equal(callbackCalled, true); | ||
try { | ||
oa._createClient= function() { | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
return { | ||
request: function(method, path, headers) { | ||
return { | ||
socket: {addListener: function(){}}, | ||
addListener: function() {}, | ||
end: function() { | ||
on: function() {}, | ||
end: function() { | ||
callbackCalled= true; | ||
} | ||
}; | ||
} | ||
var request= oa.get("http://foo.com/blah", "token", "token_secret", function(e,d) {}) | ||
assert.equal(callbackCalled, true); | ||
assert.isUndefined(request); | ||
} | ||
finally { | ||
oa._createClient= op; | ||
} | ||
} | ||
} | ||
}, | ||
'PUT' : { | ||
'if no callback is passed' : { | ||
'it should return a request object': function(oa) { | ||
var request= oa.put("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain") | ||
assert.isObject(request); | ||
assert.equal(request.method, "PUT"); | ||
request.end(); | ||
} | ||
}, | ||
'if a callback is passed' : { | ||
"it should call the internal request's end method and return nothing": function(oa) { | ||
var callbackCalled= 0; | ||
var op= oa._createClient; | ||
try { | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
return { | ||
on: function() {}, | ||
write: function(data) { | ||
callbackCalled++; | ||
}, | ||
end: function() { | ||
callbackCalled++; | ||
} | ||
}; | ||
} | ||
var request= oa.put("http://foo.com/blah", "token", "token_secret", "BLAH", "text/plain", function(e,d){}) | ||
assert.equal(callbackCalled, 2); | ||
assert.isUndefined(request); | ||
} | ||
finally { | ||
oa._createClient= op; | ||
} | ||
} | ||
}, | ||
'if the post_body is not a string' : { | ||
"It should be url encoded and the content type set to be x-www-form-urlencoded" : function(oa) { | ||
var op= oa._createClient; | ||
try { | ||
var callbackCalled= false; | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
assert.equal(headers["Content-Type"], "application/x-www-form-urlencoded") | ||
return { | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "foo=1%2C2%2C3&bar=1%2B2"); | ||
} | ||
}; | ||
} | ||
var request= oa.put("http://foo.com/blah", "token", "token_secret", {"foo":"1,2,3", "bar":"1+2"}) | ||
assert.equal(callbackCalled, true); | ||
} | ||
finally { | ||
oa._createClient= op; | ||
} | ||
} | ||
}, | ||
'if the post_body is a string' : { | ||
"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) { | ||
var op= oa._createClient; | ||
try { | ||
var callbackCalled= false; | ||
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 { | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "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); | ||
} | ||
finally { | ||
oa._createClient= op; | ||
} | ||
} | ||
}, | ||
"and a post_content_type is specified" : { | ||
"It should be written as is, with a content length specified, and the encoding should be set to be as specified" : function(oa) { | ||
var op= oa._createClient; | ||
try { | ||
var callbackCalled= false; | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
assert.equal(headers["Content-Type"], "unicorn/encoded"); | ||
assert.equal(headers["Content-length"], 23); | ||
return { | ||
write: function(data) { | ||
callbackCalled= true; | ||
assert.equal(data, "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", "unicorn/encoded") | ||
assert.equal(callbackCalled, true); | ||
} | ||
finally { | ||
oa._createClient= op; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
'DELETE' : { | ||
'if no callback is passed' : { | ||
'it should return a request object': function(oa) { | ||
var request= oa.delete("http://foo.com/blah", "token", "token_secret") | ||
assert.isObject(request); | ||
assert.equal(request.method, "DELETE"); | ||
request.end(); | ||
} | ||
}, | ||
'if a callback is passed' : { | ||
"it should call the internal request's end method and return nothing": function(oa) { | ||
var callbackCalled= false; | ||
var op= oa._createClient; | ||
try { | ||
oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { | ||
return { | ||
on: function() {}, | ||
end: function() { | ||
callbackCalled= true; | ||
} | ||
} | ||
}; | ||
} | ||
var request= oa.get("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); | ||
@@ -327,5 +504,5 @@ assert.isUndefined(request); | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
}).export(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151913
3712
51
3
5