Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};
use tauri::{Emitter, Manager, WebviewUrl, WebviewWindowBuilder};

// OS credential-store identifier. Keys are whitelisted to a narrow pattern
// so the webview can only ever read/write its own desktop-auth entries —
Expand Down Expand Up @@ -99,7 +99,18 @@ pub fn run() {
// deep-link plugin forwards the URL to it.
let _ =
app.handle()
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
.plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
// Windows may deliver the protocol URL as a second-instance
// argv rather than through the webview deep-link listener.
// Forward only our exact auth scheme; the JS flow+PKCE
// binding still validates the handoff before redemption.
for arg in &argv {
if let Ok(url) = tauri::Url::parse(arg) {
if url.scheme() == "dev-ledger" && url.host_str() == Some("auth") {
let _ = app.emit("desktop-auth-activation", arg.clone());
}
}
}
if let Some(w) = app.get_webview_window("main") {
let _ = w.unminimize();
let _ = w.set_focus();
Expand Down
13 changes: 13 additions & 0 deletions src/desktop/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { openUrl } from '@tauri-apps/plugin-opener';
import { getCurrent, onOpenUrl } from '@tauri-apps/plugin-deep-link';
import { invoke } from '@tauri-apps/api/core';
import { listen } from '@tauri-apps/api/event';
import { API_BASE, DESKTOP_SCHEME } from './runtime';
import { clearToken, loadToken, storeToken } from './keys';

Expand All @@ -31,6 +32,7 @@ import { clearToken, loadToken, storeToken } from './keys';
let pendingFlow: string | null = null;
let pendingVerifier: string | null = null;
let pendingRemember = false;
let exchangeInFlight = false;
let noticeHandler: ((msg: string | null) => void) | null = null;

export function onDesktopAuthNotice(cb: (msg: string | null) => void): () => void {
Expand Down Expand Up @@ -96,6 +98,10 @@ async function handleAuthUrl(url: string): Promise<void> {
if (!pendingFlow || flow !== pendingFlow || !code || !pendingVerifier) {
return;
}
// The same URL may arrive through both the deep-link plugin and the
// Windows second-instance event. Redeem it at most once.
if (exchangeInFlight) return;
exchangeInFlight = true;
const verifier = pendingVerifier;
const remember = pendingRemember;
pendingFlow = null;
Expand Down Expand Up @@ -126,6 +132,8 @@ async function handleAuthUrl(url: string): Promise<void> {
window.location.reload();
} catch {
notice('The Dev Ledger service is unreachable — sign-in could not finish.');
} finally {
exchangeInFlight = false;
}
}

Expand All @@ -136,6 +144,11 @@ async function handleAuthUrl(url: string): Promise<void> {
// registered.
export async function initDesktopAuth(): Promise<void> {
await loadToken();
await listen<string>('desktop-auth-activation', (event) => {
if (typeof event.payload === 'string' && isAuthDeepLink(event.payload)) {
void handleAuthUrl(event.payload);
}
}).catch(() => {});
await onOpenUrl((urls) => {
for (const u of urls) {
if (isAuthDeepLink(u)) void handleAuthUrl(u);
Expand Down
Loading