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