mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 16:46:00 +00:00
init auth strategy registry
This commit is contained in:
@@ -6,7 +6,7 @@ import type { Entity, EntityManager } from "data";
|
|||||||
import { em, entity, enumm, type FieldSchema } from "data/prototype";
|
import { em, entity, enumm, type FieldSchema } from "data/prototype";
|
||||||
import { Module } from "modules/Module";
|
import { Module } from "modules/Module";
|
||||||
import { AuthController } from "./api/AuthController";
|
import { AuthController } from "./api/AuthController";
|
||||||
import { type AppAuthSchema, authConfigSchema, STRATEGIES } from "./auth-schema";
|
import { type AppAuthSchema, authConfigSchema, AuthStrategyRegistry } from "./auth-schema";
|
||||||
import { AppUserPool } from "auth/AppUserPool";
|
import { AppUserPool } from "auth/AppUserPool";
|
||||||
import type { AppEntity } from "core/config";
|
import type { AppEntity } from "core/config";
|
||||||
import { usersFields } from "./auth-entities";
|
import { usersFields } from "./auth-entities";
|
||||||
@@ -65,7 +65,8 @@ export class AppAuth extends Module<AppAuthSchema> {
|
|||||||
// build strategies
|
// build strategies
|
||||||
const strategies = transformObject(this.config.strategies ?? {}, (strategy, name) => {
|
const strategies = transformObject(this.config.strategies ?? {}, (strategy, name) => {
|
||||||
try {
|
try {
|
||||||
return new STRATEGIES[strategy.type].cls(strategy.config as any);
|
const cls = AuthStrategyRegistry.get(strategy.type as any).cls;
|
||||||
|
return new cls(strategy.config as any);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Could not build strategy ${String(
|
`Could not build strategy ${String(
|
||||||
|
|||||||
+20
-19
@@ -1,24 +1,25 @@
|
|||||||
import { cookieConfig, jwtConfig } from "auth/authenticate/Authenticator";
|
import { cookieConfig, jwtConfig, type Strategy } from "auth/authenticate/Authenticator";
|
||||||
import { CustomOAuthStrategy, OAuthStrategy, PasswordStrategy } from "auth/authenticate/strategies";
|
|
||||||
import { objectTransform } from "core/utils";
|
import { objectTransform } from "core/utils";
|
||||||
import { s } from "core/object/schema";
|
import { s } from "core/object/schema";
|
||||||
|
import type { ProvidedOAuthConfig } from "./authenticate/strategies/oauth/OAuthStrategy";
|
||||||
|
import type { OAuthConfigCustom } from "./authenticate/strategies/oauth/CustomOAuthStrategy";
|
||||||
|
import { Registry, type Constructor } from "core";
|
||||||
|
import { PasswordStrategy } from "./authenticate/strategies/PasswordStrategy";
|
||||||
|
import { OAuthStrategy } from "./authenticate/strategies/oauth/OAuthStrategy";
|
||||||
|
import { CustomOAuthStrategy } from "./authenticate/strategies/oauth/CustomOAuthStrategy";
|
||||||
|
|
||||||
export const Strategies = {
|
export const AuthStrategyRegistry = new Registry<{
|
||||||
password: {
|
cls: Constructor<Strategy>;
|
||||||
cls: PasswordStrategy,
|
schema: s.Schema;
|
||||||
schema: PasswordStrategy.prototype.getSchema(),
|
}>((cls: Constructor<Strategy>) => ({
|
||||||
},
|
cls,
|
||||||
oauth: {
|
schema: cls.prototype.getSchema() as s.Schema,
|
||||||
cls: OAuthStrategy,
|
}))
|
||||||
schema: OAuthStrategy.prototype.getSchema(),
|
.register("password", PasswordStrategy)
|
||||||
},
|
.register("oauth", OAuthStrategy)
|
||||||
custom_oauth: {
|
.register("custom_oauth", CustomOAuthStrategy);
|
||||||
cls: CustomOAuthStrategy,
|
|
||||||
schema: CustomOAuthStrategy.prototype.getSchema(),
|
|
||||||
},
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
export const STRATEGIES = Strategies;
|
export const STRATEGIES = AuthStrategyRegistry.all();
|
||||||
const strategiesSchemaObject = objectTransform(STRATEGIES, (strategy, name) => {
|
const strategiesSchemaObject = objectTransform(STRATEGIES, (strategy, name) => {
|
||||||
return s.strictObject(
|
return s.strictObject(
|
||||||
{
|
{
|
||||||
@@ -34,8 +35,8 @@ const strategiesSchemaObject = objectTransform(STRATEGIES, (strategy, name) => {
|
|||||||
|
|
||||||
const strategiesSchema = s.anyOf(Object.values(strategiesSchemaObject));
|
const strategiesSchema = s.anyOf(Object.values(strategiesSchemaObject));
|
||||||
export type AppAuthStrategies = s.Static<typeof strategiesSchema>;
|
export type AppAuthStrategies = s.Static<typeof strategiesSchema>;
|
||||||
export type AppAuthOAuthStrategy = s.Static<typeof STRATEGIES.oauth.schema>;
|
export type AppAuthOAuthStrategy = ProvidedOAuthConfig;
|
||||||
export type AppAuthCustomOAuthStrategy = s.Static<typeof STRATEGIES.custom_oauth.schema>;
|
export type AppAuthCustomOAuthStrategy = OAuthConfigCustom;
|
||||||
|
|
||||||
const guardConfigSchema = s.object({
|
const guardConfigSchema = s.object({
|
||||||
enabled: s.boolean({ default: false }).optional(),
|
enabled: s.boolean({ default: false }).optional(),
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ const oauthSchemaCustom = s.strictObject(
|
|||||||
{ title: "Custom OAuth" },
|
{ title: "Custom OAuth" },
|
||||||
);
|
);
|
||||||
|
|
||||||
type OAuthConfigCustom = s.Static<typeof oauthSchemaCustom>;
|
export type OAuthConfigCustom = s.Static<typeof oauthSchemaCustom>;
|
||||||
|
|
||||||
export type UserProfile = {
|
export type UserProfile = {
|
||||||
sub: string;
|
sub: string;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const schemaProvided = s.object(
|
|||||||
},
|
},
|
||||||
{ title: "OAuth" },
|
{ title: "OAuth" },
|
||||||
);
|
);
|
||||||
type ProvidedOAuthConfig = s.Static<typeof schemaProvided>;
|
export type ProvidedOAuthConfig = s.Static<typeof schemaProvided>;
|
||||||
|
|
||||||
export type CustomOAuthConfig = {
|
export type CustomOAuthConfig = {
|
||||||
type: SupportedTypes;
|
type: SupportedTypes;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export {
|
|||||||
type BooleanLike,
|
type BooleanLike,
|
||||||
isBooleanLike,
|
isBooleanLike,
|
||||||
} from "./object/query/query";
|
} from "./object/query/query";
|
||||||
export { Registry, type Constructor } from "./registry/Registry";
|
export { Registry, type Constructor, type ClassThatImplements } from "./registry/Registry";
|
||||||
export { getFlashMessage } from "./server/flash";
|
export { getFlashMessage } from "./server/flash";
|
||||||
/* export {
|
/* export {
|
||||||
s,
|
s,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
export type Constructor<T> = new (...args: any[]) => T;
|
export type Constructor<T> = new (...args: any[]) => T;
|
||||||
|
export type ClassThatImplements<T> = Constructor<T> & { prototype: T };
|
||||||
export type RegisterFn<Item> = (unknown: any) => Item;
|
export type RegisterFn<Item> = (unknown: any) => Item;
|
||||||
|
|
||||||
export class Registry<
|
export class Registry<
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { type FileUploadedEventData, Storage, type StorageAdapter, MediaPermissi
|
|||||||
import { Module } from "modules/Module";
|
import { Module } from "modules/Module";
|
||||||
import { type FieldSchema, em, entity } from "../data/prototype";
|
import { type FieldSchema, em, entity } from "../data/prototype";
|
||||||
import { MediaController } from "./api/MediaController";
|
import { MediaController } from "./api/MediaController";
|
||||||
import { buildMediaSchema, type mediaConfigSchema, registry, type TAppMediaConfig } from "./media-schema";
|
import { buildMediaSchema, MediaAdapterRegistry, type TAppMediaConfig } from "./media-schema";
|
||||||
import { mediaFields } from "./media-entities";
|
import { mediaFields } from "./media-entities";
|
||||||
|
|
||||||
export type MediaFieldSchema = FieldSchema<typeof AppMedia.mediaFields>;
|
export type MediaFieldSchema = FieldSchema<typeof AppMedia.mediaFields>;
|
||||||
@@ -35,7 +35,7 @@ export class AppMedia extends Module<Required<TAppMediaConfig>> {
|
|||||||
let adapter: StorageAdapter;
|
let adapter: StorageAdapter;
|
||||||
try {
|
try {
|
||||||
const { type, config } = this.config.adapter;
|
const { type, config } = this.config.adapter;
|
||||||
const cls = registry.get(type as any).cls;
|
const cls = MediaAdapterRegistry.get(type as any).cls;
|
||||||
adapter = new cls(config as any);
|
adapter = new cls(config as any);
|
||||||
|
|
||||||
this._storage = new Storage(adapter, this.config.storage, this.ctx.emgr);
|
this._storage = new Storage(adapter, this.config.storage, this.ctx.emgr);
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { type Constructor, Registry } from "core";
|
|
||||||
|
|
||||||
export { guess as guessMimeType } from "./storage/mime-types-tiny";
|
export { guess as guessMimeType } from "./storage/mime-types-tiny";
|
||||||
export {
|
export {
|
||||||
Storage,
|
Storage,
|
||||||
@@ -15,7 +13,6 @@ import {
|
|||||||
StorageCloudinaryAdapter,
|
StorageCloudinaryAdapter,
|
||||||
} from "./storage/adapters/cloudinary/StorageCloudinaryAdapter";
|
} from "./storage/adapters/cloudinary/StorageCloudinaryAdapter";
|
||||||
import { type S3AdapterConfig, StorageS3Adapter } from "./storage/adapters/s3/StorageS3Adapter";
|
import { type S3AdapterConfig, StorageS3Adapter } from "./storage/adapters/s3/StorageS3Adapter";
|
||||||
import type { s } from "core/object/schema";
|
|
||||||
|
|
||||||
export { StorageAdapter };
|
export { StorageAdapter };
|
||||||
export { StorageS3Adapter, type S3AdapterConfig, StorageCloudinaryAdapter, type CloudinaryConfig };
|
export { StorageS3Adapter, type S3AdapterConfig, StorageCloudinaryAdapter, type CloudinaryConfig };
|
||||||
@@ -25,27 +22,4 @@ export * as MediaPermissions from "./media-permissions";
|
|||||||
export type { FileUploadedEventData } from "./storage/events";
|
export type { FileUploadedEventData } from "./storage/events";
|
||||||
export * from "./utils";
|
export * from "./utils";
|
||||||
|
|
||||||
type ClassThatImplements<T> = Constructor<T> & { prototype: T };
|
|
||||||
|
|
||||||
export const MediaAdapterRegistry = new Registry<{
|
|
||||||
cls: ClassThatImplements<StorageAdapter>;
|
|
||||||
schema: s.Schema;
|
|
||||||
}>((cls: ClassThatImplements<StorageAdapter>) => ({
|
|
||||||
cls,
|
|
||||||
schema: cls.prototype.getSchema() as s.Schema,
|
|
||||||
}))
|
|
||||||
.register("s3", StorageS3Adapter)
|
|
||||||
.register("cloudinary", StorageCloudinaryAdapter);
|
|
||||||
|
|
||||||
export const Adapters = {
|
|
||||||
s3: {
|
|
||||||
cls: StorageS3Adapter,
|
|
||||||
schema: StorageS3Adapter.prototype.getSchema(),
|
|
||||||
},
|
|
||||||
cloudinary: {
|
|
||||||
cls: StorageCloudinaryAdapter,
|
|
||||||
schema: StorageCloudinaryAdapter.prototype.getSchema(),
|
|
||||||
},
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
export { adapterTestSuite } from "./storage/adapters/adapter-test-suite";
|
export { adapterTestSuite } from "./storage/adapters/adapter-test-suite";
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
import { objectTransform } from "core/utils";
|
import { objectTransform } from "core/utils";
|
||||||
import { Adapters } from "media";
|
import { type StorageAdapter, StorageS3Adapter, StorageCloudinaryAdapter } from "media";
|
||||||
import { registries } from "modules/registries";
|
|
||||||
import { s } from "core/object/schema";
|
import { s } from "core/object/schema";
|
||||||
|
import { Registry, type ClassThatImplements } from "core";
|
||||||
|
|
||||||
export const ADAPTERS = {
|
export const MediaAdapterRegistry = new Registry<{
|
||||||
...Adapters,
|
cls: ClassThatImplements<StorageAdapter>;
|
||||||
} as const;
|
schema: s.Schema;
|
||||||
|
}>((cls: ClassThatImplements<StorageAdapter>) => ({
|
||||||
export const registry = registries.media;
|
cls,
|
||||||
|
schema: cls.prototype.getSchema() as s.Schema,
|
||||||
|
}))
|
||||||
|
.register("s3", StorageS3Adapter)
|
||||||
|
.register("cloudinary", StorageCloudinaryAdapter);
|
||||||
|
|
||||||
export function buildMediaSchema() {
|
export function buildMediaSchema() {
|
||||||
const adapterSchemaObject = objectTransform(registry.all(), (adapter, name) => {
|
const adapterSchemaObject = objectTransform(MediaAdapterRegistry.all(), (adapter, name) => {
|
||||||
return s.strictObject(
|
return s.strictObject(
|
||||||
{
|
{
|
||||||
type: s.literal(name),
|
type: s.literal(name),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { MediaAdapterRegistry } from "media";
|
import { AuthStrategyRegistry } from "auth/auth-schema";
|
||||||
|
import { MediaAdapterRegistry } from "media/media-schema";
|
||||||
|
|
||||||
const registries = {
|
const registries = {
|
||||||
media: MediaAdapterRegistry,
|
media: MediaAdapterRegistry,
|
||||||
|
auth: AuthStrategyRegistry,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export { registries };
|
export { registries };
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import {
|
|||||||
TbBrandX,
|
TbBrandX,
|
||||||
TbSettings,
|
TbSettings,
|
||||||
} from "react-icons/tb";
|
} from "react-icons/tb";
|
||||||
import { twMerge } from "tailwind-merge";
|
|
||||||
import { useBknd } from "ui/client/bknd";
|
import { useBknd } from "ui/client/bknd";
|
||||||
import { useBkndAuth } from "ui/client/schema/auth/use-bknd-auth";
|
import { useBkndAuth } from "ui/client/schema/auth/use-bknd-auth";
|
||||||
import { Button } from "ui/components/buttons/Button";
|
import { Button } from "ui/components/buttons/Button";
|
||||||
@@ -76,7 +75,7 @@ function AuthStrategiesListInternal() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
schema={schema.toJSON()}
|
schema={JSON.parse(JSON.stringify(schema))}
|
||||||
initialValues={config}
|
initialValues={config}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
options={formOptions}
|
options={formOptions}
|
||||||
|
|||||||
Reference in New Issue
Block a user