Added public/private keypairs for user (and hid from logging)

This commit is contained in:
Nathan Byrd 2023-01-05 22:33:03 -06:00
parent dc7f902182
commit 9f33c8b21d
3 changed files with 952 additions and 925 deletions

View File

@ -32,7 +32,7 @@ module.exports = class Log {
}; };
// try to remove sensitive info by default, e.g. 'password' fields // try to remove sensitive info by default, e.g. 'password' fields
['formData', 'formValue'].forEach(keyName => { ['formData', 'formValue', 'user'].forEach(keyName => {
serializers[keyName] = fd => Log.hideSensitive(fd); serializers[keyName] = fd => Log.hideSensitive(fd);
}); });
@ -65,7 +65,7 @@ module.exports = class Log {
// //
return JSON.parse( return JSON.parse(
JSON.stringify(obj).replace( JSON.stringify(obj).replace(
/"(password|passwordConfirm|key|authCode)"\s?:\s?"([^"]+)"/, /"(password|passwordConfirm|key|authCode|private_key_main)"\s?:\s?"([^"]+)"/,
(match, valueName) => { (match, valueName) => {
return `"${valueName}":"********"`; return `"${valueName}":"********"`;
} }

View File

@ -547,6 +547,11 @@ module.exports = class User {
async.series( async.series(
[ [
function setKeyPair(callback) {
self.generateMainKeyPair(err => {
return callback(err);
});
},
function saveProps(callback) { function saveProps(callback) {
self.persistProperties(self.properties, trans, err => { self.persistProperties(self.properties, trans, err => {
return callback(err); return callback(err);
@ -638,6 +643,26 @@ module.exports = class User {
); );
} }
generateMainKeyPair(cb) {
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
}, (err, publicKey, privateKey) => {
if (!err) {
this.setProperty(UserProps.PrivateKeyMain, privateKey);
this.setProperty(UserProps.PublicKeyMain, publicKey);
}
return cb(err);
});
}
persistProperties(properties, transOrDb, cb) { persistProperties(properties, transOrDb, cb) {
if (!_.isFunction(cb) && _.isFunction(transOrDb)) { if (!_.isFunction(cb) && _.isFunction(transOrDb)) {
cb = transOrDb; cb = transOrDb;

View File

@ -66,4 +66,6 @@ module.exports = {
AuthFactor2OTP: 'auth_factor2_otp', // If present, OTP type for 2FA. See OTPTypes AuthFactor2OTP: 'auth_factor2_otp', // If present, OTP type for 2FA. See OTPTypes
AuthFactor2OTPSecret: 'auth_factor2_otp_secret', // Secret used in conjunction with OTP 2FA AuthFactor2OTPSecret: 'auth_factor2_otp_secret', // Secret used in conjunction with OTP 2FA
AuthFactor2OTPBackupCodes: 'auth_factor2_otp_backup', // JSON array of backup codes AuthFactor2OTPBackupCodes: 'auth_factor2_otp_backup', // JSON array of backup codes
PublicKeyMain: 'public_key_main', // RSA public key for user
PrivateKeyMain: 'private_key_main', // RSA private key (corresponding to PublicKeyMain)
}; };