@draftlab/auth 0.10.4 → 0.12.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.d.mts +0 -3
- package/dist/core.mjs +0 -36
- package/dist/toolkit/client.d.mts +169 -0
- package/dist/toolkit/client.mjs +209 -0
- package/dist/toolkit/index.d.mts +9 -0
- package/dist/toolkit/index.mjs +9 -0
- package/dist/toolkit/providers/facebook.d.mts +12 -0
- package/dist/toolkit/providers/facebook.mjs +16 -0
- package/dist/toolkit/providers/github.d.mts +12 -0
- package/dist/toolkit/providers/github.mjs +16 -0
- package/dist/toolkit/providers/google.d.mts +12 -0
- package/dist/toolkit/providers/google.mjs +20 -0
- package/dist/toolkit/providers/strategy.d.mts +40 -0
- package/dist/toolkit/storage.d.mts +145 -0
- package/dist/toolkit/storage.mjs +157 -0
- package/dist/toolkit/utils.d.mts +21 -0
- package/dist/toolkit/utils.mjs +30 -0
- package/package.json +3 -3
- package/dist/plugin/builder.d.mts +0 -27
- package/dist/plugin/builder.mjs +0 -93
- package/dist/plugin/manager.d.mts +0 -62
- package/dist/plugin/manager.mjs +0 -160
- package/dist/plugin/plugin.d.mts +0 -42
- package/dist/plugin/types.d.mts +0 -99
- package/dist/plugin/types.mjs +0 -13
- /package/dist/{plugin/plugin.mjs → toolkit/providers/strategy.mjs} +0 -0
package/dist/plugin/manager.mjs
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { PluginError } from "./types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/plugin/manager.ts
|
|
4
|
-
var PluginManager = class {
|
|
5
|
-
plugins = /* @__PURE__ */ new Map();
|
|
6
|
-
storage;
|
|
7
|
-
constructor(storage) {
|
|
8
|
-
this.storage = storage;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Register a plugin
|
|
12
|
-
*/
|
|
13
|
-
register(plugin) {
|
|
14
|
-
if (this.plugins.has(plugin.id)) throw new PluginError(`Plugin already registered`, plugin.id);
|
|
15
|
-
this.plugins.set(plugin.id, plugin);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Register multiple plugins at once
|
|
19
|
-
*/
|
|
20
|
-
registerAll(plugins) {
|
|
21
|
-
for (const plugin of plugins) this.register(plugin);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Get all registered plugins
|
|
25
|
-
*/
|
|
26
|
-
getAll() {
|
|
27
|
-
return Array.from(this.plugins.values());
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Get plugin by id
|
|
31
|
-
*/
|
|
32
|
-
get(id) {
|
|
33
|
-
return this.plugins.get(id);
|
|
34
|
-
}
|
|
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
|
-
/**
|
|
129
|
-
* Setup plugin routes on a router
|
|
130
|
-
*/
|
|
131
|
-
setupRoutes(router) {
|
|
132
|
-
const registeredPaths = /* @__PURE__ */ new Set();
|
|
133
|
-
for (const plugin of this.plugins.values()) {
|
|
134
|
-
if (!plugin.routes) continue;
|
|
135
|
-
for (const route of plugin.routes) {
|
|
136
|
-
const fullPath = `/plugin/${plugin.id}${route.path}`;
|
|
137
|
-
if (registeredPaths.has(fullPath)) throw new PluginError(`Route conflict: ${fullPath} already registered`, plugin.id);
|
|
138
|
-
registeredPaths.add(fullPath);
|
|
139
|
-
const handler = async (ctx) => {
|
|
140
|
-
const pluginCtx = {
|
|
141
|
-
...ctx,
|
|
142
|
-
storage: this.storage
|
|
143
|
-
};
|
|
144
|
-
return await route.handler(pluginCtx);
|
|
145
|
-
};
|
|
146
|
-
switch (route.method) {
|
|
147
|
-
case "GET":
|
|
148
|
-
router.get(fullPath, handler);
|
|
149
|
-
break;
|
|
150
|
-
case "POST":
|
|
151
|
-
router.post(fullPath, handler);
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
//#endregion
|
|
160
|
-
export { PluginManager };
|
package/dist/plugin/plugin.d.mts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Plugin, PluginAuthorizeHook, PluginErrorHook, PluginInitHook, PluginRouteHandler, PluginSuccessHook } from "./types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/plugin/plugin.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
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
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
interface PluginBuilder {
|
|
26
|
-
/** Register a GET route */
|
|
27
|
-
get(path: string, handler: PluginRouteHandler): PluginBuilder;
|
|
28
|
-
/** Register a POST route */
|
|
29
|
-
post(path: string, handler: PluginRouteHandler): PluginBuilder;
|
|
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 */
|
|
39
|
-
build(): Plugin;
|
|
40
|
-
}
|
|
41
|
-
//#endregion
|
|
42
|
-
export { PluginBuilder };
|
package/dist/plugin/types.d.mts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { StorageAdapter } from "../storage/storage.mjs";
|
|
2
|
-
import { RouterContext } from "@draftlab/auth-router/types";
|
|
3
|
-
|
|
4
|
-
//#region src/plugin/types.d.ts
|
|
5
|
-
|
|
6
|
-
interface PluginContext extends RouterContext {
|
|
7
|
-
storage: StorageAdapter;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Plugin route handler function
|
|
11
|
-
*/
|
|
12
|
-
type PluginRouteHandler = (context: PluginContext) => Promise<Response>;
|
|
13
|
-
/**
|
|
14
|
-
* Plugin route definition
|
|
15
|
-
*/
|
|
16
|
-
interface PluginRoute {
|
|
17
|
-
/** Route path (e.g., "/admin", "/stats") */
|
|
18
|
-
readonly path: string;
|
|
19
|
-
/** HTTP method */
|
|
20
|
-
readonly method: "GET" | "POST";
|
|
21
|
-
/** Route handler function */
|
|
22
|
-
readonly handler: PluginRouteHandler;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
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
|
|
77
|
-
*/
|
|
78
|
-
interface Plugin {
|
|
79
|
-
/** Unique plugin identifier */
|
|
80
|
-
readonly id: string;
|
|
81
|
-
/** Custom routes added by this plugin */
|
|
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;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Plugin error types
|
|
94
|
-
*/
|
|
95
|
-
declare class PluginError extends Error {
|
|
96
|
-
constructor(message: string, pluginId: string);
|
|
97
|
-
}
|
|
98
|
-
//#endregion
|
|
99
|
-
export { Plugin, PluginAuthorizeHook, PluginContext, PluginError, PluginErrorHook, PluginHookContext, PluginInitHook, PluginRoute, PluginRouteHandler, PluginSuccessHook };
|
package/dist/plugin/types.mjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
//#region src/plugin/types.ts
|
|
2
|
-
/**
|
|
3
|
-
* Plugin error types
|
|
4
|
-
*/
|
|
5
|
-
var PluginError = class extends Error {
|
|
6
|
-
constructor(message, pluginId) {
|
|
7
|
-
super(`[Plugin: ${pluginId}] ${message}`);
|
|
8
|
-
this.name = "PluginError";
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { PluginError };
|
|
File without changes
|