Skip to main content

Refresh tokens

A Direct, Device, or Mediated Sign-In returns a refresh token: the OAuth refresh_token that keeps the user signed in after the short-lived access token expires (PAP version 0.4 §4.4, §4.5). It is the most valuable credential a personal agent holds for a user, so the SDK never hands it to a caller. It is written straight into the Reactor pap.tokenStore by the sign-in client and used only at the company's token_endpoint and revocation_endpoint.

Refresh​

from personal_agent_protocol import RefreshTokenClient, RefreshTokenContext

accounts = RefreshTokenClient(reactor=reactor, http_client=http)
context = RefreshTokenContext(
token_endpoint=metadata.token_endpoint,
revocation_endpoint=metadata.revocation_endpoint,
issuer=metadata.issuer,
client_id="https://agent.example/agent.json",
tenant_id="application-42",
dpop_signing_algs=metadata.dpop_signing_alg_values_supported,
pairwise_user_id="usr_2d7b4f0c9e8a51", # whose DPoP key proves the refresh
)
result = await accounts.refresh(refresh_token_reference, context=context)
if result.status == "signed_in":
token = result.token # a new access token; the refresh token stays in the store
elif result.status == "re_consent_required":
ask_the_user_to_sign_in_again()
else: # "signed_out"
continue_signed_out()
const accounts = new RefreshTokenClient({ reactor, fetch });
const result = await accounts.refresh(refreshTokenReference, {
context: {
tokenEndpoint,
revocationEndpoint,
issuer,
clientId,
tenantId,
dpopSigningAlgs: metadata.dpop_signing_alg_values_supported,
pairwiseUserId: 'usr_2d7b4f0c9e8a51', // whose DPoP key proves the refresh
},
scopes: ['orders:read'],
});
if (result.status === 'signed_in') {
token = result.token; // a new access token; the refresh token stays in the store
} else if (result.status === 're_consent_required') {
askTheUserToSignInAgain();
} else { // 'signed_out'
continueSignedOut();
}

A refresh is a refresh_token grant with a fresh client assertion. It names no Session: the access token it returns belongs to the account, and a Session is signed in by carrying that token. With dpop_signing_algs (dpopSigningAlgs) from the metadata, a refresh sends a DPoP proof when the issuer lists ES256, by the DPoP key of pairwise_user_id (pairwiseUserId), the User ID the refresh token is for: a DPoP key is used for one User ID only, as a delegated key is (spec 4.5), so proving without one is refused before any request. A DPoP token type with no proof sent is unsupported_token_type. Refresh tokens aren't bound, so a lost DPoP key means refreshing.

Rotation is serialized​

A company may rotate the refresh token on every refresh, and a rotated token invalidates the one it replaces. Two refreshes racing on the same reference would leave one holding a revoked credential. Every use of a reference is therefore held under the Reactor pap.refreshTokenLock contribution (RefreshTokenLocks in TypeScript), and the new token replaces the old in the store before the lock is released. The default InProcessRefreshTokenLock is for one process; a host with several workers contributes a distributed lock.

Narrower, never wider​

scopes and an RFC 8707 resource narrow a refresh. A scope the company returns that was not requested is scope_escalation. Asking for more than the token holds draws the company's invalid_scope, reported as re_consent_required: the user decides on wider access by signing in again, and the SDK does not retry. token_for_mcp (tokenForMcp) is a refresh with resource set to an MCP server's url, taking the server's protected resource metadata: its dpop_signing_alg_values_supported, never the issuer's, says whether a DPoP proof goes.

Expiry and revocation​

  • invalid_grant: the refresh token has expired, been revoked, or been rotated away. The stored copy is deleted and the result is signed_out. The Sessions continue signed out, sending the Session header alone.
  • revoke() sends an RFC 7009 revocation, with a client assertion and token_type_hint=refresh_token. The local copy is deleted only after the company has answered; a refused revocation keeps it so the host can retry, because a token the user believes revoked must be.
  • sign_out() (signOut) revokes every refresh token held for the account, the protocol's sign-out: the account is signed out once none is valid, and its Sessions continue signed out (§5.5). What happens to access tokens already issued follows RFC 7009 §2.1; the reference companies stop them at once.

A conversation endpoint at another provider​

The rules for tokens for one service cover a conversation endpoint a provider runs on another host (spec 4.5): ServiceTokens.token_for(endpoint) (tokenFor) reads the endpoint's RFC 9728 metadata and, when its resource is not the company's domain, requests a token for it from the company's issuer with RFC 8707 resource, and ConversationClient sends that token there. Metadata is used only when its resource is the URL its metadata URL was derived from (RFC 9728 §3.3); otherwise it is treated as absent, and the default token works there (resource_metadata, resourceMetadata, return nothing). The resource identifier is the endpoint URL with its tenant path, so tokens for different companies at the provider have different audiences; the Session JWT's audience and the User IDs stay the company's identifier. The reference companies play this provider with conversation_resource (conversationResource).

Security boundary​

  • The refresh token never reaches a model, a conversation, a URL, a log, or an error: errors carry the company's error code, never a response body.
  • Store tokens encrypted and tenant-isolated; the SDK ships no default pap.tokenStore.
  • A refresh token is tied to the client_id, not to a DPoP key (RFC 9449 §5), so whoever holds it and the agent's client private key can use it. Keep the client private key in a key service or hardware security module that signs without exporting it (contribute it as pap.signer, Signers), and the refresh tokens in a pap.tokenStore apart from it.
  • Sign-out in the product revokes, then deletes.

See the runnable refresh tokens example and the upstream refresh tokens specification.