Skip to content

Commit 22f2bf8

Browse files
chore: apply pr-reviewer fixes for #137
1 parent 8d1b1c2 commit 22f2bf8

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/main/auth.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,20 @@ describe('getAccountWorkspaceId', () => {
381381
)
382382
})
383383

384+
it('canonicalizes legacy agentrelay.dev metadata used by getApiUrl callers', async () => {
385+
writeAuthJson(userDataDir, {
386+
accessToken: 'cld_at_legacy_meta',
387+
refreshToken: 'cld_rt_legacy_meta',
388+
apiUrl: 'https://agentrelay.dev/cloud'
389+
})
390+
391+
const { getAccessToken, getApiUrl } = await import('./auth')
392+
await expect(getAccessToken()).resolves.toBe('cld_at_legacy_meta')
393+
394+
expect(getApiUrl()).toBe('https://agentrelay.com/cloud')
395+
expect(readMeta(userDataDir)?.apiUrl).toBe('https://agentrelay.com/cloud')
396+
})
397+
384398
it('throws cloud-auth-required when whoami rejects the access token', async () => {
385399
writeAuthJson(userDataDir, {
386400
accessToken: 'cld_at_401',

src/main/auth.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from './schemas'
1616

1717
const CLOUD_API_URL = process.env.RELAY_CLOUD_URL || 'https://agentrelay.com/cloud'
18+
const LEGACY_CLOUD_API_URL = 'https://agentrelay.dev/cloud'
1819
const TOKEN_EXPIRY_BUFFER_MS = 60_000
1920
const WHOAMI_REQUEST_TIMEOUT_MS = 10_000
2021
const ACCOUNT_WORKSPACE_RETRY_ATTEMPTS = 8
@@ -146,8 +147,9 @@ function delay(ms: number): Promise<void> {
146147

147148
function saveAuthMeta(tokens: Pick<StoredTokens, 'apiUrl' | 'user'> & Partial<Pick<StoredTokens, 'accessToken'>>): void {
148149
const previous = loadAuthMeta()
150+
const apiUrl = normalizeCloudApiUrl(tokens.apiUrl)
149151
const accountKey = tokens.accessToken
150-
? deriveCloudAuthAccountKey(tokens.apiUrl, tokens.accessToken, tokens.user)
152+
? deriveCloudAuthAccountKey(apiUrl, tokens.accessToken, tokens.user)
151153
: undefined
152154
const tokenHash = tokens.accessToken ? accountWorkspaceTokenHash(tokens.accessToken) : undefined
153155
const accountWorkspace =
@@ -162,7 +164,7 @@ function saveAuthMeta(tokens: Pick<StoredTokens, 'apiUrl' | 'user'> & Partial<Pi
162164
}
163165
: undefined
164166
const meta = {
165-
apiUrl: tokens.apiUrl,
167+
apiUrl,
166168
user: tokens.user,
167169
...(accountWorkspace ? { accountWorkspace } : {})
168170
}
@@ -173,8 +175,9 @@ function loadAuthMeta(): AuthMeta {
173175
try {
174176
const parsed = AuthMetaSchema.safeParse(JSON.parse(readFileSync(getAuthMetaPath(), 'utf8')))
175177
if (!parsed.success) return { apiUrl: CLOUD_API_URL }
178+
const apiUrl = (parsed.data.apiUrl?.trim() || CLOUD_API_URL).replace(/\/+$/, '')
176179
return {
177-
apiUrl: parsed.data.apiUrl?.trim() || CLOUD_API_URL,
180+
apiUrl: apiUrl === LEGACY_CLOUD_API_URL ? CLOUD_API_URL : apiUrl,
178181
user: parsed.data.user,
179182
accountWorkspace: parsed.data.accountWorkspace
180183
}
@@ -321,7 +324,7 @@ function accountWorkspaceIdFromWhoami(value: unknown): string | undefined {
321324
function saveAccountWorkspaceCache(auth: CloudAuth, workspaceId: string): void {
322325
const previous = loadAuthMeta()
323326
const meta = {
324-
apiUrl: auth.apiUrl || previous.apiUrl?.trim() || CLOUD_API_URL,
327+
apiUrl: normalizeCloudApiUrl(auth.apiUrl || previous.apiUrl),
325328
user: previous.user,
326329
accountWorkspace: {
327330
accountKey: auth.accountKey,
@@ -578,7 +581,7 @@ async function performTokenRefresh(stored: StoredTokens): Promise<StoredTokens |
578581

579582
export function getApiUrl(): string {
580583
if (hasStoredTokens()) {
581-
return loadAuthMeta().apiUrl || CLOUD_API_URL
584+
return normalizeCloudApiUrl(loadAuthMeta().apiUrl)
582585
}
583586
return CLOUD_API_URL
584587
}
@@ -600,7 +603,7 @@ function cloudAuthFromStored(tokens: StoredTokens): CloudAuth {
600603

601604
function normalizeCloudApiUrl(url: string | undefined): string {
602605
const normalized = (url || getApiUrl()).trim().replace(/\/+$/, '')
603-
if (normalized === 'https://agentrelay.dev/cloud') return CLOUD_API_URL
606+
if (normalized === LEGACY_CLOUD_API_URL) return CLOUD_API_URL
604607
return normalized
605608
}
606609

@@ -699,8 +702,9 @@ export async function getAccountWorkspaceId(options: AccountWorkspaceIdOptions =
699702
if (!auth) throw new Error('cloud-auth-required')
700703

701704
const cached = loadAuthMeta().accountWorkspace
702-
if (accountWorkspaceCacheMatches(cached, auth)) {
703-
return cached.workspaceId.trim()
705+
const cachedWorkspaceId = cached?.workspaceId.trim()
706+
if (cachedWorkspaceId && accountWorkspaceCacheMatches(cached, auth)) {
707+
return cachedWorkspaceId
704708
}
705709

706710
const retryAttempts = Math.max(1, Math.floor(options.retryAttempts ?? 1))

0 commit comments

Comments
 (0)