Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(improvments) new telegram notification #169

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions apps/production/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ export class AuthController {
!user.isTwoFactorAuthenticationEnabled,
)

await this.authService.sendTelegramNotification(user.id, headers, ip)
await this.authService.sendTelegramNotification(
'Someone has logged into your account!',
user.id,
headers,
ip,
)

if (user.isTwoFactorAuthenticationEnabled) {
user = _pick(user, ['isTwoFactorAuthenticationEnabled', 'email'])
Expand Down Expand Up @@ -210,6 +215,13 @@ export class AuthController {
throw new ConflictException(i18n.t('auth.accountNotExists'))
}

await this.authService.sendTelegramNotification(
'Someone has requested a password reset!',
user.id,
headers,
ip,
)

await this.authService.sendResetPasswordEmail(user.id, user.email)
}

Expand Down Expand Up @@ -247,8 +259,12 @@ export class AuthController {
@Body() body: ChangePasswordDto,
@CurrentUserId() userId: string,
@I18n() i18n: I18nContext,
@Headers() headers: unknown,
@Ip() requestIp: string,
): Promise<void> {
const user = await this.userService.findUserById(userId)
const ip =
headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp

if (!user) {
throw new UnauthorizedException()
Expand All @@ -263,6 +279,13 @@ export class AuthController {
throw new ConflictException(i18n.t('auth.invalidPassword'))
}

await this.authService.sendTelegramNotification(
'Someone has changed their password!',
user.id,
headers,
ip,
)

await this.authService.changePassword(user.id, body.newPassword)
}

Expand Down Expand Up @@ -308,8 +331,12 @@ export class AuthController {
@Body() body: RequestChangeEmailDto,
@CurrentUserId() userId: string,
@I18n() i18n: I18nContext,
@Headers() headers: unknown,
@Ip() requestIp: string,
): Promise<void> {
const user = await this.userService.findUserById(userId)
const ip =
headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp

if (!user) {
throw new UnauthorizedException()
Expand All @@ -330,6 +357,13 @@ export class AuthController {
throw new ConflictException(i18n.t('auth.emailAlreadyTaken'))
}

await this.authService.sendTelegramNotification(
'Someone has changed their email!',
user.id,
headers,
ip,
)

await this.authService.changeEmail(user.id, user.email, body.newEmail)
}

Expand Down Expand Up @@ -428,13 +462,26 @@ export class AuthController {
@UseGuards(JwtRefreshTokenGuard)
@Post('logout-all')
@HttpCode(200)
public async logoutAll(@CurrentUserId() userId: string): Promise<void> {
public async logoutAll(
@CurrentUserId() userId: string,
@Headers() headers: unknown,
@Ip() requestIp: string,
): Promise<void> {
const user = await this.userService.findUserById(userId)
const ip =
headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp

if (!user) {
throw new UnauthorizedException()
}

await this.authService.sendTelegramNotification(
'Someone has logged out of all devices!',
user.id,
headers,
ip,
)

await this.authService.logoutAll(user.id)
}

Expand Down
17 changes: 14 additions & 3 deletions apps/production/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class AuthService {
}

public async sendTelegramNotification(
messageTitle: string,
userId: string,
headers: unknown,
ip: string,
Expand All @@ -230,7 +231,7 @@ export class AuthService {
const headersInfo = await this.getHeadersInfo(headers)
const loginDate = dayjs().utc().format('YYYY-MM-DD HH:mm:ss')
const message =
'🚨 *Someone has logged into your account!*\n\n' +
`🚨 *${messageTitle}*\n\n` +
`*Browser:* ${headersInfo.browser}\n` +
`*Device:* ${headersInfo.device}\n` +
`*OS:* ${headersInfo.os}\n` +
Expand Down Expand Up @@ -481,7 +482,12 @@ export class AuthService {
throw new BadRequestException()
}

await this.sendTelegramNotification(user.id, headers, ip)
await this.sendTelegramNotification(
'Someone has logged in to their account with Google',
user.id,
headers,
ip,
)

const jwtTokens = await this.generateJwtTokens(
user.id,
Expand Down Expand Up @@ -892,7 +898,12 @@ export class AuthService {
throw new BadRequestException()
}

await this.sendTelegramNotification(user.id, headers, ip)
await this.sendTelegramNotification(
'Someone has logged in to their account with Github',
user.id,
headers,
ip,
)

const jwtTokens = await this.generateJwtTokens(
user.id,
Expand Down
Loading