mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added auth strategies form + add ability to disable strategies
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { type AppAuth, AuthPermissions, type SafeUser, type Strategy } from "auth";
|
||||
import { TypeInvalidError, parse } from "core/utils";
|
||||
import { tbValidator as tb } from "core";
|
||||
import { Type, TypeInvalidError, parse, transformObject } from "core/utils";
|
||||
import { DataPermissions } from "data";
|
||||
import type { Hono } from "hono";
|
||||
import { Controller } from "modules/Controller";
|
||||
@@ -12,6 +13,10 @@ export type AuthActionResponse = {
|
||||
errors?: any;
|
||||
};
|
||||
|
||||
const booleanLike = Type.Transform(Type.String())
|
||||
.Decode((v) => v === "1")
|
||||
.Encode((v) => (v ? "1" : "0"));
|
||||
|
||||
export class AuthController extends Controller {
|
||||
constructor(private auth: AppAuth) {
|
||||
super();
|
||||
@@ -31,6 +36,9 @@ export class AuthController extends Controller {
|
||||
}
|
||||
|
||||
private registerStrategyActions(strategy: Strategy, mainHono: Hono<ServerEnv>) {
|
||||
if (!this.auth.isStrategyEnabled(strategy)) {
|
||||
return;
|
||||
}
|
||||
const actions = strategy.getActions?.();
|
||||
if (!actions) {
|
||||
return;
|
||||
@@ -98,7 +106,8 @@ export class AuthController extends Controller {
|
||||
const strategies = this.auth.authenticator.getStrategies();
|
||||
|
||||
for (const [name, strategy] of Object.entries(strategies)) {
|
||||
//console.log("registering", name, "at", `/${name}`);
|
||||
if (!this.auth.isStrategyEnabled(strategy)) continue;
|
||||
|
||||
hono.route(`/${name}`, strategy.getController(this.auth.authenticator));
|
||||
this.registerStrategyActions(strategy, hono);
|
||||
}
|
||||
@@ -127,10 +136,25 @@ export class AuthController extends Controller {
|
||||
return c.redirect("/");
|
||||
});
|
||||
|
||||
hono.get("/strategies", async (c) => {
|
||||
const { strategies, basepath } = this.auth.toJSON(false);
|
||||
return c.json({ strategies, basepath });
|
||||
});
|
||||
hono.get(
|
||||
"/strategies",
|
||||
tb("query", Type.Object({ include_disabled: Type.Optional(booleanLike) })),
|
||||
async (c) => {
|
||||
const { include_disabled } = c.req.valid("query");
|
||||
const { strategies, basepath } = this.auth.toJSON(false);
|
||||
|
||||
if (!include_disabled) {
|
||||
return c.json({
|
||||
strategies: transformObject(strategies ?? {}, (strategy, name) => {
|
||||
return this.auth.isStrategyEnabled(name) ? strategy : undefined;
|
||||
}),
|
||||
basepath
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({ strategies, basepath });
|
||||
}
|
||||
);
|
||||
|
||||
return hono.all("*", (c) => c.notFound());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user