48 lines
2.3 KiB
JavaScript
48 lines
2.3 KiB
JavaScript
"use strict";
|
|
window.rustProxyApiTokens = (() => {
|
|
let loading = false;
|
|
const hideSecret = () => { $("api-token-secret").textContent = ""; $("api-token-secret-wrap").hidden = true; };
|
|
async function refresh() {
|
|
if (!authenticated || loading) return;
|
|
loading = true;
|
|
try {
|
|
const result = await api("/api/api-tokens");
|
|
const list = $("api-token-list"); list.replaceChildren();
|
|
for (const entry of result.tokens || []) {
|
|
const card = el("div", undefined, "entity");
|
|
card.append(el("span", `${entry.id} · ${entry.permission.toUpperCase()} · ${entry.comment || "-"}`, "entity-title"));
|
|
card.append(el("p", `${t("apiTokenIpAllow")}: ${(entry.allowed_cidrs || []).join(", ")}`, "fine"));
|
|
card.append(el("p", `${t("apiTokenLastUsed")}: ${entry.last_used_at ? formatDateTime(entry.last_used_at * 1000) : "-"}`, "fine"));
|
|
card.append(action(t("apiTokenRevoke"), async () => {
|
|
if (!(await appConfirm(t("confirmDelete"), { danger: true }))) return;
|
|
try { await api(`/api/api-tokens/${encodeURIComponent(entry.id)}`, { method: "DELETE" }); hideSecret(); await refresh(); }
|
|
catch (error) { toast(error.message, true); }
|
|
}));
|
|
list.append(card);
|
|
}
|
|
if (!result.tokens?.length) list.append(el("p", t("apiTokenEmpty"), "fine"));
|
|
} catch (error) { if (error.status !== 401) toast(error.message, true); }
|
|
finally { loading = false; }
|
|
}
|
|
$("api-tokens-form").addEventListener("submit", async event => {
|
|
event.preventDefault(); if (!authenticated) return;
|
|
const button = event.submitter; button.disabled = true;
|
|
hideSecret();
|
|
try {
|
|
const result = await api("/api/api-tokens", {
|
|
method: "POST", body: JSON.stringify({
|
|
permission: $("api-token-permission").value,
|
|
comment: $("api-token-comment").value.trim(),
|
|
allowed_cidrs: $("api-token-cidrs").value.split(/[\n,]+/).map(s => s.trim()).filter(Boolean)
|
|
})
|
|
});
|
|
$("api-token-secret").textContent = result.token;
|
|
$("api-token-secret-wrap").hidden = false;
|
|
$("api-token-comment").value = "";
|
|
await refresh();
|
|
} catch (error) { toast(error.message, true); }
|
|
finally { button.disabled = false; }
|
|
});
|
|
return { refresh, hideSecret, reset() { hideSecret(); $("api-token-list").replaceChildren(); } };
|
|
})();
|