Skip to content

Commit 3acc2d3

Browse files
Add randomString() to AuthUtils
1 parent daf1da0 commit 3acc2d3

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

lib/authorizer/digest.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var _ = require('lodash'),
33
urlEncoder = require('postman-url-encoder'),
44
RequestBody = require('postman-collection').RequestBody,
55
bodyBuilder = require('../requester/core-body-builder'),
6+
AuthUtils = require('./util'),
67

78
EMPTY = '',
89
ONE = '00000001',
@@ -17,8 +18,6 @@ var _ = require('lodash'),
1718
AUTH_INT = 'auth-int',
1819
AUTHORIZATION = 'Authorization',
1920
MD5_SESS = 'MD5-sess',
20-
ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
21-
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
2221
USERNAME_EQUALS_QUOTE = 'username="',
2322
REALM_EQUALS_QUOTE = 'realm="',
2423
NONCE_EQUALS_QUOTE = 'nonce="',
@@ -98,26 +97,6 @@ if (!_.includes(crypto.getHashes(), 'sha512-256')) {
9897
});
9998
}
10099

101-
/**
102-
* Generates a random string of given length
103-
*
104-
* @todo Move this to util.js. After moving use that for hawk auth too
105-
* @param {Number} length
106-
*/
107-
function randomString (length) {
108-
length = length || 6;
109-
110-
var result = [],
111-
i;
112-
113-
for (i = 0; i < length; i++) {
114-
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
115-
}
116-
117-
return result.join(EMPTY);
118-
}
119-
120-
121100
/**
122101
* Extracts a Digest Auth field from a WWW-Authenticate header value using a given regexp.
123102
*
@@ -321,7 +300,7 @@ module.exports = {
321300
qop && (authParams.qop = qop);
322301

323302
if (authParams.qop || auth.get(QOP)) {
324-
authParams.clientNonce = randomString(8);
303+
authParams.clientNonce = AuthUtils.randomString(8);
325304
authParams.nonceCount = ONE;
326305
}
327306

lib/authorizer/hawk.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,9 @@ var url = require('url'),
55
RequestBody = require('postman-collection').RequestBody,
66
bodyBuilder = require('../requester/core-body-builder'),
77
urlEncoder = require('postman-url-encoder'),
8+
AuthUtils = require('./util'),
89

9-
ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
10-
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
11-
AUTHORIZATION = 'Authorization',
12-
EMPTY = '';
13-
14-
/**
15-
* Generates a random string of given length (useful for nonce generation, etc).
16-
*
17-
* @param {Number} length
18-
*/
19-
function randomString (length) {
20-
length = length || 6;
21-
22-
var result = [],
23-
i;
24-
25-
for (i = 0; i < length; i++) {
26-
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
27-
}
28-
29-
return result.join(EMPTY);
30-
}
10+
AUTHORIZATION = 'Authorization';
3111

3212
/**
3313
* Calculates body hash with given algorithm and digestEncoding.
@@ -151,7 +131,7 @@ module.exports = {
151131
* @param {AuthHandlerInterface~authPreHookCallback} done
152132
*/
153133
pre: function (auth, done) {
154-
!auth.get('nonce') && auth.set('nonce', randomString(6));
134+
!auth.get('nonce') && auth.set('nonce', AuthUtils.randomString(6));
155135
!_.parseInt(auth.get('timestamp')) && auth.set('timestamp', Math.floor(Date.now() / 1e3));
156136
done(null, true);
157137
},

lib/authorizer/util.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var ASCII_SOURCE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
2+
ASCII_SOURCE_LENGTH = ASCII_SOURCE.length,
3+
EMPTY = '';
4+
5+
module.exports = {
6+
7+
/**
8+
* Generates a random string of given length
9+
*
10+
* @param {Number} length
11+
* @returns {String}
12+
*/
13+
randomString: function (length) {
14+
length = length || 6;
15+
16+
var result = [],
17+
i;
18+
19+
for (i = 0; i < length; i++) {
20+
result[i] = ASCII_SOURCE[(Math.random() * ASCII_SOURCE_LENGTH) | 0];
21+
}
22+
23+
return result.join(EMPTY);
24+
}
25+
26+
};

0 commit comments

Comments
 (0)