|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +require("chrome-extension-async"); |
| 4 | +const hash = require("hash.js"); |
| 5 | +const Authenticator = require("otplib").authenticator.Authenticator; |
| 6 | + |
| 7 | +const validSenders = [ |
| 8 | + "naepdomgkenhinolocfifgehidddafch", |
| 9 | + "pjmbgaakjkbhpopmakjoedenlfdmcdgm", |
| 10 | + "klfoddkbhleoaabpmiigbmpbjfljimgb", |
| 11 | + |
| 12 | +]; |
| 13 | + |
| 14 | +// Main entry point, invoked from browserpass. No response is expected. |
| 15 | +chrome.runtime.onMessageExternal.addListener(function(request, sender) { |
| 16 | + // reject invalid senders |
| 17 | + if (!validSenders.includes(sender.id)) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + // parse OTP object |
| 22 | + if (request.otp.key === null) { |
| 23 | + // this is an OTP URI, so extract the pieces |
| 24 | + try { |
| 25 | + let url = new URL(request.otp.data.toLowerCase()); |
| 26 | + let parts = url.pathname.split("/").filter(s => s.trim()); |
| 27 | + var otp = { |
| 28 | + type: parts[0], |
| 29 | + secret: url.searchParams.get("secret").toUpperCase(), |
| 30 | + algorithm: url.searchParams.get("algorithm") || "sha1", |
| 31 | + digits: parseInt(url.searchParams.get("digits") || "6"), |
| 32 | + period: parseInt(url.searchParams.get("period") || "30") |
| 33 | + }; |
| 34 | + } catch (e) { |
| 35 | + console.log(`Unable to parse uri: ${request.otp.data}`, e); |
| 36 | + return; |
| 37 | + } |
| 38 | + } else { |
| 39 | + var otp = { |
| 40 | + type: request.otp.key.toLowerCase(), |
| 41 | + secret: request.otp.data.toUpperCase(), |
| 42 | + algorithm: "sha1", |
| 43 | + digits: 6, |
| 44 | + period: 30 |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + // fix default type |
| 49 | + if (otp.type === "otp") { |
| 50 | + otp.type = "totp"; |
| 51 | + } |
| 52 | + |
| 53 | + // set handler |
| 54 | + if (otp.type === "totp") { |
| 55 | + otp.generate = makeTOTP.bind(otp); |
| 56 | + } else { |
| 57 | + console.log(`Unsupported OTP type: ${otp.type}`); |
| 58 | + } |
| 59 | + |
| 60 | + // generate code |
| 61 | + copyToClipboard(otp.generate()); |
| 62 | +}); |
| 63 | + |
| 64 | +/** |
| 65 | + * Gemerate a TOTP code |
| 66 | + * |
| 67 | + * @return string Generated code |
| 68 | + */ |
| 69 | +function makeTOTP() { |
| 70 | + switch (this.algorithm) { |
| 71 | + case "sha1": |
| 72 | + case "sha256": |
| 73 | + case "sha512": |
| 74 | + break; |
| 75 | + default: |
| 76 | + throw new Error(`Unsupported TOTP algorithm: ${this.algorithm}`); |
| 77 | + } |
| 78 | + |
| 79 | + var generator = new Authenticator(); |
| 80 | + generator.options = { |
| 81 | + crypto: { |
| 82 | + createHmac: (a, k) => hash.hmac(hash[a], k) |
| 83 | + }, |
| 84 | + algorithm: this.algorithm, |
| 85 | + digits: this.digits, |
| 86 | + step: this.period |
| 87 | + }; |
| 88 | + |
| 89 | + return generator.generate(this.secret); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Copy text to clipboard |
| 94 | + * |
| 95 | + * @since 3.0.0 |
| 96 | + * |
| 97 | + * @param string text Text to copy |
| 98 | + * @return void |
| 99 | + */ |
| 100 | +function copyToClipboard(text) { |
| 101 | + document.addEventListener( |
| 102 | + "copy", |
| 103 | + function(e) { |
| 104 | + e.clipboardData.setData("text/plain", text); |
| 105 | + e.preventDefault(); |
| 106 | + }, |
| 107 | + { once: true } |
| 108 | + ); |
| 109 | + document.execCommand("copy"); |
| 110 | +} |
0 commit comments