-
Notifications
You must be signed in to change notification settings - Fork 37
/
find-user-with-certificate.js
42 lines (36 loc) · 1.13 KB
/
find-user-with-certificate.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
var async = require('async')
var path = require('path')
var pem = require('pem')
var operations = require('../../operations')
var config = require('../config')
function loadUserCertificate (fingerprint, current, user, callback) {
if (current) {
return callback(null, current)
}
async.waterfall([
pem.readPkcs12.bind(null, path.join(user.home, '.config', config.DAEMON_NAME, user.name + '.p12')),
function getFingerprint (keys, next) {
pem.getFingerprint(keys.cert, next)
},
function gotFingerprint (result, next) {
next(null, result.fingerprint)
}
], function loadedFingerprint (error, loadedFingerprint) {
if (error && error.code === 'ENOENT') {
error = null
}
user.scope = 'user'
if (user.name === 'root') {
user.scope = 'admin'
}
callback(null, fingerprint === loadedFingerprint ? user : null)
})
}
module.exports = function (fingerprint, callback) {
async.waterfall([
operations.listUsers.bind(null, null),
function loadEachCertificate (users, next) {
async.reduce(users, null, loadUserCertificate.bind(null, fingerprint), next)
}
], callback)
}