You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,72 @@ The `env.OAUTH_PROVIDER` object available to the fetch handlers provides some me
196
196
197
197
See the `OAuthHelpers` interface definition for full API details.
198
198
199
+
## Token Exchange Callback
200
+
201
+
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.
202
+
203
+
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.
204
+
205
+
To use this feature, provide a `tokenExchangeCallback` in your OAuthProvider options:
206
+
207
+
```ts
208
+
newOAuthProvider({
209
+
// ... other options ...
210
+
tokenExchangeCallback: async (options) => {
211
+
// options.grantType is either 'authorization_code' or 'refresh_token'
212
+
// options.props contains the current props
213
+
// options.clientId, options.userId, and options.scope are also available
214
+
215
+
if (options.grantType==='authorization_code') {
216
+
// For authorization code exchange, might want to obtain upstream tokens
// Optionally override the default access token TTL to match the upstream token
247
+
accessTokenTTL: upstreamTokens.expires_in
248
+
};
249
+
}
250
+
}
251
+
});
252
+
```
253
+
254
+
The callback can:
255
+
- Return both `accessTokenProps` and `newProps` to update both
256
+
- Return only `accessTokenProps` to update just the current access token
257
+
- Return only `newProps` to update both the grant and access token (the access token inherits these props)
258
+
- Return `accessTokenTTL` to override the default TTL for this specific access token
259
+
- Return nothing to keep the original props unchanged
260
+
261
+
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.
262
+
263
+
The `props` values are end-to-end encrypted, so they can safely contain sensitive information.
264
+
199
265
## Written by Claude
200
266
201
267
This library (including the schema documentation) was largely written by [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.
0 commit comments