@cloudflare/workers-oauth-provider 0.1.0 → 0.2.2

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.
@@ -1,4 +1,6 @@
1
- import { WorkerEntrypoint } from 'cloudflare:workers';
1
+ import { WorkerEntrypoint } from "cloudflare:workers";
2
+
3
+ //#region src/oauth-provider.d.ts
2
4
 
3
5
  /**
4
6
  * Aliases for either type of Handler that makes .fetch required
@@ -13,594 +15,628 @@ type WorkerEntrypointWithFetch = WorkerEntrypoint & Pick<Required<WorkerEntrypoi
13
15
  * Allows updating the props stored in both the access token and the grant.
14
16
  */
15
17
  interface TokenExchangeCallbackResult {
16
- /**
17
- * New props to be stored specifically with the access token.
18
- * If not provided but newProps is, the access token will use newProps.
19
- * If neither is provided, the original props will be used.
20
- */
21
- accessTokenProps?: any;
22
- /**
23
- * New props to replace the props stored in the grant itself.
24
- * These props will be used for all future token refreshes.
25
- * If accessTokenProps is not provided, these props will also be used for the current access token.
26
- * If not provided, the original props will be used.
27
- */
28
- newProps?: any;
29
- /**
30
- * Override the default access token TTL (time-to-live) for this specific token.
31
- * This is especially useful when the application is also an OAuth client to another service
32
- * and wants to match its access token TTL to the upstream access token TTL.
33
- * Value should be in seconds.
34
- */
35
- accessTokenTTL?: number;
36
- /**
37
- * Override the default refresh token TTL (time-to-live) for this specific grant.
38
- * Value should be in seconds.
39
- * Note: This is only honored during authorization code exchange. If returned during
40
- * refresh token exchange, it will be ignored.
41
- */
42
- refreshTokenTTL?: number;
18
+ /**
19
+ * New props to be stored specifically with the access token.
20
+ * If not provided but newProps is, the access token will use newProps.
21
+ * If neither is provided, the original props will be used.
22
+ */
23
+ accessTokenProps?: any;
24
+ /**
25
+ * New props to replace the props stored in the grant itself.
26
+ * These props will be used for all future token refreshes.
27
+ * If accessTokenProps is not provided, these props will also be used for the current access token.
28
+ * If not provided, the original props will be used.
29
+ */
30
+ newProps?: any;
31
+ /**
32
+ * Override the default access token TTL (time-to-live) for this specific token.
33
+ * This is especially useful when the application is also an OAuth client to another service
34
+ * and wants to match its access token TTL to the upstream access token TTL.
35
+ * Value should be in seconds.
36
+ */
37
+ accessTokenTTL?: number;
38
+ /**
39
+ * Override the default refresh token TTL (time-to-live) for this specific grant.
40
+ * Value should be in seconds.
41
+ * Note: This is only honored during authorization code exchange. If returned during
42
+ * refresh token exchange, it will be ignored.
43
+ */
44
+ refreshTokenTTL?: number;
43
45
  }
44
46
  /**
45
47
  * Options for token exchange callback functions
46
48
  */
47
49
  interface TokenExchangeCallbackOptions {
48
- /**
49
- * The type of grant being processed.
50
- * 'authorization_code' for initial code exchange,
51
- * 'refresh_token' for refresh token exchange.
52
- */
53
- grantType: 'authorization_code' | 'refresh_token';
54
- /**
55
- * Client that received this grant
56
- */
57
- clientId: string;
58
- /**
59
- * User who authorized this grant
60
- */
61
- userId: string;
62
- /**
63
- * List of scopes that were granted
64
- */
65
- scope: string[];
66
- /**
67
- * Application-specific properties currently associated with this grant
68
- */
69
- props: any;
50
+ /**
51
+ * The type of grant being processed.
52
+ * 'authorization_code' for initial code exchange,
53
+ * 'refresh_token' for refresh token exchange.
54
+ */
55
+ grantType: 'authorization_code' | 'refresh_token';
56
+ /**
57
+ * Client that received this grant
58
+ */
59
+ clientId: string;
60
+ /**
61
+ * User who authorized this grant
62
+ */
63
+ userId: string;
64
+ /**
65
+ * List of scopes that were granted
66
+ */
67
+ scope: string[];
68
+ /**
69
+ * Application-specific properties currently associated with this grant
70
+ */
71
+ props: any;
70
72
  }
71
73
  /**
72
74
  * Input parameters for the resolveExternalToken callback function
73
75
  */
74
76
  interface ResolveExternalTokenInput {
75
- /**
76
- * The token string that was provided in the Authorization header
77
- */
78
- token: string;
79
- /**
80
- * The original HTTP request
81
- */
82
- request: Request;
83
- /**
84
- * Cloudflare Worker environment variables
85
- */
86
- env: any;
77
+ /**
78
+ * The token string that was provided in the Authorization header
79
+ */
80
+ token: string;
81
+ /**
82
+ * The original HTTP request
83
+ */
84
+ request: Request;
85
+ /**
86
+ * Cloudflare Worker environment variables
87
+ */
88
+ env: any;
87
89
  }
88
90
  /**
89
91
  * Result returned from the resolveExternalToken callback function
90
92
  */
