-
Notifications
You must be signed in to change notification settings - Fork 815
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 #1283
base: dev
Are you sure you want to change the base?
Fix OTP type #1283
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -117,7 +117,30 @@ export class OTPEntry implements OTPEntryInterface { | |
this.secret = entry.secret; | ||
} | ||
|
||
this.type = entry.type; | ||
// We may have already had some error OTP type entries in the storage | ||
// totp = 1 | ||
// hotp = 2 | ||
// battle = 3 | ||
// steam = 4 | ||
// hex = 5 | ||
// hhex = 6 | ||
|
||
if ((entry.type as unknown) === 1) { | ||
this.type = OTPType.totp; | ||
} else if ((entry.type as unknown) === 2) { | ||
this.type = OTPType.hotp; | ||
} else if ((entry.type as unknown) === 3) { | ||
this.type = OTPType.battle; | ||
} else if ((entry.type as unknown) === 4) { | ||
this.type = OTPType.steam; | ||
} else if ((entry.type as unknown) === 5) { | ||
this.type = OTPType.hex; | ||
} else if ((entry.type as unknown) === 6) { | ||
this.type = OTPType.hhex; | ||
} else { | ||
this.type = entry.type; | ||
} | ||
|
||
if (entry.issuer) { | ||
this.issuer = entry.issuer; | ||
} else { | ||
|
@@ -223,8 +246,31 @@ 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; | ||
|
||
// We may have already had some error OTP type entries in the storage | ||
// totp = 1 | ||
// hotp = 2 | ||
// battle = 3 | ||
// steam = 4 | ||
// hex = 5 | ||
// hhex = 6 | ||
|
||
if ((decryptedData.type as unknown) === 1) { | ||
This comment was marked as outdated.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (2) Error OTP type entries would only ever be |
||
this.type = OTPType.totp; | ||
} else if ((decryptedData.type as unknown) === 2) { | ||
this.type = OTPType.hotp; | ||
} else if ((decryptedData.type as unknown) === 3) { | ||
this.type = OTPType.battle; | ||
} else if ((decryptedData.type as unknown) === 4) { | ||
this.type = OTPType.steam; | ||
} else if ((decryptedData.type as unknown) === 5) { | ||
this.type = OTPType.hex; | ||
} else if ((decryptedData.type as unknown) === 6) { | ||
this.type = OTPType.hhex; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is causing the bug. This if statement is not considering if Recommend making a type guard for |
||
this.type = OTPType.totp; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (2) I recommend keeping numeric enums where you get the benefit of reverse mappings so you don't have to do this ugly thing. |
||
if (this.type !== OTPType.hotp && this.type !== OTPType.hhex) { | ||
this.generate(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,7 +222,7 @@ export class EntryStorage { | |
encrypted, | ||
hash: entry.hash, | ||
index: entry.index, | ||
type: OTPType[entry.type], | ||
type: entry.type, | ||
secret, | ||
}; | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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 || "", | ||
|
@@ -617,29 +614,53 @@ export class EntryStorage { | |
} | ||
|
||
if (!entryData.type) { | ||
entryData.type = OTPType[OTPType.totp]; | ||
entryData.type = OTPType.totp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move this into the case statement or otherwise refactor so we don't have to cast to unknown below. we can set |
||
} | ||
|
||
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]; | ||
|
||
// We may have already had some error OTP type entries in the storage | ||
// totp = 1 | ||
// hotp = 2 | ||
// battle = 3 | ||
// steam = 4 | ||
// hex = 5 | ||
// hhex = 6 | ||
|
||
if ((entryData.type as unknown) === 1) { | ||
type = OTPType.totp; | ||
} else if ((entryData.type as unknown) === 2) { | ||
type = OTPType.hotp; | ||
} else if ((entryData.type as unknown) === 3) { | ||
type = OTPType.battle; | ||
} else if ((entryData.type as unknown) === 4) { | ||
type = OTPType.steam; | ||
} else if ((entryData.type as unknown) === 5) { | ||
type = OTPType.hex; | ||
} else if ((entryData.type as unknown) === 6) { | ||
type = OTPType.hhex; | ||
} else { | ||
type = OTPType.totp; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend keeping numeric enums where you get the benefit of reverse mappings so you don't have to do this ugly thing. |
||
|
||
entryData.type = type; | ||
} | ||
|
||
let period: number | undefined; | ||
if ( | ||
entryData.type === OTPType[OTPType.totp] && | ||
entryData.type === OTPType.totp && | ||
entryData.period && | ||
entryData.period > 0 | ||
) { | ||
|
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error OTP type entries would only ever be
1
, there is no need to check the other numbers.See my comment below