Files
archie/ui/src/archie/CredentialsView.vue
T
samandCopilot 64dfad6d53 feat(runs): add typed results and model aliases (#2043)
Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-18 23:47:35 +11:00

212 lines
7.7 KiB
Vue

<script setup lang="ts">
import { onBeforeUnmount, onMounted, reactive, ref } from "vue";
import PageHeader from "@/base/PageHeader.vue";
import { Button } from "@/components/ui/button";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Input } from "@/components/ui/input";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { SettingRow } from "@/components/ui/setting-row";
import { Spinner } from "@/components/ui/spinner";
import { listCredentials, saveCredential, Unauthorized, type CredentialField, type CredentialView } from "./api";
/**
* The API returns field sources, not saved secret values. Enter those values
* again when replacing a credential.
*/
const emit = defineEmits<{ unauthorized: [] }>();
type Kind = CredentialView["kind"];
const fieldsOf: Record<Kind, { required: string[]; optional: string[] }> = {
machine: { required: ["username", "private_key"], optional: ["private_key_passphrase"] },
age: { required: ["identity"], optional: [] },
basic: { required: ["username", "password"], optional: [] },
bearer: { required: ["token"], optional: [] },
};
const labelOf: Record<string, string> = {
username: "Username",
private_key: "Private key",
private_key_passphrase: "Private key passphrase",
identity: "Age identity",
password: "Password",
token: "Token",
};
interface Field {
value: string;
engine: string;
key: string;
}
interface Form {
name: string;
kind: Kind;
fields: Record<string, Field>;
}
const blank = (kind: Kind = "bearer"): Form => ({
name: "",
kind,
fields: Object.fromEntries([...fieldsOf[kind].required, ...fieldsOf[kind].optional].map((f) => [f, { value: "", engine: "", key: "" }])),
});
const credentials = ref<CredentialView[]>([]);
const form = reactive<Form>(blank());
const editing = ref(false);
const original = ref("");
const loading = ref(true);
const error = ref("");
const saved = ref("");
const saving = ref(false);
const controller = new AbortController();
const columns: DataTableColumn<CredentialView>[] = [
{ id: "name", label: "Name", fixed: true },
{ id: "kind", label: "Kind" },
{ id: "fields", label: "Fields" },
];
async function load() {
loading.value = true;
error.value = "";
try {
credentials.value = await listCredentials(controller.signal);
} catch (err) {
fail(err);
} finally {
loading.value = false;
}
}
function chooseKind(kind: Kind) {
form.kind = kind;
form.fields = blank(kind).fields;
}
function start(name?: string) {
error.value = "";
saved.value = "";
const found = name ? credentials.value.find((c) => c.name === name) : undefined;
original.value = name ?? "";
Object.assign(form, found ? fromView(found) : blank());
editing.value = true;
}
function fromView(c: CredentialView): Form {
const out = blank(c.kind);
out.name = c.name;
for (const [name, origin] of Object.entries(c.fields)) {
out.fields[name] ??= { value: "", engine: "", key: "" };
if (origin.includes(":")) {
const [engine, key] = origin.split(":");
out.fields[name].engine = engine;
out.fields[name].key = key;
}
}
return out;
}
async function save() {
saving.value = true;
error.value = "";
saved.value = "";
try {
const fields: Record<string, CredentialField> = {};
for (const name of [...fieldsOf[form.kind].required, ...fieldsOf[form.kind].optional]) {
const f = form.fields[name];
if (!f) continue;
if (f.value) fields[name] = { value: f.value };
else if (f.engine && f.key) fields[name] = { engine: f.engine, key: f.key };
}
await saveCredential(form.name.trim(), form.kind, fields);
saved.value = "Saved.";
editing.value = false;
Object.assign(form, blank());
await load();
} catch (err) {
fail(err);
} finally {
saving.value = false;
}
}
function fail(err: unknown) {
if (controller.signal.aborted) return;
if (err instanceof Unauthorized) emit("unauthorized");
error.value = err instanceof Error ? err.message : String(err);
}
onMounted(load);
onBeforeUnmount(() => controller.abort());
</script>
<template>
<div>
<PageHeader title="Credentials">
<Button variant="outline" @click="start()">Add credential</Button>
</PageHeader>
<Spinner v-if="loading" />
<ReadError v-else-if="error && !credentials.length && !editing" :message="error" retry-label="Retry" @retry="load" />
<template v-else>
<Section v-if="editing" :title="form.name ? `Edit ${form.name}` : 'New credential'">
<form class="flex flex-col gap-3" @submit.prevent="save">
<SettingRow label="Name" for="credential-name">
<Input id="credential-name" v-model="form.name" :disabled="original !== ''" placeholder="lowercase-name" />
</SettingRow>
<SettingRow label="Kind" for="credential-kind">
<Select :model-value="form.kind" @update:model-value="(v) => chooseKind(v as Kind)">
<SelectTrigger id="credential-kind" class="w-full sm:w-64"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="bearer">Bearer token</SelectItem>
<SelectItem value="basic">Username and password</SelectItem>
<SelectItem value="machine">SSH machine</SelectItem>
<SelectItem value="age">Age identity</SelectItem>
</SelectContent>
</Select>
</SettingRow>
<SettingRow
v-for="(field, name) in form.fields"
:key="name"
:label="labelOf[name] ?? name"
:hint="fieldsOf[form.kind].optional.includes(name) ? 'Optional.' : 'Required.'"
>
<div class="flex flex-col gap-2">
<Input v-model="field.value" type="password" autocomplete="off" :aria-label="labelOf[name] ?? name" placeholder="New value" />
<div class="flex flex-wrap items-center gap-2">
<Input v-model="field.engine" placeholder="Secret engine" class="w-40" :aria-label="`${name} engine`" />
<Input v-model="field.key" placeholder="Key" class="w-40" :aria-label="`${name} key`" />
</div>
</div>
</SettingRow>
<p class="text-caption text-fg-subtle">A value is write only. Enter it, or name the engine and key that resolve it.</p>
<p v-if="error" class="text-caption text-danger">{{ error }}</p>
<div class="flex items-center gap-2">
<Button type="submit" :disabled="saving || !form.name.trim()">Save</Button>
<Button type="button" variant="ghost" @click="editing = false">Cancel</Button>
</div>
</form>
</Section>
<Empty v-if="!credentials.length">
<EmptyHeader><EmptyTitle>No credentials yet</EmptyTitle></EmptyHeader>
</Empty>
<DataTable v-else :columns="columns" :rows="credentials" :row-key="(c) => c.name" storage-key="credentials">
<template #cell-name="{ row }"><span class="font-medium">{{ row.name }}</span></template>
<template #cell-kind="{ row }">{{ row.kind }}</template>
<template #cell-fields="{ row }">
<span class="font-mono text-caption">{{ Object.entries(row.fields).map(([f, origin]) => `${f}: ${origin}`).join(", ") }}</span>
</template>
<template #actions="{ row }">
<Button variant="ghost" size="sm" @click="start(row.name)">Edit</Button>
</template>
</DataTable>
<p v-if="saved" class="mt-2 text-caption text-muted-foreground">{{ saved }}</p>
</template>
</div>
</template>