Browse Source

Merge branch 'change/logs' of gitlab.com:jetup/rws/api-rws into stage

merge-requests/217/merge
Vitalik 2 years ago
parent
commit
b77885841d
  1. 3
      src/core/namespaces/sessions.namespace.ts
  2. 4
      src/domain/sessions/services/auth-attemptions.service.ts
  3. 5
      src/domain/sessions/services/sessions.service.ts
  4. 2
      src/rest/admin/auth/services/admin-auth.service.ts
  5. 7
      src/rest/admin/auth/services/admin-password-recovery.service.ts
  6. 6
      src/rest/app/auth/services/app-auth.service.ts

3
src/core/namespaces/sessions.namespace.ts

@ -113,8 +113,9 @@ export namespace Sessions { @@ -113,8 +113,9 @@ export namespace Sessions {
/**
* Метод для блокування IP адрес після 5 невдалих спроб
* @param {string} attempterIp - Айпі того, хто намагався авторизуватись
* @param data - додаткові дані
*/
addAuthAttemption(attempterIp: string): Promise<void>
addAuthAttemption(attempterIp: string, data?: Record<string, any>): Promise<void>
/**
* Видаляє кількість спроб авторизації (потрібно в разі вдалої авторизації)

4
src/domain/sessions/services/auth-attemptions.service.ts

@ -15,7 +15,7 @@ export class AuthAttemptionsService { @@ -15,7 +15,7 @@ export class AuthAttemptionsService {
private eventEmitter: EventEmitter2,
) {}
public async add(ip: string) {
public async add(ip: string, data: Record<string, any> = {}) {
const listType = await this.ipsService.getIpListType(ip)
if (listType === IPs.IPListType.White) return
@ -24,7 +24,7 @@ export class AuthAttemptionsService { @@ -24,7 +24,7 @@ export class AuthAttemptionsService {
if (attempts < this.maxAtempts) return
await this.ipsService.store({ ip, listType: IPs.IPListType.Black })
this.eventEmitter.emit(Events.OnTooManyAuthAttempts, { ip })
this.eventEmitter.emit(Events.OnTooManyAuthAttempts, { ip, ...data })
}
private async increment(ip: string) {

5
src/domain/sessions/services/sessions.service.ts

@ -53,6 +53,7 @@ export class SessionsService implements Sessions.ISessionsService { @@ -53,6 +53,7 @@ export class SessionsService implements Sessions.ISessionsService {
if (!session) throw new WrongRefreshTokenException()
const decoded = this.jwtService.decodeToken(refreshToken)
const tokens = this.generateTokens(session.userId, decoded.role, session.id)
await this.sessionsRepository.save({
@ -160,8 +161,8 @@ export class SessionsService implements Sessions.ISessionsService { @@ -160,8 +161,8 @@ export class SessionsService implements Sessions.ISessionsService {
await this.redisService.set(token, 'true', 360)
}
public async addAuthAttemption(attempterIp: string) {
return await this.authAtteptionsService.add(attempterIp)
public async addAuthAttemption(attempterIp: string, data?: Record<string, any>) {
return await this.authAtteptionsService.add(attempterIp, data)
}
public async dropAuthAttemptions(attempterIp: string) {

2
src/rest/admin/auth/services/admin-auth.service.ts

@ -26,7 +26,7 @@ export class AdminAuthService { @@ -26,7 +26,7 @@ export class AdminAuthService {
const isCorrect = await this.usersService.compareUserPassword(user.id, dto.password)
if (!isCorrect) {
await this.sessionsService.addAuthAttemption(ip)
await this.sessionsService.addAuthAttemption(ip, { userId: user.id })
throw new InvalidCredentialsException()
}

7
src/rest/admin/auth/services/admin-password-recovery.service.ts

@ -23,7 +23,7 @@ export class AdminPasswordRecoveryService { @@ -23,7 +23,7 @@ export class AdminPasswordRecoveryService {
const user = await this.usersService.getOneByPhoneNumber(phoneNumber)
if (!user || user.status === Users.Status.Deleted || user.status === Users.Status.Blocked) {
await this.sessionsService.addAuthAttemption(ip)
await this.sessionsService.addAuthAttemption(ip, { userId: user ? user.id : null })
throw new InvalidCredentialsException()
}
@ -36,12 +36,13 @@ export class AdminPasswordRecoveryService { @@ -36,12 +36,13 @@ export class AdminPasswordRecoveryService {
public async confirmRecovery(ip: string, dto: ConfirmPasswordRecoveryDto) {
const isCorrect = await this.confirmationCodesService.confirmCode(dto.phoneNumber, dto.code)
const user = await this.usersService.getOneByPhoneNumber(dto.phoneNumber)
if (!isCorrect) {
this.sessionsService.addAuthAttemption(ip)
this.sessionsService.addAuthAttemption(ip, { userId: user ? user.id : null })
throw new WrongCodeException()
}
const user = await this.usersService.getOneByPhoneNumber(dto.phoneNumber)
await this.usersService.changeUserPassword(user.id, dto.newPassword)
const session = await this.sessionsService.start({

6
src/rest/app/auth/services/app-auth.service.ts

@ -91,13 +91,13 @@ export class AppAuthService implements OnModuleInit { @@ -91,13 +91,13 @@ export class AppAuthService implements OnModuleInit {
ip,
)
const user = await this.usersService.getOneByPhoneNumber(dto.phoneNumber)
if (!isCorrect) {
this.sessionsService.addAuthAttemption(ip)
this.sessionsService.addAuthAttemption(ip, { userId: user ? user.id : null })
throw new WrongCodeException()
}
const user = await this.usersService.getOneByPhoneNumber(dto.phoneNumber)
const session = await this.sessionsService.start({
userId: user.id,
role: user.role,

Loading…
Cancel
Save