feat(ssh): support key+password combined authentication

This commit is contained in:
huijiangyuan authored and GitHub committed 2026-07-26 17:52:33 +08:00
1 parent 000bb907e3
commit 3a2dc4225a
27 files changed
+1566 -37

No files matched your search

@@ -2721,7 +2721,8 @@ const sqlServerDriverMode = computed<"auto" | "legacy">(() => (sqlServerUsesLega
const shouldUseWideConnectionDialog = computed(() => dialogStep.value === "config" && (canChooseVisibleDatabases.value || (canChooseVisibleSchemas.value && !visibleFilterUsesSchemas.value)));
const connectionDialogContentClass = computed(() => {
if (dialogStep.value === "select") return "connection-dialog-content--picker sm:h-[720px] sm:max-w-[880px]";
return shouldUseWideConnectionDialog.value ? "connection-dialog-content--wide sm:max-w-[660px]" : "connection-dialog-content--standard sm:max-w-[560px]";
const widthClass = shouldUseWideConnectionDialog.value ? "connection-dialog-content--wide sm:max-w-[660px]" : "connection-dialog-content--standard sm:max-w-[560px]";
return `${widthClass} connection-dialog-content--config`;
});
const connectionLabelClass = "justify-self-start text-left";
const connectionLabelSmallClass = `${connectionLabelClass} text-xs`;
@@ -4103,15 +4104,16 @@ function isLegacySshAgentMethod(hop: Partial<SshTunnelConfig> | null | undefined
function updateSelectedSshAuthMethod(value: unknown) {
const layer = selectedSshLayer.value;
if (!layer) return;
layer.auth_method = value === "key" ? "key" : value === "none" ? "none" : "password";
layer.auth_method = value === "key" ? "key" : value === "key+password" ? "key+password" : value === "none" ? "none" : "password";
// Scrub credential fields that do not apply to the selected method so
// they are not accidentally submitted or used by the backend fallback.
if (layer.auth_method !== "password") layer.password = "";
if (layer.auth_method !== "key") {
// "key+password" keeps both key and password fields.
if (layer.auth_method !== "password" && layer.auth_method !== "key+password") layer.password = "";
if (layer.auth_method !== "key" && layer.auth_method !== "key+password") {
layer.key_path = "";
layer.key_passphrase = "";
}
if (layer.auth_method !== "key") {
if (layer.auth_method !== "key" && layer.auth_method !== "key+password") {
layer.use_ssh_agent = false;
}
resetTestState();
@@ -6611,12 +6613,13 @@ function openExternalUrl(url: string) {
<SelectContent>
<SelectItem value="password">{{ t("connection.sshAuthMethodPassword") }}</SelectItem>
<SelectItem value="key">{{ t("connection.sshAuthMethodKey") }}</SelectItem>
<SelectItem value="key+password">{{ t("connection.sshAuthMethodKeyPassword") }}</SelectItem>
<SelectItem value="none">{{ t("connection.sshAuthMethodNone") }}</SelectItem>
<SelectItem v-if="isLegacySshAgentMethod(selectedSshLayer)" value="agent" disabled>{{ t("connection.sshAuthMethodAgentLegacy") }}</SelectItem>
</SelectContent>
</Select>
</div>
<div v-if="selectedSshLayer.auth_method === 'key'" class="grid grid-cols-4 items-center gap-4">
<div v-if="selectedSshLayer.auth_method === 'key' || selectedSshLayer.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelSmallClass">{{ t("connection.sshKeyPath") }}</Label>
<div class="col-span-3 flex items-center gap-1">
<Input v-model="selectedSshLayer.key_path" class="flex-1" placeholder="~/.ssh/id_rsa" :disabled="selectedSshLayer.enabled === false" />
@@ -6630,11 +6633,11 @@ function openExternalUrl(url: string) {
</Tooltip>
</div>
</div>
<div v-if="selectedSshLayer.auth_method === 'key'" class="grid grid-cols-4 items-center gap-4">
<div v-if="selectedSshLayer.auth_method === 'key' || selectedSshLayer.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelSmallClass">{{ t("connection.sshKeyPassphrase") }}</Label>
<PasswordInput v-model="selectedSshLayer.key_passphrase" class="col-span-3" :placeholder="t('connection.sshKeyPassphrasePlaceholder')" :disabled="selectedSshLayer.enabled === false" />
</div>
<div v-if="!selectedSshLayer.auth_method || selectedSshLayer.auth_method === 'password'" class="grid grid-cols-4 items-center gap-4">
<div v-if="!selectedSshLayer.auth_method || selectedSshLayer.auth_method === 'password' || selectedSshLayer.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelSmallClass">{{ t("connection.sshPassword") }}</Label>
<PasswordInput v-model="selectedSshLayer.password" class="col-span-3" :placeholder="t('connection.sshPasswordPlaceholder')" :disabled="selectedSshLayer.enabled === false" />
</div>
@@ -6984,6 +6987,22 @@ function openExternalUrl(url: string) {
max-height: calc(var(--dbx-viewport-height) - 2rem);
}
.connection-dialog-content--config {
min-height: 0;
}
@media (max-height: 720px) {
.connection-dialog-content--config {
/* A definite flex height lets tab bodies shrink and scroll above the fixed footer. */
height: calc(var(--dbx-viewport-height) - 2rem);
}
.connection-dialog-content--config .connection-form-body {
/* Keep grid rows compact when the scroll viewport is taller than the form. */
align-content: start;
}
}
/* Legacy responsive layout rules live in public/connection-dialog-legacy.css
* so the production build cannot rewrite their classic media queries. */
@supports not (color: oklch(0.5 0.1 180)) {
@@ -6,7 +6,9 @@ import { Input } from "@/components/ui/input";
import PasswordInput from "@/components/ui/PasswordInput.vue";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Loader2, Plus, Trash2 } from "@lucide/vue";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { Loader2, Plus, Trash2, FolderOpen } from "@lucide/vue";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { useToast } from "@/composables/useToast";
import { useTunnelProfileStore } from "@/stores/tunnelProfileStore";
import { createTunnelProfile, createTunnelProfileTestGuard, tunnelProfileSummary, type TunnelProfileType } from "@/lib/connection/tunnelProfiles";
@@ -119,9 +121,9 @@ function removeSelected() {
function updateSshAuthMethod(value: unknown) {
const profile = selectedSsh.value;
if (!profile) return;
profile.auth_method = value === "key" ? "key" : value === "none" ? "none" : "password";
if (profile.auth_method !== "password") profile.password = "";
if (profile.auth_method !== "key") {
profile.auth_method = value === "key" ? "key" : value === "key+password" ? "key+password" : value === "none" ? "none" : "password";
if (profile.auth_method !== "password" && profile.auth_method !== "key+password") profile.password = "";
if (profile.auth_method !== "key" && profile.auth_method !== "key+password") {
profile.key_path = "";
profile.key_passphrase = "";
}
@@ -133,6 +135,19 @@ function updateProxyType(value: unknown) {
profile.proxy_type = value === "http" ? "http" : "socks5";
}
async function browseSshKeyPath(target?: { key_path?: string } | null) {
if (isTauriRuntime()) {
const { open } = await import("@tauri-apps/plugin-dialog");
const selected = await open({
title: "Select SSH Private Key",
multiple: false,
});
if (selected && typeof selected === "string" && target) {
target.key_path = selected;
}
}
}
async function save() {
if (isSaving.value) return;
invalidateProfileTest();
@@ -229,19 +244,30 @@ async function testSelected() {
<SelectContent>
<SelectItem value="password">{{ t("connection.sshAuthMethodPassword") }}</SelectItem>
<SelectItem value="key">{{ t("connection.sshAuthMethodKey") }}</SelectItem>
<SelectItem value="key+password">{{ t("connection.sshAuthMethodKeyPassword") }}</SelectItem>
<SelectItem value="none">{{ t("connection.sshAuthMethodNone") }}</SelectItem>
</SelectContent>
</Select>
</div>
<div v-if="!selectedSsh.auth_method || selectedSsh.auth_method === 'password'" class="grid grid-cols-4 items-center gap-4">
<div v-if="!selectedSsh.auth_method || selectedSsh.auth_method === 'password' || selectedSsh.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label class="text-xs">{{ t("connection.sshPassword") }}</Label>
<PasswordInput v-model="selectedSsh.password" class="col-span-3" :placeholder="t('connection.sshPasswordPlaceholder')" />
</div>
<div v-if="selectedSsh.auth_method === 'key'" class="grid grid-cols-4 items-center gap-4">
<div v-if="selectedSsh.auth_method === 'key' || selectedSsh.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label class="text-xs">{{ t("connection.sshKeyPath") }}</Label>
<Input v-model="selectedSsh.key_path" class="col-span-3" placeholder="~/.ssh/id_rsa" />
<div class="col-span-3 flex items-center gap-1">
<Input v-model="selectedSsh.key_path" class="flex-1" placeholder="~/.ssh/id_rsa" />
<Tooltip v-if="isTauriRuntime()">
<TooltipTrigger as-child>
<Button variant="outline" size="icon" class="h-9 w-9 shrink-0" @click="browseSshKeyPath(selectedSsh)">
<FolderOpen class="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>{{ t("connection.sshKeyPathBrowse") }}</TooltipContent>
</Tooltip>
</div>
</div>
<div v-if="selectedSsh.auth_method === 'key'" class="grid grid-cols-4 items-center gap-4">
<div v-if="selectedSsh.auth_method === 'key' || selectedSsh.auth_method === 'key+password'" class="grid grid-cols-4 items-center gap-4">
<Label class="text-xs">{{ t("connection.sshKeyPassphrase") }}</Label>
<PasswordInput v-model="selectedSsh.key_passphrase" class="col-span-3" :placeholder="t('connection.sshKeyPassphrasePlaceholder')" />
</div>
@@ -15,6 +15,7 @@ const TableImportDialog = defineAsyncComponent(() => import("@/components/import
const FieldLineageDialog = defineAsyncComponent(() => import("@/components/lineage/FieldLineageDialog.vue"));
const ConfigPassphraseDialog = defineAsyncComponent(() => import("@/components/config/ConfigPassphraseDialog.vue"));
const DatabaseSearchDialog = defineAsyncComponent(() => import("@/components/search/DatabaseSearchDialog.vue"));
const SshHostKeyPromptDialog = defineAsyncComponent(() => import("@/components/ssh/SshHostKeyPromptDialog.vue"));
const DatabaseExportDialog = defineAsyncComponent(() => import("@/components/export/DatabaseExportDialog.vue"));
const DataGenerateDialog = defineAsyncComponent(() => import("@/components/generate/DataGenerateDialog.vue"));
import { useConnectionStore } from "@/stores/connectionStore";
@@ -262,4 +263,5 @@ watch(
</DialogFooter>
</DialogContent>
</Dialog>
<SshHostKeyPromptDialog />
</template>
@@ -0,0 +1,205 @@
<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount } from "vue";
import { useI18n } from "vue-i18n";
import { invoke } from "@tauri-apps/api/core";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { useToast } from "@/composables/useToast";
interface SshPromptRequest {
id: string;
kind: "HostKeyVerify" | "SecretInput";
host: string;
port: number;
key_type?: string | null;
fingerprint?: string | null;
prompt?: string | null;
}
interface SshHostKeyNotice {
kind: "Changed" | "Rejected" | "LearnFailed" | string;
host: string;
port: number;
message: string;
}
const { t } = useI18n();
const { toast } = useToast();
// Queue of pending prompts. A single `activePrompt` ref would let a second
// prompt arriving before the first is resolved overwrite it, orphaning the
// first responder (that connection would then hang until the 300s timeout).
// Using a queue keyed by request id means every prompt is shown and answered.
const queue = ref<SshPromptRequest[]>([]);
const current = computed<SshPromptRequest | null>(() => queue.value[0] ?? null);
const visible = ref(false);
const remember = ref(true);
const secretCode = ref("");
const resolving = ref(false);
const unlisteners: Array<() => void> = [];
let mounted = true;
onMounted(async () => {
try {
const { listen } = await import("@tauri-apps/api/event");
if (!mounted) return;
const promptHandle = await listen<SshPromptRequest>("ssh-prompt", (event) => {
queue.value.push(event.payload);
visible.value = true;
});
if (!mounted) {
promptHandle();
return;
}
unlisteners.push(promptHandle);
const noticeHandle = await listen<SshHostKeyNotice>("ssh-host-key-notice", (event) => {
const n = event.payload;
let message: string;
if (n.kind === "Changed") {
message = t("connection.sshHostKeyNoticeChanged", { host: n.host, port: n.port });
} else if (n.kind === "Rejected") {
message = t("connection.sshHostKeyNoticeRejected", { host: n.host, port: n.port });
} else if (n.kind === "LearnFailed") {
message = t("connection.sshHostKeyNoticeLearnFailed", { host: n.host, port: n.port });
} else {
message = n.message || t("connection.sshHostKeyNoticeGeneric");
}
toast(message, 6000);
});
if (!mounted) {
noticeHandle();
return;
}
unlisteners.push(noticeHandle);
// Backend tells the UI to drop a prompt it already gave up on (timeout,
// cancellation, or event-delivery failure). Remove it by id so the queue
// keeps progressing instead of stranding the dialog.
const dismissHandle = await listen<string>("ssh-prompt-dismiss", (event) => {
dismissPrompt(event.payload);
});
if (!mounted) {
dismissHandle();
return;
}
unlisteners.push(dismissHandle);
// Do not let the backend emit prompts until every listener is installed;
// Tauri events sent before a listener exists are not replayed.
await invoke("ssh_prompt_ready");
if (!mounted) {
void invoke("ssh_prompt_not_ready").catch(() => undefined);
}
} catch {
// Not running inside Tauri (e.g. web preview) — no prompts to handle.
}
});
onBeforeUnmount(() => {
mounted = false;
unlisteners.forEach((u) => u());
void invoke("ssh_prompt_not_ready").catch(() => undefined);
});
async function resolve(action: "accept" | "reject" | "secret") {
const prompt = queue.value[0];
if (!prompt || resolving.value) return;
resolving.value = true;
try {
await invoke("resolve_ssh_prompt", {
resolution: {
id: prompt.id,
action,
remember: action === "accept" ? remember.value : undefined,
secret: action === "secret" ? secretCode.value : undefined,
},
});
// Advance to the next queued prompt (if any) and reset per-prompt state.
queue.value.shift();
remember.value = true;
secretCode.value = "";
if (queue.value.length === 0) visible.value = false;
} catch (e) {
console.error("[DBX] resolve_ssh_prompt failed:", e);
// "No pending" / "already cancelled" mean the backend already dropped this
// prompt (timeout or cancelled) — remove it from the queue and move on so
// the dialog does not get stuck.
dismissPrompt(prompt.id);
} finally {
resolving.value = false;
}
}
// Remove a prompt by id (e.g. when the backend cancels it) and reset per-prompt
// state if we just dismissed the one the user was acting on.
function dismissPrompt(id: string) {
const idx = queue.value.findIndex((p) => p.id === id);
if (idx === -1) return;
queue.value.splice(idx, 1);
if (idx === 0) {
remember.value = true;
secretCode.value = "";
}
if (queue.value.length === 0) visible.value = false;
}
function accept() {
void resolve("accept");
}
function reject() {
void resolve("reject");
}
function submitSecret() {
void resolve("secret");
}
</script>
<template>
<Dialog v-model:open="visible" :dismissible="false">
<DialogContent class="sm:max-w-[460px]">
<DialogHeader>
<DialogTitle>{{ t("connection.sshHostKeyVerifyTitle") }}</DialogTitle>
<DialogDescription class="text-muted-foreground">
{{ t("connection.sshHostKeyVerifyMessage", { host: current?.host ?? "", port: current?.port ?? "" }) }}
</DialogDescription>
</DialogHeader>
<div v-if="current" class="space-y-3 py-1">
<div class="rounded-md border border-border bg-muted/40 p-3 text-sm">
<div class="flex items-center justify-between gap-3">
<span class="text-muted-foreground">{{ t("connection.sshHostKeyVerifyKeyType") }}</span>
<span class="font-medium">{{ current.key_type || "—" }}</span>
</div>
<div class="mt-2 flex items-start justify-between gap-3">
<span class="shrink-0 text-muted-foreground">{{ t("connection.sshHostKeyVerifyFingerprint") }}</span>
<span class="break-all text-right font-mono text-xs font-medium">{{ current.fingerprint || "—" }}</span>
</div>
</div>
<label v-if="current.kind === 'HostKeyVerify'" class="flex items-center gap-2 text-sm">
<input type="checkbox" v-model="remember" class="h-4 w-4 rounded border-border accent-primary" />
<span>{{ t("connection.sshHostKeyVerifyRemember") }}</span>
</label>
<div v-else-if="current.kind === 'SecretInput'" class="space-y-2">
<p class="text-sm text-muted-foreground">{{ current.prompt || "Enter verification code" }}</p>
<input v-model="secretCode" type="text" autocomplete="off" class="w-full rounded-md border border-border bg-background px-3 py-2 font-mono text-sm outline-none focus:ring-2 focus:ring-ring" :placeholder="'••••••'" />
</div>
</div>
<DialogFooter>
<Button variant="outline" :disabled="resolving" @click="reject">{{ t("connection.sshHostKeyVerifyReject") }}</Button>
<Button v-if="current?.kind !== 'SecretInput'" :disabled="resolving" @click="accept">
{{ t("connection.sshHostKeyVerifyAccept") }}
</Button>
<Button v-else :disabled="resolving || !secretCode" @click="submitSecret">
{{ t("connection.sshHostKeyVerifyAccept") }}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</template>