91
93
  interface ResolveExternalTokenResult {
92
- /**
93
- * Application-specific properties that will be passed to the API handlers
94
- * These properties are set in the execution context (ctx.props) when the external token is validated
95
- */
96
- props: any;
97
- /**
98
- * Audience claim from the external token (RFC 7519 Section 4.1.3)
99
- * If provided, will be validated against the resource server identity
100
- *
101
- */
102
- audience?: string | string[];
94
+ /**
95
+ * Application-specific properties that will be passed to the API handlers
96
+ * These properties are set in the execution context (ctx.props) when the external token is validated
97
+ */
98
+ props: any;
99
+ /**
100
+ * Audience claim from the external token (RFC 7519 Section 4.1.3)
101
+ * If provided, will be validated against the resource server identity
102
+ *
103
+ */
104
+ audience?: string | string[];
103
105
  }
104
106
  interface OAuthProviderOptions {
105
- /**
106
- * URL(s) for API routes. Requests with URLs starting with any of these prefixes
107
- * will be treated as API requests and require a valid access token.
108
- * Can be a single route or an array of routes. Each route can be a full URL or just a path.
109
- *
110
- * Used with `apiHandler` for the single-handler configuration. This is incompatible with
111
- * the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
112
- */
113
- apiRoute?: string | string[];
114
- /**
115
- * Handler for API requests that have a valid access token.
116
- * This handler will receive the authenticated user properties in ctx.props.
117
- * Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
118
- *
119
- * Used with `apiRoute` for the single-handler configuration. This is incompatible with
120
- * the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
121
- */
122
- apiHandler?: ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch);
123
- /**
124
- * Map of API routes to their corresponding handlers for the multi-handler configuration.
125
- * The keys are the API routes (strings only, not arrays), and the values are the handlers.
126
- * Each route can be a full URL or just a path, and each handler can be either an ExportedHandler
127
- * object with a fetch method or a class extending WorkerEntrypoint.
128
- *
129
- * This is incompatible with the `apiRoute` and `apiHandler` properties. You must use either
130
- * `apiRoute` + `apiHandler` (single-handler configuration) OR `apiHandlers` (multi-handler
131
- * configuration), not both.
132
- */
133
- apiHandlers?: Record<string, ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch)>;
134
- /**
135
- * Handler for all non-API requests or API requests without a valid token.
136
- * Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
137
- */
138
- defaultHandler: ExportedHandler | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch);
139
- /**
140
- * URL of the OAuth authorization endpoint where users can grant permissions.
141
- * This URL is used in OAuth metadata and is not handled by the provider itself.
142
- */
143
- authorizeEndpoint: string;
144
- /**
145
- * URL of the token endpoint which the provider will implement.
146
- * This endpoint handles token issuance, refresh, and revocation.
147
- */
148
- tokenEndpoint: string;
149
- /**
150
- * Optional URL for the client registration endpoint.
151
- * If provided, the provider will implement dynamic client registration.
152
- */
153
- clientRegistrationEndpoint?: string;
154
- /**
155
- * Time-to-live for access tokens in seconds.
156
- * Defaults to 1 hour (3600 seconds) if not specified.
157
- */
158
- accessTokenTTL?: number;
159
- /**
160
- * Time-to-live for refresh tokens in seconds.
161
- * If not specified, refresh tokens do not expire.
162
- * For example: 3600 = 1 hour, 2592000 = 30 days
163
- */
164
- refreshTokenTTL?: number;
165
- /**
166
- * List of scopes supported by this OAuth provider.
167
- * If not provided, the 'scopes_supported' field will be omitted from the OAuth metadata.
168
- */
169
- scopesSupported?: string[];
170
- /**
171
- * Controls whether the OAuth implicit flow is allowed.
172
- * This flow is discouraged in OAuth 2.1 due to security concerns.
173
- * Defaults to false.
174
- */
175
- allowImplicitFlow?: boolean;
176
- /**
177
- * Controls whether public clients (clients without a secret, like SPAs) can register via the
178
- * dynamic client registration endpoint. When true, only confidential clients can register.
179
- * Note: Creating public clients via the OAuthHelpers.createClient() method is always allowed.
180
- * Defaults to false.
181
- */
182
- disallowPublicClientRegistration?: boolean;
183
- /**
184
- * Optional callback function that is called during token exchange.
185
- * This allows updating the props stored in both the access token and the grant.
186
- * For example, if the application itself is also a client to some other OAuth API,
187
- * it may want to perform the equivalent upstream token exchange, and store the result in the props.
188
- *
189
- * The callback can return new props values that will be stored with the token or grant.
190
- * If the callback returns nothing or undefined for a props field, the original props will be used.
191
- */
192
- tokenExchangeCallback?: (options: TokenExchangeCallbackOptions) => Promise<TokenExchangeCallbackResult | void> | TokenExchangeCallbackResult | void;
193
- /**
194
- * Optional callback function that is called when a provided token was not found in the internal KV.
195
- * This allows authentication through external OAuth servers.
196
- * For example, if a request includes an authenticated token from a different OAuth authentication server,
197
- * the callback can be used to authenticate it and set the context props through it.
198
- *
199
- * The callback can optionally return props values that will passed-through to the apiHandlers.
200
- * The callback can return `null` to signal resolution failure.
201
- */
202
- resolveExternalToken?: (input: ResolveExternalTokenInput) => Promise<ResolveExternalTokenResult | null>;
203
- /**
204
- * Optional callback function that is called whenever the OAuthProvider returns an error response
205
- * This allows the client to emit notifications or perform other actions when an error occurs.
206
- *
207
- * If the function returns a Response, that will be used in place of the OAuthProvider's default one.
208
- */
209
- onError?: (error: {
210
- code: string;
211
- description: string;
212
- status: number;
213
- headers: Record<string, string>;
214
- }) => Response | void;
107
+ /**
108
+ * URL(s) for API routes. Requests with URLs starting with any of these prefixes
109
+ * will be treated as API requests and require a valid access token.
110
+ * Can be a single route or an array of routes. Each route can be a full URL or just a path.
111
+ *
112
+ * Used with `apiHandler` for the single-handler configuration. This is incompatible with
113
+ * the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
114
+ */
115
+ apiRoute?: string | string[];
116
+ /**
117
+ * Handler for API requests that have a valid access token.
118
+ * This handler will receive the authenticated user properties in ctx.props.
119
+ * Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
120
+ *
121
+ * Used with `apiRoute` for the single-handler configuration. This is incompatible with
122
+ * the `apiHandlers` property. You must use either `apiRoute` + `apiHandler` OR `apiHandlers`, not both.
123
+ */
124
+ apiHandler?: ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch);
125
+ /**
126
+ * Map of API routes to their corresponding handlers for the multi-handler configuration.
127
+ * The keys are the API routes (strings only, not arrays), and the values are the handlers.
128
+ * Each route can be a full URL or just a path, and each handler can be either an ExportedHandler
129
+ * object with a fetch method or a class extending WorkerEntrypoint.
130
+ *
131
+ * This is incompatible with the `apiRoute` and `apiHandler` properties. You must use either
132
+ * `apiRoute` + `apiHandler` (single-handler configuration) OR `apiHandlers` (multi-handler
133
+ * configuration), not both.
134
+ */
135
+ apiHandlers?: Record<string, ExportedHandlerWithFetch | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch)>;
136
+ /**
137
+ * Handler for all non-API requests or API requests without a valid token.
138
+ * Can be either an ExportedHandler object with a fetch method or a class extending WorkerEntrypoint.
139
+ */
140
+ defaultHandler: ExportedHandler | (new (ctx: ExecutionContext, env: any) => WorkerEntrypointWithFetch);
141
+ /**
142
+ * URL of the OAuth authorization endpoint where users can grant permissions.
143
+ * This URL is used in OAuth metadata and is not handled by the provider itself.
144
+ */
145
+ authorizeEndpoint: string;
146
+ /**
147
+ * URL of the token endpoint which the provider will implement.
148
+ * This endpoint handles token issuance, refresh, and revocation.
149
+ */
150
+ tokenEndpoint: string;
151
+ /**
152
+ * Optional URL for the client registration endpoint.
153
+ * If provided, the provider will implement dynamic client registration.
154
+ */
155
+ clientRegistrationEndpoint?: string;
156
+ /**
157
+ * Time-to-live for access tokens in seconds.
158
+ * Defaults to 1 hour (3600 seconds) if not specified.
159
+ */
160
+ accessTokenTTL?: number;
161
+ /**
162
+ * Time-to-live for refresh tokens in seconds.
163
+ * If not specified, refresh tokens do not expire.
164
+ * For example: 3600 = 1 hour, 2592000 = 30 days
165
+ */
166
+ refreshTokenTTL?: number;
167
+ /**
168
+ * List of scopes supported by this OAuth provider.
169
+ * If not provided, the 'scopes_supported' field will be omitted from the OAuth metadata.
170
+ */
171
+ scopesSupported?: string[];
172
+ /**
173
+ * Controls whether the OAuth implicit flow is allowed.
174
+ * This flow is discouraged in OAuth 2.1 due to security concerns.
175
+ * Defaults to false.
176
+ */
177
+ allowImplicitFlow?: boolean;
178
+ /**
179
+ * Controls whether public clients (clients without a secret, like SPAs) can register via the
180
+ * dynamic client registration endpoint. When true, only confidential clients can register.
181
+ * Note: Creating public clients via the OAuthHelpers.createClient() method is always allowed.
182
+ * Defaults to false.
183
+ */
184
+ disallowPublicClientRegistration?: boolean;
185
+ /**
186
+ * Optional callback function that is called during token exchange.
187
+ * This allows updating the props stored in both the access token and the grant.
188
+ * For example, if the application itself is also a client to some other OAuth API,
189
+ * it may want to perform the equivalent upstream token exchange, and store the result in the props.
190
+ *
191
+ * The callback can return new props values that will be stored with the token or grant.
192
+ * If the callback returns nothing or undefined for a props field, the original props will be used.
193
+ */
194
+ tokenExchangeCallback?: (options: TokenExchangeCallbackOptions) => Promise<TokenExchangeCallbackResult | void> | TokenExchangeCallbackResult | void;
195
+ /**
196
+ * Optional callback function that is called when a provided token was not found in the internal KV.
197
+ * This allows authentication through external OAuth servers.
198
+ * For example, if a request includes an authenticated token from a different OAuth authentication server,
199
+ * the callback can be used to authenticate it and set the context props through it.
200
+ *
201
+ * The callback can optionally return props values that will passed-through to the apiHandlers.
202
+ * The callback can return `null` to signal resolution failure.
203
+ */
204
+ resolveExternalToken?: (input: ResolveExternalTokenInput) => Promise<ResolveExternalTokenResult | null>;
205
+ /**
206
+ * Optional callback function that is called whenever the OAuthProvider returns an error response
207
+ * This allows the client to emit notifications or perform other actions when an error occurs.
208
+ *
209
+ * If the function returns a Response, that will be used in place of the OAuthProvider's default one.
210
+ */
211
+ onError?: (error: {
212
+ code: string;
213
+ description: string;
214
+ status: number;
215
+ headers: Record<string, string>;
216
+ }) => Response | void;
215
217
  }
