-
Notifications
You must be signed in to change notification settings - Fork 56
refactor(desktop): Manage auth token in renderer instead of main thread for cleaner code / better interop with better auth #753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d08d2a8
feat(desktop): add core auth infrastructure (renderer-based)
saddlepaddle 7533e7f
feat(desktop): update TRPCProvider with API client and auth providers
saddlepaddle fc39b06
feat(desktop): update all components to use electronTrpc and authClient
saddlepaddle 848edce
fix(desktop): rename remaining trpcClient references to electronTrpcC…
saddlepaddle ea633d9
Huh
saddlepaddle 6de8c91
WIP
saddlepaddle 692bccd
WIP
saddlepaddle 5f452a9
WIP
saddlepaddle 92a194e
WIP
saddlepaddle 8c4a431
Hopefully done
saddlepaddle 42f40d4
Hopefully done
saddlepaddle d3c5bbc
Hopefully done
saddlepaddle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
apps/desktop/src/lib/trpc/routers/auth/utils/auth-functions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { EventEmitter } from "node:events"; | ||
| import fs from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import { PROTOCOL_SCHEMES } from "@superset/shared/constants"; | ||
| import { SUPERSET_HOME_DIR } from "main/lib/app-environment"; | ||
| import { decrypt, encrypt } from "./crypto-storage"; | ||
|
|
||
| interface StoredAuth { | ||
| token: string; | ||
| expiresAt: string; | ||
| } | ||
|
|
||
| export const TOKEN_FILE = join(SUPERSET_HOME_DIR, "auth-token.enc"); | ||
| export const stateStore = new Map<string, number>(); | ||
|
|
||
| /** | ||
| * Event emitter for auth-related events. | ||
| * Used by tRPC subscription to notify renderer of token changes. | ||
| */ | ||
| export const authEvents = new EventEmitter(); | ||
|
|
||
| /** | ||
| * Load token from encrypted disk storage. | ||
| */ | ||
| export async function loadToken(): Promise<{ | ||
| token: string | null; | ||
| expiresAt: string | null; | ||
| }> { | ||
| try { | ||
| const data = decrypt(await fs.readFile(TOKEN_FILE)); | ||
| const parsed: StoredAuth = JSON.parse(data); | ||
| return { token: parsed.token, expiresAt: parsed.expiresAt }; | ||
| } catch { | ||
| return { token: null, expiresAt: null }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Persist token to encrypted disk storage and notify subscribers. | ||
| */ | ||
| export async function saveToken({ | ||
| token, | ||
| expiresAt, | ||
| }: { | ||
| token: string; | ||
| expiresAt: string; | ||
| }): Promise<void> { | ||
| const storedAuth: StoredAuth = { token, expiresAt }; | ||
| await fs.writeFile(TOKEN_FILE, encrypt(JSON.stringify(storedAuth))); | ||
| authEvents.emit("token-saved", { token, expiresAt }); | ||
| } | ||
|
|
||
| /** | ||
| * Handle OAuth callback from deep link. | ||
| * Validates CSRF state and saves token. | ||
| */ | ||
| export async function handleAuthCallback(params: { | ||
| token: string; | ||
| expiresAt: string; | ||
| state: string; | ||
| }): Promise<{ success: boolean; error?: string }> { | ||
| if (!stateStore.has(params.state)) { | ||
| return { success: false, error: "Invalid or expired auth session" }; | ||
| } | ||
| stateStore.delete(params.state); | ||
|
|
||
| await saveToken({ token: params.token, expiresAt: params.expiresAt }); | ||
|
|
||
| return { success: true }; | ||
| } | ||
|
|
||
| /** | ||
| * Parse and validate auth deep link URL. | ||
| */ | ||
| export function parseAuthDeepLink( | ||
| url: string, | ||
| ): { token: string; expiresAt: string; state: string } | null { | ||
| try { | ||
| const parsed = new URL(url); | ||
| const validProtocols = [ | ||
| `${PROTOCOL_SCHEMES.PROD}:`, | ||
| `${PROTOCOL_SCHEMES.DEV}:`, | ||
| ]; | ||
| if (!validProtocols.includes(parsed.protocol)) return null; | ||
| if (parsed.host !== "auth" || parsed.pathname !== "/callback") return null; | ||
|
|
||
| const token = parsed.searchParams.get("token"); | ||
| const expiresAt = parsed.searchParams.get("expiresAt"); | ||
| const state = parsed.searchParams.get("state"); | ||
| if (!token || !expiresAt || !state) return null; | ||
| return { token, expiresAt, state }; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silent error swallowing in signOut.
The empty
catch {}block silently swallows errors when unlinking the token file. Per coding guidelines, never swallow errors silently—at minimum log them with context. While the file may legitimately not exist (e.g., already signed out), other errors (permissions, disk issues) should be logged.Suggested fix
signOut: publicProcedure.mutation(async () => { try { await fs.unlink(TOKEN_FILE); - } catch {} + } catch (err) { + // ENOENT is expected if already signed out; log other errors + if ((err as NodeJS.ErrnoException).code !== "ENOENT") { + console.error("[auth/signOut] Failed to remove token file:", err); + } + } return { success: true }; }),📝 Committable suggestion
🤖 Prompt for AI Agents