This repository was archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (76 loc) · 2.18 KB
/
index.js
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
/*jshint node:true */
"use strict";
var async = require('async');
var turbasen = require('turbasen');
var crypto = require('./lib/crypto');
module.exports._authGroup = function(email, password, group, cb) {
turbasen.grupper.get(group._id, function(err, res, body) {
if (err) { return cb(err); }
if (res.statusCode !== 200) {
return cb(new Error('AUTH102: Error code ' + res.statusCode));
}
var iterator = async.apply(crypto.authenticate, email, password);
async.detect(body.privat.brukere, iterator, function(user) {
if (!user) {
return cb(null, false);
}
return cb(null, {
navn: user.navn,
epost: user.epost,
gruppe: group
});
});
});
};
module.exports.authenticate = function(email, password, cb) {
turbasen.grupper({'privat.brukere.epost': email}, function(err, res, body) {
if (err) { return cb(err); }
if (res.statusCode !== 200) {
return cb(new Error('AUTH101: Error code ' + res.statusCode));
}
if (body.documents.length === 0) {
return cb(null, false);
}
var iterator = async.apply(module.exports._authGroup, email, password);
async.map(body.documents, iterator, function(err, groups) {
if (err) { return cb(err); }
for (var i = 0; i < groups.length; i++) {
if (groups[i]) {
return cb(null, groups[i]);
}
}
return cb(null, false);
});
});
};
module.exports.createUserAuth = function(name, email, pass, cb) {
var salt = crypto.salt();
var itrs = 131072;
var dkLen = 256;
crypto.pbkdf2(pass, salt, itrs, dkLen, function(err, hash) {
cb(err, {
navn: name,
epost: email,
pbkdf2: {
prf: 'HMAC-SHA1',
itrs: itrs,
salt: salt,
dkLen: dkLen,
hash: hash
}
});
});
};
module.exports.middleware = function(req, res, next) {
var email = req.body.email;
var password = req.body.password;
if (email && password) {
module.exports.authenticate(email, password, function(err, user) {
req.turbasenAuth = user;
next(err);
});
} else {
return process.nextTick(function() { next(); });
}
};
module.exports.turbasen = turbasen;