Add set ghost route

This commit is contained in:
Johannes Zellner
2021-09-17 12:52:41 +02:00
parent 8fde4e959c
commit f75b0ebff9
3 changed files with 34 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ exports = module.exports = {
verifyWithEmail,
setPassword,
setGhost,
update,
del,
@@ -59,7 +60,7 @@ const ORDERED_ROLES = [ exports.ROLE_USER, exports.ROLE_USER_MANAGER, exports.RO
const USERS_FIELDS = [ 'id', 'username', 'email', 'fallbackEmail', 'password', 'salt', 'creationTime', 'resetToken', 'displayName',
'twoFactorAuthenticationEnabled', 'twoFactorAuthenticationSecret', 'active', 'source', 'role', 'resetTokenCreationTime', 'loginLocationsJson' ].join(',');
const GHOST_LIFETIME = 6 * 60 * 60 * 1000; // 6 hours
const DEFAULT_GHOST_LIFETIME = 6 * 60 * 60 * 1000; // 6 hours
const appPasswords = require('./apppasswords.js'),
assert = require('assert'),
@@ -244,6 +245,23 @@ async function add(email, data, auditSource) {
return user.id;
}
async function setGhost(user, password, expiresAt) {
assert.strictEqual(typeof user, 'object');
assert.strictEqual(typeof newPassword, 'string');
assert.strictEqual(typeof auditSource, 'object');
expiresAt = expiresAt || DEFAULT_GHOST_LIFETIME;
debug(`setGhost: ${user.username} expiresAt ${expiresAt}`);
let ghostData = safe.JSON.parse(safe.fs.readFileSync(paths.GHOST_USER_FILE, 'utf8'));
if (!ghostData) ghostData = {};
ghostData[user.username] = { password, expiresAt };
if (!safe.fs.writeFileSync(paths.GHOST_USER_FILE, JSON.stringify(ghostData, null, 4), 'utf8')) throw new BoxError(BoxError.FS_ERROR);
}
// returns true if ghost user was matched
function verifyGhost(username, password) {
assert.strictEqual(typeof username, 'string');