@cloudflare/workers-oauth-provider 0.0.0-6ab489d

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Cloudflare, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,330 @@
1
+ # OAuth 2.1 Provider Framework for Cloudflare Workers
2
+
3
+ This is a TypeScript library that implements the provider side of the OAuth 2.1 protocol with PKCE support. The library is intended to be used on Cloudflare Workers.
4
+
5
+ ## Benefits of this library
6
+
7
+ * The library acts as a wrapper around your Worker code, which adds authorization for your API endpoints.
8
+ * All token management is handled automatically.
9
+ * Your API handler is written like a regular fetch handler, but receives the already-authenticated user details as a parameter. No need to perform any checks of your own.
10
+ * The library is agnostic to how you manage and authenticate users.
11
+ * The library is agnostic to how you build your UI. Your authorization flow can be implemented using whatever UI framework you use for everything else.
12
+ * The library's storage does not store any secrets, only hashes of them.
13
+
14
+ ## Usage
15
+
16
+ A Worker that uses the library might look like this:
17
+
18
+ ```ts
19
+ import { OAuthProvider } from "@cloudflare/workers-oauth-provider";
20
+ import { WorkerEntrypoint } from "cloudflare:workers";
21
+
22
+ // We export the OAuthProvider instance as the entrypoint to our Worker. This means it
23
+ // implements the `fetch()` handler, receiving all HTTP requests.
24
+ export default new OAuthProvider({
25
+ // Configure API routes. Any requests whose URL starts with any of these prefixes will be
26
+ // considered API requests. The OAuth provider will check the access token on these requests,
27
+ // and then, if the token is valid, send the request to the API handler.
28
+ // You can provide:
29
+ // - A single route (string) or multiple routes (array)
30
+ // - Full URLs (which will match the hostname) or just paths (which will match any hostname)
31
+ apiRoute: [
32
+ "/api/", // Path only - will match any hostname
33
+ "https://api.example.com/" // Full URL - will check hostname
34
+ ],
35
+
36
+ // When the OAuth system receives an API request with a valid access token, it passes the request
37
+ // to this handler object's fetch method.
38
+ // You can provide either an object with a fetch method (ExportedHandler)
39
+ // or a class extending WorkerEntrypoint.
40
+ apiHandler: ApiHandler, // Using a WorkerEntrypoint class
41
+
42
+ // For multi-handler setups, you can use apiHandlers instead of apiRoute+apiHandler.
43
+ // This allows you to use different handlers for different API routes.
44
+ // Note: You must use either apiRoute+apiHandler (single-handler) OR apiHandlers (multi-handler), not both.
45
+ // Example:
46
+ // apiHandlers: {
47
+ // "/api/users/": UsersApiHandler,
48
+ // "/api/documents/": DocumentsApiHandler,
49
+ // "https://api.example.com/": ExternalApiHandler,
50
+ // },
51
+
52
+ // Any requests which aren't API request will be passed to the default handler instead.
53
+ // Again, this can be either an object or a WorkerEntrypoint.
54
+ defaultHandler: defaultHandler, // Using an object with a fetch method
55
+
56
+ // This specifies the URL of the OAuth authorization flow UI. This UI is NOT implemented by
57
+ // the OAuthProvider. It is up to the application to implement a UI here. The only reason why
58
+ // this URL is given to the OAuthProvider is so that it can implement the RFC-8414 metadata
59
+ // discovery endpoint, i.e. `.well-known/oauth-authorization-server`.
60
+ // Can also be specified as just a path (e.g., "/authorize").
61
+ authorizeEndpoint: "https://example.com/authorize",
62
+
63
+ // This specifies the OAuth 2 token exchange endpoint. The OAuthProvider will implement this
64
+ // endpoint (by directly responding to requests with a matching URL).
65
+ // Can also be specified as just a path (e.g., "/oauth/token").
66
+ tokenEndpoint: "https://example.com/oauth/token",
67
+
68
+ // This specifies the RFC-7591 dynamic client registration endpoint. This setting is optional,
69
+ // but if provided, the OAuthProvider will implement this endpoint to allow dynamic client
70
+ // registration.
71
+ // Can also be specified as just a path (e.g., "/oauth/register").
72
+ clientRegistrationEndpoint: "https://example.com/oauth/register",
73
+
74
+ // Optional list of scopes supported by this OAuth provider.
75
+ // If provided, this will be included in the RFC 8414 metadata as 'scopes_supported'.
76
+ // If not provided, the 'scopes_supported' field will be omitted from the metadata.
77
+ scopesSupported: ["document.read", "document.write", "profile"],
78
+
79
+ // Optional: Controls whether the OAuth implicit flow is allowed.
80
+ // The implicit flow is discouraged in OAuth 2.1 but may be needed for some clients.
81
+ // Defaults to false.
82
+ allowImplicitFlow: false,
83
+
84
+ // Optional: Controls whether public clients (clients without a secret, like SPAs)
85
+ // can register via the dynamic client registration endpoint.
86
+ // When true, only confidential clients can register.
87
+ // Note: Creating public clients via the OAuthHelpers.createClient() method
88
+ // is always allowed regardless of this setting.
89
+ // Defaults to false.
90
+ disallowPublicClientRegistration: false
91
+ });
92
+
93
+ // The default handler object - the OAuthProvider will pass through HTTP requests to this object's fetch method
94
+ // if they aren't API requests or do not have a valid access token
95
+ const defaultHandler = {
96
+ // This fetch method works just like a standard Cloudflare Workers fetch handler
97
+ //
98
+ // The `request`, `env`, and `ctx` parameters are the same as for a normal Cloudflare Workers fetch
99
+ // handler, and are exactly the objects that the `OAuthProvider` itself received from the Workers
100
+ // runtime.
101
+ //
102
+ // The `env.OAUTH_PROVIDER` provides an API by which the application can call back to the
103
+ // OAuthProvider.
104
+ async fetch(request: Request, env, ctx) {
105
+ let url = new URL(request.url);
106
+
107
+ if (url.pathname == "/authorize") {
108
+ // This is a request for our OAuth authorization flow UI. It is up to the application to
109
+ // implement this. However, the OAuthProvider library provides some helpers to assist.
110
+
111
+ // `env.OAUTH_PROVIDER.parseAuthRequest()` parses the OAuth authorization request to extract the parameters
112
+ // required by the OAuth 2 standard, namely response_type, client_id, redirect_uri, scope, and
113
+ // state. It returns an object containing all these (using idiomatic camelCase naming).
114
+ let oauthReqInfo = await env.OAUTH_PROVIDER.parseAuthRequest(request);
115
+
116
+ // `env.OAUTH_PROVIDER.lookupClient()` looks up metadata about the client, as definetd by RFC-7591. This
117
+ // includes things like redirect_uris, client_name, logo_uri, etc.
118
+ let clientInfo = await env.OAUTH_PROVIDER.lookupClient(oauthReqInfo.clientId);
119
+
120
+ // At this point, the application should use `oauthReqInfo` and `clientInfo` to render an
121
+ // authorization consent UI to the user. The details of this are up to the app so are not
122
+ // shown here.
123
+
124
+ // After the user has granted consent, the application calls `env.OAUTH_PROVIDER.completeAuthorization()` to
125
+ // grant the authorization.
126
+ let {redirectTo} = await env.OAUTH_PROVIDER.completeAuthorization({
127
+ // The application passes back the original OAuth request info that was returned by
128
+ // `parseAuthRequest()` earlier.
129
+ request: oauthReqInfo,
130
+
131
+ // The application must specify the user's ID, which is some sort of string. This is needed
132
+ // so that the application can later query the OAuthProvider to enumerate all grants
133
+ // belonging to a particular user, e.g. to implement an audit and revocation UI.
134
+ userId: "1234",
135
+
136
+ // The application can specify some arbitary metadata which describes this grant. The
137
+ // metadata can contain any JSON-serializable content. This metadata is not used by the
138
+ // OAuthProvider, but the application can read back the metadata attached to specific
139
+ // grants when enumerating them later, again e.g. to implement an udit and revocation UI.
140
+ metadata: {label: "foo"},
141
+
142
+ // The application specifies the list of OAuth scope identifiers that were granted. This
143
+ // may or may not be the same as was requested in `oauthReqInfo.scope`.
144
+ scope: ["document.read", "document.write"],
145
+
146
+ // `props` is an arbitrary JSON-serializable object which will be passed back to the API
147
+ // handler for every request authorized by this grant.
148
+ props: {
149
+ userId: 1234,
150
+ username: "Bob"
151
+ }
152
+ });
153
+
154
+ // `completeAuthorization()` will have returned the URL to which the user should be redirected
155
+ // in order to complete the authorization flow. This is the requesting client's OAuth
156
+ // redirect_uri with the appropriate query parameters added to complete the flow and obtain
157
+ // tokens.
158
+ return Response.redirect(redirectTo, 302);
159
+ }
160
+
161
+ // ... the application can implement other non-API HTTP endpoints here ...
162
+
163
+ return new Response("Not found", {status: 404});
164
+ }
165
+ };
166
+
167
+ // The API handler object - the OAuthProivder will pass authorized API requests to this object's fetch method
168
+ // (because we provided it as the `apiHandler` setting, above). This is ONLY called for API requests
169
+ // that had a valid access token.
170
+ class ApiHandler extends WorkerEntrypoint {
171
+ // This fetch method works just like any other WorkerEntrypoint fetch method. The `request` is
172
+ // passed as a parameter, while `env` and `ctx` are available as `this.env` and `this.ctx`.
173
+ //
174
+ // The `this.env.OAUTH_PROVIDER` is available just like in the default handler.
175
+ //
176
+ // The `this.ctx.props` property contains the `props` value that was passed to
177
+ // `env.OAUTH_PROVIDER.completeAuthorization()` during the authorization flow that authorized this client.
178
+ fetch(request: Request) {
179
+ // The application can implement its API endpoints like normal. This app implements a single
180
+ // endpoint, `/api/whoami`, which returns the user's authenticated identity.
181
+
182
+ let url = new URL(request.url);
183
+ if (url.pathname == "/api/whoami") {
184
+ // Since the username is embedded in `ctx.props`, which came from the access token that the
185
+ // OAuthProivder already verified, we don't need to do any other authentication steps.
186
+ return new Response(`You are authenticated as: ${this.ctx.props.username}`);
187
+ }
188
+
189
+ return new Response("Not found", {status: 404});
190
+ }
191
+ };
192
+ ```
193
+
194
+ This implementation requires that your worker is configured with a Workers KV namespace binding called `OAUTH_KV`, which is used to store token information. See the file `storage-schema.md` for details on the schema of this namespace.
195
+
196
+ The `env.OAUTH_PROVIDER` object available to the fetch handlers provides some methods to query the storage, including:
197
+
198
+ * Create, list, modify, and delete client_id registrations (in addition to `lookupClient()`, already shown in the example code).
199
+ * List all active authorization grants for a particular user.
200
+ * Revoke (delete) an authorization grant.
201
+
202
+ See the `OAuthHelpers` interface definition for full API details.
203
+
204
+ ## Token Exchange Callback
205
+
206
+ This library allows you to update the `props` value during token exchanges by configuring a callback function. This is useful for scenarios where the application needs to perform additional processing when tokens are issued or refreshed.
207
+
208
+ For example, if your application is also a client to some other OAuth API, you might want to perform an equivalent upstream token exchange and store the result in the `props`. The callback can be used to update the props for both the grant record and specific access tokens.
209
+
210
+ To use this feature, provide a `tokenExchangeCallback` in your OAuthProvider options:
211
+
212
+ ```ts
213
+ new OAuthProvider({
214
+ // ... other options ...
215
+ tokenExchangeCallback: async (options) => {
216
+ // options.grantType is either 'authorization_code' or 'refresh_token'
217
+ // options.props contains the current props
218
+ // options.clientId, options.userId, and options.scope are also available
219
+
220
+ if (options.grantType === 'authorization_code') {
221
+ // For authorization code exchange, might want to obtain upstream tokens
222
+ const upstreamTokens = await exchangeUpstreamToken(options.props.someCode);
223
+
224
+ return {
225
+ // Update the props stored in the access token
226
+ accessTokenProps: {
227
+ ...options.props,
228
+ upstreamAccessToken: upstreamTokens.access_token
229
+ },
230
+ // Update the props stored in the grant (for future token refreshes)
231
+ newProps: {
232
+ ...options.props,
233
+ upstreamRefreshToken: upstreamTokens.refresh_token
234
+ }
235
+ };
236
+ }
237
+
238
+ if (options.grantType === 'refresh_token') {
239
+ // For refresh token exchanges, might want to refresh upstream tokens too
240
+ const upstreamTokens = await refreshUpstreamToken(options.props.upstreamRefreshToken);
241
+
242
+ return {
243
+ accessTokenProps: {
244
+ ...options.props,
245
+ upstreamAccessToken: upstreamTokens.access_token
246
+ },
247
+ newProps: {
248
+ ...options.props,
249
+ upstreamRefreshToken: upstreamTokens.refresh_token || options.props.upstreamRefreshToken
250
+ },
251
+ // Optionally override the default access token TTL to match the upstream token
252
+ accessTokenTTL: upstreamTokens.expires_in
253
+ };
254
+ }
255
+ }
256
+ });
257
+ ```
258
+
259
+ The callback can:
260
+ - Return both `accessTokenProps` and `newProps` to update both
261
+ - Return only `accessTokenProps` to update just the current access token
262
+ - Return only `newProps` to update both the grant and access token (the access token inherits these props)
263
+ - Return `accessTokenTTL` to override the default TTL for this specific access token
264
+ - Return nothing to keep the original props unchanged
265
+
266
+ The `accessTokenTTL` override is particularly useful when the application is also an OAuth client to another service and wants to match its access token TTL to the upstream access token TTL. This helps prevent situations where the downstream token is still valid but the upstream token has expired.
267
+
268
+ The `props` values are end-to-end encrypted, so they can safely contain sensitive information.
269
+
270
+ ## Custom Error Responses
271
+
272
+ By using the `onError` option, you can emit notifications or take other actions when an error response was to be emitted:
273
+
274
+ ```ts
275
+ new OAuthProvider({
276
+ // ... other options ...
277
+ onError({ code, description, status, headers }) {
278
+ Sentry.captureMessage(/* ... */)
279
+ }
280
+ })
281
+ ```
282
+
283
+ By returning a `Response` you can also override what the OAuthProvider returns to your users:
284
+
285
+ ```ts
286
+ new OAuthProvider({
287
+ // ... other options ...
288
+ onError({ code, description, status, headers }) {
289
+ if (code === 'unsupported_grant_type') {
290
+ return new Response('...', { status, headers })
291
+ }
292
+ // returning undefined (i.e. void) uses the default Response generation
293
+ }
294
+ })
295
+ ```
296
+
297
+ By default, the `onError` callback is set to ``({ status, code, description }) => console.warn(`OAuth error response: ${status} ${code} - ${description}`)``.
298
+
299
+ ## Implementation Notes
300
+
301
+ ### End-to-end encryption
302
+
303
+ This library stores records about authorization tokens in KV. The storage schema is carefully designed such that a complete leak of the storage only reveals mundane metadata about what has been granted. In particular:
304
+
305
+ * Secrets (including access tokens, refresh tokens, authorization codes, and client secrets) are stored only by hash. Hence, such secrets cannot be derived from the storage alone.
306
+ * The `props` associated with a grant (which are passed back to the application when API requests are performed) are stored encrypted with the secret token as key material. Hence, the contents of `props` are impossible to derive from storage unless a valid token is provided.
307
+
308
+ Note that the `userId` and the `metadata` associated with each grant are not encrypted, because the purpose of these values is to allow grants to be enumerated for audit and revocation purposes. However, these values are completely opaque to the library. An application is free to omit them or apply its own encryption to them before passing them into the library, if it desires.
309
+
310
+ ### Single-use refresh tokens?
311
+
312
+ OAuth 2.1 requires that refresh tokens are either "cryptographically bound" to the client, or are single-use. This library currently does not implement any cryptographic binding, thus seemingly requiring single-use tokens. Under this requirement, every token refresh request invalidates the old refresh token and issues a new one.
313
+
314
+ This requirement is seemingly fundamentally flawed as it assumes that every refresh request will complete with no errors. In the real world, a transient network error, machine failure, or software fault could mean that the client fails to store the new refresh token after a refresh request. In this case, the client would be permanently unable to make any further requests, as the only token it has is no longer valid.
315
+
316
+ This library implements a compromise: At any particular time, a grant may have two valid refresh tokens. When the client uses one of them, the other one is invalidated, and a new one is generated and returned. Thus, if the client correctly uses the new refresh token each time, then older refresh tokens are continuously invalidated. But if a transient failure prevents the client from updating its token, it can always retry the request with the token it used previously.
317
+
318
+ ## Written using Claude
319
+
320
+ This library (including the schema documentation) was largely written with the help of [Claude](https://claude.ai), the AI model by Anthropic. Claude's output was thoroughly reviewed by Cloudflare engineers with careful attention paid to security and compliance with standards. Many improvements were made on the initial output, mostly again by prompting Claude (and reviewing the results). Check out the commit history to see how Claude was prompted and what code it produced.
321
+
322
+ **"NOOOOOOOO!!!! You can't just use an LLM to write an auth library!"**
323
+
324
+ "haha gpus go brrr"
325
+
326
+ In all seriousness, two months ago (January 2025), I ([@kentonv](https://github.com/kentonv)) would have agreed. I was an AI skeptic. I thought LLMs were glorified Markov chain generators that didn't actually understand code and couldn't produce anything novel. I started this project on a lark, fully expecting the AI to produce terrible code for me to laugh at. And then, uh... the code actually looked pretty good. Not perfect, but I just told the AI to fix things, and it did. I was shocked.
327
+
328
+ To emphasize, **this is not "vibe coded"**. Every line was thoroughly reviewed and cross-referenced with relevant RFCs, by security experts with previous experience with those RFCs. I was *trying* to validate my skepticism. I ended up proving myself wrong.
329
+
330
+ Again, please check out the commit history -- especially early commits -- to understand how this went.