|
| 1 | +import { toRecord } from '@sim/utils/object' |
| 2 | +import { readResponseJsonWithLimit } from '@/lib/core/utils/stream-limits' |
| 3 | +import type { |
| 4 | + ClientCredentialAccountFields, |
| 5 | + ClientCredentialAccountMintOptions, |
| 6 | + ClientCredentialAccountMintResult, |
| 7 | +} from '@/lib/credentials/client-credential-accounts/server' |
| 8 | +import { |
| 9 | + fetchProvider, |
| 10 | + requireClientSecret, |
| 11 | + TokenServiceAccountValidationError, |
| 12 | + throwForProviderResponse, |
| 13 | +} from '@/lib/credentials/token-service-accounts/errors' |
| 14 | +import { GREENHOUSE_TOKEN_URL } from '@/lib/oauth/greenhouse' |
| 15 | + |
| 16 | +const STEP = 'greenhouse_token_mint' |
| 17 | +const MAX_GREENHOUSE_TOKEN_RESPONSE_BYTES = 64 * 1024 |
| 18 | + |
| 19 | +/** |
| 20 | + * Obtains a Harvest v3 access token for a customer's own Greenhouse API credential through the |
| 21 | + * client-credentials grant. The token acts as the credential's integration service user with the |
| 22 | + * scopes configured on the credential in Greenhouse, so the grant requests none. Greenhouse |
| 23 | + * documents no identity endpoint for this grant; a successful mint is the connect-time check. |
| 24 | + * |
| 25 | + * @see https://harvestdocs.greenhouse.io/docs/authentication |
| 26 | + */ |
| 27 | +export async function mintGreenhouseServiceAccountToken( |
| 28 | + fields: ClientCredentialAccountFields, |
| 29 | + options?: ClientCredentialAccountMintOptions |
| 30 | +): Promise<ClientCredentialAccountMintResult> { |
| 31 | + options?.signal?.throwIfAborted() |
| 32 | + const clientSecret = requireClientSecret(fields.clientSecret, STEP, 'Greenhouse') |
| 33 | + const response = await fetchProvider( |
| 34 | + GREENHOUSE_TOKEN_URL, |
| 35 | + { |
| 36 | + method: 'POST', |
| 37 | + redirect: 'error', |
| 38 | + signal: options?.signal, |
| 39 | + headers: { |
| 40 | + Accept: 'application/json', |
| 41 | + Authorization: `Basic ${Buffer.from(`${fields.clientId}:${clientSecret}`).toString('base64')}`, |
| 42 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 43 | + }, |
| 44 | + body: new URLSearchParams({ grant_type: 'client_credentials' }).toString(), |
| 45 | + }, |
| 46 | + STEP |
| 47 | + ) |
| 48 | + await throwForProviderResponse(response, STEP) |
| 49 | + |
| 50 | + let token: Record<string, unknown> |
| 51 | + try { |
| 52 | + token = toRecord( |
| 53 | + await readResponseJsonWithLimit(response, { |
| 54 | + maxBytes: MAX_GREENHOUSE_TOKEN_RESPONSE_BYTES, |
| 55 | + label: 'Greenhouse token response', |
| 56 | + signal: options?.signal, |
| 57 | + }) |
| 58 | + ) |
| 59 | + } catch { |
| 60 | + options?.signal?.throwIfAborted() |
| 61 | + throw new TokenServiceAccountValidationError('provider_unavailable', 502, { |
| 62 | + step: STEP, |
| 63 | + reason: 'provider returned an invalid or oversized response', |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + typeof token.access_token !== 'string' || |
| 69 | + !token.access_token.trim() || |
| 70 | + typeof token.expires_in !== 'number' || |
| 71 | + !Number.isSafeInteger(token.expires_in) || |
| 72 | + token.expires_in <= 0 |
| 73 | + ) { |
| 74 | + throw new TokenServiceAccountValidationError('provider_unavailable', 502, { |
| 75 | + step: STEP, |
| 76 | + reason: 'token response missing a valid access token or lifetime', |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + accessToken: token.access_token, |
| 82 | + expiresInSeconds: token.expires_in, |
| 83 | + identity: { |
| 84 | + displayName: 'Greenhouse API credential', |
| 85 | + principal: null, |
| 86 | + auditMetadata: { greenhouseClientId: fields.clientId }, |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
0 commit comments