216
218
  /**
217
219
  * Helper methods for OAuth operations provided to handler functions
218
220
  */
219
221
  interface OAuthHelpers {
220
- /**
221
- * Parses an OAuth authorization request from the HTTP request
222
- * @param request - The HTTP request containing OAuth parameters
223
- * @returns The parsed authorization request parameters
224
- */
225
- parseAuthRequest(request: Request): Promise<AuthRequest>;
226
- /**
227
- * Looks up a client by its client ID
228
- * @param clientId - The client ID to look up
229
- * @returns A Promise resolving to the client info, or null if not found
230
- */
231
- lookupClient(clientId: string): Promise<ClientInfo | null>;
232
- /**
233
- * Completes an authorization request by creating a grant and authorization code
234
- * @param options - Options specifying the grant details
235
- * @returns A Promise resolving to an object containing the redirect URL
236
- */
237
- completeAuthorization(options: CompleteAuthorizationOptions): Promise<{
238
- redirectTo: string;
239
- }>;
240
- /**
241
- * Creates a new OAuth client
242
- * @param clientInfo - Partial client information to create the client with
243
- * @returns A Promise resolving to the created client info
244
- */
245
- createClient(clientInfo: Partial<ClientInfo>): Promise<ClientInfo>;
246
- /**
247
- * Lists all registered OAuth clients with pagination support
248
- * @param options - Optional pagination parameters (limit and cursor)
249
- * @returns A Promise resolving to the list result with items and optional cursor
250
- */
251
- listClients(options?: ListOptions): Promise<ListResult<ClientInfo>>;
252
- /**
253
- * Updates an existing OAuth client
254
- * @param clientId - The ID of the client to update
255
- * @param updates - Partial client information with fields to update
256
- * @returns A Promise resolving to the updated client info, or null if not found
257
- */
258
- updateClient(clientId: string, updates: Partial<ClientInfo>): Promise<ClientInfo | null>;
259
- /**
260
- * Deletes an OAuth client
261
- * @param clientId - The ID of the client to delete
262
- * @returns A Promise resolving when the deletion is confirmed.
263
- */
264
- deleteClient(clientId: string): Promise<void>;
265
- /**
266
- * Lists all authorization grants for a specific user with pagination support
267
- * Returns a summary of each grant without sensitive information
268
- * @param userId - The ID of the user whose grants to list
269
- * @param options - Optional pagination parameters (limit and cursor)
270
- * @returns A Promise resolving to the list result with grant summaries and optional cursor
271
- */
272
- listUserGrants(userId: string, options?: ListOptions): Promise<ListResult<GrantSummary>>;
273
- /**
274
- * Revokes an authorization grant
275
- * @param grantId - The ID of the grant to revoke
276
- * @param userId - The ID of the user who owns the grant
277
- * @returns A Promise resolving when the revocation is confirmed.
278
- */
279
- revokeGrant(grantId: string, userId: string): Promise<void>;
222
+ /**
223
+ * Parses an OAuth authorization request from the HTTP request
224
+ * @param request - The HTTP request containing OAuth parameters
225
+ * @returns The parsed authorization request parameters
226
+ */
227
+ parseAuthRequest(request: Request): Promise<AuthRequest>;
228
+ /**
229
+ * Looks up a client by its client ID
230
+ * @param clientId - The client ID to look up
231
+ * @returns A Promise resolving to the client info, or null if not found
232
+ */
233
+ lookupClient(clientId: string): Promise<ClientInfo | null>;
234
+ /**
235
+ * Completes an authorization request by creating a grant and authorization code
236
+ * @param options - Options specifying the grant details
237
+ * @returns A Promise resolving to an object containing the redirect URL
238
+ */
239
+ completeAuthorization(options: CompleteAuthorizationOptions): Promise<{
240
+ redirectTo: string;
241
+ }>;
242
+ /**
243
+ * Creates a new OAuth client
244
+ * @param clientInfo - Partial client information to create the client with
245
+ * @returns A Promise resolving to the created client info
246
+ */
247
+ createClient(clientInfo: Partial<ClientInfo>): Promise<ClientInfo>;
248
+ /**
249
+ * Lists all registered OAuth clients with pagination support
250
+ * @param options - Optional pagination parameters (limit and cursor)
251
+ * @returns A Promise resolving to the list result with items and optional cursor
252
+ */
253
+ listClients(options?: ListOptions): Promise<ListResult<ClientInfo>>;
254
+ /**
255
+ * Updates an existing OAuth client
256
+ * @param clientId - The ID of the client to update
257
+ * @param updates - Partial client information with fields to update
258
+ * @returns A Promise resolving to the updated client info, or null if not found
259
+ */
260
+ updateClient(clientId: string, updates: Partial<ClientInfo>): Promise<ClientInfo | null>;
261
+ /**
262
+ * Deletes an OAuth client
263
+ * @param clientId - The ID of the client to delete
264
+ * @returns A Promise resolving when the deletion is confirmed.
265
+ */
266
+ deleteClient(clientId: string): Promise<void>;
267
+ /**
268
+ * Lists all authorization grants for a specific user with pagination support
269
+ * Returns a summary of each grant without sensitive information
270
+ * @param userId - The ID of the user whose grants to list
271
+ * @param options - Optional pagination parameters (limit and cursor)
272
+ * @returns A Promise resolving to the list result with grant summaries and optional cursor
273
+ */
274
+ listUserGrants(userId: string, options?: ListOptions): Promise<ListResult<GrantSummary>>;
275
+ /**
276
+ * Revokes an authorization grant
277
+ * @param grantId - The ID of the grant to revoke
278
+ * @param userId - The ID of the user who owns the grant
279
+ * @returns A Promise resolving when the revocation is confirmed.
280
+ */
281
+ revokeGrant(grantId: string, userId: string): Promise<void>;
282
+ /**
283
+ * Decodes a token and returns token data with decrypted props
284
+ * @param token - The token
285
+ * @returns Promise resolving to token data with decrypted props, or null if token is invalid
286
+ */
287
+ unwrapToken<T = any>(token: string): Promise<TokenSummary<T> | null>;
280
288
  }
