forked from NodeBB/nodebb-plugin-2factor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsockets.js
More file actions
56 lines (46 loc) · 1.46 KB
/
websockets.js
File metadata and controls
56 lines (46 loc) · 1.46 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
var base32 = require('thirty-two'),
user = require.main.require('./src/user'),
nconf = require.main.require('nconf'),
utils = require.main.require('./public/src/utils'),
notp = require('notp'),
meta = require.main.require('./src/meta'),
parent = module.parent.exports,
Sockets = {
admin: {}
};
Sockets.regenerate = function(socket, data, callback) {
var key = utils.generateUUID(),
encodedKey = base32.encode(key);
user.getUserField(socket.uid, 'userslug', function(err, userslug) {
var baseUrl = nconf.get('url').replace(/.*?:\/\//g, "");
var otpUrl = "otpauth://totp/" + userslug + "@" + baseUrl + "?issuer=" + meta.config.title + "&secret=" + encodedKey + '&period=30',
qrImage = 'https://chart.googleapis.com/chart?chs=166x166&chld=L|0&cht=qr&chl=' + encodeURIComponent(otpUrl);
callback(null, {
qr: qrImage,
key: key
});
});
};
Sockets.confirm = function(socket, data, callback) {
var key = data.key,
token = data.token,
confirmed = notp.totp.verify(token, key);
if (confirmed) {
parent.save(socket.uid, key, function(err) {
callback(err);
});
} else {
callback(new Error('[[error:invalid-data]]'));
}
};
Sockets.disassociate = function(socket, data, callback) {
parent.disassociate(socket.uid, callback);
};
Sockets.admin.disassociate = function(socket, data, callback) {
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (isAdmin) {
parent.disassociate(data.uid, callback);
}
});
};
module.exports = Sockets;