Permalink
Browse files

Added a method to access protected resources.

  • Loading branch information...
1 parent c9c1eb0 commit 2bf33d156be5b41f38de6e031d3cfd6e6ca5fcb7 @ciaranj committed Apr 19, 2010
Showing with 86 additions and 3 deletions.
  1. +3 −1 Readme.md
  2. +37 −0 examples/term.ie.oauth.js
  3. +46 −2 lib/oauth.js
View
@@ -1,3 +1,5 @@
node-oauth
===========
-A simple oauth API for node.js . This API allows users to authenticate against OAUTH providers, and thus act as OAuth consumers
+A simple oauth API for node.js . This API allows users to authenticate against OAUTH providers, and thus act as OAuth consumers
+
+Tested against both Twitter (http://twitter.com) and term.ie (http://term.ie/oauth/example/)
@@ -0,0 +1,37 @@
+var sys= require('sys')
+
+var OAuth= require('../lib/oauth').OAuth;
+
+var oa= new OAuth("http://term.ie/oauth/example/request_token.php",
+ "http://term.ie/oauth/example/access_token.php",
+ null,
+ "key",
+ "secret",
+ "1.0",
+ "HMAC-SHA1")
+
+oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, authorize_url, 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(oauth_access_token, oauth_access_token_secret, "http://term.ie/oauth/example/echo_api.php?foo=bar&too=roo", function (response) {
+ response.setEncoding('utf8');
+ response.addListener('data', function (chunk) {
+ data+=chunk;
+ });
+ response.addListener('end', function () {
+ sys.puts(response.statusCode + " : " + data);
+ });
+ });
+ });
+ }
+})
View
@@ -164,16 +164,60 @@ exports.OAuth.prototype.getOauthAccessToken= function(oauth_token, oauth_token_s
} else {
var results= querystring.parse( data );
var oauth_token= results["oauth_token"];
- results["oauth_token"]= undefined;
+ delete results["oauth_token"];
var oauth_token_secret= results["oauth_token_secret"];
- results["oauth_token_secret"]= undefined;
+ delete results["oauth_token_secret"];
callback(null, oauth_token, oauth_token_secret, results );
}
});
});
request.end();
}
+exports.OAuth.prototype.getProtectedResource= function(oauth_token, oauth_token_secret, url, responseListener) {
+ var oauthParameters= {
+ "oauth_timestamp": this._getTimestamp(),
+ "oauth_nonce": this._getNonce(32),
+ "oauth_version": this._version,
+ "oauth_signature_method": this._signatureMethod,
+ "oauth_consumer_key": this._consumerKey,
+ "oauth_token": oauth_token
+ };
+
+ var parsedUrl= URL.parse( url, false );
+ if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80;
+ if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
+
+ if( parsedUrl.query ) {
+ var extraParameters= querystring.parse(parsedUrl.query);
+ for(var key in extraParameters ) {
+ oauthParameters[key]= extraParameters[key];
+ }
+ }
+
+ var method= "GET";
+ var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
+
+ var orderedParameters= this._sortRequestParams( oauthParameters );
+ orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
+
+ var query="";
+ for( var i= 0 ; i < orderedParameters.length; i++) {
+ query+= orderedParameters[i][0]+"="+ orderedParameters[i][1] + "&";
+ }
+ query= query.substring(0, query.length-1);
+
+
+ var oauthProvider= http.createClient(parsedUrl.port, parsedUrl.hostname);
+ var headers= {'Host': parsedUrl.hostname}
+ var request = oauthProvider.request(method, parsedUrl.pathname + "?" + query, headers);
+ var data="";
+ var self= this;
+ request.addListener('response', responseListener);
+ request.end();
+}
+
+
exports.OAuth.prototype.getOAuthRequestToken= function(callback) {
var oauthParameters= {
"oauth_timestamp": this._getTimestamp(),

0 comments on commit 2bf33d1

Please sign in to comment.