281
289
  /**
282
290
  * Parsed OAuth authorization request parameters
283
291
  */
284
292
  interface AuthRequest {
285
- /**
286
- * OAuth response type (e.g., "code" for authorization code flow)
287
- */
288
- responseType: string;
289
- /**
290
- * Client identifier for the OAuth client
291
- */
292
- clientId: string;
293
- /**
294
- * URL to redirect to after authorization
295
- */
296
- redirectUri: string;
297
- /**
298
- * Array of requested permission scopes
299
- */
300
- scope: string[];
301
- /**
302
- * Client state value to be returned in the redirect
303
- */
304
- state: string;
305
- /**
306
- * PKCE code challenge (RFC 7636)
307
- */
308
- codeChallenge?: string;
309
- /**
310
- * PKCE code challenge method (plain or S256)
311
- */
312
- codeChallengeMethod?: string;
313
- /**
314
- * Resource parameter indicating target resource(s) (RFC 8707)
315
- */
316
- resource?: string | string[];
293
+ /**
294
+ * OAuth response type (e.g., "code" for authorization code flow)
295
+ */
296
+ responseType: string;
297
+ /**
298
+ * Client identifier for the OAuth client
299
+ */
300
+ clientId: string;
301
+ /**
302
+ * URL to redirect to after authorization
303
+ */
304
+ redirectUri: string;
305
+ /**
306
+ * Array of requested permission scopes
307
+ */
308
+ scope: string[];
309
+ /**
310
+ * Client state value to be returned in the redirect
311
+ */
312
+ state: string;
313
+ /**
314
+ * PKCE code challenge (RFC 7636)
315
+ */
316
+ codeChallenge?: string;
317
+ /**
318
+ * PKCE code challenge method (plain or S256)
319
+ */
320
+ codeChallengeMethod?: string;
321
+ /**
322
+ * Resource parameter indicating target resource(s) (RFC 8707)
323
+ */
324
+ resource?: string | string[];
317
325
  }
