summary refs log tree commit diff
path: root/src/util/entities/BackupCodes.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/entities/BackupCodes.ts')
-rw-r--r--src/util/entities/BackupCodes.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util/entities/BackupCodes.ts b/src/util/entities/BackupCodes.ts
new file mode 100644
index 00000000..d532a39a
--- /dev/null
+++ b/src/util/entities/BackupCodes.ts
@@ -0,0 +1,35 @@
+import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
+import { BaseClass } from "./BaseClass";
+import { User } from "./User";
+import crypto from "crypto";
+
+@Entity("backup_codes")
+export class BackupCode extends BaseClass {
+	@JoinColumn({ name: "user_id" })
+	@ManyToOne(() => User, { onDelete: "CASCADE" })
+	user: User;
+
+	@Column()
+	code: string;
+
+	@Column()
+	consumed: boolean;
+
+	@Column()
+	expired: boolean;
+}
+
+export function generateMfaBackupCodes(user_id: string) {
+	let backup_codes: BackupCode[] = [];
+	for (let i = 0; i < 10; i++) {
+		const code = BackupCode.create({
+			user: { id: user_id },
+			code: crypto.randomBytes(4).toString("hex"),	// 8 characters
+			consumed: false,
+			expired: false,
+		});
+		backup_codes.push(code);
+	}
+
+	return backup_codes;
+}
\ No newline at end of file