summary refs log tree commit diff
path: root/src/api/routes/users
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2023-12-16 18:17:36 -0500
committerPuyodead1 <puyodead@proton.me>2023-12-16 18:17:36 -0500
commit40db978c7d83c4db38effedb52522a5414400528 (patch)
tree8e0ae9602aeb3a9efaeab65691f5ac4476ee197e /src/api/routes/users
parentUpdate INVALID_LOGIN locale key (diff)
downloadserver-update/mfa.tar.xz
update mfa and login to reflect latest discord update/mfa
Diffstat (limited to 'src/api/routes/users')
-rw-r--r--src/api/routes/users/@me/mfa/totp/enable.ts10
-rw-r--r--src/api/routes/users/@me/mfa/webauthn/credentials/index.ts34
2 files changed, 42 insertions, 2 deletions
diff --git a/src/api/routes/users/@me/mfa/totp/enable.ts b/src/api/routes/users/@me/mfa/totp/enable.ts
index 19836e4d..5471e0b5 100644
--- a/src/api/routes/users/@me/mfa/totp/enable.ts
+++ b/src/api/routes/users/@me/mfa/totp/enable.ts
@@ -18,6 +18,7 @@
 
 import { route } from "@spacebar/api";
 import {
+	AuthenticatorType,
 	TotpEnableSchema,
 	User,
 	generateMfaBackupCodes,
@@ -74,7 +75,14 @@ router.post(
 		await Promise.all(backup_codes.map((x) => x.save()));
 		await User.update(
 			{ id: req.user_id },
-			{ mfa_enabled: true, totp_secret: body.secret },
+			{
+				mfa_enabled: true,
+				totp_secret: body.secret,
+				authenticator_types: [
+					...user.authenticator_types,
+					AuthenticatorType.TOTP,
+				],
+			},
 		);
 
 		res.send({
diff --git a/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts b/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
index f383ffb7..c8e5b67a 100644
--- a/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
+++ b/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
@@ -18,9 +18,12 @@
 
 import { route } from "@spacebar/api";
 import {
+	AuthenticatorType,
+	BackupCode,
 	CreateWebAuthnCredentialSchema,
 	DiscordApiErrors,
 	FieldErrors,
+	generateMfaBackupCodes,
 	GenerateWebAuthnCredentialsSchema,
 	generateWebAuthnTicket,
 	SecurityKey,
@@ -193,12 +196,41 @@ router.post(
 
 			await Promise.all([
 				securityKey.save(),
-				User.update({ id: req.user_id }, { webauthn_enabled: true }),
+				User.update(
+					{ id: req.user_id },
+					{
+						webauthn_enabled: true,
+						authenticator_types: [
+							...user.authenticator_types,
+							AuthenticatorType.WEBAUTHN,
+						],
+					},
+				),
 			]);
 
+			// try and get the users existing backup codes
+			let backup_codes = await BackupCode.find({
+				where: {
+					user: {
+						id: req.user_id,
+					},
+				},
+			});
+
+			// if there arent any, create them
+			if (!backup_codes.length) {
+				backup_codes = generateMfaBackupCodes(req.user_id);
+				await Promise.all(backup_codes.map((x) => x.save()));
+			}
+
 			return res.json({
 				name,
 				id: securityKey.id,
+				type: AuthenticatorType.WEBAUTHN, // I think thats what this is?
+				backup_codes: backup_codes.map((x) => ({
+					...x,
+					expired: undefined,
+				})),
 			});
 		} else {
 			throw DiscordApiErrors.INVALID_AUTHENTICATION_TOKEN;