Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] OTP type is ignored #1320

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.disabled
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
23 changes: 15 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,16 @@ 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 +235,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
11 changes: 7 additions & 4 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,13 @@ export function getOTPAuthPerLineFromOPTAuthMigration(migrationUri: string) {
byteData[isserStart + isserLength + 1]
];
const digits = [6, 6, 8][byteData[isserStart + isserLength + 3]];
const type = ["totp", "hotp", "totp"][
byteData[isserStart + isserLength + 5]
];
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
7 changes: 4 additions & 3 deletions src/models/otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { UserSettings } from "./settings";
import { EntryStorage } from "./storage";

export enum OTPType {
totp = 1,
PLACEHOLDER_DO_NOT_USE, // https://github.com/Authenticator-Extension/Authenticator/pull/1283#issuecomment-2382842440
totp,
hotp,
battle,
steam,
Expand Down Expand Up @@ -223,8 +224,8 @@ 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 =
OTPType[decryptedData.type as keyof typeof OTPType] || OTPType.totp;

if (this.type !== OTPType.hotp && this.type !== OTPType.hhex) {
this.generate();
Expand Down
16 changes: 8 additions & 8 deletions src/models/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export class EntryStorage {
algorithm: OTPAlgorithm;
pinned: boolean;
} = {
type: (parseInt(data[hash].type) as OTPType) || OTPType[OTPType.totp],
type: OTPType[data[hash].type as keyof typeof OTPType] || OTPType.totp,
index: data[hash].index || 0,
issuer: data[hash].issuer || "",
account: data[hash].account || "",
Expand Down Expand Up @@ -622,13 +622,13 @@ export class EntryStorage {

let type: OTPType;
switch (entryData.type) {
case "totp":
case "hotp":
case "battle":
case "steam":
case "hex":
case "hhex":
type = OTPType[entryData.type];
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.hhex;
break;
default:
// we need correct the type here
Expand Down