Repository navigation
feat(greenhouse): connect Harvest v3 with a customer-owned API credential #9214
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
+221
−2
Merged
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
98 changes: 98 additions & 0 deletions
98
apps/sim/lib/credentials/client-credential-accounts/minters/greenhouse.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,98 @@ | ||
| import { jsonResponse } from '@sim/testing/helpers/http' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { mintGreenhouseServiceAccountToken } from '@/lib/credentials/client-credential-accounts/minters/greenhouse' | ||
|
|
||
| const FIELDS = { clientId: 'greenhouse-client', clientSecret: 'greenhouse-secret', orgId: '' } | ||
|
|
||
| const mockFetch = vi.fn<typeof fetch>() | ||
|
|
||
| describe('Greenhouse client credential exchange', () => { | ||
| beforeEach(() => { | ||
| mockFetch.mockReset() | ||
| vi.stubGlobal('fetch', mockFetch) | ||
| }) | ||
|
|
||
| it('exchanges the credential with a scope-free client credentials grant', async () => { | ||
| const requests: Request[] = [] | ||
| mockFetch.mockImplementation(async (input, init) => { | ||
| const request = new Request(input, init) | ||
| requests.push(request) | ||
| expect(request.url).toBe('https://auth.greenhouse.io/token') | ||
| expect(request.method).toBe('POST') | ||
| expect(request.redirect).toBe('error') | ||
| const [scheme, encodedCredentials = ''] = (request.headers.get('authorization') ?? '').split( | ||
| ' ' | ||
| ) | ||
| expect(scheme).toBe('Basic') | ||
| expect(Buffer.from(encodedCredentials, 'base64').toString('utf8')).toBe( | ||
| 'greenhouse-client:greenhouse-secret' | ||
| ) | ||
| expect(request.headers.get('content-type')).toBe('application/x-www-form-urlencoded') | ||
| expect([...new URLSearchParams(await request.text())]).toEqual([ | ||
| ['grant_type', 'client_credentials'], | ||
| ]) | ||
| return jsonResponse({ | ||
| access_token: 'greenhouse-access', | ||
| token_type: 'Bearer', | ||
| expires_in: 3600, | ||
| }) | ||
| }) | ||
|
|
||
| const result = await mintGreenhouseServiceAccountToken(FIELDS) | ||
|
|
||
| expect(requests).toHaveLength(1) | ||
| expect(result).toMatchObject({ accessToken: 'greenhouse-access', expiresInSeconds: 3600 }) | ||
| expect(result.identity).toMatchObject({ | ||
| principal: null, | ||
| auditMetadata: { greenhouseClientId: 'greenhouse-client' }, | ||
| }) | ||
| expect(JSON.stringify(result.identity)).not.toContain(FIELDS.clientSecret) | ||
| expect(JSON.stringify(result.identity)).not.toContain('greenhouse-access') | ||
| }) | ||
|
|
||
| it.each([ | ||
| [401, 'invalid_credentials'], | ||
| [403, 'invalid_credentials'], | ||
| [429, 'provider_unavailable'], | ||
| [503, 'provider_unavailable'], | ||
| ])('maps an HTTP %i token response to %s', async (status, code) => { | ||
| mockFetch.mockResolvedValue(jsonResponse({ error: 'denied' }, { status })) | ||
|
|
||
| await expect(mintGreenhouseServiceAccountToken(FIELDS)).rejects.toMatchObject({ code, status }) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['no access token', { token_type: 'Bearer', expires_in: 3600 }], | ||
| ['a blank access token', { access_token: ' ', expires_in: 3600 }], | ||
| ['no lifetime', { access_token: 'greenhouse-access' }], | ||
| ['a non-positive lifetime', { access_token: 'greenhouse-access', expires_in: 0 }], | ||
| ['a fractional lifetime', { access_token: 'greenhouse-access', expires_in: 1.5 }], | ||
| ])('refuses a token response with %s', async (_label, body) => { | ||
| mockFetch.mockResolvedValue(jsonResponse(body)) | ||
|
|
||
| await expect(mintGreenhouseServiceAccountToken(FIELDS)).rejects.toMatchObject({ | ||
| code: 'provider_unavailable', | ||
| status: 502, | ||
| }) | ||
| }) | ||
|
|
||
| it('refuses an oversized token response', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| new Response(JSON.stringify({ access_token: 'x'.repeat(70 * 1024), expires_in: 3600 }), { | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }) | ||
| ) | ||
|
|
||
| await expect(mintGreenhouseServiceAccountToken(FIELDS)).rejects.toMatchObject({ | ||
| code: 'provider_unavailable', | ||
| status: 502, | ||
| }) | ||
| }) | ||
|
|
||
| it('never sends a request without a client secret', async () => { | ||
| await expect( | ||
| mintGreenhouseServiceAccountToken({ ...FIELDS, clientSecret: undefined }) | ||
| ).rejects.toMatchObject({ code: 'invalid_credentials', status: 400 }) | ||
| expect(mockFetch).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
89 changes: 89 additions & 0 deletions
89
apps/sim/lib/credentials/client-credential-accounts/minters/greenhouse.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,89 @@ | ||
| import { toRecord } from '@sim/utils/object' | ||
| import { readResponseJsonWithLimit } from '@/lib/core/utils/stream-limits' | ||
| import type { | ||
| ClientCredentialAccountFields, | ||
| ClientCredentialAccountMintOptions, | ||
| ClientCredentialAccountMintResult, | ||
| } from '@/lib/credentials/client-credential-accounts/server' | ||
| import { | ||
| fetchProvider, | ||
| requireClientSecret, | ||
| TokenServiceAccountValidationError, | ||
| throwForProviderResponse, | ||
| } from '@/lib/credentials/token-service-accounts/errors' | ||
| import { GREENHOUSE_TOKEN_URL } from '@/lib/oauth/greenhouse' | ||
|
|
||
| const STEP = 'greenhouse_token_mint' | ||
| const MAX_GREENHOUSE_TOKEN_RESPONSE_BYTES = 64 * 1024 | ||
|
|
||
| /** | ||
| * Obtains a Harvest v3 access token for a customer's own Greenhouse API credential through the | ||
| * client-credentials grant. The token acts as the credential's integration service user with the | ||
| * scopes configured on the credential in Greenhouse, so the grant requests none. Greenhouse | ||
| * documents no identity endpoint for this grant; a successful mint is the connect-time check. | ||
| * | ||
| * @see https://harvestdocs.greenhouse.io/docs/authentication | ||
| */ | ||
| export async function mintGreenhouseServiceAccountToken( | ||
| fields: ClientCredentialAccountFields, | ||
| options?: ClientCredentialAccountMintOptions | ||
| ): Promise<ClientCredentialAccountMintResult> { | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| options?.signal?.throwIfAborted() | ||
| const clientSecret = requireClientSecret(fields.clientSecret, STEP, 'Greenhouse') | ||
| const response = await fetchProvider( | ||
| GREENHOUSE_TOKEN_URL, | ||
| { | ||
| method: 'POST', | ||
| redirect: 'error', | ||
| signal: options?.signal, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| Authorization: `Basic ${Buffer.from(`${fields.clientId}:${clientSecret}`).toString('base64')}`, | ||
| 'Content-Type': 'application/x-www-form-urlencoded', | ||
| }, | ||
| body: new URLSearchParams({ grant_type: 'client_credentials' }).toString(), | ||
| }, | ||
| STEP | ||
| ) | ||
| await throwForProviderResponse(response, STEP) | ||
|
|
||
| let token: Record<string, unknown> | ||
| try { | ||
| token = toRecord( | ||
| await readResponseJsonWithLimit(response, { | ||
| maxBytes: MAX_GREENHOUSE_TOKEN_RESPONSE_BYTES, | ||
| label: 'Greenhouse token response', | ||
| signal: options?.signal, | ||
| }) | ||
| ) | ||
| } catch { | ||
| options?.signal?.throwIfAborted() | ||
| throw new TokenServiceAccountValidationError('provider_unavailable', 502, { | ||
| step: STEP, | ||
| reason: 'provider returned an invalid or oversized response', | ||
| }) | ||
| } | ||
|
|
||
| if ( | ||
| typeof token.access_token !== 'string' || | ||
| !token.access_token.trim() || | ||
| typeof token.expires_in !== 'number' || | ||
| !Number.isSafeInteger(token.expires_in) || | ||
| token.expires_in <= 0 | ||
| ) { | ||
| throw new TokenServiceAccountValidationError('provider_unavailable', 502, { | ||
| step: STEP, | ||
| reason: 'token response missing a valid access token or lifetime', | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| accessToken: token.access_token, | ||
| expiresInSeconds: token.expires_in, | ||
| identity: { | ||
| displayName: 'Greenhouse API credential', | ||
| principal: null, | ||
| auditMetadata: { greenhouseClientId: fields.clientId }, | ||
| }, | ||
| } | ||
| } | ||
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
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.