Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

some updates

Blue Mouse092a7090faa7
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index e9d51f5e2..accf393f8 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -85,6 +85,12 @@ export class AuthController {
})
} else {
const user = await this.authService.validateUser(userLoginDTO.email, userLoginDTO.password)
+
+ if (user.isTwoFactorAuthenticationEnabled) {
+ const processedUser = this.authService.postLoginProcess(user)
+ return this.authService.login(processedUser)
+ }
+
const sharedProjects = await this.projectService.findShare({
where: {
user: user.id,
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index 3a8e3544b..b88b5fe46 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -5,6 +5,7 @@ import * as crypto from 'crypto'
import * as https from 'https'
import * as _toUpper from 'lodash/toUpper'
import * as _includes from 'lodash/includes'
+import * as _pick from 'lodash/pick'
import { UserService } from '../user/user.service'
import { ACCOUNT_PLANS } from '../user/entities/user.entity'
@@ -99,18 +100,27 @@ export class AuthService {
processUser(user: User): object {
// @ts-ignore
- const maxEventsCount = ACCOUNT_PLANS[user.planCode].monthlyUsageLimit || 0
+ const maxEventsCount = ACCOUNT_PLANS[user?.planCode]?.monthlyUsageLimit || 0
const userData = {
// @ts-ignore
...user,
password: undefined,
twoFactorRecoveryCode: undefined,
+ twoFactorAuthenticationSecret: undefined,
maxEventsCount,
}
return userData
}
+ postLoginProcess(user: User): object {
+ if (user.isTwoFactorAuthenticationEnabled) {
+ return _pick(user, ['id', 'email', 'isTwoFactorAuthenticationEnabled'])
+ }
+
+ return user
+ }
+
login(user: User | object, isSecondFactorAuthenticated = false): object {
let userData = user
diff --git a/src/common/templates/en/2fa-turned-off.html b/src/common/templates/en/2fa-turned-off.html
new file mode 100644
index 000000000..088df8338
--- /dev/null
+++ b/src/common/templates/en/2fa-turned-off.html
@@ -0,0 +1,164 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>2FA has been disabled for your Swetrix account</title>
+ <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet" />
+ </head>
+ <body style="margin: 0; padding: 0; background-color: #f0f4f7;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="background: #f0f4f7; min-width: 340px; font-size: 1px; line-height: normal;">
+ <tr>
+ <td align="center" valign="top" bgcolor="#f0f4f7">
+ <!--[if (gte mso 9)|(IE)]>
+ <table border="0" cellspacing="0" cellpadding="0">
+ <tr><td align="center" valign="top" width="750"><![endif]-->
+ <table cellpadding="0" cellspacing="0" border="0" width="750" style="width: 100%; max-width: 750px; min-width: 340px; background: #f0f4f7;">
+ <tr>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ <td align="center" valign="top" style="background: #ffffff;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="right" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 23px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 40px; line-height: 40px; font-size: 38px;">&nbsp;</div>
+ <a href="https://swetrix.com" target="_blank" style="display: block; max-width: 173px;text-decoration: none;">
+ <img src="https://swetrix.com/assets/logo_blue.png" alt="Swetrix" width="175" border="0" style="display: block; width: 175px; font-size: 32px;" />
+ </a>
+ <div style="height: 40px; line-height: 40px; font-size: 40px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 36px; line-height: 45px; font-weight: 300; letter-spacing: -1px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 30px;
+ line-height: 45px;
+ font-weight: 300;
+ letter-spacing: -1px;
+ "
+ >
+ 2FA has been disabled on your Swetrix account
+ </span>
+ </font>
+ <div style="height: 38px; line-height: 38px; font-size: 36px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font face="'Source Sans Pro', sans-serif" color="#6d6d6d" style="font-size: 17px; line-height: 23px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #6d6d6d;
+ font-size: 17px;
+ line-height: 23px;
+ "
+ >
+ This is an email to let you know the 2FA (two-factor authentication) has been turned off for your Swetrix account.<br />
+ If you have not done so, please contact us at [email protected]
+ </span>
+ </font>
+ <div style="height: 30px; line-height: 30px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 32px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/terms"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Terms of Service
+ </a>
+ </span>
+ </font>
+ <font face="'Source Sans Pro', sans-serif" color="#3c4858" style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ &nbsp;&nbsp;|&nbsp;&nbsp;
+ </span>
+ </font>
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/privacy"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Privacy Policy
+ </a>
+ </span>
+ </font>
+ </td>
+ </tr>
+ </table>
+ <div style="height: 25px; line-height: 25px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ </tr>
+ </table>
+ <!--[if (gte mso 9)|(IE)]>
+ </td></tr>
+ </table><![endif]-->
+ </td>
+ </tr>
+ </table>
+ </body>
+</html>
diff --git a/src/common/templates/en/2fa-turned-on.html b/src/common/templates/en/2fa-turned-on.html
new file mode 100644
index 000000000..3f3937a59
--- /dev/null
+++ b/src/common/templates/en/2fa-turned-on.html
@@ -0,0 +1,164 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>2FA has been enabled on your Swetrix account</title>
+ <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet" />
+ </head>
+ <body style="margin: 0; padding: 0; background-color: #f0f4f7;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="background: #f0f4f7; min-width: 340px; font-size: 1px; line-height: normal;">
+ <tr>
+ <td align="center" valign="top" bgcolor="#f0f4f7">
+ <!--[if (gte mso 9)|(IE)]>
+ <table border="0" cellspacing="0" cellpadding="0">
+ <tr><td align="center" valign="top" width="750"><![endif]-->
+ <table cellpadding="0" cellspacing="0" border="0" width="750" style="width: 100%; max-width: 750px; min-width: 340px; background: #f0f4f7;">
+ <tr>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ <td align="center" valign="top" style="background: #ffffff;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="right" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 23px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 40px; line-height: 40px; font-size: 38px;">&nbsp;</div>
+ <a href="https://swetrix.com" target="_blank" style="display: block; max-width: 173px;text-decoration: none;">
+ <img src="https://swetrix.com/assets/logo_blue.png" alt="Swetrix" width="175" border="0" style="display: block; width: 175px; font-size: 32px;" />
+ </a>
+ <div style="height: 40px; line-height: 40px; font-size: 40px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 36px; line-height: 45px; font-weight: 300; letter-spacing: -1px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 30px;
+ line-height: 45px;
+ font-weight: 300;
+ letter-spacing: -1px;
+ "
+ >
+ 2FA has been enabled on your Swetrix account
+ </span>
+ </font>
+ <div style="height: 38px; line-height: 38px; font-size: 36px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font face="'Source Sans Pro', sans-serif" color="#6d6d6d" style="font-size: 17px; line-height: 23px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #6d6d6d;
+ font-size: 17px;
+ line-height: 23px;
+ "
+ >
+ This is an email to let you know the 2FA (two-factor authentication) has been turned on for your Swetrix account.<br />
+ If you have not done so, please contact us at [email protected]
+ </span>
+ </font>
+ <div style="height: 30px; line-height: 30px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 32px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/terms"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Terms of Service
+ </a>
+ </span>
+ </font>
+ <font face="'Source Sans Pro', sans-serif" color="#3c4858" style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ &nbsp;&nbsp;|&nbsp;&nbsp;
+ </span>
+ </font>
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com/privacy"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Privacy Policy
+ </a>
+ </span>
+ </font>
+ </td>
+ </tr>
+ </table>
+ <div style="height: 25px; line-height: 25px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ </tr>
+ </table>
+ <!--[if (gte mso 9)|(IE)]>
+ </td></tr>
+ </table><![endif]-->
+ </td>
+ </tr>
+ </table>
+ </body>
+</html>
diff --git a/src/mailer/letter.ts b/src/mailer/letter.ts
index cfbf8f810..69fae193a 100644
--- a/src/mailer/letter.ts
+++ b/src/mailer/letter.ts
@@ -9,4 +9,6 @@ export enum LetterTemplate {
ProjectReport = 'project-report',
TierWarning = 'tier-warning',
ProjectInvitation = 'customer_project-invitation',
+ TwoFAOn = '2fa-turned-on',
+ TwoFAOff = '2fa-turned-off',
}
diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.ts
index a8ffb2e86..036dd464c 100644
--- a/src/mailer/mailer.service.ts
+++ b/src/mailer/mailer.service.ts
@@ -58,7 +58,17 @@ const metaInfoJson = {
subject: {
en: () => 'You have been invited to join the project.',
},
- }
+ },
+ [LetterTemplate.TwoFAOn]: {
+ subject: {
+ en: () => '2FA has been enabled on your Swetrix account',
+ },
+ },
+ [LetterTemplate.TwoFAOff]: {
+ subject: {
+ en: () => '2FA has been disabled on your Swetrix account',
+ },
+ },
}
interface Params {
diff --git a/src/twoFactorAuth/twoFactorAuth.controller.ts b/src/twoFactorAuth/twoFactorAuth.controller.ts
index 894a7757c..e27634baa 100644
--- a/src/twoFactorAuth/twoFactorAuth.controller.ts
+++ b/src/twoFactorAuth/twoFactorAuth.controller.ts
@@ -1,5 +1,5 @@
import {
- Controller, Post, UseGuards, HttpCode, Body, BadRequestException,
+ Controller, Post, UseGuards, HttpCode, Body, BadRequestException, Headers, Ip,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
@@ -7,15 +7,16 @@ import { TwoFactorAuthService } from './twoFactorAuth.service'
import { UserService } from '../user/user.service'
import { AuthService } from 'src/auth/auth.service'
import { UserType } from '../user/entities/user.entity'
-import { ActionTokensService } from '../action-tokens/action-tokens.service'
import { AppLoggerService } from '../logger/logger.service'
+import { MailerService } from '../mailer/mailer.service'
+import { LetterTemplate } from '../mailer/letter'
import { CurrentUserId } from 'src/common/decorators/current-user-id.decorator'
import { SelfhostedGuard } from '../common/guards/selfhosted.guard'
import { Roles } from '../common/decorators/roles.decorator'
import { TwoFaNotRequired } from '../common/decorators/2fa-disabled.decorator'
import { RolesGuard } from 'src/common/guards/roles.guard'
import { TwoFactorAuthDTO } from './dto/2fa-auth.dto'
-import { generateRecoveryCode } from 'src/common/utils'
+import { generateRecoveryCode, checkRateLimit } from 'src/common/utils'
@ApiTags('2fa')
@Controller('2fa')
@@ -24,8 +25,8 @@ export class TwoFactorAuthController {
private twoFactorAuthService: TwoFactorAuthService,
private userService: UserService,
private authService: AuthService,
- private actionTokensService: ActionTokensService,
- private readonly logger: AppLoggerService
+ private readonly logger: AppLoggerService,
+ private readonly mailerService: MailerService,
) {}
@Post('generate')
@@ -44,8 +45,12 @@ export class TwoFactorAuthController {
@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted
@Roles(UserType.CUSTOMER, UserType.ADMIN)
@TwoFaNotRequired()
- async turnOnTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string) {
+ async turnOnTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {
this.logger.log({ body }, 'POST /2fa/enable')
+
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+ await checkRateLimit(ip, '2fa-enable', 10, 1800)
+
const user = await this.userService.findOneWhere({ id })
const { twoFactorAuthenticationCode } = body
@@ -64,6 +69,10 @@ export class TwoFactorAuthController {
twoFactorRecoveryCode,
})
+ await this.mailerService.sendEmail(user.email, LetterTemplate.TwoFAOn)
+
+ user.isTwoFactorAuthenticationEnabled = true
+
const authData = this.authService.login(user, true)
return {
@@ -77,19 +86,23 @@ export class TwoFactorAuthController {
@UseGuards(RolesGuard)
@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- async turnOffTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string) {
+ async turnOffTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {
this.logger.log({ body }, 'POST /2fa/disable')
+
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+ await checkRateLimit(ip, '2fa-disable', 10, 1800)
+
const user = await this.userService.findOneWhere({ id })
const { twoFactorAuthenticationCode } = body
- const isCodeValid = this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(
- twoFactorAuthenticationCode, user,
- )
+ const isCodeValid = user.twoFactorRecoveryCode === twoFactorAuthenticationCode || this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(twoFactorAuthenticationCode, user)
if (!isCodeValid) {
throw new BadRequestException('Wrong authentication code')
}
+ await this.mailerService.sendEmail(user.email, LetterTemplate.TwoFAOff)
+
await this.userService.update(user.id, {
isTwoFactorAuthenticationEnabled: false,
twoFactorRecoveryCode: null,
@@ -103,14 +116,16 @@ export class TwoFactorAuthController {
@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted
@Roles(UserType.CUSTOMER, UserType.ADMIN)
@TwoFaNotRequired()
- async authenticate(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string) {
+ async authenticate(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {
this.logger.log({ body }, 'POST /2fa/authenticate')
+
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+ await checkRateLimit(ip, '2fa-auth', 10, 1800)
+
const user = await this.userService.findOneWhere({ id })
const { twoFactorAuthenticationCode } = body
- const isCodeValid = this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(
- twoFactorAuthenticationCode, user,
- )
+ const isCodeValid = user.twoFactorRecoveryCode === twoFactorAuthenticationCode || this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(twoFactorAuthenticationCode, user)
if (!isCodeValid) {
throw new BadRequestException('Wrong authentication code')
diff --git a/src/twoFactorAuth/twoFactorAuth.module.ts b/src/twoFactorAuth/twoFactorAuth.module.ts
index 215f835b9..c6cddb168 100644
--- a/src/twoFactorAuth/twoFactorAuth.module.ts
+++ b/src/twoFactorAuth/twoFactorAuth.module.ts
@@ -3,15 +3,15 @@ import { TwoFactorAuthService } from './twoFactorAuth.service'
import { UserModule } from '../user/user.module'
import { AuthModule } from 'src/auth/auth.module'
import { TwoFactorAuthController } from './twoFactorAuth.controller'
-import { ActionTokensModule } from '../action-tokens/action-tokens.module'
import { AppLoggerModule } from '../logger/logger.module'
+import { MailerModule } from '../mailer/mailer.module'
@Module({
imports: [
UserModule,
- ActionTokensModule,
AppLoggerModule,
AuthModule,
+ MailerModule,
],
controllers: [TwoFactorAuthController],
providers: [TwoFactorAuthService],
diff --git a/src/twoFactorAuth/twoFactorAuth.service.ts b/src/twoFactorAuth/twoFactorAuth.service.ts
index df60d1766..2caaf3e85 100644
--- a/src/twoFactorAuth/twoFactorAuth.service.ts
+++ b/src/twoFactorAuth/twoFactorAuth.service.ts
@@ -3,7 +3,6 @@ import { authenticator } from 'otplib'
import { UserService } from '../user/user.service'
import { User } from '../user/entities/user.entity'
-import { AppLoggerService } from '../logger/logger.service'
import {
TWO_FACTOR_AUTHENTICATION_APP_NAME,
} from '../common/constants'
@@ -12,7 +11,6 @@ import {
export class TwoFactorAuthService {
constructor(
private userService: UserService,
- private readonly logger: AppLoggerService
) { }
async generateTwoFactorAuthenticationSecret(user: User) {
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index 5477ad68b..df05eae1d 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -53,7 +53,7 @@ export class UserService {
}
omitSensitiveData(user: User): User {
- return _omit(user, ['password', 'twoFactorRecoveryCode'])
+ return _omit(user, ['password', 'twoFactorRecoveryCode', 'twoFactorAuthenticationSecret'])
}
findOneWhere(where: Record<string, unknown>, relations: string[] = []): Promise<User> {