-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.js
More file actions
201 lines (182 loc) · 5.76 KB
/
Copy pathtest.js
File metadata and controls
201 lines (182 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const assume = require('assume');
const crypto = require('crypto');
const nconf = require('nconf');
const authboot = require('./');
describe('authboot.test', function () {
const defaultOpts = { users: { what: 'hash' }};
function setupApp(opts = { config: {}}) {
const app = new Map();
app.config = new nconf.Provider(opts.config);
return app;
}
let app;
beforeEach(function () {
app = setupApp();
});
it('should setup middleware and lookup functions on authboot namespace on app object', function (done) {
authboot(defaultOpts)(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
done();
});
});
it('should return an error in the callback if challenge and realm are not both defined together', function (done) {
authboot({ challenge: true })(app, {}, (err) => {
assume(err).is.truthy();
assume(err.message).includes('realm');
done();
});
});
it('should return an error in the callback if no users are defined and no lookup function', function (done) {
authboot()(app, {}, (err) => {
assume(err).is.truthy();
assume(err.message).includes('authentication');
done();
});
});
it('should handle a custom lookup function', function (done) {
authboot({
lookup: (_, callback) => {
callback(null, 'foo');
}
})(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
app.authboot.lookup({}, (_, res) => {
assume(res).equals('foo');
done();
});
});
});
it('app.authboot.lookup by default should correctly fail on unknown user', function (done) {
authboot({ users: { what: '00' }})(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
app.authboot.lookup({ name: 'who', password: '00' }, (err, valid) => {
assume(err).is.falsey();
assume(valid).is.falsey();
done();
});
});
});
it('app.authboot.lookup by default should correctly validate from user object', function (done) {
function digestPassword(password) {
const hash = crypto.createHash('sha256');
hash.update(password);
return hash.digest('hex');
}
authboot({ users: {
what: digestPassword('huh'),
when: digestPassword('umm')
}})(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
const checks = [
{ name: 'what', password: 'huh', valid: true },
{ name: 'what', password: 'umm', valid: false },
{ name: 'when', password: 'huh', valid: false },
{ name: 'when', password: 'umm', valid: true }
];
return next();
function next() {
if (checks.length === 0) {
return done();
}
const check = checks.shift();
app.authboot.lookup({
name: check.name,
password: check.password
}, (err, valid) => {
assume(err).is.falsey();
assume(valid).equals(check.valid);
next();
});
}
});
});
it('app.authboot.lookup by default should coalesce concurrent lookups', function (done) {
const password = 'huh';
const hash = crypto.createHash('sha256');
hash.update(password);
const digest = hash.digest('hex');
authboot({ users: { what: digest }})(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
const responses = Array.from({ length: 10 }, () => {
let f, r;
const p = new Promise((_f, _r) => {
f = _f;
r = _r;
});
setTimeout(() => {
app.authboot.lookup({ name: 'what', password: 'huh' }, (err, valid) => {
try {
assume(err).is.falsey();
assume(valid).is.truthy();
f();
} catch (e) {
r(e);
}
});
}, 0);
return p;
});
Promise.all(responses).then(
() => done(),
(e) => done(e)
);
});
});
it('app.authboot.lookup by default should fail on brute force heuristic', function (done) {
// default max concurrents is 4
const password = '5';
const hash = crypto.createHash('sha256');
hash.update(password);
const digest = hash.digest('hex');
authboot({ users: { what: digest }})(app, {}, (err) => {
assume(err).is.falsey();
assume(app.authboot.middleware).is.a('function');
assume(app.authboot.lookup).is.a('function');
const responses = Array.from({ length: 6 }, (_, i) => {
let f, r;
const p = new Promise((_f, _r) => {
f = _f;
r = _r;
});
setTimeout(() => {
app.authboot.lookup({ name: 'what', password: `${i}` }, (err, valid) => {
try {
assume(err).is.falsey();
assume(valid).is.falsey();
f();
} catch (e) {
r(e);
}
});
}, 0);
return p;
});
responses.push(new Promise(async (f, r) => {
await Promise.all(responses);
app.authboot.lookup({ name: 'what', password: '5' }, (err, valid) => {
try {
assume(err).is.falsey();
assume(valid).is.truthy();
f();
} catch (e) {
r(e);
}
});
}));
Promise.all(responses).then(
() => done(),
(e) => done(e)
);
});
});
});