- Add audit module with controllers, DTOs, middleware and services - Add MFA controller, routes and services - Add feature flags module with controllers, DTOs and services - Update audit entities with proper TypeORM decorators - Update auth service and DTOs - Update main.ts configuration Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
111 lines
2.5 KiB
TypeScript
111 lines
2.5 KiB
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import { MfaService } from './services/mfa.service';
|
|
import { AuthRequest } from '../../shared/types/index';
|
|
|
|
export const mfaController = {
|
|
/**
|
|
* Initialize MFA setup
|
|
*/
|
|
async setup(req: AuthRequest, res: Response, next: NextFunction) {
|
|
try {
|
|
const userId = req.user!.userId; // Assuming req.user is populated by auth middleware
|
|
const result = await MfaService.setupMfa(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Verify MFA setup and enable
|
|
*/
|
|
async verifySetup(req: AuthRequest, res: Response, next: NextFunction) {
|
|
try {
|
|
const userId = req.user!.userId;
|
|
const { secret, code } = req.body;
|
|
|
|
if (!secret || !code) {
|
|
throw new Error('Secret and code are required');
|
|
}
|
|
|
|
const result = await MfaService.verifyMfaSetup(userId, secret, code);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: result.message,
|
|
data: { backupCodes: result.backupCodes },
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Disable MFA
|
|
*/
|
|
async disable(req: AuthRequest, res: Response, next: NextFunction) {
|
|
try {
|
|
const userId = req.user!.userId;
|
|
const { code, password } = req.body;
|
|
|
|
if (!code) {
|
|
throw new Error('Verification code is required');
|
|
}
|
|
|
|
const result = await MfaService.disableMfa(userId, code, password);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: result.message,
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get MFA status
|
|
*/
|
|
async getStatus(req: AuthRequest, res: Response, next: NextFunction) {
|
|
try {
|
|
const userId = req.user!.userId;
|
|
const result = await MfaService.getMfaStatus(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: result,
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Regenerate backup codes
|
|
*/
|
|
async regenerateBackupCodes(req: AuthRequest, res: Response, next: NextFunction) {
|
|
try {
|
|
const userId = req.user!.userId;
|
|
const { code, password } = req.body;
|
|
|
|
if (!code) {
|
|
throw new Error('Verification code is required');
|
|
}
|
|
|
|
const result = await MfaService.regenerateBackupCodes(userId, code, password);
|
|
|
|
res.json({
|
|
success: true,
|
|
message: result.message,
|
|
data: { backupCodes: result.backupCodes },
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
},
|
|
};
|