Skip to content

Commit fea1d0c

Browse files
committed
Fix OTP type
1 parent c34d1c1 commit fea1d0c

File tree

7 files changed

+53
-45
lines changed

7 files changed

+53
-45
lines changed

src/background.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async function getTotp(text: string, silent = false) {
119119
}
120120
} else {
121121
let uri = text.split("otpauth://")[1];
122-
let type = uri.substr(0, 4).toLowerCase();
122+
let type = uri.substr(0, 4).toLowerCase() as OTPType;
123123
uri = uri.substr(5);
124124
let label = uri.split("?")[0];
125125
const parameterPart = uri.split("?")[1];
@@ -193,15 +193,15 @@ async function getTotp(text: string, silent = false) {
193193
if (
194194
!/^[2-7a-z]+=*$/i.test(secret) &&
195195
/^[0-9a-f]+$/i.test(secret) &&
196-
type === "totp"
196+
type === OTPType.totp
197197
) {
198-
type = "hex";
198+
type = OTPType.hex;
199199
} else if (
200200
!/^[2-7a-z]+=*$/i.test(secret) &&
201201
/^[0-9a-f]+$/i.test(secret) &&
202-
type === "hotp"
202+
type === OTPType.hotp
203203
) {
204-
type = "hhex";
204+
type = OTPType.hhex;
205205
}
206206
const entryData: { [hash: string]: RawOTPStorage } = {};
207207
entryData[hash] = {

src/components/Popup/BackupPage.vue

+9-6
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,13 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: RawOTPStorage }) {
212212
? otpStorage.issuer + ":" + (otpStorage.account || "")
213213
: otpStorage.account || "";
214214
let type = "";
215-
if (otpStorage.type === "totp" || otpStorage.type === "hex") {
216-
type = "totp";
217-
} else if (otpStorage.type === "hotp" || otpStorage.type === "hhex") {
218-
type = "hotp";
215+
if (otpStorage.type === OTPType.totp || otpStorage.type === OTPType.hex) {
216+
type = OTPType.totp;
217+
} else if (
218+
otpStorage.type === OTPType.hotp ||
219+
otpStorage.type === OTPType.hhex
220+
) {
221+
type = OTPType.hotp;
219222
} else {
220223
continue;
221224
}
@@ -228,8 +231,8 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: RawOTPStorage }) {
228231
"?secret=" +
229232
otpStorage.secret +
230233
(otpStorage.issuer ? "&issuer=" + otpStorage.issuer : "") +
231-
(type === "hotp" ? "&counter=" + otpStorage.counter : "") +
232-
(type === "totp" && otpStorage.period
234+
(type === OTPType.hotp ? "&counter=" + otpStorage.counter : "") +
235+
(type === OTPType.totp && otpStorage.period
233236
? "&period=" + otpStorage.period
234237
: "") +
235238
(otpStorage.digits ? "&digits=" + otpStorage.digits : "") +

src/components/Popup/EntryComponent.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ function getQrUrl(entry: OTPEntry) {
264264
: entry.account;
265265
const type =
266266
entry.type === OTPType.hex
267-
? OTPType[OTPType.totp]
267+
? OTPType.totp
268268
: entry.type === OTPType.hhex
269-
? OTPType[OTPType.hotp]
270-
: OTPType[entry.type];
269+
? OTPType.hotp
270+
: entry.type;
271271
const otpauth =
272272
"otpauth://" +
273273
type +

src/definitions/otp.d.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
declare enum OTPType {
2+
totp = "totp",
3+
hotp = "hotp",
4+
battle = "battle",
5+
steam = "steam",
6+
hex = "hex",
7+
hhex = "hhex",
8+
}
9+
110
interface OTPEntryInterface {
2-
type: number; // OTPType
11+
type: OTPType;
312
index: number;
413
issuer: string;
514
secret: string | null;
@@ -42,7 +51,7 @@ interface RawOTPStorage {
4251
index: number;
4352
issuer?: string;
4453
secret: string;
45-
type: string;
54+
type: OTPType;
4655
counter?: number;
4756
period?: number;
4857
digits?: number;

src/import.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
211211
}
212212

213213
let uri = item.split("otpauth://")[1];
214-
let type = uri.substr(0, 4).toLowerCase();
214+
let type = uri.substr(0, 4).toLowerCase() as OTPType;
215215
uri = uri.substr(5);
216216
let label = uri.split("?")[0];
217217
const parameterPart = uri.split("?")[1];
@@ -282,15 +282,15 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
282282
if (
283283
!/^[2-7a-z]+=*$/i.test(secret) &&
284284
/^[0-9a-f]+$/i.test(secret) &&
285-
type === "totp"
285+
type === OTPType.totp
286286
) {
287-
type = "hex";
287+
type = OTPType.hex;
288288
} else if (
289289
!/^[2-7a-z]+=*$/i.test(secret) &&
290290
/^[0-9a-f]+$/i.test(secret) &&
291-
type === "hotp"
291+
type === OTPType.hotp
292292
) {
293-
type = "hhex";
293+
type = OTPType.hhex;
294294
}
295295

296296
exportData[hash] = {

src/models/otp.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { UserSettings } from "./settings";
33
import { EntryStorage } from "./storage";
44

55
export enum OTPType {
6-
totp = 1,
7-
hotp,
8-
battle,
9-
steam,
10-
hex,
11-
hhex,
6+
totp = "totp",
7+
hotp = "hotp",
8+
battle = "battle",
9+
steam = "steam",
10+
hex = "hex",
11+
hhex = "hhex",
1212
}
1313

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

229228
if (this.type !== OTPType.hotp && this.type !== OTPType.hhex) {
230229
this.generate();

src/models/storage.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class EntryStorage {
222222
encrypted,
223223
hash: entry.hash,
224224
index: entry.index,
225-
type: OTPType[entry.type],
225+
type: entry.type,
226226
secret,
227227
};
228228

@@ -392,10 +392,7 @@ export class EntryStorage {
392392
}
393393

394394
// remove unnecessary fields
395-
if (
396-
!(entry.type === OTPType[OTPType.hotp]) &&
397-
!(entry.type === OTPType[OTPType.hhex])
398-
) {
395+
if (!(entry.type === OTPType.hotp) && !(entry.type === OTPType.hhex)) {
399396
delete entry.counter;
400397
}
401398

@@ -478,7 +475,7 @@ export class EntryStorage {
478475
algorithm: OTPAlgorithm;
479476
pinned: boolean;
480477
} = {
481-
type: (parseInt(data[hash].type) as OTPType) || OTPType[OTPType.totp],
478+
type: data[hash].type || OTPType.totp,
482479
index: data[hash].index || 0,
483480
issuer: data[hash].issuer || "",
484481
account: data[hash].account || "",
@@ -617,29 +614,29 @@ export class EntryStorage {
617614
}
618615

619616
if (!entryData.type) {
620-
entryData.type = OTPType[OTPType.totp];
617+
entryData.type = OTPType.totp;
621618
}
622619

623620
let type: OTPType;
624621
switch (entryData.type) {
625-
case "totp":
626-
case "hotp":
627-
case "battle":
628-
case "steam":
629-
case "hex":
630-
case "hhex":
631-
type = OTPType[entryData.type];
622+
case OTPType.totp:
623+
case OTPType.hotp:
624+
case OTPType.battle:
625+
case OTPType.steam:
626+
case OTPType.hex:
627+
case OTPType.hhex:
628+
type = entryData.type;
632629
break;
633630
default:
634631
// we need correct the type here
635632
// and save it
636633
type = OTPType.totp;
637-
entryData.type = OTPType[OTPType.totp];
634+
entryData.type = OTPType.totp;
638635
}
639636

640637
let period: number | undefined;
641638
if (
642-
entryData.type === OTPType[OTPType.totp] &&
639+
entryData.type === OTPType.totp &&
643640
entryData.period &&
644641
entryData.period > 0
645642
) {

0 commit comments

Comments
 (0)