@cloudflare/workers-oauth-provider 0.0.3 → 0.0.4-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/README.md +10 -0
- package/dist/oauth-provider.d.ts +19 -2
- package/dist/oauth-provider.js +54 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,6 +43,16 @@ export default new OAuthProvider({
|
|
|
43
43
|
// You can provide either an object with a fetch method (ExportedHandler)
|
|
44
44
|
// or a class extending WorkerEntrypoint.
|
|
45
45
|
apiHandler: ApiHandler, // Using a WorkerEntrypoint class
|
|
46
|
+
|
|
47
|
+
// For multi-handler setups, you can use apiHandlers instead of apiRoute+apiHandler.
|
|
48
|
+
// This allows you to use different handlers for different API routes.
|
|
49
|
+
// Note: You must use either apiRoute+apiHandler (single-handler) OR apiHandlers (multi-handler), not both.
|
|
50
|
+
// Example:
|
|
51
|
+
// apiHandlers: {
|
|
52
|
+
// "/api/users/": UsersApiHandler,
|
|
53
|
+
// "/api/documents/": DocumentsApiHandler,
|
|
54
|
+
// "https://api.example.com/": ExternalApiHandler,
|
|
55
|
+
// },
|
|
46
56
|
|
|
47
57
|
// Any requests which aren't API request will be passed to the default handler instead.
|
|
48
58
|
// Again, this can be either an object or a WorkerEntrypoint.
|
package/dist/oauth-provider.d.ts
CHANGED
|
@@ -67,14 +67,31 @@ interface OAuthProviderOptions {
|
|
|
67
67
|
* URL(s) for API routes. Requests with URLs starting with any of these prefixes
|
|
68
68
|
* will be treated as API requests and require a valid access token.
|
|
69
69
|
* Can be a single route or an array of routes. Each route can be a full URL or just a path.
|
|
70
|
+
*
|
|
71
|
+
* Used with `apiHandler` for the single-handler configuration. This is incompatible with
|
|
72
|
+
* the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
|
|
70
73
|
*/
|
|
71
|
-
apiRoute
|
|
74
|
+
apiRoute?: string | string[];
|
|
72
75
|
/**
|
|
73
76
|
* Handler for API requests that have a valid access token.
|
|
74
77
|
* This handler will receive the authenticated user properties in ctx.props.
|
|
75
78
|
* Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
|
|
79
|
+
*
|
|
80
|
+
* Used with `apiRoute` for the single-handler configuration. This is incompatible with
|
|
81
|
+
* the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
|
|
82
|
+
*/
|
|
83
|
+
apiHandler?: ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch);
|
|
84
|
+
/**
|
|
85
|
+
* Map of API routes to their corresponding handlers for the multi-handler configuration.
|
|
86
|
+
* The keys are the API routes (strings only, not arrays), and the values are the handlers.
|
|
87
|
+
* Each route can be a full URL or just a path, and each handler can be either an ExportedHandler
|
|
88
|
+
* object with a fetch method or a class extending WorkerEntrypoint.
|
|
89
|
+
*
|
|
90
|
+
* This is incompatible with the `apiRoute` and `apiHandler` properties. You must use either
|
|
91
|
+
* `apiRoute` + `apiHandler` (single-handler configuration) OR `apiHandlers` (multi-handler
|
|
92
|
+
* configuration), not both.
|
|
76
93
|
*/
|
|
77
|
-
|
|
94
|
+
apiHandlers?: Record<string, ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch)>;
|
|
78
95
|
/**
|
|
79
96
|
* Handler for all non-API requests or API requests without a valid token.
|
|
80
97
|
* Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
|
package/dist/oauth-provider.js
CHANGED
|
@@ -37,14 +37,36 @@ var OAuthProviderImpl = class {
|
|
|
37
37
|
* @param options - Configuration options for the provider
|
|
38
38
|
*/
|
|
39
39
|
constructor(options) {
|
|
40
|
-
this.
|
|
40
|
+
this.typedApiHandlers = /* @__PURE__ */ new Map();
|
|
41
|
+
const hasSingleHandlerConfig = !!(options.apiRoute && options.apiHandler);
|
|
42
|
+
const hasMultiHandlerConfig = !!options.apiHandlers;
|
|
43
|
+
if (hasSingleHandlerConfig && hasMultiHandlerConfig) {
|
|
44
|
+
throw new TypeError(
|
|
45
|
+
"Cannot use both apiRoute/apiHandler and apiHandlers. Use either apiRoute + apiHandler OR apiHandlers, not both."
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (!hasSingleHandlerConfig && !hasMultiHandlerConfig) {
|
|
49
|
+
throw new TypeError(
|
|
50
|
+
"Must provide either apiRoute + apiHandler OR apiHandlers. No API route configuration provided."
|
|
51
|
+
);
|
|
52
|
+
}
|
|
41
53
|
this.typedDefaultHandler = this.validateHandler(options.defaultHandler, "defaultHandler");
|
|
42
|
-
if (
|
|
43
|
-
options.
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
if (hasSingleHandlerConfig) {
|
|
55
|
+
const apiHandler = this.validateHandler(options.apiHandler, "apiHandler");
|
|
56
|
+
if (Array.isArray(options.apiRoute)) {
|
|
57
|
+
options.apiRoute.forEach((route, index) => {
|
|
58
|
+
this.validateEndpoint(route, `apiRoute[${index}]`);
|
|
59
|
+
this.typedApiHandlers.set(route, apiHandler);
|
|
60
|
+
});
|
|
61
|
+
} else {
|
|
62
|
+
this.validateEndpoint(options.apiRoute, "apiRoute");
|
|
63
|
+
this.typedApiHandlers.set(options.apiRoute, apiHandler);
|
|
64
|
+
}
|
|
46
65
|
} else {
|
|
47
|
-
|
|
66
|
+
for (const [route, handler] of Object.entries(options.apiHandlers)) {
|
|
67
|
+
this.validateEndpoint(route, `apiHandlers key: ${route}`);
|
|
68
|
+
this.typedApiHandlers.set(route, this.validateHandler(handler, `apiHandlers[${route}]`));
|
|
69
|
+
}
|
|
48
70
|
}
|
|
49
71
|
this.validateEndpoint(options.authorizeEndpoint, "authorizeEndpoint");
|
|
50
72
|
this.validateEndpoint(options.tokenEndpoint, "tokenEndpoint");
|
|
@@ -204,11 +226,25 @@ var OAuthProviderImpl = class {
|
|
|
204
226
|
* @returns True if the URL matches any of the API routes
|
|
205
227
|
*/
|
|
206
228
|
isApiRequest(url) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
229
|
+
for (const route of this.typedApiHandlers.keys()) {
|
|
230
|
+
if (this.matchApiRoute(url, route)) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Finds the appropriate API handler for a URL
|
|
238
|
+
* @param url - The URL to find a handler for
|
|
239
|
+
* @returns The TypedHandler for the URL, or undefined if no handler matches
|
|
240
|
+
*/
|
|
241
|
+
findApiHandlerForUrl(url) {
|
|
242
|
+
for (const [route, handler] of this.typedApiHandlers.entries()) {
|
|
243
|
+
if (this.matchApiRoute(url, route)) {
|
|
244
|
+
return handler;
|
|
245
|
+
}
|
|
211
246
|
}
|
|
247
|
+
return void 0;
|
|
212
248
|
}
|
|
213
249
|
/**
|
|
214
250
|
* Gets the full URL for an endpoint, using the provided request URL's
|
|
@@ -799,10 +835,15 @@ var OAuthProviderImpl = class {
|
|
|
799
835
|
if (!env.OAUTH_PROVIDER) {
|
|
800
836
|
env.OAUTH_PROVIDER = this.createOAuthHelpers(env);
|
|
801
837
|
}
|
|
802
|
-
|
|
803
|
-
|
|
838
|
+
const url = new URL(request.url);
|
|
839
|
+
const apiHandler = this.findApiHandlerForUrl(url);
|
|
840
|
+
if (!apiHandler) {
|
|
841
|
+
return this.createErrorResponse("invalid_request", "No handler found for API route", 404);
|
|
842
|
+
}
|
|
843
|
+
if (apiHandler.type === 0 /* EXPORTED_HANDLER */) {
|
|
844
|
+
return apiHandler.handler.fetch(request, env, ctx);
|
|
804
845
|
} else {
|
|
805
|
-
const handler = new
|
|
846
|
+
const handler = new apiHandler.handler(ctx, env);
|
|
806
847
|
return handler.fetch(request);
|
|
807
848
|
}
|
|
808
849
|
}
|