This commit is contained in:
Manav Rathi 2024-05-23 12:16:02 +05:30
parent 2504046e26
commit d2743f4121
No known key found for this signature in database

View file

@ -47,8 +47,9 @@ export class Code {
this.rawData = rawData;
this.id = id;
}
}
static fromRawData(id: string, rawData: string): Code {
const codeFromRawData = (id: string, rawData: string): Code => {
let santizedRawData = rawData
.replace(/\+/g, "%2B")
.replace(/:/g, "%3A")
@ -73,29 +74,26 @@ export class Code {
const uri = URI.parse(santizedRawData);
let uriPath = decodeURIComponent(uri.path);
if (
uriPath.startsWith("/otpauth://") ||
uriPath.startsWith("otpauth://")
) {
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),
_getAccount(uriPath),
_getIssuer(uriPath, uriParams),
_getDigits(uriParams),
_getPeriod(uriParams),
getSanitizedSecret(uriParams),
_getAlgorithm(uriParams),
_getType(uriPath),
rawData,
id,
);
}
};
private static _getAccount(uriPath: string): string {
const _getAccount = (uriPath: string): string => {
try {
const path = decodeURIComponent(uriPath);
if (path.includes(":")) {
@ -106,12 +104,9 @@ export class Code {
} catch (e) {
return "";
}
}
};
private static _getIssuer(
uriPath: string,
uriParams: { get?: any },
): string {
const _getIssuer = (uriPath: string, uriParams: { get?: any }): string => {
try {
if (uriParams["issuer"] !== undefined) {
let issuer = uriParams["issuer"];
@ -134,25 +129,25 @@ export class Code {
} catch (e) {
return "";
}
}
};
private static _getDigits(uriParams): number {
const _getDigits = (uriParams): number => {
try {
return parseInt(uriParams["digits"], 10) || Code.defaultDigits;
} catch (e) {
return Code.defaultDigits;
}
}
};
private static _getPeriod(uriParams): number {
const _getPeriod = (uriParams): number => {
try {
return parseInt(uriParams["period"], 10) || Code.defaultPeriod;
} catch (e) {
return Code.defaultPeriod;
}
}
};
private static _getAlgorithm(uriParams): AlgorithmType {
const _getAlgorithm = (uriParams): AlgorithmType => {
try {
const algorithm = uriParams["algorithm"].toLowerCase();
if (algorithm === "sha256") {
@ -164,9 +159,9 @@ export class Code {
// nothing
}
return "sha1";
}
};
private static _getType(uriPath: string): Type {
const _getType = (uriPath: string): Type => {
const oauthType = uriPath.split("/")[0].substring(0);
if (oauthType.toLowerCase() === "totp") {
return "totp";
@ -174,9 +169,8 @@ export class Code {
return "hotp";
}
throw new Error(`Unsupported format with host ${oauthType}`);
}
};
static getSanitizedSecret(uriParams): string {
const getSanitizedSecret = (uriParams): string => {
return uriParams["secret"].replace(/ /g, "").toUpperCase();
}
}
};