1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-03-09 00:18:26 +01:00

feat: log excessive logins (#8774)

This commit is contained in:
Mateusz Kwasniewski 2024-11-18 09:01:41 +01:00 committed by GitHub
parent 6d4e2e991f
commit 56db988a86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 3 deletions

View File

@ -60,8 +60,12 @@ export class SimplePasswordProvider extends Controller {
res: Response<UserSchema>, res: Response<UserSchema>,
): Promise<void> { ): Promise<void> {
const { username, password } = req.body; const { username, password } = req.body;
const userAgent = req.get('user-agent');
const user = await this.userService.loginUser(username, password); const user = await this.userService.loginUser(username, password, {
userAgent,
ip: req.ip,
});
req.session.user = user; req.session.user = user;
this.openApiService.respondWithValidation( this.openApiService.respondWithValidation(
200, 200,

View File

@ -393,7 +393,11 @@ class UserService {
); );
} }
async loginUser(usernameOrEmail: string, password: string): Promise<IUser> { async loginUser(
usernameOrEmail: string,
password: string,
device?: { userAgent: string; ip: string },
): Promise<IUser> {
const settings = await this.settingService.get<SimpleAuthSettings>( const settings = await this.settingService.get<SimpleAuthSettings>(
simpleAuthSettingsKey, simpleAuthSettingsKey,
); );
@ -417,12 +421,22 @@ class UserService {
const match = await bcrypt.compare(password, passwordHash); const match = await bcrypt.compare(password, passwordHash);
if (match) { if (match) {
const loginOrder = await this.store.successfullyLogin(user); const loginOrder = await this.store.successfullyLogin(user);
const sessions = await this.sessionService.getSessionsForUser(
user.id,
);
if (sessions.length >= 5 && device) {
this.logger.info(
`Excessive login (user id: ${user.id}, user agent: ${device.userAgent}, IP: ${device.ip})`,
);
}
const deleteStaleUserSessions = this.flagResolver.getVariant( const deleteStaleUserSessions = this.flagResolver.getVariant(
'deleteStaleUserSessions', 'deleteStaleUserSessions',
); );
if (deleteStaleUserSessions.feature_enabled) { if (deleteStaleUserSessions.feature_enabled) {
const allowedSessions = Number( const allowedSessions = Number(
deleteStaleUserSessions.payload?.value || 30, deleteStaleUserSessions.payload?.value || 5,
); );
// subtract current user session that will be created // subtract current user session that will be created
const deletedSessionsCount = const deletedSessionsCount =
@ -433,6 +447,7 @@ class UserService {
user.deletedSessions = deletedSessionsCount; user.deletedSessions = deletedSessionsCount;
user.activeSessions = allowedSessions; user.activeSessions = allowedSessions;
} }
this.eventBus.emit(USER_LOGIN, { loginOrder }); this.eventBus.emit(USER_LOGIN, { loginOrder });
return user; return user;
} }