From 11967df2aeb955d0cc22d8d80ccb57f7e9ffc014 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 22 Feb 2024 05:25:17 -0500 Subject: [PATCH] Implement auth --- src/api/api/auth.ts | 0 src/api/api/server.ts | 5 +++-- src/api/api/trpc.ts | 32 ++++++++++++++------------------ src/mpp/api/trpc.ts | 16 ++++++++++++++-- src/mpp/bot/Bot.ts | 3 +-- 5 files changed, 32 insertions(+), 24 deletions(-) delete mode 100644 src/api/api/auth.ts diff --git a/src/api/api/auth.ts b/src/api/api/auth.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/api/api/server.ts b/src/api/api/server.ts index d62617c..9564797 100644 --- a/src/api/api/server.ts +++ b/src/api/api/server.ts @@ -1,12 +1,13 @@ import { createBunServeHandler } from "trpc-bun-adapter"; -import { appRouter } from "./trpc"; +import { appRouter, createContext } from "./trpc"; import { Logger } from "@util/Logger"; const logger = new Logger("Server"); export const server = Bun.serve( createBunServeHandler({ - router: appRouter + router: appRouter, + createContext: createContext }) ); diff --git a/src/api/api/trpc.ts b/src/api/api/trpc.ts index 6ed12de..06be244 100644 --- a/src/api/api/trpc.ts +++ b/src/api/api/trpc.ts @@ -8,29 +8,31 @@ import { z } from "zod"; const logger = new Logger("tRPC"); -export interface Context { - isAuthed: boolean; -} - export const createContext = async (opts: CreateBunContextOptions) => { return { - isAuthed: false - } as Context; + isAuthed: false, + req: opts.req + }; }; -const t = initTRPC.context().create(); +export type Context = Awaited>; +const t = initTRPC.context().create(); export const router = t.router; export const publicProcedure = t.procedure; export const privateProcedure = publicProcedure.use(async opts => { const { ctx } = opts; + const { req } = ctx; + + const token = req.headers.get("authorization"); + if (!token) throw new TRPCError({ code: "UNAUTHORIZED" }); + + opts.ctx.isAuthed = await checkToken(token); + if (!ctx.isAuthed) throw new TRPCError({ code: "UNAUTHORIZED" }); - return opts.next({ - ctx: { - isAuthed: true - } - }); + + return opts.next(opts); }); export const appRouter = router({ @@ -38,12 +40,6 @@ export const appRouter = router({ return prefixes; }), - auth: publicProcedure.input(z.string()).query(async opts => { - logger.debug(opts); - const token = opts.input; - opts.ctx.isAuthed = await checkToken(token); - }), - command: privateProcedure .input( z.object({ diff --git a/src/mpp/api/trpc.ts b/src/mpp/api/trpc.ts index 7a85c1b..54452d3 100644 --- a/src/mpp/api/trpc.ts +++ b/src/mpp/api/trpc.ts @@ -1,13 +1,25 @@ import { createTRPCClient, httpBatchLink } from "@trpc/client"; import type { AppRouter } from "@server/api/trpc"; +const apiToken = process.env.FISHING_TOKEN as string; + export const trpc = createTRPCClient({ links: [ httpBatchLink({ - url: "http://localhost:3000" + url: "http://localhost:3000", + headers() { + return { + Authorization: apiToken + }; + } }), httpBatchLink({ - url: "https://fishing.hri7566.info/api" + url: "https://fishing.hri7566.info/api", + headers() { + return { + Authorization: apiToken + }; + } }) ] }); diff --git a/src/mpp/bot/Bot.ts b/src/mpp/bot/Bot.ts index 6bd203b..0e49cc4 100644 --- a/src/mpp/bot/Bot.ts +++ b/src/mpp/bot/Bot.ts @@ -37,9 +37,8 @@ export class MPPNetBot { } public bindEventListeners() { - this.client.on("hi", msg => { + this.client.on("hi", async msg => { this.logger.info(`Connected to ${this.client.uri}`); - trpc.auth.query(process.env.FISHING_TOKEN as string); }); this.client.on("ch", msg => {