|
| 1 | +import { DICTS, getActiveLocale, type Locale } from "./i18n/shared"; |
| 2 | + |
| 3 | +const ADMIN_TOKEN_DIALOG_ID = "opencodex-admin-token-dialog"; |
| 4 | +const ADMIN_TOKEN_USERNAME = "OpenCodex"; |
| 5 | + |
| 6 | +export type AdminTokenValidation = "accepted" | "rejected" | "unavailable"; |
| 7 | +export type AdminTokenVerifier = (token: string) => Promise<AdminTokenValidation>; |
| 8 | + |
| 9 | +/** |
| 10 | + * Ask for the management credential with a real sign-in form so browsers and |
| 11 | + * password managers can offer save/autofill. OpenCodex itself still keeps the |
| 12 | + * submitted token in memory only; persistence remains entirely browser-owned. |
| 13 | + */ |
| 14 | +export function promptForAdminToken( |
| 15 | + verifyToken: AdminTokenVerifier, |
| 16 | + locale: Locale = getActiveLocale(), |
| 17 | +): Promise<string | null> { |
| 18 | + const messages = DICTS[locale]; |
| 19 | + const titleText = messages["auth.adminTokenTitle"]; |
| 20 | + |
| 21 | + return new Promise((resolve) => { |
| 22 | + const previouslyFocused = document.activeElement instanceof HTMLElement |
| 23 | + ? document.activeElement |
| 24 | + : null; |
| 25 | + let settled = false; |
| 26 | + |
| 27 | + const dialog = document.createElement("dialog"); |
| 28 | + dialog.id = ADMIN_TOKEN_DIALOG_ID; |
| 29 | + dialog.className = "modal-overlay"; |
| 30 | + dialog.setAttribute("aria-labelledby", `${ADMIN_TOKEN_DIALOG_ID}-title`); |
| 31 | + |
| 32 | + const form = document.createElement("form"); |
| 33 | + form.className = "modal-card"; |
| 34 | + form.method = "post"; |
| 35 | + form.action = window.location.href; |
| 36 | + form.autocomplete = "on"; |
| 37 | + |
| 38 | + const heading = document.createElement("div"); |
| 39 | + heading.className = "modal-head"; |
| 40 | + const title = document.createElement("h3"); |
| 41 | + title.id = `${ADMIN_TOKEN_DIALOG_ID}-title`; |
| 42 | + title.textContent = titleText; |
| 43 | + heading.append(title); |
| 44 | + |
| 45 | + const accountField = document.createElement("div"); |
| 46 | + const accountLabel = document.createElement("label"); |
| 47 | + accountLabel.className = "field-label"; |
| 48 | + accountLabel.htmlFor = `${ADMIN_TOKEN_DIALOG_ID}-username`; |
| 49 | + accountLabel.textContent = messages["auth.adminAccountLabel"]; |
| 50 | + const username = document.createElement("input"); |
| 51 | + username.id = accountLabel.htmlFor; |
| 52 | + username.className = "input"; |
| 53 | + username.type = "text"; |
| 54 | + username.name = "username"; |
| 55 | + username.autocomplete = "username"; |
| 56 | + username.value = ADMIN_TOKEN_USERNAME; |
| 57 | + username.readOnly = true; |
| 58 | + accountField.append(accountLabel, username); |
| 59 | + |
| 60 | + const tokenField = document.createElement("div"); |
| 61 | + tokenField.style.marginTop = "var(--space-4)"; |
| 62 | + const tokenLabel = document.createElement("label"); |
| 63 | + tokenLabel.className = "field-label"; |
| 64 | + tokenLabel.htmlFor = `${ADMIN_TOKEN_DIALOG_ID}-password`; |
| 65 | + tokenLabel.textContent = messages["auth.adminTokenFieldLabel"]; |
| 66 | + const password = document.createElement("input"); |
| 67 | + password.id = tokenLabel.htmlFor; |
| 68 | + password.className = "input"; |
| 69 | + password.type = "password"; |
| 70 | + password.name = "password"; |
| 71 | + password.autocomplete = "current-password"; |
| 72 | + password.required = true; |
| 73 | + password.spellcheck = false; |
| 74 | + password.autocapitalize = "none"; |
| 75 | + tokenField.append(tokenLabel, password); |
| 76 | + |
| 77 | + const validationError = document.createElement("div"); |
| 78 | + validationError.className = "notice notice-err"; |
| 79 | + validationError.setAttribute("role", "alert"); |
| 80 | + validationError.hidden = true; |
| 81 | + |
| 82 | + const actions = document.createElement("div"); |
| 83 | + actions.className = "modal-actions"; |
| 84 | + const cancel = document.createElement("button"); |
| 85 | + cancel.type = "button"; |
| 86 | + cancel.className = "btn btn-ghost"; |
| 87 | + cancel.textContent = messages["common.cancel"]; |
| 88 | + const submit = document.createElement("button"); |
| 89 | + submit.type = "submit"; |
| 90 | + submit.className = "btn btn-primary"; |
| 91 | + submit.textContent = messages["common.ok"]; |
| 92 | + actions.append(cancel, submit); |
| 93 | + |
| 94 | + form.append(heading, accountField, tokenField, validationError, actions); |
| 95 | + dialog.append(form); |
| 96 | + |
| 97 | + const finish = (value: string | null): void => { |
| 98 | + if (settled) return; |
| 99 | + settled = true; |
| 100 | + if (dialog.open) dialog.close(); |
| 101 | + dialog.remove(); |
| 102 | + previouslyFocused?.focus(); |
| 103 | + resolve(value); |
| 104 | + }; |
| 105 | + |
| 106 | + form.addEventListener("submit", (event) => { |
| 107 | + event.preventDefault(); |
| 108 | + const token = password.value.trim(); |
| 109 | + if (!token) { |
| 110 | + password.value = ""; |
| 111 | + password.reportValidity(); |
| 112 | + return; |
| 113 | + } |
| 114 | + password.disabled = true; |
| 115 | + submit.disabled = true; |
| 116 | + validationError.hidden = true; |
| 117 | + |
| 118 | + void verifyToken(token).then((result) => { |
| 119 | + if (settled) return; |
| 120 | + if (result === "accepted") { |
| 121 | + finish(token); |
| 122 | + return; |
| 123 | + } |
| 124 | + password.value = ""; |
| 125 | + password.disabled = false; |
| 126 | + submit.disabled = false; |
| 127 | + validationError.textContent = result === "rejected" |
| 128 | + ? messages["auth.adminTokenRejected"] |
| 129 | + : messages["auth.adminTokenUnavailable"]; |
| 130 | + validationError.hidden = false; |
| 131 | + password.focus(); |
| 132 | + }).catch(() => { |
| 133 | + if (settled) return; |
| 134 | + password.value = ""; |
| 135 | + password.disabled = false; |
| 136 | + submit.disabled = false; |
| 137 | + validationError.textContent = messages["auth.adminTokenUnavailable"]; |
| 138 | + validationError.hidden = false; |
| 139 | + password.focus(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + cancel.addEventListener("click", () => finish(null)); |
| 143 | + dialog.addEventListener("cancel", (event) => { |
| 144 | + event.preventDefault(); |
| 145 | + finish(null); |
| 146 | + }); |
| 147 | + |
| 148 | + document.body.append(dialog); |
| 149 | + if (typeof dialog.showModal === "function") dialog.showModal(); |
| 150 | + else dialog.setAttribute("open", ""); |
| 151 | + queueMicrotask(() => password.focus()); |
| 152 | + }); |
| 153 | +} |
0 commit comments