Repository navigation
feat(apollo): add optional OAuth connections #9922
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
Open
waleedlatif1
wants to merge
1
commit into
staging
Choose a base branch
from
codex/apollo-oauth
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+573
−35
Open
Changes from all 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18513,6 +18513,7 @@ | |
| "ramp", | ||
| "helpscout", | ||
| "hubspot", | ||
| "apollo", | ||
| "linkedin", | ||
| "instagram", | ||
| "salesforce", | ||
|
|
||
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
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
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,100 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { createApolloProvider, formatApolloAuthorizationUrl } from '@/lib/oauth/apollo' | ||
|
|
||
| const connector = createApolloProvider({ | ||
| clientId: 'fixture-client', | ||
| clientSecret: 'fixture-secret', | ||
| redirectURI: 'https://example.invalid/api/auth/oauth2/callback/apollo', | ||
| scopes: ['read_user_profile'], | ||
| }) | ||
|
|
||
| describe('Apollo OAuth protocol', () => { | ||
| it('preserves state and callback encoding inside Apollo’s authorization fragment', () => { | ||
| const callback = 'https://example.invalid/api/auth/oauth2/callback/apollo' | ||
| const state = 'fixture+state/with=padding' | ||
| const generated = new URL('https://app.apollo.io/#/oauth/authorize') | ||
| generated.searchParams.set('client_id', 'fixture-client') | ||
| generated.searchParams.set('redirect_uri', callback) | ||
| generated.searchParams.set('state', state) | ||
| generated.searchParams.set('scope', 'read_user_profile contacts_search') | ||
| generated.searchParams.set('response_type', 'code') | ||
|
|
||
| const authorization = new URL(formatApolloAuthorizationUrl(generated.toString())) | ||
| expect(authorization.origin).toBe('https://app.apollo.io') | ||
| expect(authorization.search).toBe('') | ||
| const [path, query] = authorization.hash.slice(1).split('?') | ||
| expect(path).toBe('/oauth/authorize') | ||
| const params = new URLSearchParams(query) | ||
| expect(params.get('state')).toBe(state) | ||
| expect(params.get('redirect_uri')).toBe(callback) | ||
| expect(params.get('scope')).toBe('read_user_profile contacts_search') | ||
| expect(params.get('client_id')).toBe('fixture-client') | ||
| }) | ||
|
|
||
| it('refuses an unexpected authorization destination', () => { | ||
| expect(() => | ||
| formatApolloAuthorizationUrl('https://example.invalid/?state=fixture#/oauth/authorize') | ||
| ).toThrow('Invalid Apollo authorization URL') | ||
| }) | ||
|
|
||
| it('uses the authenticated profile without asserting that Apollo verified its email', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue( | ||
| Response.json({ | ||
| id: 'fixture-user', | ||
| first_name: 'Example', | ||
| last_name: 'User', | ||
| email: 'fixture@example.invalid', | ||
| }) | ||
| ) | ||
| ) | ||
| if (!connector.getUserInfo) throw new Error('Apollo needs a profile lookup') | ||
| const profile = await connector.getUserInfo({ accessToken: 'fixture-access' }) | ||
| expect(profile).toMatchObject({ | ||
| name: 'Example User', | ||
| email: 'fixture@example.invalid', | ||
| emailVerified: false, | ||
| }) | ||
| expect(profile?.id).toMatch(/^fixture-user-/) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { email: 'fixture@example.invalid' }, | ||
| { id: '', email: 'fixture@example.invalid' }, | ||
| { id: 'fixture-user' }, | ||
| ])('refuses a profile without a usable provider identity and email', async (payload) => { | ||
| vi.stubGlobal('fetch', vi.fn().mockResolvedValue(Response.json(payload))) | ||
| if (!connector.getUserInfo) throw new Error('Apollo needs a profile lookup') | ||
| await expect(connector.getUserInfo({ accessToken: 'fixture-access' })).rejects.toThrow( | ||
| 'Invalid Apollo user profile' | ||
| ) | ||
| }) | ||
|
|
||
| it('does not expose an upstream error body containing a credential', async () => { | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| vi.fn().mockResolvedValue(Response.json({ error: 'fixture-access' }, { status: 401 })) | ||
| ) | ||
| if (!connector.getUserInfo) throw new Error('Apollo needs a profile lookup') | ||
| await expect(connector.getUserInfo({ accessToken: 'fixture-access' })).rejects.toThrow( | ||
| 'Apollo user profile request failed with HTTP 401' | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| () => new Response('{"secret":"fixture-access", malformed json'), | ||
| () => | ||
| Response.json({ | ||
| id: 'fixture-user', | ||
| email: 'fixture@example.invalid', | ||
| extra: 'x'.repeat(65_537), | ||
| }), | ||
| ])('refuses malformed or oversized profiles without echoing their bodies', async (response) => { | ||
| vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response())) | ||
| if (!connector.getUserInfo) throw new Error('Apollo needs a profile lookup') | ||
| await expect(connector.getUserInfo({ accessToken: 'fixture-access' })).rejects.toThrow( | ||
| 'Invalid Apollo user profile' | ||
| ) | ||
| }) | ||
| }) |
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,72 @@ | ||
| import { generateId } from '@sim/utils/id' | ||
| import type { GenericOAuthConfig } from 'better-auth/plugins' | ||
| import { z } from 'zod' | ||
| import { readResponseJsonWithLimit } from '@/lib/core/utils/stream-limits' | ||
|
|
||
| export const APOLLO_TOKEN_URL = 'https://app.apollo.io/api/v1/oauth/token' | ||
|
|
||
| const profileSchema = z.object({ | ||
| id: z.string().trim().min(1), | ||
| email: z.email(), | ||
| first_name: z.string().nullish(), | ||
| last_name: z.string().nullish(), | ||
| }) | ||
|
|
||
| /** Moves Better Auth's query into the fragment Apollo's client-side authorize page reads. */ | ||
| export function formatApolloAuthorizationUrl(value: string): string { | ||
| const url = new URL(value) | ||
| if ( | ||
| url.origin !== 'https://app.apollo.io' || | ||
| url.pathname !== '/' || | ||
| url.hash !== '#/oauth/authorize' | ||
| ) { | ||
| throw new Error('Invalid Apollo authorization URL') | ||
| } | ||
| const query = url.search | ||
| url.search = '' | ||
| url.hash = `/oauth/authorize${query}` | ||
| return url.toString() | ||
| } | ||
|
|
||
| /** Connects an Apollo partner app using its documented OAuth and current-user profile endpoints. */ | ||
| export function createApolloProvider( | ||
| config: Pick<GenericOAuthConfig, 'clientId' | 'clientSecret' | 'redirectURI' | 'scopes'> | ||
| ): GenericOAuthConfig { | ||
| return { | ||
| ...config, | ||
| providerId: 'apollo', | ||
| authorizationUrl: 'https://app.apollo.io/#/oauth/authorize', | ||
| tokenUrl: APOLLO_TOKEN_URL, | ||
| authentication: 'post', | ||
| getUserInfo: async (tokens) => { | ||
| // boundary-raw-fetch: Apollo's external current-user endpoint verifies the OAuth token's identity. | ||
| const response = await fetch('https://app.apollo.io/api/v1/users/api_profile', { | ||
| headers: { Authorization: `Bearer ${tokens.accessToken}` }, | ||
| redirect: 'error', | ||
| signal: AbortSignal.timeout(15_000), | ||
| }) | ||
| if (!response.ok) { | ||
| await response.body?.cancel() | ||
| throw new Error(`Apollo user profile request failed with HTTP ${response.status}`) | ||
| } | ||
| let payload: unknown | ||
| try { | ||
| payload = await readResponseJsonWithLimit(response, { | ||
| maxBytes: 64 * 1024, | ||
| label: 'Apollo user profile', | ||
| }) | ||
| } catch { | ||
| throw new Error('Invalid Apollo user profile') | ||
| } | ||
| const parsed = profileSchema.safeParse(payload) | ||
| if (!parsed.success) throw new Error('Invalid Apollo user profile') | ||
| const profile = parsed.data | ||
| return { | ||
| id: `${profile.id}-${generateId()}`, | ||
| name: [profile.first_name, profile.last_name].filter(Boolean).join(' ') || profile.email, | ||
| email: profile.email, | ||
| emailVerified: false, | ||
| } | ||
| }, | ||
| } | ||
| } |
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.
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.
If
APOLLO_CLIENT_IDorAPOLLO_CLIENT_SECRETis missing, changing this picker tocredentialKind: 'any'still shows “Connect Apollo account.” The merged picker always includes that action, but the server requires a configured client before starting OAuth. Self-hosted users can open and submit a connection that cannot work.Hide the OAuth action unless the client is ready, while keeping saved credentials and API-key setup available.
Knowledge Base Used: Integrations, connectors, and tools