318
326
  /**
319
327
  * OAuth client registration information
320
328
  */
321
329
  interface ClientInfo {
322
- /**
323
- * Unique identifier for the client
324
- */
325
- clientId: string;
326
- /**
327
- * Secret used to authenticate the client (stored as a hash)
328
- * Only present for confidential clients; undefined for public clients.
329
- */
330
- clientSecret?: string;
331
- /**
332
- * List of allowed redirect URIs for the client
333
- */
334
- redirectUris: string[];
335
- /**
336
- * Human-readable name of the client application
337
- */
338
- clientName?: string;
339
- /**
340
- * URL to the client's logo
341
- */
342
- logoUri?: string;
343
- /**
344
- * URL to the client's homepage
345
- */
346
- clientUri?: string;
347
- /**
348
- * URL to the client's privacy policy
349
- */
350
- policyUri?: string;
351
- /**
352
- * URL to the client's terms of service
353
- */
354
- tosUri?: string;
355
- /**
356
- * URL to the client's JSON Web Key Set for validating signatures
357
- */
358
- jwksUri?: string;
359
- /**
360
- * List of email addresses for contacting the client developers
361
- */
362
- contacts?: string[];
363
- /**
364
- * List of grant types the client supports
365
- */
366
- grantTypes?: string[];
367
- /**
368
- * List of response types the client supports
369
- */
370
- responseTypes?: string[];
371
- /**
372
- * Unix timestamp when the client was registered
373
- */
374
- registrationDate?: number;
375
- /**
376
- * The authentication method used by the client at the token endpoint.
377
- * Values include:
378
- * - 'client_secret_basic': Uses HTTP Basic Auth with client ID and secret (default for confidential clients)
379
- * - 'client_secret_post': Uses POST parameters for client authentication
380
- * - 'none': Used for public clients that can't securely store secrets (SPAs, mobile apps, etc.)
381
- *
382
- * Public clients use 'none', while confidential clients use either 'client_secret_basic' or 'client_secret_post'.
383
- */
384
- tokenEndpointAuthMethod: string;
330
+ /**
331
+ * Unique identifier for the client
332
+ */
333
+ clientId: string;
334
+ /**
335
+ * Secret used to authenticate the client (stored as a hash)
336
+ * Only present for confidential clients; undefined for public clients.
337
+ */
338
+ clientSecret?: string;
339
+ /**
340
+ * List of allowed redirect URIs for the client
341
+ */
342
+ redirectUris: string[];
343
+ /**
344
+ * Human-readable name of the client application
345
+ */
346
+ clientName?: string;
347
+ /**
348
+ * URL to the client's logo
349
+ */
350
+ logoUri?: string;
351
+ /**
352
+ * URL to the client's homepage
353
+ */
354
+ clientUri?: string;
355
+ /**
356
+ * URL to the client's privacy policy
357
+ */
358
+ policyUri?: string;
359
+ /**
360
+ * URL to the client's terms of service
361
+ */
362
+ tosUri?: string;
363
+ /**
364
+ * URL to the client's JSON Web Key Set for validating signatures
365
+ */
366
+ jwksUri?: string;
367
+ /**
368
+ * List of email addresses for contacting the client developers
369
+ */
370
+ contacts?: string[];
371
+ /**
372
+ * List of grant types the client supports
373
+ */
374
+ grantTypes?: string[];
375
+ /**
376
+ * List of response types the client supports
377
+ */
378
+ responseTypes?: string[];
379
+ /**
380
+ * Unix timestamp when the client was registered
381
+ */
382
+ registrationDate?: number;
383
+ /**
384
+ * The authentication method used by the client at the token endpoint.
385
+ * Values include:
386
+ * - 'client_secret_basic': Uses HTTP Basic Auth with client ID and secret (default for confidential clients)
387
+ * - 'client_secret_post': Uses POST parameters for client authentication
388
+ * - 'none': Used for public clients that can't securely store secrets (SPAs, mobile apps, etc.)
389
+ *
390
+ * Public clients use 'none', while confidential clients use either 'client_secret_basic' or 'client_secret_post'.
391
+ */
392
+ tokenEndpointAuthMethod: string;
385
393
  }
