Skip to content

Commit

Permalink
Use OTPType enum everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
olfek committed Oct 5, 2024
1 parent 039aa31 commit f56defe
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getCurrentTab,
okToInjectContentScript,
} from "./utils";
import { CodeState } from "./models/otp";
import { CodeState, OTPType } from "./models/otp";

import { getOTPAuthPerLineFromOPTAuthMigration } from "./models/migration";
import { isChrome, isFirefox } from "./browser";
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[OTPType.totp]
) {
type = "hex";
type = OTPType[OTPType.hex];
} else if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "hotp"
type === OTPType[OTPType.hotp]
) {
type = "hhex";
type = OTPType[OTPType.hhex];
}
const entryData: { [hash: string]: RawOTPStorage } = {};
entryData[hash] = {
Expand Down
17 changes: 9 additions & 8 deletions src/components/Popup/BackupPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<script lang="ts">
import Vue from "vue";
import { isSafari } from "../../browser";
import { OTPType } from "../../models/otp";
export default Vue.extend({
data: function () {
Expand Down Expand Up @@ -176,8 +177,8 @@ export default Vue.extend({
function hasUnsupportedAccounts(exportData: { [h: string]: RawOTPStorage }) {
for (const entry of Object.keys(exportData)) {
if (
exportData[entry].type === "battle" ||
exportData[entry].type === "steam"
exportData[entry].type === OTPType[OTPType.battle] ||
exportData[entry].type === OTPType[OTPType.steam]
) {
return true;
}
Expand Down Expand Up @@ -212,10 +213,10 @@ 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[OTPType.totp] || otpStorage.type === OTPType[OTPType.hex]) {
type = OTPType[OTPType.totp];
} else if (otpStorage.type === OTPType[OTPType.hotp] || otpStorage.type === OTPType[OTPType.hhex]) {
type = OTPType[OTPType.hotp];
} else {
continue;
}
Expand All @@ -228,8 +229,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[OTPType.hotp] ? "&counter=" + otpStorage.counter : "") +
(type === OTPType[OTPType.totp] && otpStorage.period
? "&period=" + otpStorage.period
: "") +
(otpStorage.digits ? "&digits=" + otpStorage.digits : "") +
Expand Down
3 changes: 2 additions & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import jsQR from "jsqr";

// @ts-expect-error - injected by vue-svg-loader
import scanGIF from "../images/scan.gif";
import { OTPType } from "./models/otp";

if (!document.getElementById("__ga_grayLayout__")) {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
Expand Down Expand Up @@ -304,7 +305,7 @@ function pasteCode(code: string) {
"authenticator",
"factor",
"code",
"totp",
OTPType[OTPType.totp],
"twoFactorCode",
];
for (const inputBox of inputBoxes) {
Expand Down
9 changes: 5 additions & 4 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Encryption } from "./models/encryption";
import { EntryStorage } from "./models/storage";
import { getOTPAuthPerLineFromOPTAuthMigration } from "./models/migration";
import * as CryptoJS from "crypto-js";
import { OTPType } from "./models/otp";

async function init() {
// i18n
Expand Down Expand Up @@ -282,15 +283,15 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "totp"
type === OTPType[OTPType.totp]
) {
type = "hex";
type = OTPType[OTPType.hex];
} else if (
!/^[2-7a-z]+=*$/i.test(secret) &&
/^[0-9a-f]+$/i.test(secret) &&
type === "hotp"
type === OTPType[OTPType.hotp]
) {
type = "hhex";
type = OTPType[OTPType.hhex];
}

exportData[hash] = {
Expand Down
5 changes: 3 additions & 2 deletions src/models/migration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as CryptoJS from "crypto-js";
import { OTPType } from "./otp";

function byteArray2Base32(bytes: number[]) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
Expand Down Expand Up @@ -111,11 +112,11 @@ export function getOTPAuthPerLineFromOPTAuthMigration(migrationUri: string) {
byteData[isserStart + isserLength + 1]
];
const digits = [6, 6, 8][byteData[isserStart + isserLength + 3]];
const type = ["totp", "hotp", "totp"][
const type = [OTPType[OTPType.totp], OTPType[OTPType.hotp], OTPType[OTPType.totp]][
byteData[isserStart + isserLength + 5]
];
let line = `otpauth://${type}/${account}?secret=${secret}&issuer=${issuer}&algorithm=${algorithm}&digits=${digits}`;
if (type === "hotp") {
if (type === OTPType[OTPType.hotp]) {
let counter = 1;
if (isserStart + isserLength + 7 <= lineLength) {
counter = byteData[isserStart + isserLength + 7];
Expand Down
12 changes: 6 additions & 6 deletions src/models/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,12 @@ export class EntryStorage {

let type: OTPType;
switch (entryData.type) {
case "totp":
case "hotp":
case "battle":
case "steam":
case "hex":
case "hhex":
case OTPType[OTPType.totp]:
case OTPType[OTPType.hotp]:
case OTPType[OTPType.battle]:
case OTPType[OTPType.steam]:
case OTPType[OTPType.hex]:
case OTPType[OTPType.hhex]:
type = OTPType[entryData.type];
break;
default:
Expand Down

0 comments on commit f56defe

Please sign in to comment.