fix: support for different otpauth url syntax

adds support for otpauth://totp? and otpauth://totp/
This commit is contained in:
Aetherinox 2024-04-06 09:38:36 -07:00
parent d0d163bf5e
commit 80370d128c
No known key found for this signature in database
GPG Key ID: CB5C4C30CD0D4028
2 changed files with 5 additions and 3 deletions

View File

@ -467,6 +467,7 @@ class EntryModel extends Model {
if (otpUrl.isProtected) {
otpUrl = otpUrl.getText();
}
// called only if secret provided, no formatted url
if (Otp.isSecret(otpUrl.replace(/\s/g, ''))) {
otpUrl = Otp.makeUrl(otpUrl.replace(/\s/g, '').toUpperCase());
} else if (otpUrl.toLowerCase().lastIndexOf('otpauth:', 0) !== 0) {

View File

@ -135,12 +135,12 @@ Otp.leftPad = function (str, len) {
};
Otp.parseUrl = function (url) {
const match = /^otpauth:\/\/(\w+)\/([^\?]+)\?(.*)/i.exec(url);
const match = /^otpauth:\/\/(\w+)(?:\/([^\?]+)\?|\?)(.*)/i.exec(url);
if (!match) {
throw 'Not OTP url';
}
const params = {};
const label = decodeURIComponent(match[2]);
const label = decodeURIComponent(match[2] ?? 'default');
if (label) {
const parts = label.split(':');
params.issuer = parts[0].trim();
@ -148,7 +148,8 @@ Otp.parseUrl = function (url) {
params.account = parts[1].trim();
}
}
params.type = match[1].toLowerCase();
params.type = match[1].toLowerCase(); // returns "totp"
// match[3] = secret=XXXXXXXXXXXXX&period=30&digits=6&algorithm=SHA1
match[3].split('&').forEach((part) => {
const parts = part.split('=', 2);
params[parts[0].toLowerCase()] = decodeURIComponent(parts[1]);