From 51568e6c56162606b55ead2d39db1851ba707af1 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 12:20:04 +0530 Subject: [PATCH] non optional --- web/apps/auth/src/pages/auth.tsx | 2 +- web/apps/auth/src/services/code.ts | 13 ++++++------- web/apps/auth/src/services/remote.ts | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx index c8e5166cf..a76b687fc 100644 --- a/web/apps/auth/src/pages/auth.tsx +++ b/web/apps/auth/src/pages/auth.tsx @@ -326,7 +326,7 @@ const OTPDisplay: React.FC = ({ codeInfo }) => { secret: codeInfo.secret, algorithm: codeInfo.algorithm ?? Code.defaultAlgo, period: codeInfo.period ?? Code.defaultPeriod, - digits: codeInfo.digits ?? Code.defaultDigits, + digits: codeInfo.digits, }); setCode(totp.generate()); setNextCode( diff --git a/web/apps/auth/src/services/code.ts b/web/apps/auth/src/services/code.ts index af3408f20..e7afe691e 100644 --- a/web/apps/auth/src/services/code.ts +++ b/web/apps/auth/src/services/code.ts @@ -11,7 +11,6 @@ type AlgorithmType = | "SHA512"; export class Code { - static readonly defaultDigits = 6; static readonly defaultAlgo = "sha1"; static readonly defaultPeriod = 30; @@ -19,7 +18,7 @@ export class Code { id?: String; account: string; issuer: string; - digits?: number; + digits: number; period: number; secret: string; algorithm: AlgorithmType; @@ -49,7 +48,7 @@ export class Code { } } -const codeFromRawData = (id: string, rawData: string): Code => { +export const codeFromRawData = (id: string, rawData: string): Code => { let santizedRawData = rawData .replace(/\+/g, "%2B") .replace(/:/g, "%3A") @@ -83,7 +82,7 @@ const codeFromRawData = (id: string, rawData: string): Code => { return new Code( _getAccount(uriPath), _getIssuer(uriPath, uriParams), - _getDigits(uriParams), + _getDigits(uriParams) ?? 6, _getPeriod(uriParams), getSanitizedSecret(uriParams), _getAlgorithm(uriParams), @@ -131,11 +130,11 @@ const _getIssuer = (uriPath: string, uriParams: { get?: any }): string => { } }; -const _getDigits = (uriParams): number => { +const _getDigits = (uriParams): number | undefined => { try { - return parseInt(uriParams["digits"], 10) || Code.defaultDigits; + return parseInt(uriParams["digits"], 10); } catch (e) { - return Code.defaultDigits; + return undefined; } }; diff --git a/web/apps/auth/src/services/remote.ts b/web/apps/auth/src/services/remote.ts index 633d5f9d0..325aff2d7 100644 --- a/web/apps/auth/src/services/remote.ts +++ b/web/apps/auth/src/services/remote.ts @@ -6,7 +6,7 @@ import { getEndpoint } from "@ente/shared/network/api"; import { getToken } from "@ente/shared/storage/localStorage/helpers"; import { getActualKey } from "@ente/shared/user"; import { HttpStatusCode } from "axios"; -import { Code } from "services/code"; +import { Code, codeFromRawData } from "services/code"; const ENDPOINT = getEndpoint(); @@ -33,7 +33,7 @@ export const getAuthCodes = async (): Promise => { entity.header, authenticatorKey, ); - return Code.fromRawData(entity.id, decryptedCode); + return codeFromRawData(entity.id, decryptedCode); } catch (e) { log.error(`failed to parse codeId = ${entity.id}`); return null;