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!

Hash 2FA recovery codes

Blue Mouse661f55bee033
diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.ts
index 3307125e6..2a02fc9bf 100644
--- a/backend/apps/cloud/src/auth/auth.service.ts
+++ b/backend/apps/cloud/src/auth/auth.service.ts
@@ -1268,9 +1268,12 @@ export class AuthService {
)
}
- const isRecoveryCodeValid =
- !!user.twoFactorRecoveryCode &&
- user.twoFactorRecoveryCode === twoFactorAuthenticationCode
+ const isRecoveryCodeValid = user.twoFactorRecoveryCode
+ ? await this.twoFactorAuthService.compareRecoveryCode(
+ twoFactorAuthenticationCode,
+ user.twoFactorRecoveryCode,
+ )
+ : false
const isTotpValid =
await this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(
twoFactorAuthenticationCode,
diff --git a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.controller.ts b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.controller.ts
index d08ad1f24..996e05cc9 100644
--- a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.controller.ts
+++ b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.controller.ts
@@ -103,10 +103,13 @@ export class TwoFactorAuthController {
}
const twoFactorRecoveryCode = generateRecoveryCode()
+ const hashedRecoveryCode = await this.twoFactorAuthService.hashRecoveryCode(
+ twoFactorRecoveryCode,
+ )
await this.userService.update(user.id, {
isTwoFactorAuthenticationEnabled: true,
- twoFactorRecoveryCode,
+ twoFactorRecoveryCode: hashedRecoveryCode,
})
try {
@@ -148,9 +151,12 @@ export class TwoFactorAuthController {
throw new BadRequestException('User not found')
}
- const isRecoveryCodeValid =
- !!user.twoFactorRecoveryCode &&
- user.twoFactorRecoveryCode === twoFactorAuthenticationCode
+ const isRecoveryCodeValid = user.twoFactorRecoveryCode
+ ? await this.twoFactorAuthService.compareRecoveryCode(
+ twoFactorAuthenticationCode,
+ user.twoFactorRecoveryCode,
+ )
+ : false
const isTotpValid =
await this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(
twoFactorAuthenticationCode,
@@ -203,9 +209,12 @@ export class TwoFactorAuthController {
throw new BadRequestException('Two-factor authentication is not enabled')
}
- const isRecoveryCodeValid =
- !!user.twoFactorRecoveryCode &&
- user.twoFactorRecoveryCode === twoFactorAuthenticationCode
+ const isRecoveryCodeValid = user.twoFactorRecoveryCode
+ ? await this.twoFactorAuthService.compareRecoveryCode(
+ twoFactorAuthenticationCode,
+ user.twoFactorRecoveryCode,
+ )
+ : false
const isTotpValid =
await this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(
twoFactorAuthenticationCode,
diff --git a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
index 98a4d1c05..994b4f5e9 100644
--- a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
+++ b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'
import { generateSecret, generateURI, verify } from 'otplib'
+import { genSalt, hash, compare } from 'bcrypt'
import { UserService } from '../user/user.service'
import { User } from '../user/entities/user.entity'
@@ -9,6 +10,18 @@ import { TWO_FACTOR_AUTHENTICATION_APP_NAME } from '../common/constants'
export class TwoFactorAuthService {
constructor(private userService: UserService) {}
+ async hashRecoveryCode(code: string): Promise<string> {
+ const salt = await genSalt(10)
+ return hash(code, salt)
+ }
+
+ async compareRecoveryCode(
+ code: string,
+ hashedCode: string,
+ ): Promise<boolean> {
+ return compare(code, hashedCode)
+ }
+
async generateTwoFactorAuthenticationSecret(user: User) {
const secret = generateSecret()
const otpauthUrl = generateURI({
diff --git a/backend/apps/cloud/src/user/entities/user.entity.ts b/backend/apps/cloud/src/user/entities/user.entity.ts
index 1dda90654..3e1beaa3b 100644
--- a/backend/apps/cloud/src/user/entities/user.entity.ts
+++ b/backend/apps/cloud/src/user/entities/user.entity.ts
@@ -293,7 +293,7 @@ export class User {
@Column('varchar', { length: 32, nullable: true })
twoFactorAuthenticationSecret: string
- @Column('varchar', { length: 30, nullable: true })
+ @Column('varchar', { length: 60, nullable: true })
twoFactorRecoveryCode: string
@Column('int', { default: 50 })
diff --git a/backend/migrations/mysql/2026_03_26_2fa_recovery_code_length.sql b/backend/migrations/mysql/2026_03_26_2fa_recovery_code_length.sql
new file mode 100644
index 000000000..5abe3d2b8
--- /dev/null
+++ b/backend/migrations/mysql/2026_03_26_2fa_recovery_code_length.sql
@@ -0,0 +1 @@
+ALTER TABLE `user` MODIFY COLUMN `twoFactorRecoveryCode` varchar(60) DEFAULT NULL;