|
| 1 | +import { type Labrinth, ModrinthApiError } from '@modrinth/api-client' |
| 2 | +import { SignJWT } from 'jose' |
| 3 | + |
| 4 | +import { useServerModrinthClient } from '~/server/utils/api-client' |
| 5 | + |
| 6 | +type IntercomTokenResponse = { |
| 7 | + token: string |
| 8 | +} |
| 9 | + |
| 10 | +async function signIntercomUserJwt( |
| 11 | + user: { id: string; username: string; email?: string; created: string }, |
| 12 | + secret: string, |
| 13 | +): Promise<string> { |
| 14 | + const createdAt = Math.floor(new Date(user.created).getTime() / 1000) |
| 15 | + |
| 16 | + const payload: Record<string, string | number> = { |
| 17 | + user_id: user.id, |
| 18 | + name: user.username, |
| 19 | + } |
| 20 | + |
| 21 | + if (user.email) { |
| 22 | + payload.email = user.email |
| 23 | + } |
| 24 | + |
| 25 | + if (Number.isFinite(createdAt)) { |
| 26 | + payload.created_at = createdAt |
| 27 | + } |
| 28 | + |
| 29 | + return await new SignJWT(payload) |
| 30 | + .setProtectedHeader({ alg: 'HS256', typ: 'JWT' }) |
| 31 | + .setIssuedAt() |
| 32 | + .setExpirationTime('1h') |
| 33 | + .sign(new TextEncoder().encode(secret)) |
| 34 | +} |
| 35 | + |
| 36 | +export default defineEventHandler(async (event): Promise<IntercomTokenResponse> => { |
| 37 | + if (event.method !== 'GET') { |
| 38 | + throw createError({ |
| 39 | + statusCode: 405, |
| 40 | + message: 'Method not allowed', |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + const authToken = getCookie(event, 'auth-token') |
| 45 | + if (!authToken) { |
| 46 | + throw createError({ |
| 47 | + statusCode: 401, |
| 48 | + message: 'Authentication required', |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + setHeader(event, 'cache-control', 'private, no-store, max-age=0') |
| 53 | + |
| 54 | + const config = useRuntimeConfig(event) |
| 55 | + if (!config.intercomIdentitySecret) { |
| 56 | + throw createError({ |
| 57 | + statusCode: 500, |
| 58 | + message: 'Intercom identity secret is not configured', |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + const client = useServerModrinthClient({ |
| 63 | + event, |
| 64 | + authToken, |
| 65 | + }) |
| 66 | + |
| 67 | + let user: { id: string; username: string; email?: string; created: string } |
| 68 | + try { |
| 69 | + const currentUser = await client.request<Labrinth.Users.v2.User>('/user', { |
| 70 | + api: 'labrinth', |
| 71 | + version: 2, |
| 72 | + method: 'GET', |
| 73 | + }) |
| 74 | + user = { |
| 75 | + id: currentUser.id, |
| 76 | + username: currentUser.username, |
| 77 | + email: currentUser.email, |
| 78 | + created: currentUser.created, |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + if (error instanceof ModrinthApiError && error.statusCode === 401) { |
| 82 | + throw createError({ |
| 83 | + statusCode: 401, |
| 84 | + message: 'Authentication required', |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + throw createError({ |
| 89 | + statusCode: 502, |
| 90 | + message: 'Failed to resolve current user', |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + const token = await signIntercomUserJwt(user, config.intercomIdentitySecret) |
| 95 | + |
| 96 | + return { |
| 97 | + token, |
| 98 | + } |
| 99 | +}) |
0 commit comments