Skip to content

Commit 3f28cf0

Browse files
committed
Merge pull request ericelliott#37 from mastilver/factory
(breaking) switch from static module to factory
2 parents 14c33d2 + 397d282 commit 3f28cf0

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

credential.js

+34-40
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ var crypto = require('crypto'),
2727
msPerDay = 24 * 60 * 60 * 1000,
2828
msPerYear = 366 * msPerDay,
2929
y2k = new Date(2000, 0, 1),
30+
defaultOptions = {
31+
keyLength: 66,
32+
work: 1,
33+
hashMethod: 'pbkdf2'
34+
},
35+
3036

3137
/**
3238
* pdkdf(password, salt, iterations,
@@ -100,28 +106,33 @@ var crypto = require('crypto'),
100106
},
101107

102108
/**
103-
* expired(hash)
109+
* isExpired(hash, days, work)
104110
*
105111
* Checks if a hash is older than the amount of days.
106112
*
107-
* @param {Number} hash
108-
* @return {Number} days
113+
* @param {Number} hash
114+
* @param {Number} days
115+
* @param {Number} work
116+
* @return {bool}
109117
*/
110118

111-
expired = function expired (hash, days){
119+
isExpired = function isExpired (hash, days, work){
112120
var base = Date.now() - (days || 90) * msPerDay;
113-
var minIterations = iterations(this.work, base);
121+
var minIterations = iterations(work, base);
114122

115123
return JSON.parse(hash).iterations < minIterations;
116124
},
117125

118126
/**
119-
* toHash(password, callback) callback(err, hash)
127+
* toHash(password, hashMethod, keyLength, work, callback) callback(err, hash)
120128
*
121129
* Takes a new password and creates a unique hash. Passes
122130
* a JSON encoded object to the callback.
123131
*
124132
* @param {[type]} password
133+
* @param {String} hashMethod
134+
* @param {Number} keyLength
135+
* @param {Number} work
125136
* @param {Function} callback
126137
*/
127138
/**
@@ -135,10 +146,8 @@ var crypto = require('crypto'),
135146
* @param {Number} hashObject.iterations
136147
* @return {undefined}
137148
*/
138-
toHash = function toHash (password, callback) {
139-
var hashMethod = this.hashMethod,
140-
keyLength = this.keyLength,
141-
n = iterations(this.work);
149+
toHash = function toHash (password, hashMethod, keyLength, work, callback) {
150+
var n = iterations(work);
142151

143152
if (typeof (password) !== 'string' || password.length === 0) {
144153
return callback(new Error('Password must be a ' +
@@ -213,36 +222,21 @@ var crypto = require('crypto'),
213222
}
214223
callback(null, constantTimeCompare(newHash, storedHash.hash));
215224
});
216-
},
225+
};
217226

218-
/**
219-
* configure(options)
220-
*
221-
* Alter settings.
222-
*
223-
* Warning: Decreasing `keyLength` or `work`
224-
* can make your password database less secure.
225-
*
226-
* @param {Object} options Options object.
227-
* @param {Number} options.keyLength
228-
* @param {Number} options.work
229-
* @return {Object} credential object
230-
*/
231-
configure = function configure (options) {
232-
mixIn(this, this.defaults, options);
233-
return this;
234-
},
235227

236-
defaults = {
237-
keyLength: 66,
238-
work: 1,
239-
hashMethod: 'pbkdf2'
240-
};
228+
module.exports = function credential (opts) {
229+
230+
var options = mixIn({}, defaultOptions, opts);
241231

242-
module.exports = mixIn({}, defaults, {
243-
hash: toHash,
244-
verify: verify,
245-
expired: expired,
246-
configure: configure,
247-
iterations: iterations
248-
});
232+
return {
233+
verify: verify,
234+
iterations: iterations,
235+
hash: function (password, callback) {
236+
toHash(password, options.hashMethod, options.keyLength, options.work, callback);
237+
},
238+
expired: function (hash, days) {
239+
return isExpired(hash, days, options.work);
240+
}
241+
};
242+
};

test/credential-test.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22
var test = require('tape'),
3-
pw = require('../credential.js');
3+
credential = require('../credential.js');
44

55
test('hash', function (t) {
66

7+
var pw = credential();
8+
79
pw.hash('foo', function (err, hash) {
810

911
t.equal(typeof hash, 'string',
@@ -19,6 +21,8 @@ test('hash', function (t) {
1921

2022
test('hash with different passwords', function (t) {
2123

24+
var pw = credential();
25+
2226
pw.hash('foo', function (err, fooHash) {
2327

2428
pw.hash('bar', function (err, barHash) {
@@ -33,6 +37,8 @@ test('hash with different passwords', function (t) {
3337

3438
test('hash with same passwords', function (t) {
3539

40+
var pw = credential();
41+
3642
pw.hash('foo', function (err, fooHash) {
3743

3844
pw.hash('foo', function (err, barHash) {
@@ -47,6 +53,8 @@ test('hash with same passwords', function (t) {
4753

4854
test('hash with undefined password', function (t) {
4955

56+
var pw = credential();
57+
5058
try {
5159
pw.hash(undefined, function (err) {
5260
t.ok(err,
@@ -61,6 +69,8 @@ test('hash with undefined password', function (t) {
6169

6270
test('hash with empty password', function (t) {
6371

72+
var pw = credential();
73+
6474
try {
6575
pw.hash('', function (err) {
6676
t.ok(err,
@@ -75,7 +85,8 @@ test('hash with empty password', function (t) {
7585

7686

7787
test('verify with right pw', function (t) {
78-
var pass = 'foo';
88+
var pass = 'foo',
89+
pw = credential();
7990

8091
pw.hash(pass, function (err, storedHash) {
8192
pw.verify(storedHash, pass, function (err, isValid) {
@@ -92,7 +103,8 @@ test('verify with right pw', function (t) {
92103

93104
test('verify with broken stored hash', function (t) {
94105
var pass = 'foo',
95-
storedHash = 'aoeuntkh;kbanotehudil,.prcgidax$aoesnitd,riouxbx;qjkwmoeuicgr';
106+
storedHash = 'aoeuntkh;kbanotehudil,.prcgidax$aoesnitd,riouxbx;qjkwmoeuicgr',
107+
pw = credential();
96108

97109
pw.verify(storedHash, pass, function (err) {
98110

@@ -106,7 +118,8 @@ test('verify with broken stored hash', function (t) {
106118

107119

108120
test('verify with wrong pw', function (t) {
109-
var pass = 'foo';
121+
var pass = 'foo',
122+
pw = credential();
110123

111124
pw.hash(pass, function (err, storedHash) {
112125
pw.verify(storedHash, 'bar', function (err, isValid) {
@@ -119,7 +132,8 @@ test('verify with wrong pw', function (t) {
119132
});
120133

121134
test('verify with undefined password', function (t) {
122-
var pass = 'foo';
135+
var pass = 'foo',
136+
pw = credential();
123137

124138
pw.hash(pass, function (err, storedHash) {
125139
try {
@@ -138,7 +152,8 @@ test('verify with undefined password', function (t) {
138152
});
139153

140154
test('verify with empty password', function (t) {
141-
var pass = 'foo';
155+
var pass = 'foo',
156+
pw = credential();
142157

143158
pw.hash(pass, function (err, storedHash) {
144159
try {
@@ -157,7 +172,8 @@ test('verify with empty password', function (t) {
157172
});
158173

159174
test('expired with valid hash and default expiry', function (t) {
160-
var pass = 'foo';
175+
var pass = 'foo',
176+
pw = credential();
161177

162178
pw.hash(pass, function (err, storedHash) {
163179
t.notOk(pw.expired(storedHash),
@@ -168,7 +184,8 @@ test('expired with valid hash and default expiry', function (t) {
168184
});
169185

170186
test('expired with short expiry', function (t) {
171-
var pass = 'foo';
187+
var pass = 'foo',
188+
pw = credential();
172189

173190
pw.hash(pass, function (err, storedHash) {
174191
t.notOk(pw.expired(storedHash, 2),
@@ -179,7 +196,8 @@ test('expired with short expiry', function (t) {
179196
});
180197

181198
test('expired with expiry in the past', function (t) {
182-
var pass = 'foo';
199+
var pass = 'foo',
200+
pw = credential();
183201

184202
pw.hash(pass, function (err, storedHash) {
185203
t.ok(pw.expired(storedHash, -2),
@@ -243,7 +261,8 @@ test('constantEquals exposes no timings', function (t) {
243261
test('overrides', function (t) {
244262
var work = 0.5;
245263
var keyLength = 12;
246-
pw.configure({
264+
265+
var pw = credential({
247266
work: work,
248267
keyLength: keyLength
249268
});

0 commit comments

Comments
 (0)