mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): 🚚 Refactor authentication middleware and implement some OpenAPI routes
This commit is contained in:
parent
edf5edca9f
commit
1ab1c68d36
66 changed files with 383 additions and 279 deletions
33
utils/api.ts
33
utils/api.ts
|
|
@ -1,6 +1,5 @@
|
|||
import type { Context } from "@hono/hono";
|
||||
import { createMiddleware } from "@hono/hono/factory";
|
||||
import { validator } from "@hono/hono/validator";
|
||||
import type { OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { getLogger } from "@logtape/logtape";
|
||||
import { extractParams, verifySolution } from "altcha-lib";
|
||||
|
|
@ -29,7 +28,7 @@ import { db } from "~/drizzle/db";
|
|||
import { Challenges } from "~/drizzle/schema";
|
||||
import { config } from "~/packages/config-manager/index";
|
||||
import type { User } from "~/packages/database-interface/user";
|
||||
import type { ApiRouteMetadata, HttpVerb } from "~/types/api";
|
||||
import type { ApiRouteMetadata, HonoEnv, HttpVerb } from "~/types/api";
|
||||
|
||||
export const applyConfig = (routeMeta: ApiRouteMetadata) => {
|
||||
const newMeta = routeMeta;
|
||||
|
|
@ -45,13 +44,7 @@ export const applyConfig = (routeMeta: ApiRouteMetadata) => {
|
|||
return newMeta;
|
||||
};
|
||||
|
||||
export const apiRoute = (
|
||||
fn: (
|
||||
app: OpenAPIHono /* <{
|
||||
Bindings: {};
|
||||
}> */,
|
||||
) => void,
|
||||
) => fn;
|
||||
export const apiRoute = (fn: (app: OpenAPIHono<HonoEnv>) => void) => fn;
|
||||
|
||||
export const idValidator = createRegExp(
|
||||
anyOf(digit, charIn("ABCDEF")).times(8),
|
||||
|
|
@ -151,12 +144,6 @@ export const handleZodError = (
|
|||
}
|
||||
};
|
||||
|
||||
const getAuth = async (value: Record<string, string>) => {
|
||||
return value.authorization
|
||||
? await getFromHeader(value.authorization)
|
||||
: null;
|
||||
};
|
||||
|
||||
const checkPermissions = (
|
||||
auth: AuthData | null,
|
||||
permissionData: ApiRouteMetadata["permissions"],
|
||||
|
|
@ -300,8 +287,10 @@ export const auth = (
|
|||
permissionData?: ApiRouteMetadata["permissions"],
|
||||
challengeData?: ApiRouteMetadata["challenge"],
|
||||
) =>
|
||||
validator("header", async (value, context) => {
|
||||
const auth = await getAuth(value);
|
||||
createMiddleware<HonoEnv>(async (context, next) => {
|
||||
const header = context.req.header("Authorization");
|
||||
|
||||
const auth = header ? await getFromHeader(header) : null;
|
||||
|
||||
// Only exists for type casting, as otherwise weird errors happen with Hono
|
||||
const fakeResponse = context.json({});
|
||||
|
|
@ -328,13 +317,21 @@ export const auth = (
|
|||
}
|
||||
}
|
||||
|
||||
return checkRouteNeedsAuth(auth, authData, context) as
|
||||
const authCheck = checkRouteNeedsAuth(auth, authData, context) as
|
||||
| typeof fakeResponse
|
||||
| {
|
||||
user: User | null;
|
||||
token: string | null;
|
||||
application: Application | null;
|
||||
};
|
||||
|
||||
if (authCheck instanceof Response) {
|
||||
return authCheck;
|
||||
}
|
||||
|
||||
context.set("auth", authCheck);
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
// Helper function to parse form data
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import type { OpenAPIHono } from "@hono/zod-openapi";
|
||||
import type { Config } from "~/packages/config-manager/config.type";
|
||||
import type { HonoEnv } from "~/types/api";
|
||||
|
||||
export const createServer = (config: Config, app: OpenAPIHono) =>
|
||||
export const createServer = (config: Config, app: OpenAPIHono<HonoEnv>) =>
|
||||
Bun.serve({
|
||||
port: config.http.bind_port,
|
||||
reusePort: true,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue