improve totp security (#2668)

improve security
This commit is contained in:
Bernd Storath
2026-06-16 14:12:00 +02:00
committed by GitHub
parent 30498520d2
commit 5f54fa3e58
3 changed files with 21 additions and 0 deletions
+14
View File
@@ -23,6 +23,13 @@ export default definePermissionEventHandler(
checkPermissions(user);
if (body.type === 'setup') {
if (user.totpVerified) {
throw createError({
statusCode: 409,
statusMessage: 'TOTP is already enabled',
});
}
const key = new Secret({ size: 20 });
const totp = new TOTP({
@@ -50,6 +57,13 @@ export default definePermissionEventHandler(
type: 'created',
} as Response;
} else if (body.type === 'delete') {
if (!user.totpVerified) {
throw createError({
statusCode: 409,
statusMessage: 'TOTP is not enabled',
});
}
await Database.users.deleteTotpKey(user.id, body.currentPassword);
return {
@@ -221,6 +221,10 @@ export class UserService {
throw new Error('User not found');
}
if (txUser.totpVerified) {
throw new Error('TOTP is already verified');
}
const totpKey = txUser.totpKey;
if (!totpKey) {
throw new Error('TOTP key is not set');
@@ -18,7 +18,10 @@ const remember = z.boolean({ message: t('zod.user.remember') });
const totpCode = z
.string({ message: t('zod.user.totpCode') })
// min and max to improve error messages
.min(6, t('zod.user.totpCode'))
.max(6, t('zod.user.totpCode'))
.regex(/^\d{6}$/, t('zod.user.totpCode'))
.pipe(safeStringRefine);
export const UserLoginSchema = z.object({