Skip to content

The agent that grows with you

fix(desktop): authenticate gated file downloads like REST

Brooklyn Nicholsoncce042790509
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index af28a90bc82..91b3224f5c4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -188,6 +188,7 @@ import { createMediaProtocolHandler, MEDIA_PROTOCOL } from './media-protocol'
import {
oauthGuardMayHardFail,
oauthSessionIsLive,
+ resolveGatedDownloadAuth,
resolveJsonBody,
resolveOauthRestAuth,
resolveReadinessProbeAuth
@@ -4858,7 +4859,8 @@ function fetchJson(url, token, options: any = {}) {
// Token-auth download that streams the response body straight to a
// user-selected destination (via finalizeGatewayDownload) instead of buffering
// the whole file in memory. The connect timeout is cleared once headers arrive
-// so a slow save dialog or a large stream doesn't trip it.
+// so a slow save dialog or a large stream doesn't trip it. `options.bearer`
+// switches the header to Authorization (RFC 8252 native flow), matching fetchJson.
function downloadViaTokenToFile(url, token, ctx, options: any = {}) {
return new Promise((resolve, reject) => {
let parsed
@@ -4884,9 +4886,9 @@ function downloadViaTokenToFile(url, token, ctx, options: any = {}) {
parsed,
{
method: 'GET',
- headers: {
- 'X-Hermes-Session-Token': token
- }
+ headers: options.bearer
+ ? { Authorization: `Bearer ${options.bearer}` }
+ : { 'X-Hermes-Session-Token': token }
},
res => {
// Headers arrived — the connection phase is done. Drop the idle timeout
@@ -7250,6 +7252,13 @@ function readGatewayErrorText(res): Promise<string> {
})
}
+async function gatedFileAuth(connection) {
+ const nativeAt =
+ connection.authMode === 'oauth' ? await ensureNativeAccessToken(connection.baseUrl).catch(() => null) : null
+
+ return resolveGatedDownloadAuth(connection.authMode, nativeAt, connection.token)
+}
+
async function saveGatewayFile(payload: any = {}) {
const filePath = gatewayFilePath(payload.path)
@@ -7272,9 +7281,17 @@ async function saveGatewayFile(payload: any = {}) {
const url = `${connection.baseUrl}${requestPath}`
try {
- return await (connection.authMode === 'oauth'
- ? downloadViaOauthSessionToFile(url, ctx)
- : downloadViaTokenToFile(url, connection.token, ctx))
+ const auth = await gatedFileAuth(connection)
+
+ if (auth.kind === 'bearer') {
+ return await downloadViaTokenToFile(url, auth.token, ctx, { bearer: auth.token })
+ }
+
+ if (auth.kind === 'cookie') {
+ return await downloadViaOauthSessionToFile(url, ctx)
+ }
+
+ return await downloadViaTokenToFile(url, auth.token, ctx)
} catch (error) {
// Desktop and the remote gateway update independently. A gateway predating
// /api/fs/download 404s here; fall back (ONLY on 404) to the older capped
@@ -7299,10 +7316,16 @@ async function saveGatewayFileViaDataUrl(connection, profile, filePath, ctx: any
)
const url = `${connection.baseUrl}${requestPath}`
+ const auth = await gatedFileAuth(connection)
+ let json: any
- const json = (
- connection.authMode === 'oauth' ? await fetchJsonViaOauthSession(url) : await fetchJson(url, connection.token)
- ) as any
+ if (auth.kind === 'bearer') {
+ json = await fetchJson(url, null, { bearer: auth.token })
+ } else if (auth.kind === 'cookie') {
+ json = await fetchJsonViaOauthSession(url)
+ } else {
+ json = await fetchJson(url, auth.token)
+ }
const dataUrl = json?.dataUrl