From f973a1459818158aacb10820685282bb01ce0be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 16 Aug 2023 13:06:20 +0200 Subject: [PATCH 1/2] put exports last --- lib/oauth.js | 67 +++++++++++++++++++++++++++------------------------ lib/oauth2.js | 32 +++++++++++++----------- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 50dccf99..c5bb78da 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -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; @@ -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; @@ -59,13 +59,13 @@ exports.OAuthEcho= function(realm, verify_credentials, consumerKey, consumerSecr this._oauthParameterSeperator = ","; } -exports.OAuthEcho.prototype = exports.OAuth.prototype; +OAuthEcho.prototype = exports.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); @@ -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 ) { @@ -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; @@ -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 + '",'; @@ -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)) { @@ -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] ) { @@ -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 # "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 # Build "Bearer " -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:" ) { @@ -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 ) { @@ -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); @@ -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; @@ -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; @@ -231,3 +231,7 @@ exports.OAuth2.prototype.get= function(url, access_token, callback) { } this._request("GET", url, headers, "", access_token, callback ); } + +module.exports = { + OAuth2 +} \ No newline at end of file From 4d2eefdecd40d2758dabf49efd38d2e6dde46f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Wed, 16 Aug 2023 13:07:37 +0200 Subject: [PATCH 2/2] remove prefix --- lib/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oauth.js b/lib/oauth.js index c5bb78da..63ce7638 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -59,7 +59,7 @@ function OAuthEcho(realm, verify_credentials, consumerKey, consumerSecret, versi this._oauthParameterSeperator = ","; } -OAuthEcho.prototype = exports.OAuth.prototype; +OAuthEcho.prototype = OAuth.prototype; OAuth.prototype._getTimestamp= function() { return Math.floor( (new Date()).getTime() / 1000 );