|
| 1 | +import { describeRoute } from "hono-openapi"; |
| 2 | +import { authRoutesFactory } from "./factory"; |
| 3 | +import { resolver, validator } from "hono-openapi/zod"; |
| 4 | +import { |
| 5 | + engineErrToHttpException, |
| 6 | + mapDbError, |
| 7 | + zErrorMapper, |
| 8 | +} from "../../../lib/errors"; |
| 9 | +import * as z from "zod"; |
| 10 | +import { adminAccount } from "../../../lib/admin-account"; |
| 11 | +import { config } from "../../../lib/config"; |
| 12 | +import { encodeJWT } from "thirdweb/utils"; |
| 13 | +import { db } from "../../../db/connection"; |
| 14 | +import { tokens } from "../../../db/schema"; |
| 15 | +import { ResultAsync } from "neverthrow"; |
| 16 | +import { accessTokenDbEntrySchema } from "../../../db/derived-schemas"; |
| 17 | +import { wrapResponseSchema } from "../../schemas/shared-api-schemas"; |
| 18 | + |
| 19 | +export const createAccessTokenRoute = authRoutesFactory.createHandlers( |
| 20 | + describeRoute({ |
| 21 | + tags: ["Auth"], |
| 22 | + summary: "Create Access Token", |
| 23 | + description: "Create an access token for a client", |
| 24 | + responses: { |
| 25 | + 200: { |
| 26 | + description: "Access token created successfully", |
| 27 | + content: { |
| 28 | + "application/json": { |
| 29 | + schema: resolver( |
| 30 | + wrapResponseSchema( |
| 31 | + accessTokenDbEntrySchema.extend({ |
| 32 | + accessToken: z.string().openapi({ |
| 33 | + description: "The access token created", |
| 34 | + }), |
| 35 | + }) |
| 36 | + ) |
| 37 | + ), |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }), |
| 43 | + validator( |
| 44 | + "json", |
| 45 | + z.object({ |
| 46 | + label: z.string(), |
| 47 | + }), |
| 48 | + zErrorMapper |
| 49 | + ), |
| 50 | + async (c) => { |
| 51 | + const { label } = c.req.valid("json"); |
| 52 | + |
| 53 | + const user = c.get("user"); |
| 54 | + |
| 55 | + const expiresAt = new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 100); |
| 56 | + |
| 57 | + const id = crypto.randomUUID(); |
| 58 | + const jwt = await encodeJWT({ |
| 59 | + account: adminAccount, |
| 60 | + payload: { |
| 61 | + iss: adminAccount.address, |
| 62 | + sub: user?.address ?? adminAccount.address, |
| 63 | + aud: config.authDomain, |
| 64 | + nbf: new Date(), |
| 65 | + // Set to expire in 100 years |
| 66 | + exp: expiresAt, |
| 67 | + iat: new Date(), |
| 68 | + ctx: { |
| 69 | + permissions: user?.permissions ?? ["ADMIN"], |
| 70 | + }, |
| 71 | + jti: id, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const dbResult = await ResultAsync.fromPromise( |
| 76 | + db |
| 77 | + .insert(tokens) |
| 78 | + .values({ |
| 79 | + id, |
| 80 | + accountAddress: user?.address ?? adminAccount.address, |
| 81 | + isAccessToken: true, |
| 82 | + label: label, |
| 83 | + expiresAt, |
| 84 | + tokenMask: `${jwt.slice(0, 10)}...${jwt.slice(-10)}`, |
| 85 | + }) |
| 86 | + .returning(), |
| 87 | + mapDbError |
| 88 | + ); |
| 89 | + |
| 90 | + if (dbResult.isErr()) { |
| 91 | + throw engineErrToHttpException(dbResult.error); |
| 92 | + } |
| 93 | + |
| 94 | + const createdToken = dbResult.value[0]; |
| 95 | + |
| 96 | + if (!createdToken) { |
| 97 | + throw engineErrToHttpException({ |
| 98 | + kind: "database", |
| 99 | + code: "query_failed", |
| 100 | + status: 500, |
| 101 | + message: "Failed to create access token", |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + return c.json({ result: { accessToken: jwt, ...createdToken } }); |
| 106 | + } |
| 107 | +); |
0 commit comments