@draftlab/auth 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.mjs CHANGED
@@ -187,6 +187,15 @@ const issuer = (input) => {
187
187
  const authorization = await getAuthorization(ctx);
188
188
  const currentProvider = ctx.get("provider") || "unknown";
189
189
  if (!authorization.client_id) throw new Error("client_id is required");
190
+ if (manager) try {
191
+ const subjectProperties = properties && typeof properties === "object" ? properties : {};
192
+ await manager.executeSuccessHooks(authorization.client_id, currentProvider, {
193
+ type: currentProvider,
194
+ properties: subjectProperties
195
+ });
196
+ } catch (error$1) {
197
+ console.error("Plugin success hook failed:", error$1);
198
+ }
190
199
  return await input.success({ async subject(type, properties$1, subjectOpts) {
191
200
  const subject = subjectOpts?.subject ?? await resolveSubject(type, properties$1);
192
201
  await successOpts?.invalidate?.(await resolveSubject(type, properties$1));
@@ -276,10 +285,21 @@ const issuer = (input) => {
276
285
  storage
277
286
  };
278
287
  const app = new Router({ basePath: input.basePath });
279
- if (input.plugins && input.plugins.length > 0) {
280
- const manager = new PluginManager(input.storage);
288
+ const manager = input.plugins && input.plugins.length > 0 ? new PluginManager(input.storage) : null;
289
+ let pluginsInitialized = false;
290
+ if (manager && input.plugins) {
281
291
  manager.registerAll(input.plugins);
282
292
  manager.setupRoutes(app);
293
+ app.use(async (c, next) => {
294
+ if (!pluginsInitialized) try {
295
+ await manager.initialize();
296
+ pluginsInitialized = true;
297
+ } catch (error$1) {
298
+ console.error("Plugin initialization failed:", error$1);
299
+ return c.newResponse("Plugin initialization failed", { status: 500 });
300
+ }
301
+ return await next();
302
+ });
283
303
  }
284
304
  for (const [name, value] of Object.entries(input.providers)) {
285
305
  const route = new Router();
@@ -498,13 +518,14 @@ const issuer = (input) => {
498
518
  const audience = c.query("audience");
499
519
  const code_challenge = c.query("code_challenge");
500
520
  const code_challenge_method = c.query("code_challenge_method");
521
+ const scope = c.query("scope");
501
522
  const authorization = {
502
523
  response_type,
503
524
  redirect_uri,
504
525
  state,
505
526
  client_id,
506
527
  audience,
507
- scope: c.query("scope"),
528
+ scope,
508
529
  ...code_challenge && code_challenge_method && { pkce: {
509
530
  challenge: code_challenge,
510
531
  method: code_challenge_method
@@ -520,6 +541,10 @@ const issuer = (input) => {
520
541
  redirectURI: redirect_uri,
521
542
  audience
522
543
  }, c.request)) throw new UnauthorizedClientError(client_id, redirect_uri);
544
+ if (manager) {
545
+ const scopes = scope ? scope.split(" ") : void 0;
546
+ await manager.executeAuthorizeHooks(client_id, provider, scopes);
547
+ }
523
548
  await auth.set(c, "authorization", 900, authorization);
524
549
  if (provider) return c.redirect(`${provider}/authorize`);
525
550
  const availableProviders = Object.keys(input.providers);
@@ -527,6 +552,12 @@ const issuer = (input) => {
527
552
  return auth.forward(c, await select()(Object.fromEntries(Object.entries(input.providers).map(([key, value]) => [key, value.type])), c.request));
528
553
  });
529
554
  app.onError(async (err, c) => {
555
+ if (manager) try {
556
+ const errorObj = err instanceof Error ? err : new Error(String(err));
557
+ await manager.executeErrorHooks(errorObj);
558
+ } catch (hookError) {
559
+ console.error("Plugin error hook failed:", hookError);
560
+ }
530
561
  if (err instanceof UnknownStateError) return auth.forward(c, await error(err, c.request));
531
562
  try {
532
563
  const authorization = await getAuthorization(c);
@@ -3,7 +3,24 @@ import { PluginBuilder } from "./plugin.mjs";
3
3
  //#region src/plugin/builder.d.ts
4
4
 
5
5
  /**
6
- * Create a new plugin
6
+ * Create a new plugin builder.
7
+ * Plugins are built using a fluent API that supports routes and lifecycle hooks.
8
+ *
9
+ * @param id - Unique identifier for the plugin
10
+ * @returns Plugin builder with chainable methods
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const analytics = plugin("analytics")
15
+ * .onSuccess(async (ctx) => {
16
+ * await ctx.storage.set(`success:${ctx.clientID}`, ctx.subject)
17
+ * })
18
+ * .post("/stats", async (ctx) => {
19
+ * const stats = await ctx.pluginStorage.get("stats")
20
+ * return ctx.json(stats)
21
+ * })
22
+ * .build()
23
+ * ```
7
24
  */
8
25
  declare const plugin: (id: string) => PluginBuilder;
9
26
  //#endregion
@@ -1,11 +1,32 @@
1
1
  //#region src/plugin/builder.ts
2
2
  /**
3
- * Create a new plugin
3
+ * Create a new plugin builder.
4
+ * Plugins are built using a fluent API that supports routes and lifecycle hooks.
5
+ *
6
+ * @param id - Unique identifier for the plugin
7
+ * @returns Plugin builder with chainable methods
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const analytics = plugin("analytics")
12
+ * .onSuccess(async (ctx) => {
13
+ * await ctx.storage.set(`success:${ctx.clientID}`, ctx.subject)
14
+ * })
15
+ * .post("/stats", async (ctx) => {
16
+ * const stats = await ctx.pluginStorage.get("stats")
17
+ * return ctx.json(stats)
18
+ * })
19
+ * .build()
20
+ * ```
4
21
  */
5
22
  const plugin = (id) => {
6
23
  if (!id || typeof id !== "string") throw new Error("Plugin id must be a non-empty string");
7
24
  const routes = [];
8
25
  const registeredPaths = /* @__PURE__ */ new Set();
26
+ let initHook;
27
+ let authorizeHook;
28
+ let successHook;
29
+ let errorHook;
9
30
  const validatePath = (path) => {
10
31
  if (!path || typeof path !== "string") throw new Error("Route path must be a non-empty string");
11
32
  if (!path.startsWith("/")) throw new Error("Route path must start with '/'");
@@ -35,11 +56,34 @@ const plugin = (id) => {
35
56
  });
36
57
  return this;
37
58
  },
59
+ onInit(handler) {
60
+ if (initHook) throw new Error(`onInit hook already defined for plugin '${id}'`);
61
+ initHook = handler;
62
+ return this;
63
+ },
64
+ onAuthorize(handler) {
65
+ if (authorizeHook) throw new Error(`onAuthorize hook already defined for plugin '${id}'`);
66
+ authorizeHook = handler;
67
+ return this;
68
+ },
69
+ onSuccess(handler) {
70
+ if (successHook) throw new Error(`onSuccess hook already defined for plugin '${id}'`);
71
+ successHook = handler;
72
+ return this;
73
+ },
74
+ onError(handler) {
75
+ if (errorHook) throw new Error(`onError hook already defined for plugin '${id}'`);
76
+ errorHook = handler;
77
+ return this;
78
+ },
38
79
  build() {
39
- if (routes.length === 0) throw new Error(`Plugin '${id}' has no routes defined`);
40
80
  return {
41
81
  id,
42
- routes
82
+ routes: routes.length > 0 ? routes : void 0,
83
+ onInit: initHook,
84
+ onAuthorize: authorizeHook,
85
+ onSuccess: successHook,
86
+ onError: errorHook
43
87
  };
44
88
  }
45
89
  };
@@ -24,6 +24,35 @@ declare class PluginManager {
24
24
  * Get plugin by id
25
25
  */
26
26
  get(id: string): Plugin | undefined;
27
+ /**
28
+ * Initialize all plugins.
29
+ * Called once during issuer setup.
30
+ * Plugins can set up initial state or validate configuration.
31
+ *
32
+ * @throws PluginError if any plugin initialization fails
33
+ */
34
+ initialize(): Promise<void>;
35
+ /**
36
+ * Execute authorize hooks for all plugins.
37
+ * Called before processing an authorization request.
38
+ * Can validate, rate limit, or enhance the request.
39
+ */
40
+ executeAuthorizeHooks(clientID: string, provider?: string, scopes?: string[]): Promise<void>;
41
+ /**
42
+ * Execute success hooks for all plugins.
43
+ * Called after successful authentication.
44
+ * Runs in parallel for better performance.
45
+ * Plugins cannot modify the response.
46
+ */
47
+ executeSuccessHooks(clientID: string, provider: string | undefined, subject: {
48
+ type: string;
49
+ properties: Record<string, unknown>;
50
+ }): Promise<void>;
51
+ /**
52
+ * Execute error hooks for all plugins.
53
+ * Called when an authentication error occurs.
54
+ */
55
+ executeErrorHooks(error: Error, clientID?: string, provider?: string): Promise<void>;
27
56
  /**
28
57
  * Setup plugin routes on a router
29
58
  */
@@ -33,6 +33,99 @@ var PluginManager = class {
33
33
  return this.plugins.get(id);
34
34
  }
35
35
  /**
36
+ * Initialize all plugins.
37
+ * Called once during issuer setup.
38
+ * Plugins can set up initial state or validate configuration.
39
+ *
40
+ * @throws PluginError if any plugin initialization fails
41
+ */
42
+ async initialize() {
43
+ for (const plugin of this.plugins.values()) {
44
+ if (!plugin.onInit) continue;
45
+ try {
46
+ const context = {
47
+ pluginId: plugin.id,
48
+ request: new Request("http://internal/init"),
49
+ now: /* @__PURE__ */ new Date(),
50
+ storage: this.storage
51
+ };
52
+ await plugin.onInit(context);
53
+ } catch (error) {
54
+ throw new PluginError(`Initialization failed: ${error instanceof Error ? error.message : String(error)}`, plugin.id);
55
+ }
56
+ }
57
+ }
58
+ /**
59
+ * Execute authorize hooks for all plugins.
60
+ * Called before processing an authorization request.
61
+ * Can validate, rate limit, or enhance the request.
62
+ */
63
+ async executeAuthorizeHooks(clientID, provider, scopes) {
64
+ for (const plugin of this.plugins.values()) {
65
+ if (!plugin.onAuthorize) continue;
66
+ try {
67
+ const context = {
68
+ pluginId: plugin.id,
69
+ request: new Request("http://internal/authorize"),
70
+ now: /* @__PURE__ */ new Date(),
71
+ storage: this.storage,
72
+ clientID,
73
+ provider,
74
+ scopes
75
+ };
76
+ await plugin.onAuthorize(context);
77
+ } catch (error) {
78
+ throw new PluginError(`Authorization hook failed: ${error instanceof Error ? error.message : String(error)}`, plugin.id);
79
+ }
80
+ }
81
+ }
82
+ /**
83
+ * Execute success hooks for all plugins.
84
+ * Called after successful authentication.
85
+ * Runs in parallel for better performance.
86
+ * Plugins cannot modify the response.
87
+ */
88
+ async executeSuccessHooks(clientID, provider, subject) {
89
+ const hooks = Array.from(this.plugins.values()).filter((p) => p.onSuccess).map(async (plugin) => {
90
+ const context = {
91
+ pluginId: plugin.id,
92
+ request: new Request("http://internal/success"),
93
+ now: /* @__PURE__ */ new Date(),
94
+ storage: this.storage,
95
+ clientID,
96
+ provider,
97
+ subject
98
+ };
99
+ return plugin.onSuccess?.(context).catch((error) => {
100
+ console.error(`[Plugin: ${plugin.id}] Success hook failed:`, error instanceof Error ? error.message : String(error));
101
+ });
102
+ });
103
+ await Promise.all(hooks);
104
+ }
105
+ /**
106
+ * Execute error hooks for all plugins.
107
+ * Called when an authentication error occurs.
108
+ */
109
+ async executeErrorHooks(error, clientID, provider) {
110
+ for (const plugin of this.plugins.values()) {
111
+ if (!plugin.onError) continue;
112
+ try {
113
+ const context = {
114
+ pluginId: plugin.id,
115
+ request: new Request("http://internal/error"),
116
+ now: /* @__PURE__ */ new Date(),
117
+ storage: this.storage,
118
+ error,
119
+ clientID,
120
+ provider
121
+ };
122
+ await plugin.onError(context);
123
+ } catch (hookError) {
124
+ console.error(`[Plugin: ${plugin.id}] Error hook failed:`, hookError instanceof Error ? hookError.message : String(hookError));
125
+ }
126
+ }
127
+ }
128
+ /**
36
129
  * Setup plugin routes on a router
37
130
  */
38
131
  setupRoutes(router) {
@@ -40,7 +133,7 @@ var PluginManager = class {
40
133
  for (const plugin of this.plugins.values()) {
41
134
  if (!plugin.routes) continue;
42
135
  for (const route of plugin.routes) {
43
- const fullPath = `/${plugin.id}${route.path}`;
136
+ const fullPath = `/plugin/${plugin.id}${route.path}`;
44
137
  if (registeredPaths.has(fullPath)) throw new PluginError(`Route conflict: ${fullPath} already registered`, plugin.id);
45
138
  registeredPaths.add(fullPath);
46
139
  const handler = async (ctx) => {
@@ -1,16 +1,41 @@
1
- import { Plugin, PluginRouteHandler } from "./types.mjs";
1
+ import { Plugin, PluginAuthorizeHook, PluginErrorHook, PluginInitHook, PluginRouteHandler, PluginSuccessHook } from "./types.mjs";
2
2
 
3
3
  //#region src/plugin/plugin.d.ts
4
4
 
5
5
  /**
6
- * Plugin builder interface
6
+ * Plugin builder interface for creating plugins with a fluent API.
7
+ *
8
+ * The builder pattern allows for elegant plugin definition:
9
+ * - Chain route definitions with lifecycle hooks
10
+ * - Each method returns this for chaining
11
+ * - Build finalizes the plugin definition
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const myPlugin = plugin("my-plugin")
16
+ * .onInit(async (ctx) => {
17
+ * console.log("Plugin initialized")
18
+ * })
19
+ * .post("/action", async (ctx) => {
20
+ * return ctx.json({ success: true })
21
+ * })
22
+ * .build()
23
+ * ```
7
24
  */
8
25
  interface PluginBuilder {
9
- /** Add GET route */
26
+ /** Register a GET route */
10
27
  get(path: string, handler: PluginRouteHandler): PluginBuilder;
11
- /** Add POST route */
28
+ /** Register a POST route */
12
29
  post(path: string, handler: PluginRouteHandler): PluginBuilder;
13
- /** Build the plugin */
30
+ /** Register initialization hook (called once during issuer setup) */
31
+ onInit(handler: PluginInitHook): PluginBuilder;
32
+ /** Register authorization hook (called before authorization request) */
33
+ onAuthorize(handler: PluginAuthorizeHook): PluginBuilder;
34
+ /** Register success hook (called after successful authentication) */
35
+ onSuccess(handler: PluginSuccessHook): PluginBuilder;
36
+ /** Register error hook (called when authentication fails) */
37
+ onError(handler: PluginErrorHook): PluginBuilder;
38
+ /** Build the final plugin */
14
39
  build(): Plugin;
15
40
  }
16
41
  //#endregion
@@ -22,13 +22,72 @@ interface PluginRoute {
22
22
  readonly handler: PluginRouteHandler;
23
23
  }
24
24
  /**
25
- * Main plugin interface
25
+ * Lifecycle hook context provided to plugin hooks.
26
+ * Contains information about the current operation and access to isolated storage.
27
+ */
28
+ interface PluginHookContext {
29
+ /** Unique identifier for the plugin */
30
+ pluginId: string;
31
+ /** Raw request object */
32
+ request: Request;
33
+ /** Current time for consistency across hook execution */
34
+ now: Date;
35
+ /** Storage adapter for data persistence */
36
+ storage: StorageAdapter;
37
+ }
38
+ /**
39
+ * Hook called when the issuer is being initialized.
40
+ * Useful for plugins that need to set up initial state or validate configuration.
41
+ * Should complete quickly - takes place during server startup.
42
+ */
43
+ type PluginInitHook = (context: PluginHookContext) => Promise<void>;
44
+ /**
45
+ * Hook called before an authorization request is processed.
46
+ * Use for validation, rate limiting, or request enhancement.
47
+ */
48
+ type PluginAuthorizeHook = (context: PluginHookContext & {
49
+ clientID: string;
50
+ provider?: string;
51
+ scopes?: string[];
52
+ }) => Promise<void>;
53
+ /**
54
+ * Hook called after successful authentication.
55
+ * Use for logging, analytics, webhooks, or side effects.
56
+ * Cannot modify the response - hooks run in parallel.
57
+ */
58
+ type PluginSuccessHook = (context: PluginHookContext & {
59
+ clientID: string;
60
+ provider?: string;
61
+ subject: {
62
+ type: string;
63
+ properties: Record<string, unknown>;
64
+ };
65
+ }) => Promise<void>;
66
+ /**
67
+ * Hook called when an authentication error occurs.
68
+ * Use for error logging, custom error pages, or error transformation.
69
+ */
70
+ type PluginErrorHook = (context: PluginHookContext & {
71
+ error: Error;
72
+ clientID?: string;
73
+ provider?: string;
74
+ }) => Promise<void>;
75
+ /**
76
+ * Main plugin interface with lifecycle hooks and storage isolation
26
77
  */
27
78
  interface Plugin {
28
79
  /** Unique plugin identifier */
29
80
  readonly id: string;
30
81
  /** Custom routes added by this plugin */
31
82
  readonly routes?: readonly PluginRoute[];
83
+ /** Called once when the issuer initializes */
84
+ readonly onInit?: PluginInitHook;
85
+ /** Called before authorization request is processed */
86
+ readonly onAuthorize?: PluginAuthorizeHook;
87
+ /** Called after successful authentication */
88
+ readonly onSuccess?: PluginSuccessHook;
89
+ /** Called when an error occurs during authentication */
90
+ readonly onError?: PluginErrorHook;
32
91
  }
33
92
  /**
34
93
  * Plugin error types
@@ -37,4 +96,4 @@ declare class PluginError extends Error {
37
96
  constructor(message: string, pluginId: string);
38
97
  }
39
98
  //#endregion
40
- export { Plugin, PluginContext, PluginError, PluginRoute, PluginRouteHandler };
99
+ export { Plugin, PluginAuthorizeHook, PluginContext, PluginError, PluginErrorHook, PluginHookContext, PluginInitHook, PluginRoute, PluginRouteHandler, PluginSuccessHook };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@draftlab/auth",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "description": "Core implementation for @draftlab/auth",
6
6
  "author": "Matheus Pergoli",