mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
fixed auth tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
||||||
import { App, createApp } from "../../src";
|
import { App, createApp } from "../../src";
|
||||||
import type { AuthResponse } from "../../src/auth";
|
import type { AuthResponse } from "../../src/auth";
|
||||||
import { randomString, secureRandomString } from "../../src/core/utils";
|
import { randomString, secureRandomString, withDisabledConsole } from "../../src/core/utils";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "../helper";
|
import { disableConsoleLog, enableConsoleLog } from "../helper";
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
beforeAll(disableConsoleLog);
|
||||||
@@ -200,4 +200,14 @@ describe("integration auth", () => {
|
|||||||
expect(await $fns.me()).toEqual({ user: null as any });
|
expect(await $fns.me()).toEqual({ user: null as any });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should check for permissions", async () => {
|
||||||
|
const app = createAuthApp();
|
||||||
|
await app.build();
|
||||||
|
|
||||||
|
await withDisabledConsole(async () => {
|
||||||
|
const res = await app.server.request("/api/system/schema");
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,8 +3,13 @@ import type { Context } from "hono";
|
|||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
import type { ServerEnv } from "modules/Module";
|
import type { ServerEnv } from "modules/Module";
|
||||||
|
|
||||||
|
function getPath(reqOrCtx: Request | Context) {
|
||||||
|
const req = reqOrCtx instanceof Request ? reqOrCtx : reqOrCtx.req.raw;
|
||||||
|
return new URL(req.url).pathname;
|
||||||
|
}
|
||||||
|
|
||||||
export function shouldSkipAuth(req: Request) {
|
export function shouldSkipAuth(req: Request) {
|
||||||
const skip = new URL(req.url).pathname.startsWith(config.server.assets_path);
|
const skip = getPath(req).startsWith(config.server.assets_path);
|
||||||
if (skip) {
|
if (skip) {
|
||||||
//console.log("skip auth for", req.url);
|
//console.log("skip auth for", req.url);
|
||||||
}
|
}
|
||||||
@@ -14,7 +19,7 @@ export function shouldSkipAuth(req: Request) {
|
|||||||
export const auth = createMiddleware<ServerEnv>(async (c, next) => {
|
export const auth = createMiddleware<ServerEnv>(async (c, next) => {
|
||||||
// make sure to only register once
|
// make sure to only register once
|
||||||
if (c.get("auth_registered")) {
|
if (c.get("auth_registered")) {
|
||||||
throw new Error("auth middleware already registered");
|
throw new Error(`auth middleware already registered for ${getPath(c)}`);
|
||||||
}
|
}
|
||||||
c.set("auth_registered", true);
|
c.set("auth_registered", true);
|
||||||
|
|
||||||
@@ -47,21 +52,21 @@ export const auth = createMiddleware<ServerEnv>(async (c, next) => {
|
|||||||
|
|
||||||
export const permission = (...permissions: Permission[]) =>
|
export const permission = (...permissions: Permission[]) =>
|
||||||
createMiddleware<ServerEnv>(async (c, next) => {
|
createMiddleware<ServerEnv>(async (c, next) => {
|
||||||
if (!c.get("auth_registered")) {
|
|
||||||
throw new Error("auth middleware not registered, cannot check permissions");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!shouldSkipAuth(c.req.raw)) {
|
|
||||||
const app = c.get("app");
|
const app = c.get("app");
|
||||||
if (app) {
|
// in tests, app is not defined
|
||||||
|
if (!c.get("auth_registered")) {
|
||||||
|
const msg = `auth middleware not registered, cannot check permissions for ${getPath(c)}`;
|
||||||
|
if (app?.module.auth.enabled) {
|
||||||
|
throw new Error(msg);
|
||||||
|
} else {
|
||||||
|
console.warn(msg);
|
||||||
|
}
|
||||||
|
} else if (!shouldSkipAuth(c.req.raw)) {
|
||||||
const p = Array.isArray(permissions) ? permissions : [permissions];
|
const p = Array.isArray(permissions) ? permissions : [permissions];
|
||||||
const guard = app.modules.ctx().guard;
|
const guard = app.modules.ctx().guard;
|
||||||
for (const permission of p) {
|
for (const permission of p) {
|
||||||
guard.throwUnlessGranted(permission);
|
guard.throwUnlessGranted(permission);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.warn("app not in context, skip permission check");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await next();
|
await next();
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const _oldConsoles = {
|
|||||||
|
|
||||||
export async function withDisabledConsole<R>(
|
export async function withDisabledConsole<R>(
|
||||||
fn: () => Promise<R>,
|
fn: () => Promise<R>,
|
||||||
severities: ConsoleSeverity[] = ["log"]
|
severities: ConsoleSeverity[] = ["log", "warn", "error"]
|
||||||
): Promise<R> {
|
): Promise<R> {
|
||||||
const _oldConsoles = {
|
const _oldConsoles = {
|
||||||
log: console.log,
|
log: console.log,
|
||||||
@@ -30,7 +30,7 @@ export async function withDisabledConsole<R>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) {
|
export function disableConsoleLog(severities: ConsoleSeverity[] = ["log", "warn"]) {
|
||||||
severities.forEach((severity) => {
|
severities.forEach((severity) => {
|
||||||
console[severity] = () => null;
|
console[severity] = () => null;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user