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
Ask Claude to implement a token exchange callback.
This lets the app specify a callback to invoke during token exchanges which can update the `props`. This is particularly useful when the application is itself is a client to some other OAuth API, and needs to perform upstream token refreshes. These can be tied to the downstream refreshes, which maintains the end-to-end encryption. Otherwise, the upstream tokens would need to be stored in some separate storage which may not be e2e.
Full transcript: https://claude-workerd-transcript.pages.dev/oauth-provider-token-exchange-callback
Copy file name to clipboardExpand all lines: README.md
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,66 @@ 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
- Return both `tokenProps` and `grantProps` to update both
254
+
- Return only `tokenProps` or `grantProps` to update just one
255
+
- Return nothing to keep the original props unchanged
256
+
257
+
The `props` values are end-to-end encrypted, so they can safely contain sensitive information.
258
+
199
259
## Written by Claude
200
260
201
261
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