-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
391 lines (318 loc) · 8.01 KB
/
index.js
File metadata and controls
391 lines (318 loc) · 8.01 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Name: haveibeenpwned - index.js
Description: API methods for HaveIBeenPwnd.com (unofficial)
Author: Franklin (https://fvdm.com)
Source code: https://github.com/fvdm/nodejs-haveibeenpwned
Feedback: https://github.com/fvdm/nodejs-haveibeenpwned/issues
License: Unlicense (public domain, see LICENSE file)
*/
const httpreq = require ('httpreq');
let config = {
timeout: 5000,
userAgent: 'nodejs-haveibeenpwned (https://github.com/fvdm/nodejs-haveibeenpwned)'
};
/**
* Sort an object by its values
* in descending order
*
* @return {object} The sorted object
* @param {object} obj The object to sort
*/
function sortObjectByValues (obj) {
let sorting = [];
let result = {};
let key;
for (key in obj) {
sorting.push ({ key, count: obj [key] });
}
sorting.sort ((a, b) => {
if (a.count > b.count) {
return -1;
}
if (a.count < b.count) {
return 1;
}
return 0;
});
sorting.forEach (itm => {
result [itm.key] = itm.count;
});
return result;
}
/**
* Process API error
*
* @callback callback
* @return {void}
*
* @param {object} res httpreq response details
* @param {Error} err Additional Error to include
* @param {function} callback `(err, data)`
*/
function processApiError (res, err, callback) {
let error = new Error ('API error');
error.statusCode = res.statusCode;
error.body = res.body;
error.error = err;
callback (error);
}
/**
* Process HTTP response
*
* @callback callback
* @return {void}
*
* @param {Error|null} err httpreq error
* @param {object} res httpreq response details
* @param {function} callback `(err, data)`
*/
function processResponse (err, res, callback) {
let data;
let error;
if (err) {
return callback (err);
}
if (res.statusCode === 404) {
error = new Error ('not found');
error.statusCode = res.statusCode;
return callback (error);
}
try {
data = JSON.parse (res.body);
} catch (e) {
return processApiError (res, e, callback);
}
return callback (null, data);
}
/**
* Send HTTP request to API
*
* @callback callback
* @return {void}
*
* @param {string} service API service name
* @param {string} method API service method name
* @param {object} [params] Additional parameters to include
* @param {function} callback `(err, data)`
*/
function httpRequestHIBP (service, method, params, callback) {
let options = {
url: 'https://haveibeenpwned.com/api/v2/' + service + '/' + method,
method: 'GET',
timeout: config.timeout,
headers: {
'User-Agent': config.userAgent
}
};
if (typeof params === 'function') {
callback = params;
params = {};
}
options.parameters = params;
httpreq.doRequest (options, (err, res) => {
processResponse (err, res, callback);
});
}
/**
* All breaches for an account
*
* @callback callback
* @return {void}
*
* @param {string} account Email or username
* @param {object} [params] Additional parameters to include
* @param {function} callback `(err, data)`
*/
function methodBreachedAccount (account, params, callback) {
httpRequestHIBP ('breachedaccount', account, params, callback);
}
/**
* All breached sites in the system
*
* @callback callback
^ @return {void}
*
* @param {object} [params] Additional parameters to include
* @param {function} callback `(err, data)`
*/
function methodBreaches (params, callback) {
httpRequestHIBP ('breaches', '', params, callback);
}
/**
* A single breached site
*
* @callback callback
* @return {void}
*
* @param {string} name Email or username
* @param {function} callback `(err, data)`
*/
function methodBreach (name, callback) {
httpRequestHIBP ('breach', name, callback);
}
/**
* All pastes for an account
*
* @callback callback
* @return {void}
*
* @param {string} amount Email or username
* @param {function} callback `(err, data)`
*/
function methodPasteAccount (account, callback) {
httpRequestHIBP ('pasteaccount', account, callback);
}
/**
* All data classes in the system
*
* @callback callback
* @return {void}
*
* @param {function} callback `(err, data)`
*/
function methodDataclasses (callback) {
httpRequestHIBP ('dataclasses', '', callback);
}
/**
* Send HTTP request to API
*
* @callback callback
* @return {void}
*
* @param {string} path API method path
* @param {object} [params] Additional parameters to include
* @param {function} callback `(err, data)`
*/
function httpRequestPP (path, params, callback) {
let options = {
url: 'https://api.pwnedpasswords.com/' + path,
method: 'GET',
timeout: config.timeout,
headers: {
'User-Agent': config.userAgent
}
};
if (typeof params === 'function') {
callback = params;
params = {};
}
options.parameters = params;
httpreq.doRequest (options, (err, res) => {
let error;
let msg;
if (err) {
callback (err);
} else if (res.statusCode === 404) {
callback (null, 0);
} else if (res.statusCode >= 300) {
msg = res.body
.replace (/.+<p>(.+)<\/p>.+/, '$')
.trim();
error = new Error (msg);
error.statusCode = res.statusCode;
error.body = res.body;
callback (error);
} else {
callback (null, res.body);
}
});
}
/**
* Search the Pwned Passwords database for a password
*
* Password not found: callback `data` is {int} `0`
* Password found: callback `data` is {int} amount
*
* @callback callback
* @return {void}
*
* @param {string} password Password to check
* @param {bool} [hashed] Is the password already SHA-1 hashed
* @param {function} callback `(err, data)`
*/
function methodPwnedPasswordsByPassword (password, hashed, callback) {
let params = {};
if (typeof hashed === 'function') {
callback = hashed;
hashed = false;
}
if (hashed) {
params.originalPasswordIsAHash = 'true';
}
httpRequestPP ('pwnedpassword/' + password, params, (err, data) => {
if (err) {
callback (err);
} else {
callback (null, parseInt (data, 10));
}
});
}
/**
* Search the Pwned Passwords database for the first 5 chars of a hash
* and callback the matched hashed with their counts.
*
* Not found: callback `data` is {int} `0`
* Found: callback `data` is {object}
*
* @callback callback
* @return {void}
*
* @param {string} hash SHA-1 hash to check
* @param {bool} [sort] Sort by counts in descending order
* @param {function} callback `(err, data)`
*/
function methodPwnedPasswordsByRange (hash, sort, callback) {
hash = hash.substr (0, 5);
if (typeof sort === 'function') {
callback = sort;
sort = false;
}
httpRequestPP ('range/' + hash, (err, data) => {
let result = {};
let i;
let str;
let sha;
if (err) {
callback (err);
return;
}
data = data.trim().split ('\n');
if (data.length) {
for (i = 0; i < data.length; i++) {
str = data[i].split (':');
sha = str[0].toLowerCase();
result[sha] = parseInt (str[1], 10);
}
if (sort) {
result = sortObjectByValues (result);
}
callback (null, result);
return;
}
callback (null, 0);
});
}
/**
* Module interface
*
* @return {object} Methods
*
* @param {object} set Configuration params
* @param {int} [set.timeout=5000] Wait timeout in ms
*/
module.exports = set => {
if (set && set.timeout) {
config.timeout = set.timeout;
}
return {
breachedAccount: methodBreachedAccount,
breaches: methodBreaches,
breach: methodBreach,
pasteAccount: methodPasteAccount,
dataclasses: methodDataclasses,
pwnedpasswords: {
byPassword: methodPwnedPasswordsByPassword,
byRange: methodPwnedPasswordsByRange
}
};
};