Permalink
Browse files

Refactored OAuthEcho object into oauth.js to remove lots of redundancy

  • Loading branch information...
1 parent 333f2a3 commit ed01a23da10053c255575cee81fa9bc715bc1662 @meltingice meltingice committed May 9, 2011
Showing with 63 additions and 345 deletions.
  1. +1 −1 Readme.md
  2. +1 −1 index.js
  3. +61 −21 lib/oauth.js
  4. +0 −322 lib/oauthecho.js
View
@@ -8,7 +8,7 @@ Also provides rudimentary OAuth2 support, tested against facebook connect and gi
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.
+If you're running a node.js version in the 0.2x stable branch, 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.
View
@@ -1,3 +1,3 @@
exports.OAuth = require("./lib/oauth").OAuth;
-exports.OAuthEcho = require("./lib/oauthecho").OAuthEcho;
+exports.OAuthEcho = require("./lib/oauth").OAuthEcho;
exports.OAuth2 = require("./lib/oauth2").OAuth2;
View
@@ -6,6 +6,8 @@ var crypto= require('crypto'),
querystring= require('querystring');
exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
+ this._echo = false;
+
this._requestUrl= requestUrl;
this._accessUrl= accessUrl;
this._consumerKey= consumerKey;
@@ -27,6 +29,26 @@ exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, vers
"User-Agent" : "Node authentication"}
};
+exports.OAuthEcho= function(realm, verify_credentials, consumerKey, consumerSecret, version, signatureMethod, nonceSize, customHeaders) {
+ this._echo = true;
+
+ this._realm= realm;
+ this._verifyCredentials = verify_credentials;
+ this._consumerKey= consumerKey;
+ this._consumerSecret= this._encodeData( consumerSecret );
+ this._version= version;
+
+ if( signatureMethod != "PLAINTEXT" && signatureMethod != "HMAC-SHA1")
+ throw new Error("Un-supported signature method: " + signatureMethod );
+ this._signatureMethod= signatureMethod;
+ this._nonceSize= nonceSize || 32;
+ this._headers= customHeaders || {"Accept" : "*/*",
+ "Connection" : "close",
+ "User-Agent" : "Node authentication"};
+}
+
+exports.OAuthEcho.prototype = exports.OAuth.prototype;
+
exports.OAuth.prototype._getTimestamp= function() {
return Math.floor( (new Date()).getTime() / 1000 );
}
@@ -85,13 +107,18 @@ exports.OAuth.prototype._isParameterNameAnOAuthParameter= function(parameter) {
// build the OAuth request authorization header
exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) {
var authHeader="OAuth ";
+ if (this._echo) {
+ authHeader += 'realm="' + this._realm + '",';
+ }
+
for( var i= 0 ; i < orderedParameters.length; i++) {
// Whilst the all the parameters should be included within the signature, only the oauth_ arguments
// should appear within the authorization header.
if( this._isParameterNameAnOAuthParameter(orderedParameters[i][0]) ) {
- authHeader+= this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
+ authHeader+= "" + this._encodeData(orderedParameters[i][0])+"=\""+ this._encodeData(orderedParameters[i][1])+"\",";
}
}
+
authHeader= authHeader.substring(0, authHeader.length-1);
return authHeader;
}
@@ -223,30 +250,37 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
if( oauth_token ) {
oauthParameters["oauth_token"]= oauth_token;
}
- if( extra_params ) {
- for( var key in extra_params ) {
- oauthParameters[key]= extra_params[key];
+
+ var sig;
+ if (!this._echo) {
+ if( extra_params ) {
+ for( var key in extra_params ) {
+ oauthParameters[key]= extra_params[key];
+ }
}
- }
- var parsedUrl= URL.parse( url, false );
-
- if( parsedUrl.query ) {
- 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];
+ var parsedUrl= URL.parse( url, false );
+
+ if( parsedUrl.query ) {
+ 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;
}
- } else {
- oauthParameters[key]= value;
}
- }
+ }
+
+ sig = this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
+ } else {
+ sig = this._getSignature( "GET", this._verifyCredentials, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
}
- var sig= this._getSignature( method, url, this._normaliseRequestParams(oauthParameters), oauth_token_secret);
var orderedParameters= this._sortRequestParams( this._makeArrayOfArgumentsHash(oauthParameters) );
orderedParameters[orderedParameters.length]= ["oauth_signature", sig];
return orderedParameters;
@@ -263,7 +297,13 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443;
var headers= {};
- headers["Authorization"]= this._buildAuthorizationHeaders(orderedParameters);
+ var authorization = this._buildAuthorizationHeaders(orderedParameters);
+ if (this._echo) {
+ headers["X-Verify-Credentials-Authorization"]= authorization;
+ } else {
+ headers["Authorization"]= authorization;
+ }
+
headers["Host"] = parsedUrl.host
for( var key in this._headers ) {
Oops, something went wrong.

0 comments on commit ed01a23

Please sign in to comment.