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 enableOTP handling to match docs: prioritize store, then extension config #308

Merged
merged 2 commits into from
Nov 22, 2022
Merged
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
29 changes: 5 additions & 24 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ async function fillFields(settings, login, fields) {
// build focus or submit request
let focusOrSubmitRequest = {
origin: new BrowserpassURL(settings.tab.url).origin,
autoSubmit: getSetting("autoSubmit", login, settings),
autoSubmit: helpers.getSetting("autoSubmit", login, settings),
filledFields: filledFields,
};

Expand Down Expand Up @@ -561,25 +561,6 @@ async function getFullSettings() {
return settings;
}

/**
* Get most relevant setting value
*
* @param string key Setting key
* @param object login Login object
* @param object settings Settings object
* @return object Setting value
*/
function getSetting(key, login, settings) {
if (typeof login.settings[key] !== "undefined") {
return login.settings[key];
}
if (typeof settings.stores[login.store.id].settings[key] !== "undefined") {
return settings.stores[login.store.id].settings[key];
}

return settings[key];
}

/**
* Deep copy an object
*
Expand Down Expand Up @@ -743,7 +724,7 @@ async function handleMessage(settings, message, sendResponse) {
}
break;
case "copyOTP":
if (settings.enableOTP) {
if (helpers.getSetting("enableOTP", message.login, settings)) {
try {
if (!message.login.fields.otp) {
throw new Exception("No OTP seed available");
Expand Down Expand Up @@ -815,8 +796,8 @@ async function handleMessage(settings, message, sendResponse) {

// copy OTP token after fill
if (
settings.enableOTP &&
typeof message.login !== "undefined" &&
helpers.getSetting("enableOTP", message.login, settings) &&
message.login.fields.hasOwnProperty("otp")
) {
copyToClipboard(helpers.makeTOTP(message.login.fields.otp.params));
Expand Down Expand Up @@ -968,7 +949,7 @@ async function parseFields(settings, login) {
if (key === "secret" && lines.length) {
login.fields.secret = lines[0];
} else if (key === "login") {
const defaultUsername = getSetting("username", login, settings);
const defaultUsername = helpers.getSetting("username", login, settings);
login.fields[key] = defaultUsername || login.login.match(/([^\/]+)$/)[1];
} else {
delete login.fields[key];
Expand All @@ -982,7 +963,7 @@ async function parseFields(settings, login) {
}

// preprocess otp
if (settings.enableOTP && login.fields.hasOwnProperty("otp")) {
if (helpers.getSetting("enableOTP", login, settings) && login.fields.hasOwnProperty("otp")) {
if (login.fields.otp.match(/^otpauth:\/\/.+/i)) {
// attempt to parse otp data as URI
try {
Expand Down
20 changes: 20 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,32 @@ const BrowserpassURL = require("@browserpass/url");
module.exports = {
prepareLogins,
filterSortLogins,
getSetting,
ignoreFiles,
makeTOTP,
};

//----------------------------------- Function definitions ----------------------------------//

/**
* Get most relevant setting value
*
* @param string key Setting key
* @param object login Login object
* @param object settings Settings object
* @return object Setting value
*/
function getSetting(key, login, settings) {
if (typeof login.settings[key] !== "undefined") {
return login.settings[key];
}
if (typeof settings.stores[login.store.id].settings[key] !== "undefined") {
return settings.stores[login.store.id].settings[key];
}

return settings[key];
}

/**
* Get the deepest available domain component of a path
*
Expand Down
2 changes: 1 addition & 1 deletion src/popup/detailsInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function view(ctl, params) {
]),
(() => {
if (
this.settings.enableOTP &&
helpers.getSetting("enableOTP", login, this.settings) &&
login.fields.otp &&
login.fields.otp.params.type === "totp"
) {
Expand Down