Token VaultDocs
ReferenceWebhook Protocol

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

ItemToken VaultYour webhook
OAuth client idyes (not a secret)yes
OAuth client secretnoyes (reference webhook: OAUTH_PROVIDERS_JSON secret)
Authorization codenoyes
Access and refresh tokensnoyes
State ticket signingyes (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-notify uses:
OAUTH_PROVIDERS_JSON (reference webhook secret)
{ "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 carries prv (the key), tku, clid and scp exactly 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 for prv: {"acme": {"clientId": "...", "clientSecret": "...", "authorizeUrl": "https://...", "tokenUrl": "https://..."}}. With no local entry for prv, answer provider_not_configured. If tku differs from your local token URL, answer token_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 never google, github or spotify.
  • Advertise oauth-callback at /v1/exchange and /v1/health only if you implement both endpoints. The app shows Connect only for a webhook that advertises it.

Sequence

Loading diagram...

State ticket

Signed like every ticket: base64url(json).hex_hmac, HMAC-SHA256 over the base64url payload string.

FieldMeaning
subUser id
svcService name to store the token under
puroauth_connect
prvProvider key, lower case (google)
tkuToken URL Token Vault expects. Cross-check only
clidClient id
rduRedirect URI. Must equal this webhook's own https://<webhook>/v1/oauth/callback
scpSpace-separated scopes, optional
mode, cidOptional, 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, expexp is 10 minutes after iat
nonceOne-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.com

The webhook must:

  1. Verify the ticket (signature, exp, pur, rdu) without consuming the nonce.
  2. Require a client secret for prv in local config.
  3. Build the provider authorize URL itself from its built-in table or a local authorizeUrl: response_type=code, client_id (local clientId if set, else clid; 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 also access_type=offline and prompt=consent.
  4. Set the cookie that binds the flow to this browser.
  5. Answer 302 to the authorize URL.
Response
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=Lax

GET /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:

  1. Verify the ticket.
  2. Require the cookie to equal sha256(nonce). If it does not, fail with browser_mismatch. This stops a victim from being made to finish an attacker's flow.
  3. Consume the nonce, one shot. Remember it for at least the ticket's 10 minutes.
  4. 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.
  5. Store the result as service svc, in the normal token document, with this meta: {tokenType: "oauth", provider, oauth: {provider, tokenUrl, clientId}, hasRefreshToken, expiryTime, scope}.
  6. Clear the cookie and answer 302 to the result URL.
Result redirect
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

codeCause
state_invalidTicket signature or structure is wrong
state_expiredTicket older than 10 minutes
redirect_mismatchrdu is not this webhook's callback URL
browser_mismatchCookie missing or does not match the nonce
already_existsmode: create and the service already exists
access_deniedThe user denied consent at the provider
code_missingNo code on the callback
provider_not_configuredNo client secret for prv in local config
token_url_mismatchResolved token URL differs from the ticket's tku
client_mismatchLocal clientId differs from the ticket's clid
exchange_failedThe provider rejected the code exchange
exchange_unreachableThe provider did not respond
setup_requiredWebhook not configured yet
internal_errorUnexpected 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-cM

Where 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.

On this page