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

Add randomString() to AuthUtils #1141

Open
wants to merge 1 commit into
base: develop
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
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);
}

};