Skip to content

Commit

Permalink
Add randomString() to AuthUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavbhatia336 committed Mar 26, 2021
1 parent daf1da0 commit 3acc2d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
25 changes: 2 additions & 23 deletions lib/authorizer/digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _ = require('lodash'),
urlEncoder = require('postman-url-encoder'),
RequestBody = require('postman-collection').RequestBody,
bodyBuilder = require('../requester/core-body-builder'),
AuthUtils = require('./util'),

EMPTY = '',
ONE = '00000001',
Expand All @@ -17,8 +18,6 @@ var _ = require('lodash'),
AUTH_INT = 'auth-int',
AUTHORIZATION = 'Authorization',
MD5_SESS = 'MD5-sess',
ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
USERNAME_EQUALS_QUOTE = 'username="',
REALM_EQUALS_QUOTE = 'realm="',
NONCE_EQUALS_QUOTE = 'nonce="',
Expand Down Expand Up @@ -98,26 +97,6 @@ if (!_.includes(crypto.getHashes(), 'sha512-256')) {
});
}

/**
* Generates a random string of given length
*
* @todo Move this to util.js. After moving use that for hawk auth too
* @param {Number} length
*/
function randomString (length) {
length = length || 6;

var result = [],
i;

for (i = 0; i < length; i++) {
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
}

return result.join(EMPTY);
}


/**
* Extracts a Digest Auth field from a WWW-Authenticate header value using a given regexp.
*
Expand Down Expand Up @@ -321,7 +300,7 @@ module.exports = {
qop && (authParams.qop = qop);

if (authParams.qop || auth.get(QOP)) {
authParams.clientNonce = randomString(8);
authParams.clientNonce = AuthUtils.randomString(8);
authParams.nonceCount = ONE;
}

Expand Down
26 changes: 3 additions & 23 deletions lib/authorizer/hawk.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,9 @@ var url = require('url'),
RequestBody = require('postman-collection').RequestBody,
bodyBuilder = require('../requester/core-body-builder'),
urlEncoder = require('postman-url-encoder'),
AuthUtils = require('./util'),

ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
AUTHORIZATION = 'Authorization',
EMPTY = '';

/**
* Generates a random string of given length (useful for nonce generation, etc).
*
* @param {Number} length
*/
function randomString (length) {
length = length || 6;

var result = [],
i;

for (i = 0; i < length; i++) {
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
}

return result.join(EMPTY);
}
AUTHORIZATION = 'Authorization';

/**
* Calculates body hash with given algorithm and digestEncoding.
Expand Down Expand Up @@ -151,7 +131,7 @@ module.exports = {
* @param {AuthHandlerInterface~authPreHookCallback} done
*/
pre: function (auth, done) {
!auth.get('nonce') && auth.set('nonce', randomString(6));
!auth.get('nonce') && auth.set('nonce', AuthUtils.randomString(6));
!_.parseInt(auth.get('timestamp')) && auth.set('timestamp', Math.floor(Date.now() / 1e3));
done(null, true);
},
Expand Down
26 changes: 26 additions & 0 deletions lib/authorizer/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
EMPTY = '';

module.exports = {

/**
* Generates a random string of given length
*
* @param {Number} length
* @returns {String}
*/
randomString: function (length) {
length = length || 6;

var result = [],
i;

for (i = 0; i < length; i++) {
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
}

return result.join(EMPTY);
}

};

0 comments on commit 3acc2d3

Please sign in to comment.