386
394
  /**
387
395
  * Options for completing an authorization request
388
396
  */
389
397
  interface CompleteAuthorizationOptions {
390
- /**
391
- * The original parsed authorization request
392
- */
393
- request: AuthRequest;
394
- /**
395
- * Identifier for the user granting the authorization
396
- */
397
- userId: string;
398
- /**
399
- * Application-specific metadata to associate with this grant
400
- */
401
- metadata: any;
402
- /**
403
- * List of scopes that were actually granted (may differ from requested scopes)
404
- */
405
- scope: string[];
406
- /**
407
- * Application-specific properties to include with API requests
408
- * authorized by this grant
409
- */
410
- props: any;
398
+ /**
399
+ * The original parsed authorization request
400
+ */
401
+ request: AuthRequest;
402
+ /**
403
+ * Identifier for the user granting the authorization
404
+ */
405
+ userId: string;
406
+ /**
407
+ * Application-specific metadata to associate with this grant
408
+ */
409
+ metadata: any;
410
+ /**
411
+ * List of scopes that were actually granted (may differ from requested scopes)
412
+ */
413
+ scope: string[];
414
+ /**
415
+ * Application-specific properties to include with API requests
416
+ * authorized by this grant
417
+ */
418
+ props: any;
411
419
  }
412
420
  /**
413
421
  * Authorization grant record
414
422
  */
