Skip to content

Commit 8e42007

Browse files
committed
add digits support
1 parent 71357a5 commit 8e42007

File tree

9 files changed

+65
-14
lines changed

9 files changed

+65
-14
lines changed

_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,8 @@
321321
},
322322
"invalid": {
323323
"message": "Invalid"
324+
},
325+
"digits": {
326+
"message": "Digits"
324327
}
325328
}

src/background.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ async function getTotp(text: string) {
126126
if (!label || !parameterPart) {
127127
chrome.tabs.sendMessage(id, { action: "errorqr" });
128128
} else {
129-
let account = "";
130129
let secret = "";
131-
let issuer = "";
132-
let period: number | undefined = undefined;
130+
let account: string | undefined;
131+
let issuer: string | undefined;
132+
let period: number | undefined;
133+
let digits: number | undefined;
133134

134135
try {
135136
label = decodeURIComponent(label);
@@ -163,6 +164,9 @@ async function getTotp(text: string) {
163164
isNaN(period) || period < 0 || period > 60 || 60 % period !== 0
164165
? undefined
165166
: period;
167+
} else if (parameter[0].toLowerCase() === "digits") {
168+
digits = Number(parameter[1]);
169+
digits = isNaN(digits) ? 6 : digits;
166170
}
167171
});
168172

@@ -203,6 +207,9 @@ async function getTotp(text: string) {
203207
if (period) {
204208
entryData[hash].period = period;
205209
}
210+
if (digits) {
211+
entryData[hash].digits = digits;
212+
}
206213
if (
207214
(await EntryStorage.hasEncryptedEntry()) !==
208215
encryption.getEncryptionStatus()

src/components/Popup/AddAccountPage.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
v-model.number="newAccount.period"
1717
v-bind:disabled="newAccount.type === OTPType.hotp"
1818
/>
19+
<label class="combo-label">{{ i18n.digits }}</label>
20+
<select v-model="newAccount.digits">
21+
<option v-bind:value="6">6</option>
22+
<option v-bind:value="8">8</option>
23+
</select>
24+
<br>
1925
<label class="combo-label">{{ i18n.type }}</label>
2026
<select v-model="newAccount.type">
2127
<option v-bind:value="OTPType.totp">{{ i18n.based_on_time }}</option>
@@ -40,6 +46,7 @@ export default Vue.extend({
4046
secret: string;
4147
type: OTPType;
4248
period: number | undefined;
49+
digits: number;
4350
};
4451
} {
4552
return {
@@ -48,7 +55,8 @@ export default Vue.extend({
4855
account: "",
4956
secret: "",
5057
type: OTPType.totp,
51-
period: undefined
58+
period: undefined,
59+
digits: 6
5260
}
5361
};
5462
},
@@ -104,7 +112,8 @@ export default Vue.extend({
104112
encrypted: false,
105113
secret: this.newAccount.secret,
106114
counter: 0,
107-
period: this.newAccount.period
115+
period: this.newAccount.period,
116+
digits: this.newAccount.digits
108117
},
109118
this.$store.state.accounts.encryption
110119
);

src/components/Popup/ExportPage.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: OTPStorage }) {
106106
otpStorage.account = removeUnsafeData(otpStorage.account);
107107
}
108108
const label = otpStorage.issuer
109-
? otpStorage.issuer + ":" + otpStorage.account
110-
: otpStorage.account;
109+
? otpStorage.issuer + ":" + (otpStorage.account || "")
110+
: otpStorage.account || "";
111111
let type = "";
112112
if (otpStorage.type === "totp" || otpStorage.type === "hex") {
113113
type = "totp";
@@ -128,7 +128,8 @@ function getOneLineOtpBackupFile(entryData: { [hash: string]: OTPStorage }) {
128128
(type === "hotp" ? "&counter=" + otpStorage.counter : "") +
129129
(type === "totp" && otpStorage.period
130130
? "&period=" + otpStorage.period
131-
: "");
131+
: "") +
132+
(otpStorage.digits ? "&digits=" + otpStorage.digits : "");
132133
133134
otpAuthLines.push(otpAuthLine);
134135
}

src/definitions/otp.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface IOTPEntry {
99
counter: number;
1010
code: string;
1111
period: number;
12+
digits: number;
1213
create(): Promise<void>;
1314
update(): Promise<void>;
1415
next(): Promise<void>;
@@ -36,4 +37,5 @@ interface OTPStorage {
3637
type: string;
3738
counter?: number;
3839
period?: number;
40+
digits?: number;
3941
}

src/import.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
108108
let account: string | undefined;
109109
let issuer: string | undefined;
110110
let period: number | undefined;
111+
let digits: number | undefined;
111112

112113
try {
113114
label = decodeURIComponent(label);
@@ -141,6 +142,9 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
141142
isNaN(period) || period < 0 || period > 60 || 60 % period !== 0
142143
? undefined
143144
: period;
145+
} else if (parameter[0].toLowerCase() === "digits") {
146+
digits = Number(parameter[1]);
147+
digits = isNaN(digits) ? 6 : digits;
144148
}
145149
});
146150

@@ -180,6 +184,9 @@ export async function getEntryDataFromOTPAuthPerLine(importCode: string) {
180184
if (period) {
181185
exportData[hash].period = period;
182186
}
187+
if (digits) {
188+
exportData[hash].digits = digits;
189+
}
183190
}
184191
}
185192
}

