49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import { connect } from '@tursodatabase/database';
|
|
import { argon2id, hash } from 'argon2';
|
|
import { randomBytes, randomUUID } from 'node:crypto';
|
|
import { resolve } from 'node:path';
|
|
import process from 'node:process';
|
|
|
|
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db');
|
|
const database = await connect(databasePath);
|
|
|
|
const users = [
|
|
{ username: 'Aewyn', role: 'admin' },
|
|
{ username: 'Quartermaster', role: 'user' }
|
|
];
|
|
|
|
const findUser = await database.prepare(
|
|
'SELECT id, username, role FROM users WHERE username = ? COLLATE NOCASE'
|
|
);
|
|
const insertUser = await database.prepare(
|
|
'INSERT INTO users (id, username, password_hash, role, created_at) VALUES (?, ?, ?, ?, ?)'
|
|
);
|
|
|
|
for (const user of users) {
|
|
const existing = await findUser.get(user.username);
|
|
|
|
if (existing) {
|
|
console.log(`${user.username}: already exists (${existing.role}); left unchanged`);
|
|
continue;
|
|
}
|
|
|
|
// 120 bits of entropy encoded without punctuation that is awkward to paste.
|
|
const temporaryPassword = randomBytes(15).toString('base64url');
|
|
const passwordHash = await hash(temporaryPassword, {
|
|
type: argon2id,
|
|
memoryCost: 19_456,
|
|
timeCost: 2,
|
|
parallelism: 1
|
|
});
|
|
|
|
await insertUser.run(
|
|
randomUUID(),
|
|
user.username,
|
|
passwordHash,
|
|
user.role,
|
|
new Date().toISOString()
|
|
);
|
|
|
|
console.log(`${user.username} (${user.role}) temporary password: ${temporaryPassword}`);
|
|
}
|