From d2743f4121e89c869ab74f432c1877e64f858151 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 12:16:02 +0530 Subject: [PATCH] Unclass --- web/apps/auth/src/services/code.ts | 230 ++++++++++++++--------------- 1 file changed, 112 insertions(+), 118 deletions(-) diff --git a/web/apps/auth/src/services/code.ts b/web/apps/auth/src/services/code.ts index d61a2dcd6..af3408f20 100644 --- a/web/apps/auth/src/services/code.ts +++ b/web/apps/auth/src/services/code.ts @@ -47,136 +47,130 @@ export class Code { this.rawData = rawData; this.id = id; } +} - static fromRawData(id: string, rawData: string): Code { - let santizedRawData = rawData - .replace(/\+/g, "%2B") - .replace(/:/g, "%3A") - .replaceAll("\r", ""); - if (santizedRawData.startsWith('"')) { - santizedRawData = santizedRawData.substring(1); - } - if (santizedRawData.endsWith('"')) { - santizedRawData = santizedRawData.substring( - 0, - santizedRawData.length - 1, - ); - } - - const uriParams = {}; - const searchParamsString = - decodeURIComponent(santizedRawData).split("?")[1]; - searchParamsString.split("&").forEach((pair) => { - const [key, value] = pair.split("="); - uriParams[key] = value; - }); - - const uri = URI.parse(santizedRawData); - let uriPath = decodeURIComponent(uri.path); - if ( - uriPath.startsWith("/otpauth://") || - uriPath.startsWith("otpauth://") - ) { - uriPath = uriPath.split("otpauth://")[1]; - } else if (uriPath.startsWith("otpauth%3A//")) { - uriPath = uriPath.split("otpauth%3A//")[1]; - } - - return new Code( - Code._getAccount(uriPath), - Code._getIssuer(uriPath, uriParams), - Code._getDigits(uriParams), - Code._getPeriod(uriParams), - Code.getSanitizedSecret(uriParams), - Code._getAlgorithm(uriParams), - Code._getType(uriPath), - rawData, - id, +const codeFromRawData = (id: string, rawData: string): Code => { + let santizedRawData = rawData + .replace(/\+/g, "%2B") + .replace(/:/g, "%3A") + .replaceAll("\r", ""); + if (santizedRawData.startsWith('"')) { + santizedRawData = santizedRawData.substring(1); + } + if (santizedRawData.endsWith('"')) { + santizedRawData = santizedRawData.substring( + 0, + santizedRawData.length - 1, ); } - private static _getAccount(uriPath: string): string { - try { - const path = decodeURIComponent(uriPath); - if (path.includes(":")) { - return path.split(":")[1]; - } else if (path.includes("/")) { - return path.split("/")[1]; + const uriParams = {}; + const searchParamsString = + decodeURIComponent(santizedRawData).split("?")[1]; + searchParamsString.split("&").forEach((pair) => { + const [key, value] = pair.split("="); + uriParams[key] = value; + }); + + const uri = URI.parse(santizedRawData); + let uriPath = decodeURIComponent(uri.path); + if (uriPath.startsWith("/otpauth://") || uriPath.startsWith("otpauth://")) { + uriPath = uriPath.split("otpauth://")[1]; + } else if (uriPath.startsWith("otpauth%3A//")) { + uriPath = uriPath.split("otpauth%3A//")[1]; + } + + return new Code( + _getAccount(uriPath), + _getIssuer(uriPath, uriParams), + _getDigits(uriParams), + _getPeriod(uriParams), + getSanitizedSecret(uriParams), + _getAlgorithm(uriParams), + _getType(uriPath), + rawData, + id, + ); +}; + +const _getAccount = (uriPath: string): string => { + try { + const path = decodeURIComponent(uriPath); + if (path.includes(":")) { + return path.split(":")[1]; + } else if (path.includes("/")) { + return path.split("/")[1]; + } + } catch (e) { + return ""; + } +}; + +const _getIssuer = (uriPath: string, uriParams: { get?: any }): string => { + try { + if (uriParams["issuer"] !== undefined) { + let issuer = uriParams["issuer"]; + // This is to handle bug in the ente auth app + if (issuer.endsWith("period")) { + issuer = issuer.substring(0, issuer.length - 6); } - } catch (e) { - return ""; + return issuer; } - } - - private static _getIssuer( - uriPath: string, - uriParams: { get?: any }, - ): string { - try { - if (uriParams["issuer"] !== undefined) { - let issuer = uriParams["issuer"]; - // This is to handle bug in the ente auth app - if (issuer.endsWith("period")) { - issuer = issuer.substring(0, issuer.length - 6); - } - return issuer; - } - let path = decodeURIComponent(uriPath); - if (path.startsWith("totp/") || path.startsWith("hotp/")) { - path = path.substring(5); - } - if (path.includes(":")) { - return path.split(":")[0]; - } else if (path.includes("-")) { - return path.split("-")[0]; - } - return path; - } catch (e) { - return ""; + let path = decodeURIComponent(uriPath); + if (path.startsWith("totp/") || path.startsWith("hotp/")) { + path = path.substring(5); } - } - - private static _getDigits(uriParams): number { - try { - return parseInt(uriParams["digits"], 10) || Code.defaultDigits; - } catch (e) { - return Code.defaultDigits; + if (path.includes(":")) { + return path.split(":")[0]; + } else if (path.includes("-")) { + return path.split("-")[0]; } + return path; + } catch (e) { + return ""; } +}; - private static _getPeriod(uriParams): number { - try { - return parseInt(uriParams["period"], 10) || Code.defaultPeriod; - } catch (e) { - return Code.defaultPeriod; +const _getDigits = (uriParams): number => { + try { + return parseInt(uriParams["digits"], 10) || Code.defaultDigits; + } catch (e) { + return Code.defaultDigits; + } +}; + +const _getPeriod = (uriParams): number => { + try { + return parseInt(uriParams["period"], 10) || Code.defaultPeriod; + } catch (e) { + return Code.defaultPeriod; + } +}; + +const _getAlgorithm = (uriParams): AlgorithmType => { + try { + const algorithm = uriParams["algorithm"].toLowerCase(); + if (algorithm === "sha256") { + return algorithm; + } else if (algorithm === "sha512") { + return algorithm; } + } catch (e) { + // nothing } + return "sha1"; +}; - private static _getAlgorithm(uriParams): AlgorithmType { - try { - const algorithm = uriParams["algorithm"].toLowerCase(); - if (algorithm === "sha256") { - return algorithm; - } else if (algorithm === "sha512") { - return algorithm; - } - } catch (e) { - // nothing - } - return "sha1"; +const _getType = (uriPath: string): Type => { + const oauthType = uriPath.split("/")[0].substring(0); + if (oauthType.toLowerCase() === "totp") { + return "totp"; + } else if (oauthType.toLowerCase() === "hotp") { + return "hotp"; } + throw new Error(`Unsupported format with host ${oauthType}`); +}; - private static _getType(uriPath: string): Type { - const oauthType = uriPath.split("/")[0].substring(0); - if (oauthType.toLowerCase() === "totp") { - return "totp"; - } else if (oauthType.toLowerCase() === "hotp") { - return "hotp"; - } - throw new Error(`Unsupported format with host ${oauthType}`); - } - - static getSanitizedSecret(uriParams): string { - return uriParams["secret"].replace(/ /g, "").toUpperCase(); - } -} +const getSanitizedSecret = (uriParams): string => { + return uriParams["secret"].replace(/ /g, "").toUpperCase(); +};