Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(backend:sync): add support for TOTP recovery codes during client registration

johaven3cb3ea41eefe
diff --git a/backend/src/applications/sync/services/sync-clients-manager.service.ts b/backend/src/applications/sync/services/sync-clients-manager.service.ts
index 7045eb25..37e7b338 100644
--- a/backend/src/applications/sync/services/sync-clients-manager.service.ts
+++ b/backend/src/applications/sync/services/sync-clients-manager.service.ts
@@ -59,15 +59,20 @@ export class SyncClientsManager {
throw new HttpException('Missing permission', HttpStatus.FORBIDDEN)
}
if (configuration.auth.mfa.totp.enabled && user.twoFaEnabled) {
+ // Checking TOTP code and recovery code
if (!clientRegistrationDto.code) {
this.logger.warn(`${this.register.name} - missing two-fa code for user *${user.login}* (${user.id})`)
throw new HttpException('Missing TWO-FA code', HttpStatus.UNAUTHORIZED)
}
- const auth = this.authMethod2Fa.validateTwoFactorCode(clientRegistrationDto.code, user.secrets.twoFaSecret)
- if (!auth.success) {
- this.logger.warn(`${this.register.name} - wrong two-fa code for user *${user.login}* (${user.id})`)
- this.usersManager.updateAccesses(user, ip, false).catch((e: Error) => this.logger.error(`${this.register.name} - ${e}`))
- throw new HttpException(auth.message, HttpStatus.UNAUTHORIZED)
+ const authCode = this.authMethod2Fa.validateTwoFactorCode(clientRegistrationDto.code, user.secrets.twoFaSecret)
+ if (!authCode.success) {
+ this.logger.warn(`${this.register.name} - two-fa code for *${user.login}* (${user.id}) - ${authCode.message}`)
+ const authRCode = await this.authMethod2Fa.validateRecoveryCode(user.id, clientRegistrationDto.code, user.secrets.recoveryCodes)
+ if (!authRCode.success) {
+ this.logger.warn(`${this.register.name} - two-fa recovery code for *${user.login}* (${user.id}) - ${authRCode.message}`)
+ this.usersManager.updateAccesses(user, ip, false).catch((e: Error) => this.logger.error(`${this.register.name} - ${e}`))
+ throw new HttpException(authCode.message, HttpStatus.UNAUTHORIZED)
+ }
}
}
try {
diff --git a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
index b3568581..75181f1e 100644
--- a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
+++ b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
@@ -143,30 +143,31 @@ export class AuthProvider2FA {
return auth
}
- private async validateRecoveryCode(userId: number, code: string, encryptedCodes: string[]): Promise<TwoFaVerifyResult> {
+ async validateRecoveryCode(userId: number, code: string, encryptedCodes: string[]): Promise<TwoFaVerifyResult> {
const auth: TwoFaVerifyResult = { success: false, message: '' }
if (!encryptedCodes || encryptedCodes.length === 0) {
auth.message = 'Invalid code'
- } else {
- try {
- for (const encCode of encryptedCodes) {
- if (code === this.decryptSecret(encCode)) {
- auth.success = true
- // removed used code
- encryptedCodes.splice(encryptedCodes.indexOf(encCode), 1)
- break
- }
- }
- if (auth.success) {
- // update recovery codes
- await this.usersManager.updateSecrets(userId, { recoveryCodes: encryptedCodes })
- } else {
- auth.message = 'Invalid code'
+ return auth
+ }
+ try {
+ for (const encCode of encryptedCodes) {
+ const decryptedCode = this.decryptSecret(encCode)
+ if (code === decryptedCode) {
+ auth.success = true
+ // removed used code
+ encryptedCodes.splice(encryptedCodes.indexOf(encCode), 1)
+ break
}
- } catch (e) {
- this.logger.error(`${this.validateRecoveryCode.name} - ${e}`)
- auth.message = e.message
}
+ if (auth.success) {
+ // update recovery codes
+ await this.usersManager.updateSecrets(userId, { recoveryCodes: encryptedCodes })
+ } else {
+ auth.message = 'Invalid code'
+ }
+ } catch (e) {
+ this.logger.error(`${this.validateRecoveryCode.name} - ${e}`)
+ auth.message = e.message
}
return auth
}
diff --git a/frontend/src/app/auth/auth.interface.ts b/frontend/src/app/auth/auth.interface.ts
index add99280..5001bdb7 100644
--- a/frontend/src/app/auth/auth.interface.ts
+++ b/frontend/src/app/auth/auth.interface.ts
@@ -4,6 +4,12 @@
* See the LICENSE file for licensing details
*/
+export interface AuthResult {
+ success: boolean
+ message: any
+ twoFaEnabled?: boolean
+}
+
export interface AuthOIDCQueryParams {
oidc: string
access_expiration: string