|
| 1 | +import { CommandModule } from "yargs"; |
| 2 | +import { hashPassword, verifyPassword } from "@server/auth/password"; |
| 3 | +import { db, resourceSessions, sessions } from "@server/db"; |
| 4 | +import { users } from "@server/db"; |
| 5 | +import { eq, inArray } from "drizzle-orm"; |
| 6 | +import moment from "moment"; |
| 7 | +import { fromError } from "zod-validation-error"; |
| 8 | +import { passwordSchema } from "@server/auth/passwordSchema"; |
| 9 | +import { UserType } from "@server/types/UserTypes"; |
| 10 | +import { generateRandomString, RandomReader } from "@oslojs/crypto/random"; |
| 11 | + |
| 12 | +type SetAdminCredentialsArgs = { |
| 13 | + email: string; |
| 14 | + password: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export const setAdminCredentials: CommandModule<{}, SetAdminCredentialsArgs> = { |
| 18 | + command: "set-admin-credentials", |
| 19 | + describe: "Set the server admin credentials", |
| 20 | + builder: (yargs) => { |
| 21 | + return yargs |
| 22 | + .option("email", { |
| 23 | + type: "string", |
| 24 | + demandOption: true, |
| 25 | + describe: "Admin email address" |
| 26 | + }) |
| 27 | + .option("password", { |
| 28 | + type: "string", |
| 29 | + demandOption: true, |
| 30 | + describe: "Admin password" |
| 31 | + }); |
| 32 | + }, |
| 33 | + handler: async (argv: { email: string; password: string }) => { |
| 34 | + try { |
| 35 | + const { email, password } = argv; |
| 36 | + |
| 37 | + const parsed = passwordSchema.safeParse(password); |
| 38 | + |
| 39 | + if (!parsed.success) { |
| 40 | + throw Error( |
| 41 | + `Invalid server admin password: ${fromError(parsed.error).toString()}` |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + const passwordHash = await hashPassword(password); |
| 46 | + |
| 47 | + await db.transaction(async (trx) => { |
| 48 | + try { |
| 49 | + const [existing] = await trx |
| 50 | + .select() |
| 51 | + .from(users) |
| 52 | + .where(eq(users.serverAdmin, true)); |
| 53 | + |
| 54 | + if (existing) { |
| 55 | + const passwordChanged = !(await verifyPassword( |
| 56 | + password, |
| 57 | + existing.passwordHash! |
| 58 | + )); |
| 59 | + |
| 60 | + if (passwordChanged) { |
| 61 | + await trx |
| 62 | + .update(users) |
| 63 | + .set({ passwordHash }) |
| 64 | + .where(eq(users.userId, existing.userId)); |
| 65 | + |
| 66 | + await invalidateAllSessions(existing.userId); |
| 67 | + console.log("Server admin password updated"); |
| 68 | + } |
| 69 | + |
| 70 | + if (existing.email !== email) { |
| 71 | + await trx |
| 72 | + .update(users) |
| 73 | + .set({ email, username: email }) |
| 74 | + .where(eq(users.userId, existing.userId)); |
| 75 | + |
| 76 | + console.log("Server admin email updated"); |
| 77 | + } |
| 78 | + } else { |
| 79 | + const userId = generateId(15); |
| 80 | + |
| 81 | + await trx.update(users).set({ serverAdmin: false }); |
| 82 | + |
| 83 | + await db.insert(users).values({ |
| 84 | + userId: userId, |
| 85 | + email: email, |
| 86 | + type: UserType.Internal, |
| 87 | + username: email, |
| 88 | + passwordHash, |
| 89 | + dateCreated: moment().toISOString(), |
| 90 | + serverAdmin: true, |
| 91 | + emailVerified: true |
| 92 | + }); |
| 93 | + |
| 94 | + console.log("Server admin created"); |
| 95 | + } |
| 96 | + } catch (e) { |
| 97 | + console.error("Failed to set admin credentials", e); |
| 98 | + trx.rollback(); |
| 99 | + throw e; |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + console.log("Admin credentials updated successfully"); |
| 104 | + process.exit(0); |
| 105 | + } catch (error) { |
| 106 | + console.error("Error:", error); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +export async function invalidateAllSessions(userId: string): Promise<void> { |
| 113 | + try { |
| 114 | + await db.transaction(async (trx) => { |
| 115 | + const userSessions = await trx |
| 116 | + .select() |
| 117 | + .from(sessions) |
| 118 | + .where(eq(sessions.userId, userId)); |
| 119 | + await trx.delete(resourceSessions).where( |
| 120 | + inArray( |
| 121 | + resourceSessions.userSessionId, |
| 122 | + userSessions.map((s) => s.sessionId) |
| 123 | + ) |
| 124 | + ); |
| 125 | + await trx.delete(sessions).where(eq(sessions.userId, userId)); |
| 126 | + }); |
| 127 | + } catch (e) { |
| 128 | + console.log("Failed to all invalidate user sessions", e); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +const random: RandomReader = { |
| 133 | + read(bytes: Uint8Array): void { |
| 134 | + crypto.getRandomValues(bytes); |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +export function generateId(length: number): string { |
| 139 | + const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 140 | + return generateRandomString(random, alphabet, length); |
| 141 | +} |
0 commit comments