Skip to content

Commit

Permalink
Fix OTP type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneezry committed Aug 26, 2024
1 parent c34d1c1 commit fea1d0c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function getTotp(text: string, silent = false) {
}
} else {
let uri = text.split("otpauth://")[1];
let type = uri.substr(0, 4).toLowerCase();
let type = uri.substr(0, 4).toLowerCase() as OTPType;
uri = uri.substr(5);
let label = uri.split("?")[0];
const parameterPart = uri.split("?")[1];
Expand Down Expand Up @@ -193,15 +193,15 @@ async function getTotp(text: string, silent = false) {
if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "totp"
type === OTPType.totp
) {
type = "hex";
type = OTPType.hex;
} else if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "hotp"
type === OTPType.hotp
) {
type = "hhex";
type = OTPType.hhex;
}
const entryData: { [hash: string]: RawOTPStorage } = {};
entryData[hash] = {
Expand Down
15 changes: 9 additions & 6 deletions src/components/Popup/BackupPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,13 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: RawOTPStorage }) {
? otpStorage.issuer + ":" + (otpStorage.account || "")
: otpStorage.account || "";
let type = "";
if (otpStorage.type === "totp" || otpStorage.type === "hex") {
type = "totp";
} else if (otpStorage.type === "hotp" || otpStorage.type === "hhex") {
type = "hotp";
if (otpStorage.type === OTPType.totp || otpStorage.type === OTPType.hex) {
type = OTPType.totp;
} else if (
otpStorage.type === OTPType.hotp ||
otpStorage.type === OTPType.hhex
) {
type = OTPType.hotp;
} else {
continue;
}
Expand All @@ -228,8 +231,8 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: RawOTPStorage }) {
"?secret=" +
otpStorage.secret +
(otpStorage.issuer ? "&issuer=" + otpStorage.issuer : "") +
(type === "hotp" ? "&counter=" + otpStorage.counter : "") +
(type === "totp" && otpStorage.period
(type === OTPType.hotp ? "&counter=" + otpStorage.counter : "") +
(type === OTPType.totp && otpStorage.period
? "&period=" + otpStorage.period
: "") +
(otpStorage.digits ? "&digits=" + otpStorage.digits : "") +
Expand Down
6 changes: 3 additions & 3 deletions src/components/Popup/EntryComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ function getQrUrl(entry: OTPEntry) {
: entry.account;
const type =
entry.type === OTPType.hex
? OTPType[OTPType.totp]
? OTPType.totp
: entry.type === OTPType.hhex
? OTPType[OTPType.hotp]
: OTPType[entry.type];
? OTPType.hotp
: entry.type;
const otpauth =
"otpauth://" +
type +
Expand Down
13 changes: 11 additions & 2 deletions src/definitions/otp.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
declare enum OTPType {
totp = "totp",
hotp = "hotp",
battle = "battle",
steam = "steam",
hex = "hex",
hhex = "hhex",
}

interface OTPEntryInterface {
type: number; // OTPType
type: OTPType;
index: number;
issuer: string;
secret: string | null;
Expand Down Expand Up @@ -42,7 +51,7 @@ interface RawOTPStorage {
index: number;
issuer?: string;
secret: string;
type: string;
type: OTPType;
counter?: number;
period?: number;
digits?: number;
Expand Down
10 changes: 5 additions & 5 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
}

let uri = item.split("otpauth://")[1];
let type = uri.substr(0, 4).toLowerCase();
let type = uri.substr(0, 4).toLowerCase() as OTPType;
uri = uri.substr(5);
let label = uri.split("?")[0];
const parameterPart = uri.split("?")[1];
Expand Down Expand Up @@ -282,15 +282,15 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "totp"
type === OTPType.totp
) {
type = "hex";
type = OTPType.hex;
} else if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "hotp"
type === OTPType.hotp
) {
type = "hhex";
type = OTPType.hhex;
}

exportData[hash] = {
Expand Down
15 changes: 7 additions & 8 deletions src/models/otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { UserSettings } from "./settings";
import { EntryStorage } from "./storage";

export enum OTPType {
totp = 1,
hotp,
battle,
steam,
hex,
hhex,
totp = "totp",
hotp = "hotp",
battle = "battle",
steam = "steam",
hex = "hex",
hhex = "hhex",
}

export enum CodeState {
Expand Down Expand Up @@ -223,8 +223,7 @@ export class OTPEntry implements OTPEntryInterface {
this.period = decryptedData.period || 30;
this.pinned = decryptedData.pinned || false;
this.secret = decryptedData.secret;
// @ts-expect-error need a better way to do this
this.type = OTPType[decryptedData.type] || OTPType.totp;
this.type = decryptedData.type || OTPType.totp;

if (this.type !== OTPType.hotp && this.type !== OTPType.hhex) {
this.generate();
Expand Down
29 changes: 13 additions & 16 deletions src/models/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class EntryStorage {
encrypted,
hash: entry.hash,
index: entry.index,
type: OTPType[entry.type],
type: entry.type,
secret,
};

Expand Down Expand Up @@ -392,10 +392,7 @@ export class EntryStorage {
}

// remove unnecessary fields
if (
!(entry.type === OTPType[OTPType.hotp]) &&
!(entry.type === OTPType[OTPType.hhex])
) {
if (!(entry.type === OTPType.hotp) && !(entry.type === OTPType.hhex)) {
delete entry.counter;
}

Expand Down Expand Up @@ -478,7 +475,7 @@ export class EntryStorage {
algorithm: OTPAlgorithm;
pinned: boolean;
} = {
type: (parseInt(data[hash].type) as OTPType) || OTPType[OTPType.totp],
type: data[hash].type || OTPType.totp,
index: data[hash].index || 0,
issuer: data[hash].issuer || "",
account: data[hash].account || "",
Expand Down Expand Up @@ -617,29 +614,29 @@ export class EntryStorage {
}

if (!entryData.type) {
entryData.type = OTPType[OTPType.totp];
entryData.type = OTPType.totp;
}

let type: OTPType;
switch (entryData.type) {
case "totp":
case "hotp":
case "battle":
case "steam":
case "hex":
case "hhex":
type = OTPType[entryData.type];
case OTPType.totp:
case OTPType.hotp:
case OTPType.battle:
case OTPType.steam:
case OTPType.hex:
case OTPType.hhex:
type = entryData.type;
break;
default:
// we need correct the type here
// and save it
type = OTPType.totp;
entryData.type = OTPType[OTPType.totp];
entryData.type = OTPType.totp;
}

let period: number | undefined;
if (
entryData.type === OTPType[OTPType.totp] &&
entryData.type === OTPType.totp &&
entryData.period &&
entryData.period > 0
) {
Expand Down

0 comments on commit fea1d0c

Please sign in to comment.