Skip to content

Commit aa4bab0

Browse files
authored
Pass OTP data across to OTP extension for further action (#118)
Provides OTP into, current hostname & current tab to the OTP extension.
1 parent d177d95 commit aa4bab0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/background.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var idb = require("idb");
99
// native application id
1010
var appID = "com.github.browserpass.native";
1111

12+
// OTP extension id
13+
var otpID = [
14+
"afjjoildnccgmjbblnklbohcbjehjaph", // webstore releases
15+
"jbnpmhhgnchcoljeobafpinmchnpdpin" // github releases
16+
];
17+
1218
// default settings
1319
var defaultSettings = {
1420
autoSubmit: false,
@@ -785,13 +791,20 @@ async function parseFields(settings, login) {
785791
secret: ["secret", "password", "pass"],
786792
login: ["login", "username", "user"],
787793
openid: ["openid"],
794+
otp: ["otp", "totp", "hotp"],
788795
url: ["url", "uri", "website", "site", "link", "launch"]
789796
};
790797
login.settings = {
791798
autoSubmit: { name: "autosubmit", type: "bool" }
792799
};
793800
var lines = login.raw.split(/[\r\n]+/).filter(line => line.trim().length > 0);
794801
lines.forEach(function(line) {
802+
// check for uri-encoded otp
803+
if (line.match(/^otpauth:\/\/.+/)) {
804+
login.fields.otp = { key: null, data: line };
805+
return;
806+
}
807+
795808
// split key / value & ignore non-k/v lines
796809
var parts = line.match(/^(.+?):(.+)$/);
797810
if (parts === null) {
@@ -811,7 +824,11 @@ async function parseFields(settings, login) {
811824
Array.isArray(login.fields[key]) &&
812825
login.fields[key].includes(parts[0].toLowerCase())
813826
) {
814-
login.fields[key] = parts[1];
827+
if (key === "otp") {
828+
login.fields[key] = { key: parts[0].toLowerCase(), data: parts[1] };
829+
} else {
830+
login.fields[key] = parts[1];
831+
}
815832
break;
816833
}
817834
}
@@ -851,6 +868,23 @@ async function parseFields(settings, login) {
851868
delete login.settings[key];
852869
}
853870
}
871+
872+
// trigger otp extension
873+
if (login.fields.hasOwnProperty("otp")) {
874+
for (let key in otpID) {
875+
chrome.runtime
876+
.sendMessage(otpID[key], {
877+
otp: login.fields.otp,
878+
host: settings.host,
879+
tab: settings.tab
880+
})
881+
// Both response & error are noop functions, because we don't care about
882+
// the response, and if there's an error it just means the otp extension
883+
// is probably not installed. We can't detect that without requesting the
884+
// management permission, so this is an acceptable workaround.
885+
.then(noop => null, noop => null);
886+
}
887+
}
854888
}
855889

856890
/**

0 commit comments

Comments
 (0)