Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(api-keys): route API key calls through IPC for proper cookie auth

Gabriel Steinabf7ea7ed745
diff --git a/preload.js b/preload.js
index cab0970a0..9a7ce890a 100644
--- a/preload.js
+++ b/preload.js
@@ -439,6 +439,9 @@ contextBridge.exposeInMainWorld("electronAPI", {
cloudBillingPortal: () => ipcRenderer.invoke("cloud-billing-portal"),
cloudSwitchPlan: (opts) => ipcRenderer.invoke("cloud-switch-plan", opts),
cloudPreviewSwitch: (opts) => ipcRenderer.invoke("cloud-preview-switch", opts),
+ cloudApiKeysList: () => ipcRenderer.invoke("cloud-api-keys-list"),
+ cloudApiKeysCreate: (opts) => ipcRenderer.invoke("cloud-api-keys-create", opts),
+ cloudApiKeysRevoke: (id) => ipcRenderer.invoke("cloud-api-keys-revoke", id),
getSttConfig: () => ipcRenderer.invoke("get-stt-config"),
// Cloud audio file transcription
@@ -717,31 +720,40 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.invoke("db-semantic-search-conversations", query, limit),
// Sync operations
- getPendingNotes: () => ipcRenderer.invoke('db-get-pending-notes'),
- getPendingNoteDeletes: () => ipcRenderer.invoke('db-get-pending-note-deletes'),
- getNoteByClientId: (clientNoteId) => ipcRenderer.invoke('db-get-note-by-client-id', clientNoteId),
- upsertNoteFromCloud: (cloudNote, localFolderId) => ipcRenderer.invoke('db-upsert-note-from-cloud', cloudNote, localFolderId),
- markNoteSynced: (id, cloudId) => ipcRenderer.invoke('db-mark-note-synced', id, cloudId),
- markNoteSyncError: (id) => ipcRenderer.invoke('db-mark-note-sync-error', id),
- hardDeleteNote: (id) => ipcRenderer.invoke('db-hard-delete-note', id),
-
- getPendingFolders: () => ipcRenderer.invoke('db-get-pending-folders'),
- getFolderByClientId: (clientFolderId) => ipcRenderer.invoke('db-get-folder-by-client-id', clientFolderId),
- upsertFolderFromCloud: (cloudFolder) => ipcRenderer.invoke('db-upsert-folder-from-cloud', cloudFolder),
- markFolderSynced: (id, cloudId) => ipcRenderer.invoke('db-mark-folder-synced', id, cloudId),
- getFolderIdMap: () => ipcRenderer.invoke('db-get-folder-id-map'),
-
- getPendingConversations: () => ipcRenderer.invoke('db-get-pending-conversations'),
- getPendingConversationDeletes: () => ipcRenderer.invoke('db-get-pending-conversation-deletes'),
- getConversationByClientId: (clientId) => ipcRenderer.invoke('db-get-conversation-by-client-id', clientId),
- upsertConversationFromCloud: (cloudConv, messages) => ipcRenderer.invoke('db-upsert-conversation-from-cloud', cloudConv, messages),
- markConversationSynced: (id, cloudId) => ipcRenderer.invoke('db-mark-conversation-synced', id, cloudId),
- hardDeleteConversation: (id) => ipcRenderer.invoke('db-hard-delete-conversation', id),
-
- getPendingTranscriptions: () => ipcRenderer.invoke('db-get-pending-transcriptions'),
- getTranscriptionByClientId: (clientId) => ipcRenderer.invoke('db-get-transcription-by-client-id', clientId),
- upsertTranscriptionFromCloud: (cloudTranscription) => ipcRenderer.invoke('db-upsert-transcription-from-cloud', cloudTranscription),
- markTranscriptionSynced: (id, cloudId) => ipcRenderer.invoke('db-mark-transcription-synced', id, cloudId),
+ getPendingNotes: () => ipcRenderer.invoke("db-get-pending-notes"),
+ getPendingNoteDeletes: () => ipcRenderer.invoke("db-get-pending-note-deletes"),
+ getNoteByClientId: (clientNoteId) => ipcRenderer.invoke("db-get-note-by-client-id", clientNoteId),
+ upsertNoteFromCloud: (cloudNote, localFolderId) =>
+ ipcRenderer.invoke("db-upsert-note-from-cloud", cloudNote, localFolderId),
+ markNoteSynced: (id, cloudId) => ipcRenderer.invoke("db-mark-note-synced", id, cloudId),
+ markNoteSyncError: (id) => ipcRenderer.invoke("db-mark-note-sync-error", id),
+ hardDeleteNote: (id) => ipcRenderer.invoke("db-hard-delete-note", id),
+
+ getPendingFolders: () => ipcRenderer.invoke("db-get-pending-folders"),
+ getFolderByClientId: (clientFolderId) =>
+ ipcRenderer.invoke("db-get-folder-by-client-id", clientFolderId),
+ upsertFolderFromCloud: (cloudFolder) =>
+ ipcRenderer.invoke("db-upsert-folder-from-cloud", cloudFolder),
+ markFolderSynced: (id, cloudId) => ipcRenderer.invoke("db-mark-folder-synced", id, cloudId),
+ getFolderIdMap: () => ipcRenderer.invoke("db-get-folder-id-map"),
+
+ getPendingConversations: () => ipcRenderer.invoke("db-get-pending-conversations"),
+ getPendingConversationDeletes: () => ipcRenderer.invoke("db-get-pending-conversation-deletes"),
+ getConversationByClientId: (clientId) =>
+ ipcRenderer.invoke("db-get-conversation-by-client-id", clientId),
+ upsertConversationFromCloud: (cloudConv, messages) =>
+ ipcRenderer.invoke("db-upsert-conversation-from-cloud", cloudConv, messages),
+ markConversationSynced: (id, cloudId) =>
+ ipcRenderer.invoke("db-mark-conversation-synced", id, cloudId),
+ hardDeleteConversation: (id) => ipcRenderer.invoke("db-hard-delete-conversation", id),
+
+ getPendingTranscriptions: () => ipcRenderer.invoke("db-get-pending-transcriptions"),
+ getTranscriptionByClientId: (clientId) =>
+ ipcRenderer.invoke("db-get-transcription-by-client-id", clientId),
+ upsertTranscriptionFromCloud: (cloudTranscription) =>
+ ipcRenderer.invoke("db-upsert-transcription-from-cloud", cloudTranscription),
+ markTranscriptionSynced: (id, cloudId) =>
+ ipcRenderer.invoke("db-mark-transcription-synced", id, cloudId),
// Google Calendar
gcalStartOAuth: () => ipcRenderer.invoke("gcal-start-oauth"),
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 252d3c45b..dfbdaf647 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -5104,6 +5104,92 @@ class IPCHandlers {
}
});
+ ipcMain.handle("cloud-api-keys-list", async (event) => {
+ try {
+ const apiUrl = getApiUrl();
+ if (!apiUrl) throw new Error("OpenWhispr API URL not configured");
+
+ const cookieHeader = await getSessionCookies(event);
+ if (!cookieHeader) throw new Error("No session cookies available");
+
+ const response = await fetch(`${apiUrl}/api/v1/keys/list`, {
+ headers: { Cookie: cookieHeader },
+ });
+
+ if (response.status === 401) {
+ return { success: false, error: "Session expired", code: "AUTH_EXPIRED" };
+ }
+
+ if (!response.ok) throw new Error(`API error: ${response.status}`);
+
+ const { data } = await response.json();
+ return { success: true, keys: data.keys };
+ } catch (error) {
+ debugLogger.error("Cloud API keys list error:", error);
+ return { success: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("cloud-api-keys-create", async (event, opts) => {
+ try {
+ const apiUrl = getApiUrl();
+ if (!apiUrl) throw new Error("OpenWhispr API URL not configured");
+
+ const cookieHeader = await getSessionCookies(event);
+ if (!cookieHeader) throw new Error("No session cookies available");
+
+ const response = await fetch(`${apiUrl}/api/v1/keys/create`, {
+ method: "POST",
+ headers: { Cookie: cookieHeader, "Content-Type": "application/json" },
+ body: JSON.stringify(opts),
+ });
+
+ if (response.status === 401) {
+ return { success: false, error: "Session expired", code: "AUTH_EXPIRED" };
+ }
+
+ if (!response.ok) {
+ const errorData = await response.json().catch(() => ({}));
+ throw new Error(errorData.error?.message || `API error: ${response.status}`);
+ }
+
+ const { data } = await response.json();
+ return { success: true, ...data };
+ } catch (error) {
+ debugLogger.error("Cloud API keys create error:", error);
+ return { success: false, error: error.message };
+ }
+ });
+
+ ipcMain.handle("cloud-api-keys-revoke", async (event, id) => {
+ try {
+ const apiUrl = getApiUrl();
+ if (!apiUrl) throw new Error("OpenWhispr API URL not configured");
+
+ const cookieHeader = await getSessionCookies(event);
+ if (!cookieHeader) throw new Error("No session cookies available");
+
+ const response = await fetch(`${apiUrl}/api/v1/keys/${id}/revoke`, {
+ method: "POST",
+ headers: { Cookie: cookieHeader },
+ });
+
+ if (response.status === 401) {
+ return { success: false, error: "Session expired", code: "AUTH_EXPIRED" };
+ }
+
+ if (!response.ok) {
+ const errorData = await response.json().catch(() => ({}));
+ throw new Error(errorData.error?.message || `API error: ${response.status}`);
+ }
+
+ return { success: true, revoked: true };
+ } catch (error) {
+ debugLogger.error("Cloud API keys revoke error:", error);
+ return { success: false, error: error.message };
+ }
+ });
+
ipcMain.handle("get-stt-config", async (event) => {
try {
const apiUrl = getApiUrl();
diff --git a/src/services/ApiKeysService.ts b/src/services/ApiKeysService.ts
index 536b93594..2e83b7371 100644
--- a/src/services/ApiKeysService.ts
+++ b/src/services/ApiKeysService.ts
@@ -1,5 +1,3 @@
-import { OPENWHISPR_API_URL } from "../config/constants.js";
-
interface ApiKey {
id: string;
name: string;
@@ -21,42 +19,25 @@ interface CreateApiKeyOptions {
}
async function listApiKeys(): Promise<{ keys: ApiKey[] }> {
- const res = await fetch(`${OPENWHISPR_API_URL}/api/v1/keys/list`, {
- credentials: "include",
- });
- if (!res.ok) throw new Error(await res.text());
- const json = await res.json();
- return json.data as { keys: ApiKey[] };
+ const result = await window.electronAPI?.cloudApiKeysList?.();
+ if (!result?.success) throw new Error(result?.error ?? "Failed to list API keys");
+ return { keys: result.keys ?? [] };
}
async function createApiKey(options: CreateApiKeyOptions): Promise<CreateApiKeyResponse> {
- const body: Record<string, unknown> = {
+ const result = await window.electronAPI?.cloudApiKeysCreate?.({
name: options.name,
scopes: options.scopes,
- };
- if (options.expiresInDays != null) {
- body.expires_in_days = options.expiresInDays;
- }
-
- const res = await fetch(`${OPENWHISPR_API_URL}/api/v1/keys/create`, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- credentials: "include",
- body: JSON.stringify(body),
+ expires_in_days: options.expiresInDays ?? undefined,
});
- if (!res.ok) throw new Error(await res.text());
- const json = await res.json();
- return json.data as CreateApiKeyResponse;
+ if (!result?.success) throw new Error(result?.error ?? "Failed to create API key");
+ return result as unknown as CreateApiKeyResponse;
}
async function revokeApiKey(id: string): Promise<{ revoked: true }> {
- const res = await fetch(`${OPENWHISPR_API_URL}/api/v1/keys/${id}/revoke`, {
- method: "POST",
- credentials: "include",
- });
- if (!res.ok) throw new Error(await res.text());
- const json = await res.json();
- return json.data as { revoked: true };
+ const result = await window.electronAPI?.cloudApiKeysRevoke?.(id);
+ if (!result?.success) throw new Error(result?.error ?? "Failed to revoke API key");
+ return { revoked: true };
}
export { listApiKeys, createApiKey, revokeApiKey };
diff --git a/src/types/electron.ts b/src/types/electron.ts
index 10d61baa3..e191b03fd 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -1008,6 +1008,44 @@ declare global {
error?: string;
}>;
+ // Cloud API keys
+ cloudApiKeysList?: () => Promise<{
+ success: boolean;
+ keys?: Array<{
+ id: string;
+ name: string;
+ key_prefix: string;
+ scopes: string[];
+ last_used_at: string | null;
+ expires_at: string | null;
+ created_at: string;
+ }>;
+ error?: string;
+ code?: string;
+ }>;
+ cloudApiKeysCreate?: (opts: {
+ name: string;
+ scopes: string[];
+ expires_in_days?: number | null;
+ }) => Promise<{
+ success: boolean;
+ key?: string;
+ id?: string;
+ name?: string;
+ key_prefix?: string;
+ scopes?: string[];
+ expires_at?: string | null;
+ created_at?: string;
+ error?: string;
+ code?: string;
+ }>;
+ cloudApiKeysRevoke?: (id: string) => Promise<{
+ success: boolean;
+ revoked?: boolean;
+ error?: string;
+ code?: string;
+ }>;
+
// Cloud audio file transcription
transcribeAudioFileCloud?: (filePath: string) => Promise<{
success: boolean;