415
423
  interface Grant {
416
- /**
417
- * Unique identifier for the grant
418
- */
419
- id: string;
424
+ /**
425
+ * Unique identifier for the grant
426
+ */
427
+ id: string;
428
+ /**
429
+ * Client that received this grant
430
+ */
431
+ clientId: string;
432
+ /**
433
+ * User who authorized this grant
434
+ */
435
+ userId: string;
436
+ /**
437
+ * List of scopes that were granted
438
+ */
439
+ scope: string[];
440
+ /**
441
+ * Application-specific metadata associated with this grant
442
+ */
443
+ metadata: any;
444
+ /**
445
+ * Encrypted application-specific properties
446
+ */
447
+ encryptedProps: string;
448
+ /**
449
+ * Unix timestamp when the grant was created
450
+ */
451
+ createdAt: number;
452
+ /**
453
+ * Unix timestamp when the grant expires (if TTL is configured)
454
+ */
455
+ expiresAt?: number;
456
+ /**
457
+ * The hash of the current refresh token associated with this grant
458
+ */
459
+ refreshTokenId?: string;
460
+ /**
461
+ * Wrapped encryption key for the current refresh token
462
+ */
463
+ refreshTokenWrappedKey?: string;
464
+ /**
465
+ * The hash of the previous refresh token associated with this grant
466
+ * This token is still valid until the new token is first used
467
+ */
468
+ previousRefreshTokenId?: string;
469
+ /**
470
+ * Wrapped encryption key for the previous refresh token
471
+ */
472
+ previousRefreshTokenWrappedKey?: string;
473
+ /**
474
+ * The hash of the authorization code associated with this grant
475
+ * Only present during the authorization code exchange process
476
+ */
477
+ authCodeId?: string;
478
+ /**
479
+ * Wrapped encryption key for the authorization code
480
+ * Only present during the authorization code exchange process
481
+ */
482
+ authCodeWrappedKey?: string;
483
+ /**
484
+ * PKCE code challenge for this authorization
485
+ * Only present during the authorization code exchange process
486
+ */
487
+ codeChallenge?: string;
488
+ /**
489
+ * PKCE code challenge method (plain or S256)
490
+ * Only present during the authorization code exchange process
491
+ */
492
+ codeChallengeMethod?: string;
493
+ /**
494
+ * Resource parameter from authorization request (RFC 8707 Section 2.1)
495
+ * Indicates the protected resource(s) for which access is requested
496
+ */
497
+ resource?: string | string[];
498
+ }
499
+ /**
500
+ * Shared fields for Token and TokenSummary
501
+ */
502
+ interface TokenBase {
503
+ /**
504
+ * Unique identifier for the token (hash of the actual token)
505
+ */
506
+ id: string;
507
+ /**
508
+ * Identifier of the grant this token is associated with
509
+ */
510
+ grantId: string;
511
+ /**
512
+ * User ID associated with this token
513
+ */
514
+ userId: string;
515
+ /**
516
+ * Unix timestamp when the token was created
517
+ */
518
+ createdAt: number;
519
+ /**
520
+ * Unix timestamp when the token expires
521
+ */
522
+ expiresAt: number;
523
+ /**
524
+ * Intended audience for this token (RFC 7519 Section 4.1.3)
525
+ * Can be a single string or array of strings
526
+ */
527
+ audience?: string | string[];
528
+ }
529
+ /**
530
+ * Token record stored in KV
531
+ * Note: The actual token format is "{userId}:{grantId}:{random-secret}"
532
+ * but we still only store the hash of the full token string.
533
+ * This contains only access tokens; refresh tokens are stored within the grant records.
534
+ */
535
+ interface Token extends TokenBase {
536
+ /**
537
+ * The encryption key for props, wrapped with this token
538
+ */
539
+ wrappedEncryptionKey: string;
540
+ /**
541
+ * Denormalized grant information for faster access
542
+ */
543
+ grant: {
420
544
  /**
421
545
  * Client that received this grant
422
546
  */
423
547
  clientId: string;
424
- /**
425
- * User who authorized this grant
426
- */
427
- userId: string;
428
548
  /**
429
549
  * List of scopes that were granted
430
550
  */
431
551
  scope: string[];
432
- /**
433
- * Application-specific metadata associated with this grant
434
- */
435
- metadata: any;
436
552
  /**
437
553
  * Encrypted application-specific properties
438
554
  */
439
555
  encryptedProps: string;
440
- /**
441
- * Unix timestamp when the grant was created
442
- */
443
- createdAt: number;
444
- /**
445
- * Unix timestamp when the grant expires (if TTL is configured)
446
- */
447
- expiresAt?: number;
448
- /**
449
- * The hash of the current refresh token associated with this grant
450
- */
451
- refreshTokenId?: string;
452
- /**
453
- * Wrapped encryption key for the current refresh token
454
- */
455
- refreshTokenWrappedKey?: string;
456
- /**
457
- * The hash of the previous refresh token associated with this grant
458
- * This token is still valid until the new token is first used
459
- */
460
- previousRefreshTokenId?: string;
461
- /**
462
- * Wrapped encryption key for the previous refresh token
463
- */
464
- previousRefreshTokenWrappedKey?: string;
465
- /**
466
- * The hash of the authorization code associated with this grant
467
- * Only present during the authorization code exchange process
468
- */
469
- authCodeId?: string;
470
- /**
471
- * Wrapped encryption key for the authorization code
472
- * Only present during the authorization code exchange process
473
- */
474
- authCodeWrappedKey?: string;
475
- /**
476
- * PKCE code challenge for this authorization
477
- * Only present during the authorization code exchange process
478
- */
479
- codeChallenge?: string;
480
- /**
481
- * PKCE code challenge method (plain or S256)
482
- * Only present during the authorization code exchange process
483
- */
484
- codeChallengeMethod?: string;
485
- /**
486
- * Resource parameter from authorization request (RFC 8707 Section 2.1)
487
- * Indicates the protected resource(s) for which access is requested
488
- */
489
- resource?: string | string[];
556
+ };
490
557
  }
491
558
  /**
492
- * Token record stored in KV
493
- * Note: The actual token format is "{userId}:{grantId}:{random-secret}"
494
- * but we still only store the hash of the full token string.
495
- * This contains only access tokens; refresh tokens are stored within the grant records.
559
+ * Token record with decrypted properties
560
+ * Derived from Token but with wrappedEncryptionKey removed and encryptedProps replaced with props
496
561
  */
