Repository navigation
Persist rotated OAuth refresh tokens in codemode helpers #125
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
kentcdodds
merged 3 commits into
main
from
cursor/rotated-refresh-token-persistence-86fa
Apr 1, 2026
Merged
Changes from 2 commits
Commits
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
198 changes: 198 additions & 0 deletions
198
packages/worker/src/mcp/execute-modules/codemode-utils.node.test.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,198 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { | ||
| createAuthenticatedFetch, | ||
| createExecuteHelperPrelude, | ||
| getExecuteHelperCapabilityNames, | ||
| refreshAccessToken, | ||
| } from './codemode-utils.ts' | ||
|
|
||
| type SecretSetCall = { | ||
| name: string | ||
| value: string | ||
| scope: string | ||
| } | ||
|
|
||
| const spotifyConnector = { | ||
| name: 'spotify', | ||
| tokenUrl: 'https://accounts.spotify.test/api/token', | ||
| apiBaseUrl: 'https://api.spotify.test/v1', | ||
| flow: 'pkce' as const, | ||
| clientIdValueName: 'spotifyClientId', | ||
| clientSecretSecretName: null, | ||
| accessTokenSecretName: 'spotifyAccessToken', | ||
| refreshTokenSecretName: 'spotifyRefreshToken', | ||
| requiredHosts: ['api.spotify.test'], | ||
| } | ||
|
|
||
| function createCodemode(payload: Record<string, unknown>) { | ||
| const secretSetCalls: Array<SecretSetCall> = [] | ||
| const codemode = { | ||
| async connector_get({ name }: { name: string }) { | ||
| expect(name).toBe('spotify') | ||
| return { connector: spotifyConnector } | ||
| }, | ||
| async value_get({ name }: { name: string }) { | ||
| expect(name).toBe('spotifyClientId') | ||
| return { value: 'spotify-client-id' } | ||
| }, | ||
| async secret_set(args: SecretSetCall) { | ||
| secretSetCalls.push(args) | ||
| return { | ||
| name: args.name, | ||
| scope: args.scope, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| const fetchCalls: Array<Request> = [] | ||
| const fetchStub = async (input: RequestInfo | URL, init?: RequestInit) => { | ||
| const request = new Request(input, init) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| fetchCalls.push(request) | ||
| if (request.url === spotifyConnector.tokenUrl) { | ||
| return new Response(JSON.stringify(payload), { | ||
| status: 200, | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) | ||
| } | ||
| return new Response(JSON.stringify({ ok: true }), { | ||
| status: 200, | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) | ||
| } | ||
|
|
||
| return { codemode, secretSetCalls, fetchCalls, fetchStub } | ||
| } | ||
|
|
||
| async function withPatchedFetch<T>( | ||
| fetchImpl: typeof globalThis.fetch, | ||
| callback: () => Promise<T>, | ||
| ) { | ||
| const originalFetch = globalThis.fetch | ||
| globalThis.fetch = fetchImpl | ||
| try { | ||
| return await callback() | ||
| } finally { | ||
| globalThis.fetch = originalFetch | ||
| } | ||
| } | ||
|
|
||
| test('refreshAccessToken persists rotated refresh token and access token', async () => { | ||
| const { codemode, secretSetCalls, fetchStub, fetchCalls } = createCodemode({ | ||
| access_token: 'new-access-token', | ||
| refresh_token: 'new-refresh-token', | ||
| }) | ||
|
|
||
| const accessToken = await withPatchedFetch(fetchStub, () => | ||
| refreshAccessToken(codemode, 'spotify'), | ||
|
Check failure on line 86 in packages/worker/src/mcp/execute-modules/codemode-utils.node.test.ts
|
||
| ) | ||
|
|
||
| expect(accessToken).toBe('new-access-token') | ||
| expect(secretSetCalls).toEqual([ | ||
| { | ||
| name: 'spotifyRefreshToken', | ||
| value: 'new-refresh-token', | ||
| scope: 'user', | ||
| }, | ||
| { | ||
| name: 'spotifyAccessToken', | ||
| value: 'new-access-token', | ||
| scope: 'user', | ||
| }, | ||
| ]) | ||
| expect(fetchCalls).toHaveLength(1) | ||
| expect(fetchCalls[0]?.method).toBe('POST') | ||
| expect(await fetchCalls[0]?.text()).toContain( | ||
| 'refresh_token=%7B%7Bsecret%3AspotifyRefreshToken%7Cscope%3Duser%7D%7D', | ||
| ) | ||
| }) | ||
|
|
||
| test('createAuthenticatedFetch persists refreshed access token even without refresh token rotation', async () => { | ||
| const { codemode, secretSetCalls, fetchStub, fetchCalls } = createCodemode({ | ||
| access_token: 'new-access-token', | ||
| }) | ||
|
|
||
| const authenticatedFetch = await withPatchedFetch(fetchStub, () => | ||
| createAuthenticatedFetch(codemode, 'spotify'), | ||
|
Check failure on line 115 in packages/worker/src/mcp/execute-modules/codemode-utils.node.test.ts
|
||
| ) | ||
| const response = await withPatchedFetch(fetchStub, () => | ||
| authenticatedFetch('/me?market=US'), | ||
| ) | ||
|
|
||
| expect(secretSetCalls).toEqual([ | ||
| { | ||
| name: 'spotifyAccessToken', | ||
| value: 'new-access-token', | ||
| scope: 'user', | ||
| }, | ||
| ]) | ||
| expect(fetchCalls).toHaveLength(2) | ||
| expect(fetchCalls[1]?.url).toBe('https://api.spotify.test/v1/me?market=US') | ||
| expect(fetchCalls[1]?.headers.get('authorization')).toBe( | ||
| 'Bearer new-access-token', | ||
| ) | ||
| expect(await response.json()).toEqual({ ok: true }) | ||
| }) | ||
|
|
||
| test('createExecuteHelperPrelude persists rotated refresh token and access token', async () => { | ||
| const { codemode, secretSetCalls, fetchStub, fetchCalls } = createCodemode({ | ||
| access_token: 'new-access-token', | ||
| refresh_token: 'new-refresh-token', | ||
| }) | ||
| const prelude = createExecuteHelperPrelude() | ||
| const createSandboxHelpers = new Function( | ||
| 'codemode', | ||
| `${prelude}; return { refreshAccessToken, createAuthenticatedFetch };`, | ||
| ) as ( | ||
| codemode: typeof codemode, | ||
| ) => { | ||
| refreshAccessToken: (providerName: string) => Promise<string> | ||
| createAuthenticatedFetch: ( | ||
| providerName: string, | ||
| ) => Promise<(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>> | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| const helpers = createSandboxHelpers(codemode) | ||
| const accessToken = await withPatchedFetch(fetchStub, () => | ||
| helpers.refreshAccessToken('spotify'), | ||
| ) | ||
| const authenticatedFetch = await withPatchedFetch(fetchStub, () => | ||
| helpers.createAuthenticatedFetch('spotify'), | ||
| ) | ||
| await withPatchedFetch(fetchStub, () => authenticatedFetch('/me')) | ||
|
|
||
| expect(accessToken).toBe('new-access-token') | ||
| expect(secretSetCalls).toEqual([ | ||
| { | ||
| name: 'spotifyRefreshToken', | ||
| value: 'new-refresh-token', | ||
| scope: 'user', | ||
| }, | ||
| { | ||
| name: 'spotifyAccessToken', | ||
| value: 'new-access-token', | ||
| scope: 'user', | ||
| }, | ||
| { | ||
| name: 'spotifyRefreshToken', | ||
| value: 'new-refresh-token', | ||
| scope: 'user', | ||
| }, | ||
| { | ||
| name: 'spotifyAccessToken', | ||
| value: 'new-access-token', | ||
| scope: 'user', | ||
| }, | ||
| ]) | ||
| expect(fetchCalls).toHaveLength(3) | ||
| expect(fetchCalls[2]?.headers.get('authorization')).toBe( | ||
| 'Bearer new-access-token', | ||
| ) | ||
| }) | ||
|
|
||
| test('getExecuteHelperCapabilityNames includes secret_set for refresh persistence', () => { | ||
| expect(getExecuteHelperCapabilityNames()).toEqual([ | ||
| 'connector_get', | ||
| 'value_get', | ||
| 'secret_set', | ||
| ]) | ||
| }) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.