OAuth Callback (Own OAuth App)
The oauth-callback capability, GET /v1/oauth/start and GET /v1/oauth/callback, with the state ticket, derived PKCE, the browser-binding cookie and the result codes.
The oauth-callback capability (spec 2.7.0) lets a user connect Google, GitHub, Spotify or a custom provider with their own OAuth app. The provider redirects to your webhook, so the authorization code and the tokens never reach Token Vault. For the user-side setup, see OAuth apps. For the Token Vault API, see OAuth apps API.
Who holds what
| Item | Token Vault | Your webhook |
|---|---|---|
| OAuth client id | yes (not a secret) | yes |
| OAuth client secret | no | yes (reference webhook: OAUTH_PROVIDERS_JSON secret) |
| Authorization code | no | yes |
| Access and refresh tokens | no | yes |
| State ticket signing | yes (signs it) | verifies it |
Token Vault makes no outbound call to the provider in this flow.
Setup the webhook needs
- Register the app at the provider with this redirect URI:
https://<webhook>/v1/oauth/callback. - Put the client secret in the webhook's provider config, the same entry
/v1/refresh-notifyuses:
{ "google": { "clientId": "...apps.googleusercontent.com", "clientSecret": "..." } }- Built-in providers:
google(https://oauth2.googleapis.com/token),github(https://github.com/login/oauth/access_token),spotify(https://accounts.spotify.com/api/token). Beyond those three, a user can define a custom provider in Token Vault (spec 2.8.0): a key, an authorize URL, a token URL, scopes and a client id, all non-secret. The state ticket carriesprv(the key),tku,clidandscpexactly as for a built-in. The authorize URL is not in the ticket. Your webhook builds the authorize URL, and picks the token URL and the client secret, from its own config entry forprv:{"acme": {"clientId": "...", "clientSecret": "...", "authorizeUrl": "https://...", "tokenUrl": "https://..."}}. With no local entry forprv, answerprovider_not_configured. Iftkudiffers from your local token URL, answertoken_url_mismatch. Never dial a URL from the ticket. A provider key is 2 to 32 lower-case letters, digits,-or_, starting with a letter, and is nevergoogle,githuborspotify. - Advertise
oauth-callbackat/v1/exchangeand/v1/healthonly if you implement both endpoints. The app shows Connect only for a webhook that advertises it.
Sequence
State ticket
Signed like every ticket: base64url(json).hex_hmac, HMAC-SHA256 over the base64url payload string.
| Field | Meaning |
|---|---|
sub | User id |
svc | Service name to store the token under |
pur | oauth_connect |
prv | Provider key, lower case (google) |
tku | Token URL Token Vault expects. Cross-check only |
clid | Client id |
rdu | Redirect URI. Must equal this webhook's own https://<webhook>/v1/oauth/callback |
scp | Space-separated scopes, optional |
mode, cid | Optional, only to a ticketConstraints webhook. Applied exactly as /v1/store applies them: create over an existing service is refused with already_exists before the code is exchanged, and meta.creationId comes from cid |
iat, exp | exp is 10 minutes after iat |
nonce | One-shot id |
GET /v1/oauth/start
Called by the browser. No HMAC. The same URL-denylist rules apply as for the rest of your webhook.
GET /v1/oauth/start?state=<ticket> HTTP/1.1
Host: webhook.example.comThe webhook must:
- Verify the ticket (signature,
exp,pur,rdu) without consuming the nonce. - Require a client secret for
prvin local config. - Build the provider authorize URL itself from its built-in table or a local
authorizeUrl:response_type=code,client_id(localclientIdif set, elseclid; if both are set they must match),redirect_uri=rdu,scope=scp,state=<the ticket as received>,code_challenge,code_challenge_method=S256. For Google alsoaccess_type=offlineandprompt=consent. - Set the cookie that binds the flow to this browser.
- Answer
302to the authorize URL.
HTTP/1.1 302 Found
Location: https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=...&redirect_uri=https%3A%2F%2Fwebhook.example.com%2Fv1%2Foauth%2Fcallback&state=<ticket>&code_challenge=fO8Zordy...&code_challenge_method=S256
Set-Cookie: tv_oauth_state=<hex sha256(nonce)>; Path=/v1/oauth; Max-Age=600; HttpOnly; Secure; SameSite=LaxGET /v1/oauth/callback
Called by the browser after the provider redirects. On denial the provider sends ?error=... instead of code.
GET /v1/oauth/callback?code=<code>&state=<ticket> HTTP/1.1
Host: webhook.example.com
Cookie: tv_oauth_state=<hex sha256(nonce)>The webhook must:
- Verify the ticket.
- Require the cookie to equal
sha256(nonce). If it does not, fail withbrowser_mismatch. This stops a victim from being made to finish an attacker's flow. - Consume the nonce, one shot. Remember it for at least the ticket's 10 minutes.
- Exchange the code at the token URL with the local client secret:
grant_type=authorization_code,code,redirect_uri,client_id,client_secret,code_verifier. - Store the result as service
svc, in the normal token document, with this meta:{tokenType: "oauth", provider, oauth: {provider, tokenUrl, clientId}, hasRefreshToken, expiryTime, scope}. - Clear the cookie and answer
302to the result URL.
HTTP/1.1 302 Found
Location: https://tokenvault.uk/tokens/oauth-result?status=ok&service=google-drive&provider=google<frontend> is the Token Vault frontend origin your webhook is configured with. The path /tokens/oauth-result is what deployed webhooks emit; the app forwards it to /credentials/oauth-return. On failure status=error and code=<error code> is added. Never put the code, a token or provider error text in the redirect.
Result codes
code | Cause |
|---|---|
state_invalid | Ticket signature or structure is wrong |
state_expired | Ticket older than 10 minutes |
redirect_mismatch | rdu is not this webhook's callback URL |
browser_mismatch | Cookie missing or does not match the nonce |
already_exists | mode: create and the service already exists |
access_denied | The user denied consent at the provider |
code_missing | No code on the callback |
provider_not_configured | No client secret for prv in local config |
token_url_mismatch | Resolved token URL differs from the ticket's tku |
client_mismatch | Local clientId differs from the ticket's clid |
exchange_failed | The provider rejected the code exchange |
exchange_unreachable | The provider did not respond |
setup_required | Webhook not configured yet |
internal_error | Unexpected error |
This flow does not use the HTTP error table. Failures are always a 302 carrying code=.
Derived PKCE
The verifier is derived, never sent through the browser, so the callback can recompute it from the nonce alone. Only your webhook can derive it (spec 2.12.0).
verifier = base64url_nopad( HMAC-SHA256(webhookKey, "tv-oauth-pkce-v2:" + nonce) ) (43 chars)
challenge = base64url_nopad( SHA-256( ascii(verifier) ) )webhookKey is key material that Token Vault never learns. The reference webhook uses its encryption key. It must not be the HMAC secret: Token Vault holds that secret and signs the nonce, so it could compute the verifier.
Token Vault never computes or checks the verifier. Any derivation that is stable for a nonce and private to your webhook conforms, so there is no shared test vector (the one from spec 2.8.0 is withdrawn). The challenge is plain RFC 7636 S256:
verifier = dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
challenge = E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cMWhere the client secret may go
Only to a token URL from the webhook's own config or its built-in table. A token URL named by the state ticket (tku), by a stored hint or by a /v1/refresh-notify refreshHint is a cross-check: if it differs from the resolved URL, refuse. The same rule applies to /v1/refresh-notify.
Refresh
The stored meta.oauth hint ({provider, tokenUrl, clientId}) lets /v1/refresh-notify refresh the token under any service name. Keep it when you rewrite the document after a refresh.
Ready to try it?
Sign in with Google or email. Your credentials stay on your own webhook, and the quickstart gets Claude Code making its first MCP tool call in about 15 minutes.
Webhook Endpoint Reference
Request and response schemas for every /v1 endpoint of the webhook protocol, with sequence diagrams for bind, store, credential fetch and proxy.
Error Codes
The error responses your webhook returns, how Token Vault maps them for callers, and its retry, timeout and circuit-breaker policy.