| import fs from 'node:fs'; |
| import yaml from 'yaml'; |
| import storage from 'node-persist'; |
| import { |
| initUserStorage, |
| getPasswordSalt, |
| getPasswordHash, |
| toKey, |
| } from './users.js'; |
|
|
| |
| |
| |
| |
| async function initStorage(configPath) { |
| const config = yaml.parse(fs.readFileSync(configPath, 'utf8')); |
| const dataRoot = config.dataRoot; |
|
|
| if (!dataRoot) { |
| console.error('No "dataRoot" setting found in config.yaml file.'); |
| process.exit(1); |
| } |
|
|
| await initUserStorage(dataRoot); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export async function recoverPassword(configPath, userAccount, userPassword) { |
| await initStorage(configPath); |
|
|
| |
| |
| |
| const user = await storage.get(toKey(userAccount)); |
|
|
| if (!user) { |
| console.error(`User "${userAccount}" not found.`); |
| process.exit(1); |
| } |
|
|
| if (!user.enabled) { |
| console.log('User is disabled. Enabling...'); |
| user.enabled = true; |
| } |
|
|
| if (userPassword) { |
| console.log('Setting new password...'); |
| const salt = getPasswordSalt(); |
| const passwordHash = getPasswordHash(userPassword, salt); |
| user.password = passwordHash; |
| user.salt = salt; |
| } else { |
| console.log('Setting an empty password...'); |
| user.password = ''; |
| user.salt = ''; |
| } |
|
|
| await storage.setItem(toKey(userAccount), user); |
| console.log('User recovered. A program will exit now.'); |
| } |
|
|