Skip to content

Commit 78164f5

Browse files
committed
feat: update security endpoint response and fix for last 24 hours
1 parent fc39adf commit 78164f5

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

src/controllers/internalSecurity.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,59 @@ import { Request, Response } from 'express';
88
import { Op } from 'sequelize';
99

1010
import { AuthEvent } from '../models/authEvents.js';
11+
import getLogger from '../utils/logger.js';
12+
13+
const logger = getLogger('internalSecurity');
1114

1215
export const getSecurityAnomalies = async (_req: Request, res: Response) => {
1316
const now = new Date();
14-
const windowStart = new Date(now.getTime() - 60 * 60 * 1000);
17+
const windowStart = new Date(now.getTime() - 60 * 60 * 1000 * 24);
1518

1619
try {
17-
const failedLogins = await AuthEvent.findAll({
20+
const FAILURE_TYPES = [
21+
'login_failed',
22+
'cookie_token_failed',
23+
'bearer_token_failed',
24+
'jwks_failed',
25+
'mfa_otp_failed',
26+
'otp_failed',
27+
'recovery_otp_failed',
28+
'refresh_token_failed',
29+
'registration_failed',
30+
'service_token_failed',
31+
'user_data_failed',
32+
'webauthn_login_failed',
33+
'webauthn_registration_failed',
34+
];
35+
36+
const events = await AuthEvent.findAll({
1837
where: {
19-
type: 'login_failed',
20-
created_at: { [Op.gte]: windowStart },
38+
created_at: {
39+
[Op.gte]: windowStart,
40+
},
41+
[Op.or]: [
42+
{
43+
type: {
44+
[Op.in]: FAILURE_TYPES,
45+
},
46+
},
47+
{
48+
type: {
49+
[Op.like]: '%suspicious%',
50+
},
51+
},
52+
],
2153
},
22-
attributes: ['ip_address'],
54+
attributes: ['user_id', 'type', 'ip_address', 'user_agent', 'metadata', 'created_at'],
2355
});
2456

25-
const ipCounts: Record<string, number> = {};
26-
27-
for (const event of failedLogins) {
28-
const ip = event.ip_address || 'unknown';
29-
ipCounts[ip] = (ipCounts[ip] || 0) + 1;
30-
}
31-
32-
const suspicious = Object.entries(ipCounts)
33-
.filter(([_, count]) => count > 10)
34-
.map(([ip, count]) => ({ ip, count }));
35-
57+
console.log('events', events);
3658
return res.json({
37-
suspiciousIps: suspicious,
38-
totalFailedLogins: failedLogins.length,
59+
suspiciousEvents: events,
60+
total: events.length,
3961
});
4062
} catch {
63+
logger.error(`Failed to get security events`);
4164
return res.status(500).json({ message: 'Failed to detect anomalies' });
4265
}
4366
};

0 commit comments

Comments
 (0)