|
| 1 | +/* global Notify storePathComponents */ |
| 2 | + |
| 3 | +const notify = new Notify(document.querySelector('#notify')); |
| 4 | +const addKeyForm = document.getElementById('addKeyForm'); |
| 5 | +const cancelButton = document.getElementById('cancelButton'); |
| 6 | +const secretPathSelect = document.getElementById('secretPath'); |
| 7 | + |
| 8 | +async function mainLoaded() { |
| 9 | + // Load available secret paths |
| 10 | + await loadSecretPaths(); |
| 11 | + |
| 12 | + // Set up event listeners |
| 13 | + addKeyForm.addEventListener('submit', handleFormSubmit); |
| 14 | + cancelButton.addEventListener('click', handleCancel); |
| 15 | +} |
| 16 | + |
| 17 | +async function loadSecretPaths() { |
| 18 | + try { |
| 19 | + const secrets = (await browser.storage.sync.get('secrets')).secrets; |
| 20 | + |
| 21 | + if (!secrets || secrets.length === 0) { |
| 22 | + notify.error( |
| 23 | + 'No secret paths configured. Please go to Options and configure at least one secret path first.' |
| 24 | + ); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Populate the dropdown |
| 29 | + secrets.forEach((secret) => { |
| 30 | + const option = document.createElement('option'); |
| 31 | + option.value = secret; |
| 32 | + option.textContent = secret; |
| 33 | + secretPathSelect.appendChild(option); |
| 34 | + }); |
| 35 | + |
| 36 | + // If there's only one path, auto-select it |
| 37 | + if (secrets.length === 1) { |
| 38 | + secretPathSelect.value = secrets[0]; |
| 39 | + } |
| 40 | + } catch (err) { |
| 41 | + notify.error(`Error loading secret paths: ${err.message}`); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async function handleFormSubmit(event) { |
| 46 | + event.preventDefault(); |
| 47 | + |
| 48 | + // Clear any existing messages |
| 49 | + notify.clear(); |
| 50 | + |
| 51 | + // Scroll to top to show messages |
| 52 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 53 | + |
| 54 | + const secretPath = document.getElementById('secretPath').value.trim(); |
| 55 | + const urlRegex = document.getElementById('urlRegex').value.trim(); |
| 56 | + const username = document.getElementById('username').value.trim(); |
| 57 | + const password = document.getElementById('password').value.trim(); |
| 58 | + const title = document.getElementById('title').value.trim(); |
| 59 | + |
| 60 | + // Validate inputs |
| 61 | + if (!secretPath) { |
| 62 | + notify.error('Please select a secret path.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (!urlRegex || !username || !password) { |
| 67 | + notify.error('URL pattern, username, and password are required.'); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Check if URL pattern starts with http:// or https:// |
| 72 | + if (urlRegex.startsWith('http://') || urlRegex.startsWith('https://')) { |
| 73 | + notify.error( |
| 74 | + 'URL pattern cannot start with http:// or https://. Please use a regex pattern instead (e.g., ^https://example\\.com.*).' |
| 75 | + ); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Validate regex pattern |
| 80 | + try { |
| 81 | + new RegExp(urlRegex); |
| 82 | + } catch { |
| 83 | + notify.error('Invalid URL regex pattern. Please check your pattern.'); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // Double-encode the URL regex to use as the key name in Vault |
| 88 | + // First encoding: a/b -> a%2Fb |
| 89 | + // Second encoding: a%2Fb -> a%252Fb |
| 90 | + // Vault will decode once: a%252Fb -> a%2Fb (stored in Vault) |
| 91 | + const encodedKeyName = encodeURIComponent(encodeURIComponent(urlRegex)); |
| 92 | + |
| 93 | + try { |
| 94 | + // Get Vault credentials |
| 95 | + const vaultToken = (await browser.storage.local.get('vaultToken')) |
| 96 | + .vaultToken; |
| 97 | + const vaultServerAddress = (await browser.storage.sync.get('vaultAddress')) |
| 98 | + .vaultAddress; |
| 99 | + const storePath = (await browser.storage.sync.get('storePath')).storePath; |
| 100 | + |
| 101 | + if (!vaultToken || !vaultServerAddress) { |
| 102 | + notify.error( |
| 103 | + 'Not authenticated with Vault. Please go to Options and login first.' |
| 104 | + ); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const storeComponents = storePathComponents(storePath); |
| 109 | + |
| 110 | + // Create the data object to save |
| 111 | + const keyData = { |
| 112 | + username: username, |
| 113 | + password: password, |
| 114 | + }; |
| 115 | + |
| 116 | + if (title) { |
| 117 | + keyData.title = title; |
| 118 | + } |
| 119 | + |
| 120 | + // Save to Vault using the encoded key name |
| 121 | + // The key name is already encoded earlier in the function |
| 122 | + const vaultUrl = `${vaultServerAddress}/v1/${storeComponents.root}/data/${storeComponents.subPath}/${secretPath}${encodedKeyName}`; |
| 123 | + |
| 124 | + const response = await fetch(vaultUrl, { |
| 125 | + method: 'POST', |
| 126 | + headers: { |
| 127 | + 'X-Vault-Token': vaultToken, |
| 128 | + 'Content-Type': 'application/json', |
| 129 | + }, |
| 130 | + body: JSON.stringify({ data: keyData }), |
| 131 | + }); |
| 132 | + |
| 133 | + if (!response.ok) { |
| 134 | + const errorText = await response.text(); |
| 135 | + throw new Error(`Failed to save to Vault: ${errorText}`); |
| 136 | + } |
| 137 | + |
| 138 | + notify.success('Key saved successfully to Vault!', { time: 2000 }); |
| 139 | + |
| 140 | + // Clear form |
| 141 | + addKeyForm.reset(); |
| 142 | + |
| 143 | + // Redirect back to popup after a short delay |
| 144 | + setTimeout(() => { |
| 145 | + window.location.href = '/popup.html'; |
| 146 | + }, 1500); |
| 147 | + } catch (err) { |
| 148 | + notify.error(`Error saving key: ${err.message}`); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +function handleCancel() { |
| 153 | + window.location.href = '/popup.html'; |
| 154 | +} |
| 155 | + |
| 156 | +document.addEventListener('DOMContentLoaded', mainLoaded, false); |
0 commit comments