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 recovery codes

Blue Mouse17f5333fdc0f
diff --git a/backend/apps/community/src/twoFactorAuth/twoFactorAuth.controller.ts b/backend/apps/community/src/twoFactorAuth/twoFactorAuth.controller.ts
index bd706f9da..c36507795 100644
--- a/backend/apps/community/src/twoFactorAuth/twoFactorAuth.controller.ts
+++ b/backend/apps/community/src/twoFactorAuth/twoFactorAuth.controller.ts
@@ -101,10 +101,12 @@ export class TwoFactorAuthController {
}
const twoFactorRecoveryCode = generateRecoveryCode()
+ const hashedRecoveryCode =
+ await this.twoFactorAuthService.hashRecoveryCode(twoFactorRecoveryCode)
await this.userService.update(user.id, {
isTwoFactorAuthenticationEnabled: 1,
- twoFactorRecoveryCode,
+ twoFactorRecoveryCode: hashedRecoveryCode,
})
try {
@@ -144,9 +146,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,
@@ -199,9 +204,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/community/src/twoFactorAuth/twoFactorAuth.service.ts b/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts
index 15010a8b9..626e90df5 100644
--- a/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts
+++ b/backend/apps/community/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 '../common/types'
@@ -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({