497
- interface Token {
498
- /**
499
- * Unique identifier for the token (hash of the actual token)
500
- */
501
- id: string;
502
- /**
503
- * Identifier of the grant this token is associated with
504
- */
505
- grantId: string;
506
- /**
507
- * User ID associated with this token
508
- */
509
- userId: string;
510
- /**
511
- * Unix timestamp when the token was created
512
- */
513
- createdAt: number;
514
- /**
515
- * Unix timestamp when the token expires
516
- */
517
- expiresAt: number;
562
+ interface TokenSummary<T = any> extends TokenBase {
563
+ /**
564
+ * Denormalized grant information for faster access
565
+ */
566
+ grant: {
518
567
  /**
519
- * Intended audience for this token (RFC 7519 Section 4.1.3)
520
- * Can be a single string or array of strings
568
+ * Client that received this grant
521
569
  */
522
- audience?: string | string[];
570
+ clientId: string;
523
571
  /**
524
- * The encryption key for props, wrapped with this token
572
+ * List of scopes that were granted
525
573
  */
526
- wrappedEncryptionKey: string;
574
+ scope: string[];
527
575
  /**
528
- * Denormalized grant information for faster access
576
+ * Decrypted application-specific properties
529
577
  */
530
- grant: {
531
- /**
532
- * Client that received this grant
533
- */
534
- clientId: string;
535
- /**
536
- * List of scopes that were granted
537
- */
538
- scope: string[];
539
- /**
540
- * Encrypted application-specific properties
541
- */
542
- encryptedProps: string;
543
- };
578
+ props: T;
579
+ };
544
580
  }
545
581
  /**
546
582
  * Options for listing operations that support pagination
547
583
  */
548
584
  interface ListOptions {
549
- /**
550
- * Maximum number of items to return (max 1000)
551
- */
552
- limit?: number;
553
- /**
554
- * Cursor for pagination (from a previous listing operation)
555
- */
556
- cursor?: string;
585
+ /**
586
+ * Maximum number of items to return (max 1000)
587
+ */
588
+ limit?: number;
589
+ /**
590
+ * Cursor for pagination (from a previous listing operation)
591
+ */
592
+ cursor?: string;
557
593
  }
558
594
  /**
559
595
  * Result of a listing operation with pagination support
560
596
  */
561
597
  interface ListResult<T> {
562
- /**
563
- * The list of items
564
- */
565
- items: T[];
566
- /**
567
- * Cursor to get the next page of results, if there are more results
568
- */
569
- cursor?: string;
598
+ /**
599
+ * The list of items
600
+ */
601
+ items: T[];
602
+ /**
603
+ * Cursor to get the next page of results, if there are more results
604
+ */
605
+ cursor?: string;
570
606
  }
571
607
  /**
572
608
  * Public representation of a grant, with sensitive data removed
573
609
  * Used for list operations where the complete grant data isn't needed
574
610
  */
575
611
  interface GrantSummary {
576
- /**
577
- * Unique identifier for the grant
578
- */
579
- id: string;
580
- /**
581
- * Client that received this grant
582
- */
583
- clientId: string;
584
- /**
585
- * User who authorized this grant
586
- */
587
- userId: string;
588
- /**
589
- * List of scopes that were granted
590
- */
591
- scope: string[];
592
- /**
593
- * Application-specific metadata associated with this grant
594
- */
595
- metadata: any;
596
- /**
597
- * Unix timestamp when the grant was created
598
- */
599
- createdAt: number;
600
- /**
601
- * Unix timestamp when the grant expires (if TTL is configured)
602
- */
603
- expiresAt?: number;
612
+ /**
613
+ * Unique identifier for the grant
614
+ */
615
+ id: string;
616
+ /**
617
+ * Client that received this grant
618
+ */
619
+ clientId: string;
620
+ /**
621
+ * User who authorized this grant
622
+ */
623
+ userId: string;
624
+ /**
625
+ * List of scopes that were granted
626
+ */
627
+ scope: string[];
628
+ /**
629
+ * Application-specific metadata associated with this grant
630
+ */
631
+ metadata: any;
632
+ /**
633
+ * Unix timestamp when the grant was created
634
+ */
635
+ createdAt: number;
636
+ /**
637
+ * Unix timestamp when the grant expires (if TTL is configured)
638
+ */
639
+ expiresAt?: number;
604
640
  }
605
641
  /**
606
642
  * OAuth 2.0 Provider implementation for Cloudflare Workers
@@ -608,21 +644,21 @@ interface GrantSummary {
608
644
  * and dynamic client registration.
609
645
  */
610
646
  declare class OAuthProvider {
611
- #private;
612
- /**
613
- * Creates a new OAuth provider instance
614
- * @param options - Configuration options for the provider
615
- */
616
- constructor(options: OAuthProviderOptions);
617
- /**
618
- * Main fetch handler for the Worker
619
- * Routes requests to the appropriate handler based on the URL
620
- * @param request - The HTTP request
621
- * @param env - Cloudflare Worker environment variables
622
- * @param ctx - Cloudflare Worker execution context
623
- * @returns A Promise resolving to an HTTP Response
624
- */
625
- fetch(request: Request, env: any, ctx: ExecutionContext): Promise<Response>;
647
+ #private;
648
+ /**
649
+ * Creates a new OAuth provider instance
650
+ * @param options - Configuration options for the provider
651
+ */
652
+ constructor(options: OAuthProviderOptions);
653
+ /**
654
+ * Main fetch handler for the Worker
655
+ * Routes requests to the appropriate handler based on the URL
656
+ * @param request - The HTTP request
657
+ * @param env - Cloudflare Worker environment variables
658
+ * @param ctx - Cloudflare Worker execution context
659
+ * @returns A Promise resolving to an HTTP Response
660
+ */
661
+ fetch(request: Request, env: any, ctx: ExecutionContext): Promise<Response>;
626
662
  }
627
-
628
- export { type AuthRequest, type ClientInfo, type CompleteAuthorizationOptions, type Grant, type GrantSummary, type ListOptions, type ListResult, type OAuthHelpers, OAuthProvider, type OAuthProviderOptions, type ResolveExternalTokenInput, type ResolveExternalTokenResult, type Token, type TokenExchangeCallbackOptions, type TokenExchangeCallbackResult, OAuthProvider as default };
663
+ //#endregion
664
+ export { AuthRequest, ClientInfo, CompleteAuthorizationOptions, Grant, GrantSummary, ListOptions, ListResult, OAuthHelpers, OAuthProvider, OAuthProvider as default, OAuthProviderOptions, ResolveExternalTokenInput, ResolveExternalTokenResult, Token, TokenBase, TokenExchangeCallbackOptions, TokenExchangeCallbackResult, TokenSummary };