src/models/key-utilities.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ export class KeyUtilities {
9595
type: OTPType,
9696
secret: string,
9797
counter: number,
98-
period: number
98+
period: number,
99+
len?: number
99100
) {
100101
secret = secret.replace(/\s/g, "");
101-
let len = 6;
102+
if (!len) {
103+
len = 6;
104+
}
102105
let b26 = false;
103106
let key: string;
104107
switch (type) {

src/models/otp.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class OTPEntry implements IOTPEntry {
2727
hash: string;
2828
counter: number;
2929
period: number;
30+
digits: number;
3031
code = "&bull;&bull;&bull;&bull;&bull;&bull;";
3132

3233
constructor(
@@ -40,6 +41,7 @@ export class OTPEntry implements IOTPEntry {
4041
counter?: number;
4142
period?: number;
4243
hash?: string;
44+
digits?: number;
4345
},
4446
encryption?: Encryption
4547
) {
@@ -65,7 +67,6 @@ export class OTPEntry implements IOTPEntry {
6567
this.encSecret = encryption.getEncryptedString(this.secret);
6668
}
6769
}
68-
6970
if (entry.hash) {
7071
this.hash = entry.hash;
7172
} else {
@@ -76,6 +77,11 @@ export class OTPEntry implements IOTPEntry {
7677
} else {
7778
this.counter = 0;
7879
}
80+
if (entry.digits) {
81+
this.digits = entry.digits;
82+
} else {
83+
this.digits = 6;
84+
}
7985
if (this.type === OTPType.totp && entry.period) {
8086
this.period = entry.period;
8187
} else {
@@ -155,7 +161,8 @@ export class OTPEntry implements IOTPEntry {
155161
this.type,
156162
this.secret,
157163
this.counter,
158-
this.period
164+
this.period,
165+
this.digits
159166
);
160167
} catch (error) {
161168
this.code = CodeState.Invalid;

src/models/storage.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ export class EntryStorage {
185185
storageItem.account = entry.account;
186186
}
187187

188+
if (entry.digits && entry.digits !== 6) {
189+
storageItem.digits = entry.digits;
190+
}
191+
188192
return storageItem;
189193
}
190194

@@ -348,6 +352,7 @@ export class EntryStorage {
348352
data[hash].issuer = data[hash].issuer || "";
349353
data[hash].type = data[hash].type || OTPType[OTPType.totp];
350354
data[hash].counter = data[hash].counter || 0;
355+
data[hash].digits = data[hash].digits || 6;
351356
const period = data[hash].period;
352357
if (
353358
data[hash].type !== OTPType[OTPType.totp] ||
@@ -356,6 +361,12 @@ export class EntryStorage {
356361
delete data[hash].period;
357362
}
358363

364+
// If invalid digits, then use defualt.
365+
const digits = data[hash].digits;
366+
if (digits && (digits > 10 || digits < 1)) {
367+
data[hash].digits = 6;
368+
}
369+
359370
if (/^(blz\-|bliz\-)/.test(data[hash].secret)) {
360371
const secretMatches = data[hash].secret.match(
361372
/^(blz\-|bliz\-)(.*)/
@@ -521,7 +532,7 @@ export class EntryStorage {
521532
entryData.type = OTPType[OTPType.totp];
522533
}
523534

524-
let period = 30;
535+
let period: number | undefined;
525536
if (
526537
entryData.type === OTPType[OTPType.totp] &&
527538
entryData.period &&
@@ -539,7 +550,8 @@ export class EntryStorage {
539550
secret: entryData.secret,
540551
type,
541552
counter: entryData.counter,
542-
period
553+
period,
554+
digits: entryData.digits
543555
});
544556

545557
data.push(entry);

0 commit comments

Comments
 (0)