exposed bknd middlewares to be used for custom routes

This commit is contained in:
dswbx
2025-01-11 15:45:32 +01:00
parent bd4bc14282
commit d8671355a6
11 changed files with 20 additions and 32 deletions

View File

@@ -1,9 +1 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { shouldSkipAuth } from "../../src/auth/middlewares";
describe("auth middleware", () => {
it("should skip auth on asset paths", () => {
expect(shouldSkipAuth(new Request("http://localhost/assets/test.js"))).toBe(true);
expect(shouldSkipAuth(new Request("http://localhost/"))).toBe(false);
});
});

View File

@@ -79,7 +79,7 @@ describe("AppAuth", () => {
} }
}); });
test("registers auth middleware automatically", async () => { test("registers auth middleware for bknd routes only", async () => {
const app = createApp({ const app = createApp({
initialConfig: { initialConfig: {
auth: { auth: {
@@ -101,7 +101,7 @@ describe("AppAuth", () => {
await app.server.request("/api/system/ping"); await app.server.request("/api/system/ping");
await app.server.request("/test"); await app.server.request("/test");
expect(spy.mock.calls.length).toBe(2); expect(spy.mock.calls.length).toBe(1);
}); });
test("should allow additional user fields", async () => { test("should allow additional user fields", async () => {

View File

@@ -3,7 +3,7 @@
"type": "module", "type": "module",
"sideEffects": false, "sideEffects": false,
"bin": "./dist/cli/index.js", "bin": "./dist/cli/index.js",
"version": "0.5.0-rc13", "version": "0.5.0-rc14",
"scripts": { "scripts": {
"build:all": "NODE_ENV=production bun run build.ts --minify --types --clean && bun run build:cli", "build:all": "NODE_ENV=production bun run build.ts --minify --types --clean && bun run build:cli",
"dev": "vite", "dev": "vite",

View File

@@ -111,7 +111,6 @@ export class App {
await Promise.all(this.plugins.map((plugin) => plugin(this))); await Promise.all(this.plugins.map((plugin) => plugin(this)));
} }
//console.log("emitting built", options);
await this.emgr.emit(new AppBuiltEvent({ app: this })); await this.emgr.emit(new AppBuiltEvent({ app: this }));
server.all("/api/*", async (c) => c.notFound()); server.all("/api/*", async (c) => c.notFound());

View File

@@ -33,17 +33,17 @@ export const auth = (options?: {
c.set("auth_registered", true); 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 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) { if (!skipped) {
const resolved = c.get("auth_resolved"); const resolved = c.get("auth_resolved");
if (!resolved) { if (!resolved) {
if (!app.module.auth.enabled) { if (!app.module.auth.enabled) {
guard.setUserContext(undefined); guard?.setUserContext(undefined);
} else { } else {
guard.setUserContext(await authenticator.resolveAuthFromRequest(c)); guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c));
c.set("auth_resolved", true); c.set("auth_resolved", true);
} }
} }
@@ -53,11 +53,11 @@ export const auth = (options?: {
if (!skipped) { if (!skipped) {
// renew cookie if applicable // renew cookie if applicable
authenticator.requestCookieRefresh(c); authenticator?.requestCookieRefresh(c);
} }
// release // release
guard.setUserContext(undefined); guard?.setUserContext(undefined);
authenticator?.resetUser(); authenticator?.resetUser();
c.set("auth_resolved", false); c.set("auth_resolved", false);
}); });
@@ -75,7 +75,7 @@ export const permission = (
//console.log("skip?", c.get("auth_skip")); //console.log("skip?", c.get("auth_skip"));
// in tests, app is not defined // in tests, app is not defined
if (!c.get("auth_registered")) { if (!c.get("auth_registered") || !app) {
const msg = `auth middleware not registered, cannot check permissions for ${getPath(c)}`; const msg = `auth middleware not registered, cannot check permissions for ${getPath(c)}`;
if (app?.module.auth.enabled) { if (app?.module.auth.enabled) {
throw new Error(msg); throw new Error(msg);

View File

@@ -9,6 +9,7 @@ export {
type ModuleBuildContext type ModuleBuildContext
} from "./modules/ModuleManager"; } from "./modules/ModuleManager";
export * as middlewares from "modules/middlewares";
export { registries } from "modules/registries"; export { registries } from "modules/registries";
export type * from "./adapter"; export type * from "./adapter";

View File

@@ -1,11 +1,6 @@
import { auth, permission } from "auth/middlewares";
import { Hono } from "hono"; import { Hono } from "hono";
import type { ServerEnv } from "modules/Module"; import type { ServerEnv } from "modules/Module";
import * as middlewares from "modules/middlewares";
const middlewares = {
auth,
permission
} as const;
export class Controller { export class Controller {
protected middlewares = middlewares; protected middlewares = middlewares;

View File

@@ -9,13 +9,13 @@ import type { Hono } from "hono";
export type ServerEnv = { export type ServerEnv = {
Variables: { Variables: {
app: App; app?: App;
// to prevent resolving auth multiple times // to prevent resolving auth multiple times
auth_resolved: boolean; auth_resolved?: boolean;
// to only register once // to only register once
auth_registered: boolean; auth_registered?: boolean;
// whether or not to bypass auth // whether or not to bypass auth
auth_skip: boolean; auth_skip?: boolean;
html?: string; html?: string;
}; };
}; };

View File

@@ -11,7 +11,7 @@ export {
MODULE_NAMES, MODULE_NAMES,
type ModuleKey type ModuleKey
} from "./ModuleManager"; } from "./ModuleManager";
export { /*Module,*/ type ModuleBuildContext } from "./Module"; export type { ModuleBuildContext } from "./Module";
export { export {
type PrimaryFieldType, type PrimaryFieldType,

View File

@@ -0,0 +1 @@
export { auth, permission } from "auth/middlewares";

View File

@@ -292,7 +292,7 @@ export class SystemController extends Controller {
return c.json({ return c.json({
version: this.app.version(), version: this.app.version(),
test: 2, test: 2,
app: c.get("app").version() app: c.get("app")?.version()
}); });
}); });