fix double registration of auth middleware on data routes

This commit is contained in:
dswbx
2025-01-16 15:45:29 +01:00
parent 26a5fd8b34
commit 8226b644ae
2 changed files with 19 additions and 18 deletions

View File

@@ -26,25 +26,28 @@ export const auth = (options?: {
skip?: (string | RegExp)[]; skip?: (string | RegExp)[];
}) => }) =>
createMiddleware<ServerEnv>(async (c, next) => { createMiddleware<ServerEnv>(async (c, next) => {
// make sure to only register once
if (c.get("auth_registered")) {
throw new Error(`auth middleware already registered for ${getPath(c)}`);
}
c.set("auth_registered", true);
const app = c.get("app"); const app = c.get("app");
const skipped = shouldSkip(c, options?.skip) || !app?.module.auth.enabled;
const guard = app?.modules.ctx().guard; const guard = app?.modules.ctx().guard;
const authenticator = app?.module.auth.authenticator; const authenticator = app?.module.auth.authenticator;
if (!skipped) { let skipped = shouldSkip(c, options?.skip) || !app?.module.auth.enabled;
const resolved = c.get("auth_resolved");
if (!resolved) { // make sure to only register once
if (!app.module.auth.enabled) { if (c.get("auth_registered")) {
guard?.setUserContext(undefined); skipped = true;
} else { console.warn(`auth middleware already registered for ${getPath(c)}`);
guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c)); } else {
c.set("auth_resolved", true); c.set("auth_registered", true);
if (!skipped) {
const resolved = c.get("auth_resolved");
if (!resolved) {
if (!app?.module.auth.enabled) {
guard?.setUserContext(undefined);
} else {
guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c));
c.set("auth_resolved", true);
}
} }
} }
} }

View File

@@ -70,7 +70,7 @@ export class DataController extends Controller {
override getController() { override getController() {
const { permission, auth } = this.middlewares; const { permission, auth } = this.middlewares;
const hono = this.create().use(auth()); const hono = this.create().use(auth(), permission(SystemPermissions.accessApi));
const definedEntities = this.em.entities.map((e) => e.name); const definedEntities = this.em.entities.map((e) => e.name);
const tbNumber = Type.Transform(Type.String({ pattern: "^[1-9][0-9]{0,}$" })) const tbNumber = Type.Transform(Type.String({ pattern: "^[1-9][0-9]{0,}$" }))
@@ -85,8 +85,6 @@ export class DataController extends Controller {
return func; return func;
} }
hono.use("*", permission(SystemPermissions.accessApi));
// info // info
hono.get( hono.get(
"/", "/",