|
| 1 | +import type { H3Event } from 'h3' |
| 2 | +import { eventHandler, getQuery, sendRedirect } from 'h3' |
| 3 | +import { withQuery } from 'ufo' |
| 4 | +import defu from 'defu' |
| 5 | +import { |
| 6 | + getOAuthRedirectURL, |
| 7 | + handleAccessTokenErrorResponse, |
| 8 | + handleMissingConfiguration, |
| 9 | + requestAccessToken, |
| 10 | +} from '../utils' |
| 11 | +import { useRuntimeConfig } from '#imports' |
| 12 | +import type { OAuthConfig } from '#auth-utils' |
| 13 | + |
| 14 | +export interface OAuthHubspotConfig { |
| 15 | + /** |
| 16 | + * Hubspot OAuth Client ID |
| 17 | + * @default process.env.NUXT_OAUTH_HUBSPOT_CLIENT_ID |
| 18 | + */ |
| 19 | + clientId?: string |
| 20 | + |
| 21 | + /** |
| 22 | + * Hubspot OAuth Client Secret |
| 23 | + * @default process.env.NUXT_OAUTH_HUBSPOT_CLIENT_SECRET |
| 24 | + */ |
| 25 | + clientSecret?: string |
| 26 | + |
| 27 | + /** |
| 28 | + * Hubspot OAuth Redirect URL |
| 29 | + * @default process.env.NUXT_OAUTH_HUBSPOT_REDIRECT_URL |
| 30 | + */ |
| 31 | + redirectURL?: string |
| 32 | + |
| 33 | + /** |
| 34 | + * Hubspot OAuth Scope |
| 35 | + * @default ['oauth'] |
| 36 | + * @see https://developers.hubspot.com/beta-docs/guides/apps/authentication/scopes |
| 37 | + * @example ['accounting', 'automation', 'actions'] |
| 38 | + */ |
| 39 | + scope?: string[] |
| 40 | +} |
| 41 | +interface SignedAccessToken { |
| 42 | + expiresAt: number |
| 43 | + scopes: string |
| 44 | + hubId: number |
| 45 | + userId: number |
| 46 | + appId: number |
| 47 | + signature: string |
| 48 | + scopeToScopeGroupPks?: string |
| 49 | + newSignature?: string |
| 50 | + hublet?: string |
| 51 | + trialScopes?: string |
| 52 | + trialScopeToScopeGroupPks?: string |
| 53 | + isUserLevel: boolean |
| 54 | +} |
| 55 | + |
| 56 | +interface OAuthHubspotAccessInfo { |
| 57 | + token: string |
| 58 | + user: string |
| 59 | + hub_domain: string |
| 60 | + scopes: string[] |
| 61 | + signed_access_token: SignedAccessToken |
| 62 | + hub_id: number |
| 63 | + app_id: number |
| 64 | + expires_in: number |
| 65 | + user_id: number |
| 66 | + token_type: string |
| 67 | +} |
| 68 | + |
| 69 | +export function defineOAuthHubspotEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthHubspotConfig>) { |
| 70 | + return eventHandler(async (event: H3Event) => { |
| 71 | + config = defu(config, useRuntimeConfig(event).oauth?.hubspot) as OAuthHubspotConfig |
| 72 | + |
| 73 | + if (!config.clientId || !config.clientSecret || !config.redirectURL) { |
| 74 | + return handleMissingConfiguration(event, 'hubspot', ['clientId', 'clientSecret', 'redirectURL'], onError) |
| 75 | + } |
| 76 | + |
| 77 | + const query = getQuery<{ code?: string, state?: string, error?: string, error_description?: string }>(event) |
| 78 | + const redirectURL = config.redirectURL || getOAuthRedirectURL(event) |
| 79 | + |
| 80 | + if (query.error) { |
| 81 | + return handleAccessTokenErrorResponse(event, 'hubspot', query, onError) |
| 82 | + } |
| 83 | + |
| 84 | + if (!query.code) { |
| 85 | + return sendRedirect( |
| 86 | + event, |
| 87 | + withQuery('https://app.hubspot.com/oauth/authorize', { |
| 88 | + client_id: config.clientId, |
| 89 | + redirect_uri: redirectURL, |
| 90 | + scope: config.scope?.join(' ') || 'oauth', |
| 91 | + }), |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + const tokens = await requestAccessToken( |
| 96 | + 'https://api.hubapi.com/oauth/v1/token', { |
| 97 | + body: { |
| 98 | + client_id: config.clientId, |
| 99 | + client_secret: config.clientSecret, |
| 100 | + code: query.code as string, |
| 101 | + redirect_uri: redirectURL, |
| 102 | + grant_type: 'authorization_code', |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + if (tokens.error) { |
| 107 | + return handleAccessTokenErrorResponse(event, 'hubspot', tokens, onError) |
| 108 | + } |
| 109 | + |
| 110 | + const info: OAuthHubspotAccessInfo = await $fetch('https://api.hubapi.com/oauth/v1/access-tokens/' + tokens.access_token) |
| 111 | + |
| 112 | + return onSuccess(event, { |
| 113 | + user: { |
| 114 | + id: info.user_id, |
| 115 | + email: info.user, |
| 116 | + domain: info.hub_domain, |
| 117 | + }, |
| 118 | + tokens, |
| 119 | + }) |
| 120 | + }) |
| 121 | +} |
0 commit comments