Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: put exports last #373

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var crypto= require('crypto'),
querystring= require('querystring'),
OAuthUtils= require('./_utils');

exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
function OAuth(requestUrl, accessUrl, consumerKey, consumerSecret, version, authorize_callback, signatureMethod, nonceSize, customHeaders) {
this._isEcho = false;

this._requestUrl= requestUrl;
Expand Down Expand Up @@ -37,7 +37,7 @@ exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, vers
this._oauthParameterSeperator = ",";
};

exports.OAuthEcho= function(realm, verify_credentials, consumerKey, consumerSecret, version, signatureMethod, nonceSize, customHeaders) {
function OAuthEcho(realm, verify_credentials, consumerKey, consumerSecret, version, signatureMethod, nonceSize, customHeaders) {
this._isEcho = true;

this._realm= realm;
Expand All @@ -59,13 +59,13 @@ exports.OAuthEcho= function(realm, verify_credentials, consumerKey, consumerSecr
this._oauthParameterSeperator = ",";
}

exports.OAuthEcho.prototype = exports.OAuth.prototype;
OAuthEcho.prototype = OAuth.prototype;

exports.OAuth.prototype._getTimestamp= function() {
OAuth.prototype._getTimestamp= function() {
return Math.floor( (new Date()).getTime() / 1000 );
}

exports.OAuth.prototype._encodeData= function(toEncode){
OAuth.prototype._encodeData= function(toEncode){
if( toEncode == null || toEncode == "" ) return ""
else {
var result= encodeURIComponent(toEncode);
Expand All @@ -78,19 +78,19 @@ exports.OAuth.prototype._encodeData= function(toEncode){
}
}

exports.OAuth.prototype._decodeData= function(toDecode) {
OAuth.prototype._decodeData= function(toDecode) {
if( toDecode != null ) {
toDecode = toDecode.replace(/\+/g, " ");
}
return decodeURIComponent( toDecode);
}

exports.OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) {
OAuth.prototype._getSignature= function(method, url, parameters, tokenSecret) {
var signatureBase= this._createSignatureBase(method, url, parameters);
return this._createSignature( signatureBase, tokenSecret );
}

exports.OAuth.prototype._normalizeUrl= function(url) {
OAuth.prototype._normalizeUrl= function(url) {
var parsedUrl= URL.parse(url, true)
var port ="";
if( parsedUrl.port ) {
Expand All @@ -106,7 +106,7 @@ exports.OAuth.prototype._normalizeUrl= function(url) {
}

// Is the parameter considered an OAuth parameter
exports.OAuth.prototype._isParameterNameAnOAuthParameter= function(parameter) {
OAuth.prototype._isParameterNameAnOAuthParameter= function(parameter) {
var m = parameter.match('^oauth_');
if( m && ( m[0] === "oauth_" ) ) {
return true;
Expand All @@ -117,7 +117,7 @@ exports.OAuth.prototype._isParameterNameAnOAuthParameter= function(parameter) {
};

// build the OAuth request authorization header
exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) {
OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters) {
var authHeader="OAuth ";
if( this._isEcho ) {
authHeader += 'realm="' + this._realm + '",';
Expand All @@ -137,7 +137,7 @@ exports.OAuth.prototype._buildAuthorizationHeaders= function(orderedParameters)

// Takes an object literal that represents the arguments, and returns an array
// of argument/value pairs.
exports.OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) {
OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) {
var argument_pairs= [];
for(var key in argumentsHash ) {
if (argumentsHash.hasOwnProperty(key)) {
Expand All @@ -156,7 +156,7 @@ exports.OAuth.prototype._makeArrayOfArgumentsHash= function(argumentsHash) {
}

// Sorts the encoded key value pairs by encoded name, then encoded value
exports.OAuth.prototype._sortRequestParams= function(argument_pairs) {
OAuth.prototype._sortRequestParams= function(argument_pairs) {
// Sort by name, then value.
argument_pairs.sort(function(a,b) {
if ( a[0]== b[0] ) {
Expand All @@ -168,7 +168,7 @@ exports.OAuth.prototype._sortRequestParams= function(argument_pairs) {
return argument_pairs;
}

exports.OAuth.prototype._normaliseRequestParams= function(args) {
OAuth.prototype._normaliseRequestParams= function(args) {
var argument_pairs= this._makeArrayOfArgumentsHash(args);
// First encode them #3.4.1.3.2 .1
for(var i=0;i<argument_pairs.length;i++) {
Expand All @@ -190,13 +190,13 @@ exports.OAuth.prototype._normaliseRequestParams= function(args) {
return args;
}

exports.OAuth.prototype._createSignatureBase= function(method, url, parameters) {
OAuth.prototype._createSignatureBase= function(method, url, parameters) {
url= this._encodeData( this._normalizeUrl(url) );
parameters= this._encodeData( parameters );
return method.toUpperCase() + "&" + url + "&" + parameters;
}

exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
if( tokenSecret === undefined ) var tokenSecret= "";
else tokenSecret= this._encodeData( tokenSecret );
// consumerSecret is already encoded
Expand All @@ -220,13 +220,13 @@ exports.OAuth.prototype._createSignature= function(signatureBase, tokenSecret) {
}
return hash;
}
exports.OAuth.prototype.NONCE_CHARS= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n',
OAuth.prototype.NONCE_CHARS= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','A','B',
'C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3',
'4','5','6','7','8','9'];

exports.OAuth.prototype._getNonce= function(nonceSize) {
OAuth.prototype._getNonce= function(nonceSize) {
var result = [];
var chars= this.NONCE_CHARS;
var char_pos;
Expand All @@ -239,7 +239,7 @@ exports.OAuth.prototype._getNonce= function(nonceSize) {
return result.join('');
}

exports.OAuth.prototype._createClient= function( port, hostname, method, path, headers, sslEnabled ) {
OAuth.prototype._createClient= function( port, hostname, method, path, headers, sslEnabled ) {
var options = {
host: hostname,
port: port,
Expand All @@ -256,7 +256,7 @@ exports.OAuth.prototype._createClient= function( port, hostname, method, path, h
return httpModel.request(options);
}

exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_secret, method, url, extra_params ) {
var oauthParameters= {
"oauth_timestamp": this._getTimestamp(),
"oauth_nonce": this._getNonce(this._nonceSize),
Expand Down Expand Up @@ -305,7 +305,7 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
return orderedParameters;
}

exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);

if( !post_content_type ) {
Expand Down Expand Up @@ -437,7 +437,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
return;
}

exports.OAuth.prototype.setClientOptions= function(options) {
OAuth.prototype.setClientOptions= function(options) {
var key,
mergedOptions= {},
hasOwnProperty= Object.prototype.hasOwnProperty;
Expand All @@ -453,7 +453,7 @@ exports.OAuth.prototype.setClientOptions= function(options) {
this._clientOptions= mergedOptions;
};

exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
var extraParams= {};
if( typeof oauth_verifier == "function" ) {
callback= oauth_verifier;
Expand All @@ -475,19 +475,19 @@ exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_s
}

// Deprecated
exports.OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
OAuth.prototype.getProtectedResource= function(url, method, oauth_token, oauth_token_secret, callback) {
this._performSecureRequest( oauth_token, oauth_token_secret, method, url, null, "", null, callback );
}

exports.OAuth.prototype.delete= function(url, oauth_token, oauth_token_secret, callback) {
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) {
OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, callback) {
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
}

exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
var extra_params= null;
if( typeof post_content_type == "function" ) {
callback= post_content_type;
Expand All @@ -502,11 +502,11 @@ exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_tok
}


exports.OAuth.prototype.put= function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
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) {
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);
}

Expand All @@ -530,7 +530,7 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
* need to provide a requestTokenHttpMethod option when creating the client.
*
**/
exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback ) {
OAuth.prototype.getOAuthRequestToken= function( extraParams, callback ) {
if( typeof extraParams == "function" ){
callback = extraParams;
extraParams = {};
Expand All @@ -553,7 +553,7 @@ exports.OAuth.prototype.getOAuthRequestToken= function( extraParams, callback )
});
}

exports.OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret, method) {
OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret, method) {

if( method === undefined ) {
var method= "GET";
Expand All @@ -571,11 +571,16 @@ exports.OAuth.prototype.signUrl= function(url, oauth_token, oauth_token_secret,
return parsedUrl.protocol + "//"+ parsedUrl.host + parsedUrl.pathname + "?" + query;
};

exports.OAuth.prototype.authHeader= function(url, oauth_token, oauth_token_secret, method) {
OAuth.prototype.authHeader= function(url, oauth_token, oauth_token_secret, method) {
if( method === undefined ) {
var method= "GET";
}

var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, {});
return this._buildAuthorizationHeaders(orderedParameters);
};

module.exports = {
OAuth,
OAuthEcho
};
32 changes: 18 additions & 14 deletions lib/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var querystring= require('querystring'),
URL= require('url'),
OAuthUtils= require('./_utils');

exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath, customHeaders) {
function OAuth2(clientId, clientSecret, baseSite, authorizePath, accessTokenPath, customHeaders) {
this._clientId= clientId;
this._clientSecret= clientSecret;
this._baseSite= baseSite;
Expand All @@ -22,7 +22,7 @@ exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, access

// Allows you to set an agent to use instead of the default HTTP or
// HTTPS agents. Useful when dealing with your own certificates.
exports.OAuth2.prototype.setAgent = function(agent) {
OAuth2.prototype.setAgent = function(agent) {
this._agent = agent;
};

Expand All @@ -31,34 +31,34 @@ exports.OAuth2.prototype.setAgent = function(agent) {
// ( http://tools.ietf.org/html/draft-ietf-oauth-v2-16#section-7 )
// it isn't clear what the correct value should be atm, so allowing
// for specific (temporary?) override for now.
exports.OAuth2.prototype.setAccessTokenName= function ( name ) {
OAuth2.prototype.setAccessTokenName= function ( name ) {
this._accessTokenName= name;
}

// Sets the authorization method for Authorization header.
// e.g. Authorization: Bearer <token> # "Bearer" is the authorization method.
exports.OAuth2.prototype.setAuthMethod = function ( authMethod ) {
OAuth2.prototype.setAuthMethod = function ( authMethod ) {
this._authMethod = authMethod;
};


// If you use the OAuth2 exposed 'get' method (and don't construct your own _request call )
// this will specify whether to use an 'Authorize' header instead of passing the access_token as a query parameter
exports.OAuth2.prototype.useAuthorizationHeaderforGET = function(useIt) {
OAuth2.prototype.useAuthorizationHeaderforGET = function(useIt) {
this._useAuthorizationHeaderForGET= useIt;
}

exports.OAuth2.prototype._getAccessTokenUrl= function() {
OAuth2.prototype._getAccessTokenUrl= function() {
return this._baseSite + this._accessTokenUrl; /* + "?" + querystring.stringify(params); */
}

// Build the authorization header. In particular, build the part after the colon.
// e.g. Authorization: Bearer <token> # Build "Bearer <token>"
exports.OAuth2.prototype.buildAuthHeader= function(token) {
OAuth2.prototype.buildAuthHeader= function(token) {
return this._authMethod + ' ' + token;
};

exports.OAuth2.prototype._chooseHttpLibrary= function( parsedUrl ) {
OAuth2.prototype._chooseHttpLibrary= function( parsedUrl ) {
var http_library= https;
// As this is OAUth2, we *assume* https unless told explicitly otherwise.
if( parsedUrl.protocol != "https:" ) {
Expand All @@ -67,7 +67,7 @@ exports.OAuth2.prototype._chooseHttpLibrary= function( parsedUrl ) {
return http_library;
};

exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) {
OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) {

var parsedUrl= URL.parse( url, true );
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) {
Expand Down Expand Up @@ -120,7 +120,7 @@ exports.OAuth2.prototype._request= function(method, url, headers, post_body, acc
this._executeRequest( http_library, options, post_body, callback );
}

exports.OAuth2.prototype._executeRequest= function( http_library, options, post_body, callback ) {
OAuth2.prototype._executeRequest= function( http_library, options, post_body, callback ) {
// Some hosts *cough* google appear to close the connection early / send no content-length header
// allow this behaviour.
var allowEarlyClose= OAuthUtils.isAnEarlyCloseHost(options.host);
Expand Down Expand Up @@ -173,13 +173,13 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_
request.end();
}

exports.OAuth2.prototype.getAuthorizeUrl= function( params ) {
OAuth2.prototype.getAuthorizeUrl= function( params ) {
var params= params || {};
params['client_id'] = this._clientId;
return this._baseSite + this._authorizeUrl + "?" + querystring.stringify(params);
}

exports.OAuth2.prototype.getOAuthAccessToken= function(code, params, callback) {
OAuth2.prototype.getOAuthAccessToken= function(code, params, callback) {
var params= params || {};
params['client_id'] = this._clientId;
params['client_secret'] = this._clientSecret;
Expand Down Expand Up @@ -217,11 +217,11 @@ exports.OAuth2.prototype.getOAuthAccessToken= function(code, params, callback) {
}

// Deprecated
exports.OAuth2.prototype.getProtectedResource= function(url, access_token, callback) {
OAuth2.prototype.getProtectedResource= function(url, access_token, callback) {
this._request("GET", url, {}, "", access_token, callback );
}

exports.OAuth2.prototype.get= function(url, access_token, callback) {
OAuth2.prototype.get= function(url, access_token, callback) {
if( this._useAuthorizationHeaderForGET ) {
var headers= {'Authorization': this.buildAuthHeader(access_token) }
access_token= null;
Expand All @@ -231,3 +231,7 @@ exports.OAuth2.prototype.get= function(url, access_token, callback) {
}
this._request("GET", url, headers, "", access_token, callback );
}

module.exports = {
OAuth2
}