@@ -23,6 +23,13 @@ export default definePermissionEventHandler(
|
|||||||
checkPermissions(user);
|
checkPermissions(user);
|
||||||
|
|
||||||
if (body.type === 'setup') {
|
if (body.type === 'setup') {
|
||||||
|
if (user.totpVerified) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 409,
|
||||||
|
statusMessage: 'TOTP is already enabled',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const key = new Secret({ size: 20 });
|
const key = new Secret({ size: 20 });
|
||||||
|
|
||||||
const totp = new TOTP({
|
const totp = new TOTP({
|
||||||
@@ -50,6 +57,13 @@ export default definePermissionEventHandler(
|
|||||||
type: 'created',
|
type: 'created',
|
||||||
} as Response;
|
} as Response;
|
||||||
} else if (body.type === 'delete') {
|
} 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);
|
await Database.users.deleteTotpKey(user.id, body.currentPassword);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -221,6 +221,10 @@ export class UserService {
|
|||||||
throw new Error('User not found');
|
throw new Error('User not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (txUser.totpVerified) {
|
||||||
|
throw new Error('TOTP is already verified');
|
||||||
|
}
|
||||||
|
|
||||||
const totpKey = txUser.totpKey;
|
const totpKey = txUser.totpKey;
|
||||||
if (!totpKey) {
|
if (!totpKey) {
|
||||||
throw new Error('TOTP key is not set');
|
throw new Error('TOTP key is not set');
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ const remember = z.boolean({ message: t('zod.user.remember') });
|
|||||||
|
|
||||||
const totpCode = z
|
const totpCode = z
|
||||||
.string({ message: t('zod.user.totpCode') })
|
.string({ message: t('zod.user.totpCode') })
|
||||||
|
// min and max to improve error messages
|
||||||
.min(6, t('zod.user.totpCode'))
|
.min(6, t('zod.user.totpCode'))
|
||||||
|
.max(6, t('zod.user.totpCode'))
|
||||||
|
.regex(/^\d{6}$/, t('zod.user.totpCode'))
|
||||||
.pipe(safeStringRefine);
|
.pipe(safeStringRefine);
|
||||||
|
|
||||||
export const UserLoginSchema = z.object({
|
export const UserLoginSchema = z.object({
|
||||||
|
|||||||
Reference in New Issue
Block a user