Skip to content

Commit

Permalink
Merge branch '0.4_compat_fixes'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/oauth.js
  • Loading branch information
ciaranj committed Feb 13, 2011
2 parents 40b70bb + 531c088 commit 13d9420
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 209 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Expand Up @@ -7,8 +7,15 @@ Tested against both Twitter (http://twitter.com), term.ie (http://term.ie/oauth
Also provides rudimentary OAuth2 support, tested against facebook connect and github. For more complete usage examples please take a look
at connect-auth (http://github.com/ciaranj/connect-auth)

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
Expand Down
49 changes: 26 additions & 23 deletions examples/term.ie.oauth-PLAINTEXT.js
Expand Up @@ -2,30 +2,33 @@ var sys= require('sys')

var OAuth= require('../lib/oauth').OAuth;

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);
});
}
});
})
43 changes: 28 additions & 15 deletions lib/oauth.js
@@ -1,6 +1,7 @@
var crypto= require('crypto'),
sha1= require('./sha1'),
http= require('http'),
https= require('https'),
URL= require('url'),
querystring= require('querystring');

Expand Down Expand Up @@ -193,8 +194,21 @@ exports.OAuth.prototype._getNonce= function(nonceSize) {
return result.join('');
}

exports.OAuth.prototype._createClient= function( port, hostname, sslEnabled, credentials ) {
return http.createClient(port, hostname, sslEnabled, 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);
}

exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
Expand Down Expand Up @@ -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 == "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= {};
headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters);
headers["Host"] = parsedUrl.host
Expand Down Expand Up @@ -285,16 +291,23 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
if( parsedUrl.query ) path= parsedUrl.pathname + "?"+ parsedUrl.query ;
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 ) {
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 ) {
callback({ statusCode: response.statusCode, data: data });
} else {
Expand All @@ -303,7 +316,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
});
});

request.socket.addListener("error",callback);
request.on("error", callback);

if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
request.write(post_body);
Expand Down
27 changes: 14 additions & 13 deletions lib/oauth2.js
@@ -1,10 +1,8 @@
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) {
this._clientId= clientId;
this._clientSecret= clientSecret;
Expand All @@ -29,7 +27,6 @@ exports.OAuth2.prototype._request= function(method, url, headers, access_token,
var creds = crypto.createCredentials({ });
var parsedUrl= URL.parse( url, true );
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
var httpClient = http.createClient(parsedUrl.port, parsedUrl.hostname, true, creds);

var realHeaders= {};
if( headers ) {
Expand All @@ -46,17 +43,17 @@ exports.OAuth2.prototype._request= function(method, url, headers, access_token,
parsedUrl.query["access_token"]= access_token;
}

var request = httpClient.request(method, parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query), realHeaders );

httpClient.addListener("secure", function () {
/* // disable verification for now.
var result= "";

var verified = httpClient.verifyPeer();
if(!verified) this.end(); */
});
var options = {
host:parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query),
method: method,
headers: realHeaders
};

var result= "";
request.addListener('response', function (response) {
request = https.request(options, function (response) {
response.addListener("data", function (chunk) {
result+= chunk
});
Expand All @@ -69,6 +66,10 @@ var verified = httpClient.verifyPeer();
});
});

request.on('error', function(e) {
callback(e);
});

request.end();
}

Expand Down

0 comments on commit 13d9420

Please sign in to comment.