Fix recover.js script
| @@ -1,16 +1,9 @@ | ||
| 1 | -import fs from 'node:fs'; | |
| 2 | 1 | import process from 'node:process'; |
| 3 | 2 | import yaml{ setConfigFilePath } from 'yaml./src/util.js'; |
| 4 | -import storage from 'node-persist'; | |
| 5 | -import { | |
| 6 | - initUserStorage, | |
| 7 | - getPasswordSalt, | |
| 8 | - getPasswordHash, | |
| 9 | - toKey, | |
| 10 | -} from './src/users.js'; | |
| 11 | 3 | |
| 12 | 4 | const userAccount = process.argv[2]; |
| 13 | 5 | const userPassword = process.argv[3]; |
| 6 | +const configPath = './config.yaml'; | |
| 14 | 7 | |
| 15 | 8 | if (!userAccount) { |
| 16 | 9 | console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.'); |
| @@ -19,50 +12,10 @@ if (!userAccount) { | ||
| 19 | 12 | process.exit(1); |
| 20 | 13 | } |
| 21 | 14 | |
| 22 | -async function initStorage() { | |
| 23 | - const config = yaml.parse(fs.readFileSync('config.yaml', 'utf8')); | |
| 24 | - const dataRoot = config.dataRoot; | |
| 25 | - | |
| 26 | - if (!dataRoot) { | |
| 27 | - console.error('No "dataRoot" setting found in config.yaml file.'); | |
| 28 | - process.exit(1); | |
| 29 | - } | |
| 30 | - | |
| 31 | - await initUserStorage(dataRoot); | |
| 32 | -} | |
| 33 | - | |
| 34 | 15 | async function main() { |
| 35 | 16 | await initStoragesetConfigFilePath(configPath); |
| 36 | - | |
| 17 | + const { recoverPassword } = await import('./src/recover-password.js'); | |
| 37 | - /** | |
| 18 | + await recoverPassword(configPath, userAccount, userPassword); | |
| 38 | - * @type {import('./src/users').User} | |
| 39 | - */ | |
| 40 | - const user = await storage.get(toKey(userAccount)); | |
| 41 | - | |
| 42 | - if (!user) { | |
| 43 | - console.error(`User "${userAccount}" not found.`); | |
| 44 | - process.exit(1); | |
| 45 | - } | |
| 46 | - | |
| 47 | - if (!user.enabled) { | |
| 48 | - console.log('User is disabled. Enabling...'); | |
| 49 | - user.enabled = true; | |
| 50 | - } | |
| 51 | - | |
| 52 | - if (userPassword) { | |
| 53 | - console.log('Setting new password...'); | |
| 54 | - const salt = getPasswordSalt(); | |
| 55 | - const passwordHash = getPasswordHash(userPassword, salt); | |
| 56 | - user.password = passwordHash; | |
| 57 | - user.salt = salt; | |
| 58 | - } else { | |
| 59 | - console.log('Setting an empty password...'); | |
| 60 | - user.password = ''; | |
| 61 | - user.salt = ''; | |
| 62 | - } | |
| 63 | - | |
| 64 | - await storage.setItem(toKey(userAccount), user); | |
| 65 | - console.log('User recovered. A program will exit now.'); | |
| 66 | 19 | } |
| 67 | 20 | |
| 68 | 21 | main(); |
| @@ -0,0 +1,65 @@ | ||
| 1 | +import fs from 'node:fs'; | |
| 2 | +import yaml from 'yaml'; | |
| 3 | +import storage from 'node-persist'; | |
| 4 | +import { | |
| 5 | + initUserStorage, | |
| 6 | + getPasswordSalt, | |
| 7 | + getPasswordHash, | |
| 8 | + toKey, | |
| 9 | +} from './users.js'; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Initializes the storage with the data root specified in the config file. | |
| 13 | + * @param {string} configPath - The path to the config file. | |
| 14 | + */ | |
| 15 | +async function initStorage(configPath) { | |
| 16 | + const config = yaml.parse(fs.readFileSync(configPath, 'utf8')); | |
| 17 | + const dataRoot = config.dataRoot; | |
| 18 | + | |
| 19 | + if (!dataRoot) { | |
| 20 | + console.error('No "dataRoot" setting found in config.yaml file.'); | |
| 21 | + process.exit(1); | |
| 22 | + } | |
| 23 | + | |
| 24 | + await initUserStorage(dataRoot); | |
| 25 | +} | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Recovers a user account by enabling it and optionally setting a new password. | |
| 29 | + * @param {string} configPath - The path to the config file. | |
| 30 | + * @param {string} userAccount - The username of the account to recover. | |
| 31 | + * @param {string} [userPassword] - The new password for the account. If not provided, sets an empty password. | |
| 32 | + */ | |
| 33 | +export async function recoverPassword(configPath, userAccount, userPassword) { | |
| 34 | + await initStorage(configPath); | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * @type {import('./users').User} | |
| 38 | + */ | |
| 39 | + const user = await storage.get(toKey(userAccount)); | |
| 40 | + | |
| 41 | + if (!user) { | |
| 42 | + console.error(`User "${userAccount}" not found.`); | |
| 43 | + process.exit(1); | |
| 44 | + } | |
| 45 | + | |
| 46 | + if (!user.enabled) { | |
| 47 | + console.log('User is disabled. Enabling...'); | |
| 48 | + user.enabled = true; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if (userPassword) { | |
| 52 | + console.log('Setting new password...'); | |
| 53 | + const salt = getPasswordSalt(); | |
| 54 | + const passwordHash = getPasswordHash(userPassword, salt); | |
| 55 | + user.password = passwordHash; | |
| 56 | + user.salt = salt; | |
| 57 | + } else { | |
| 58 | + console.log('Setting an empty password...'); | |
| 59 | + user.password = ''; | |
| 60 | + user.salt = ''; | |
| 61 | + } | |
| 62 | + | |
| 63 | + await storage.setItem(toKey(userAccount), user); | |
| 64 | + console.log('User recovered. A program will exit now.'); | |
| 65 | +} | |