-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
395 lines (312 loc) · 12.9 KB
/
auth.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import type { H3Event } from 'h3'
import { type GraphqlClient, InvalidJwtError, type JwtPayload, RequestedTokenType, Session } from '@shopify/shopify-api'
import { shopifyTokenHeader } from '~/consts'
const SESSION_TOKEN_PARAM = 'id_token' as const
interface QueryParams {
shop?: string
host?: string
[SESSION_TOKEN_PARAM]?: string
embedded?: '1'
}
const logger = createLogger('Auth Utils')
export const authRoutes = {
path: '/auth',
callbackPath: `/auth/callback`,
patchSessionTokenPath: `/auth/session-token`,
exitIframePath: `/auth/exit-iframe`,
loginPath: `/login`,
}
export function getShop(event: H3Event): string | null {
const params = getQuery<QueryParams>(event)
if (!params.shop) {
return null
}
return shopifyApi.utils.sanitizeShop(params.shop, false)
}
export function getHost(event: H3Event): string | null {
const params = getQuery<QueryParams>(event)
if (!params.host) {
return null
}
return shopifyApi.utils.sanitizeHost(params.host, false)
}
export function validateShopAndHostParams(event: H3Event) {
const shop = getShop(event)
if (!shop) {
event.context.$sentry?.addBreadcrumb({
level: 'error',
message: 'Missing or invalid shop, redirecting to login path',
data: { shop, redirectPath: authRoutes.loginPath, query: getQuery(event) },
})
throw sendRedirect(event, authRoutes.loginPath)
}
const host = getHost(event)
if (!host) {
event.context.$sentry?.addBreadcrumb({
level: 'error',
message: 'Missing or invalid host, redirecting to login path',
data: { shop, host, redirectPath: authRoutes.loginPath, query: getQuery(event) },
})
throw sendRedirect(event, authRoutes.loginPath)
}
}
/** Gets the session token for the current request */
export async function getSessionId(event: H3Event) {
const sessionId = await shopifyApi.session.getCurrentId({
isOnline: true,
rawRequest: event,
})
return sessionId
}
export function getSessionTokenHeader(event: H3Event): string | undefined {
return getHeader(event, shopifyTokenHeader)
}
export function getSessionTokenFromUrlParam(event: H3Event): string | undefined {
const params = getQuery<QueryParams>(event)
return params[SESSION_TOKEN_PARAM]
}
export async function respondToBouncePageRequest(event: H3Event) {
const url = getRequestURL(event)
if (url.pathname === authRoutes.patchSessionTokenPath) {
logger.debug('Rendering bounce page')
throw renderAppBridge(event)
}
}
export async function respondToExitIframeRequest(event: H3Event) {
const url = getRequestURL(event)
if (url.pathname === authRoutes.exitIframePath) {
const destination = url.searchParams.get('exitIframe')!
logger.debug('Rendering exit iframe page', { destination })
throw renderAppBridge(event, { url: destination })
}
}
export type RedirectTarget = '_self' | '_parent' | '_top'
export interface RedirectToOptions {
url: string | URL
target?: RedirectTarget
}
export function renderAppBridge(event: H3Event, redirectTo?: RedirectToOptions) {
const { shopify } = useRuntimeConfig()
let redirectToScript = ''
setResponseHeader(event, 'content-type', 'text/html;charset=utf-8')
const shop = getShop(event)
if (shop) {
setResponseHeaders(event, {
'Link': '<https://cdn.shopify.com/shopifycloud/app-bridge.js>; rel="preload"; as="script";',
'Content-Security-Policy': `frame-ancestors https://${shop} https://admin.shopify.com ${shopify.appUrl};`,
})
}
if (redirectTo) {
const destination = redirectTo.url
const target = redirectTo.target ?? '_top'
redirectToScript = `<script>window.open(${JSON.stringify(
destination.toString(),
)}, ${JSON.stringify(target)})</script>`
}
throw send(event, `
<script data-api-key="${shopify.apiKey}" src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script>
${redirectToScript}
`)
}
export async function ensureAppIsEmbeddedIfRequired(event: H3Event) {
const shop = getShop(event)
const params = getQuery<QueryParams>(event)
if (params.embedded !== '1') {
logger.debug('App is not embedded, redirecting to Shopify', { shop })
await redirectToShopifyOrAppRoot(event)
}
};
export function ensureSessionTokenSearchParamIfRequired(event: H3Event) {
const params = getQuery<QueryParams>(event)
const shop = getShop(event)
const searchParamSessionToken = getSessionTokenFromUrlParam(event)
const isEmbedded = params.embedded === '1'
if (isEmbedded && !searchParamSessionToken) {
logger.debug('Missing session token in search params, going to bounce page', { shop })
throw redirectToBouncePage(event)
}
};
export async function redirectToShopifyOrAppRoot(event: H3Event, _responseHeaders?: Headers) {
const redirectUrl = await shopifyApi.auth.getEmbeddedAppUrl({ rawRequest: event })
throw sendRedirect(event, redirectUrl)
}
export function redirectToBouncePage(event: H3Event) {
const { shopify } = useRuntimeConfig()
const url = getRequestURL(event)
url.searchParams.delete('id_token')
// Make sure we always point to the configured app URL so it also works behind reverse proxies (that alter the Host header).
url.searchParams.set('shopify-reload', `${shopify.appUrl}${url.pathname}?${url.searchParams.toString()}`)
const redirectPath = `${authRoutes.patchSessionTokenPath}?${url.searchParams.toString()}`
event.context.$sentry?.addBreadcrumb({ message: 'Redirecting to bounce page', data: { redirectPath } })
throw sendRedirect(event, redirectPath)
};
export interface SessionTokenContext {
shop: string
sessionId: string
sessionToken: string
payload: JwtPayload
}
export async function getSessionTokenContext(event: H3Event): Promise<SessionTokenContext> {
event.context.$sentry?.addBreadcrumb({ message: 'Running getSessionTokenContext' })
const headerSessionToken = getSessionTokenHeader(event)
const searchParamSessionToken = getSessionTokenFromUrlParam(event)
const sessionToken = headerSessionToken ?? searchParamSessionToken
if (!sessionToken) {
event.context.$sentry?.addBreadcrumb({ message: 'No session token found, redirecting to bounce page', level: 'error', data: { redirectUrl: authRoutes.patchSessionTokenPath } })
throw redirectToBouncePage(event)
}
event.context.$sentry?.addBreadcrumb({ message: 'Attempting to authenticate session token' })
const payload = await validateSessionToken(event, sessionToken)
const dest = new URL(payload.dest)
const shop = dest.hostname
const sessionId = shopifyApi.session.getOfflineId(shop)
event.context.$sentry?.addBreadcrumb({ message: 'Session token is valid, and got sessionId', data: { shop, sessionId } })
return { shop, payload, sessionId, sessionToken }
}
export async function validateSessionToken(event: H3Event, token: string): Promise<JwtPayload> {
logger.debug('Validating session token')
try {
const payload = await shopifyApi.session.decodeSessionToken(token, { checkAudience: true })
logger.debug('Session token is valid', { dest: payload.dest })
return payload
} catch (error) {
if (error instanceof Error) {
logger.debug(`Failed to validate session token: ${error.message}`)
}
throw respondToInvalidSessionToken(event, true)
}
}
export function respondToInvalidSessionToken(event: H3Event, retryRequest = false) {
event.context.$sentry?.addBreadcrumb({ message: 'Running respondToInvalidSessionToken', data: { retryRequest } })
const isDocumentRequest = !getSessionTokenHeader(event)
if (isDocumentRequest) {
event.context.$sentry?.addBreadcrumb({
message: `Document request, redirecting to bounce page`,
data: { isDocumentRequest, token: getSessionTokenHeader(event) },
})
return redirectToBouncePage(event)
}
if (retryRequest) {
event.context.$sentry?.addBreadcrumb({
message: `Retry request, setting shopify retry header`,
data: { 'X-Shopify-Retry-Invalid-Session-Request': '1' },
})
setResponseHeader(event, 'X-Shopify-Retry-Invalid-Session-Request', '1')
}
event.context.$sentry?.addBreadcrumb({
message: `Throwing error as we don't know what to do here!`,
level: 'error',
})
throw createError({
statusCode: 401,
statusText: 'Unauthorized',
})
}
export interface SessionContext {
shop: string
session?: Session
sessionToken?: string
}
/**
* Accepts a `sessionToken` (from the current request), and an optional existing `session` (from the DB).
* It validates the `sessionToken`, and either returns the existing session if it exists, or handles
* the token exchange to get a new access token, saves it to the DB, and runs any post-install steps
* (i.e. registering webhooks).
*/
export async function authenticateSession(event: H3Event, { shop, session, sessionToken }: SessionContext): Promise<Session> {
event.context.$sentry?.addBreadcrumb({
message: 'Authenticating session',
data: { shop, sessionToken },
})
if (!sessionToken) {
throw sendError(event, new InvalidJwtError())
}
if (session?.isActive(undefined)) {
event.context.$sentry?.addBreadcrumb({
message: 'Session is active, returning session',
data: { shop, isActive: session.isActive(undefined), sessionToken },
})
return session
}
event.context.$sentry?.addBreadcrumb({ message: 'No valid session found, requesting offline access token' })
const { session: offlineSession } = await shopifyApi.auth.tokenExchange({
requestedTokenType: RequestedTokenType.OfflineAccessToken,
sessionToken,
shop,
})
event.context.$sentry?.addBreadcrumb({ message: 'Storing session in DB' })
await orm.sessions.upsert(offlineSession.toObject())
try {
event.context.$sentry?.addBreadcrumb({ message: 'Registering webhooks', data: { webhooks: shopifyApi.webhooks.getTopicsAdded() } })
await shopifyApi.webhooks.register({ session: offlineSession })
} catch (error) {
event.context.$sentry?.captureException(error)
if (error instanceof Error) {
logger.error(error.message)
throw createError({
message: `Failed to register webhooks - ${error.message}`,
statusMessage: 'Failed to register webhooks',
statusCode: 500,
})
} else {
logger.error(error)
}
}
event.context.$sentry?.addBreadcrumb({ message: 'Finished authenticating session, returning new offline session' })
return offlineSession
}
/**
* Fetch a saved session from the DB.
*/
export async function fetchExistingSession(sessionId: string): Promise<Session | undefined> {
const dbSession = await orm.sessions.find(sessionId)
if (dbSession) {
return new Session(dbSession)
}
}
/**
* Runs a literal ton of checks, to handle the various cases where we may/may not be embedded,
* may have missing tokens, may not be installed, etc., and redirects to the relevant pages as needed.
*
* If everything is successful, it returns a `Session` object that we can use for API requests etc.
*/
export async function assertSession(event: H3Event): Promise<Session> {
event.context.$sentry?.addBreadcrumb({ message: 'Running assertSession function' })
event.context.$sentry?.addBreadcrumb({ message: 'Running redirect request handlers' })
await respondToBouncePageRequest(event)
await respondToExitIframeRequest(event)
event.context.$sentry?.addBreadcrumb({ message: 'Checking for valid session token' })
// If this is a valid request, but it doesn't have a session token header, this is a document request. We need to
// ensure we're embedded if needed and we have the information needed to load the session.
const sessionTokenHeader = getSessionTokenHeader(event)
if (!sessionTokenHeader) {
event.context.$sentry?.addBreadcrumb({ message: 'No session token header, so running embedded checks', level: 'warning' })
validateShopAndHostParams(event)
await ensureAppIsEmbeddedIfRequired(event)
ensureSessionTokenSearchParamIfRequired(event)
}
event.context.$sentry?.addBreadcrumb({ message: 'Authenticating admin request' })
const { shop, sessionId, sessionToken } = await getSessionTokenContext(event)
event.context.$sentry?.addBreadcrumb({ message: 'Loading session from storage', data: { sessionId, shop } })
const existingSession = await fetchExistingSession(sessionId)
event.context.$sentry?.addBreadcrumb({ message: 'Fetched existing session from DB, validating and authehticating sessions' })
const session = await authenticateSession(event, {
session: existingSession,
sessionToken,
shop,
})
event.context.$sentry?.addBreadcrumb({ message: 'Request is valid, loaded session from session token', data: { shop: session.shop } })
return session
}
interface UseShopify {
session: Session
admin: GraphqlClient
}
/**
* Not sure of naming, but in our API routes we want to assert a session, and return the session + instantiate graphql client.
*/
export async function useShopify(event: H3Event): Promise<UseShopify> {
const session = await assertSession(event)
const admin = new shopifyApi.clients.Graphql({ session, apiVersion: shopifyApi.config.apiVersion })
return { session, admin }
}