Skip to main content

Direct Sign-In

Both SDKs implement safe Direct Sign-In primitives: random OAuth state, RFC 7636 S256 PKCE, authorization URL construction, callback state and issuer validation, and a redacted authorization-code exchange grant. Both also implement the complete personal-agent orchestration through DirectSignInClient.

Direct Sign-In is the OAuth 2.0 authorization code flow with PKCE (§4.2). It names no Session: a Session is signed in by carrying the access token it gives. A code taken from the redirect is useless without the PKCE verifier and the agent's client assertion.

Host services remain mandatory

The orchestrator resolves pap.authorizationStateStore, pap.browserHandoff, and pap.signer from Reactor. The core installs no in-memory fallback. The state store must provide atomic take; the browser handoff must open the URL only for the user; the signer must keep private keys in protected custody.

Python​

import httpx

from personal_agent_protocol import DirectSignInClient

async with httpx.AsyncClient() as http:
sign_in = DirectSignInClient(reactor=reactor, http_client=http)
started = await sign_in.begin(
authorization_endpoint="https://auth.example.com/oauth/authorize",
token_endpoint="https://auth.example.com/oauth/token",
issuer="https://auth.example.com",
client_id="https://agent.example/agent.json",
redirect_uri="https://agent.example/oauth/callback",
scopes=("orders:read",), # the company's own scopes, from `auth.scopes`
tenant_id="application-42",
# A DPoP proof goes with the code exchange when the issuer's metadata lists ES256,
# by the DPoP key of the User ID the sign-in is for (one per User ID, spec 4.5):
# dpop_signing_algs=metadata.dpop_signing_alg_values_supported,
# pairwise_user_id=session.sub,
)

# Later, at the registered callback route. Never pass this URL to a model.
result = await sign_in.complete(incoming_callback_url)
print(result.granted_scopes, result.missing_scopes)
# The refresh token is already in `pap.tokenStore`; only its reference is here.
refresh_reference = result.refresh_token_reference

begin() stores the redacted pending request under a SHA-256 reference to its state and then invokes the trusted browser handoff. complete() atomically consumes that state before validating the exact redirect and issuer, so a callback cannot be replayed. Expired, unknown, and already-used states produce the same safe error class.

The exchange signs a fresh client assertion for every attempt and follows no redirects. It names no Session: the access token belongs to the account, and a Session is signed in by carrying it. Extra scopes are rejected; omitted scopes are returned in missing_scopes so the application can continue with reduced authority or ask the user what to do. If the response includes a refresh token, complete() moves it into the Reactor pap.tokenStore contribution behind an opaque refresh_… reference before returning (§4.4): the result's token never carries it, and refresh_token_reference names it. Refresh, narrowing, and revocation take that reference (RefreshTokenClient), exactly as after Device and Mediated Sign-In. Denial consumes state and exposes no remote description; recovery starts a new flow.

A sign-in someone else started (§4.2, §10.8)​

Nothing in the redirect says who approved the sign-in: someone can start one, send the link to another person, and get tokens for that person's account if they approve. The protocol accepts that risk, as OAuth does, and limits it. What a host does:

  • Receive the redirect only on the device that opened the authorization URL when you can: a loopback redirect_uri, or your own app signed in as the user who started. A forwarded link then fails.
  • When you can tell which of your users is in the browser at redirect_uri, say so: complete(callback_url, completed_by=tenant_id) (complete({ callbackUrl, completedBy })) refuses a redirect that arrived for another user than the one who started, sign_in_started_by_another_user, with the state used up and no code exchanged.
  • When the link opens on another device than the one the user is on, such as a link sent to a phone, pass cross_device=True (crossDevice) to begin: it is then a cross-device flow, as Device Sign-In always is, and its request lives at most 300 seconds (CROSS_DEVICE_LIFETIME_SECONDS). Ask only for the scopes the task needs, and say who the link is for where you show it.

The company's page does its part: it names the personal agent that wants to connect and tells the person to continue only if they just asked it to (see the company guide).

Every user of a personal agent shares its client_id, so a company that skipped its consent page because the account had granted every scope before would let a forwarded link get tokens with no page at all. Since 1.0 a Direct Sign-In request carries prompt=consent (PROMPT_CONSENT): the company shows its page whatever the account granted. begin adds it; pass same_device_redirect=True (sameDeviceRedirect) only when you receive the redirect on the device that opened the URL, where a forwarded link fails anyway.

Pushed Authorization Requests (§4.2, RFC 9126)​

When the issuer's metadata lists pushed_authorization_request_endpoint, pass it to begin with require_pushed_authorization_requests, both read from the OAuthServerMetadata (TypeScript pushedAuthorizationRequestEndpoint, requirePushedAuthorizationRequests). The request is then pushed with a client assertion, and a DPoP proof when tokens are bound, so the code is bound to the key, and the browser opens the authorization endpoint with only client_id and request_uri. A company that requires it takes nothing else; the protocol asks agents to push whenever it is offered. A request_uri expires quickly, so a link the user opens later, such as on their phone, goes to a page of your own: pass link_page="https://agent.example/sign-in/{link}" (linkPage) and the browser handoff gets that page; when it is opened, it calls open_link(link) (openLink), which pushes the request then, and redirects to the URL it returns. Opening a link reads its record without consuming it, through the state store's get; give your store one (a store with only put and take is read by taking and putting back, which a completing callback can race). A link may be opened again until its sign-in completes; then it opens nothing.

With PAR or a link page, the URL the browser opens carries no state. When your redirect URI is served by another service than the one that called begin, key the callback there by started.state (DirectSignInStart.state, a secret), the value the company sends back with the code.

A sign-in names no resource: it covers every service the company offers, those it adds later too, and each token request names one (§4.5, see Refresh tokens).

TypeScript​

import {
DirectSignInClient,
} from '@datalayer/personal-agent-protocol';

const signIn = new DirectSignInClient({ reactor, fetch });
const started = await signIn.begin({
authorizationEndpoint: 'https://auth.example.com/oauth/authorize',
tokenEndpoint: 'https://auth.example.com/oauth/token',
issuer: 'https://auth.example.com',
clientId: 'https://agent.example/agent.json',
redirectUri: 'https://agent.example/oauth/callback',
scopes: ['orders:read'],
tenantId: 'application-42',
// A DPoP proof goes with the code exchange when the issuer's metadata lists ES256,
// by the DPoP key of the User ID the sign-in is for (one per User ID, spec 4.5):
// dpopSigningAlgs: metadata.dpop_signing_alg_values_supported,
// pairwiseUserId: session.sub,
});

// Later, at the registered callback route. Never put this URL in UI/model state.
const result = await signIn.complete({
callbackUrl: incomingCallbackUrl,
signal: request.signal,
});
console.log(started.expiresAt, result.grantedScopes, result.missingScopes);
// The refresh token is already in `pap.tokenStore`; only its reference is here.
const refreshReference = result.refreshTokenReference;

The TypeScript flow has the same one-use, redirect, issuer, and scope guarantees as Python and propagates an optional AbortSignal to the exchange. Use it in a trusted backend or desktop host whose Reactor contributions keep assertion signing and authorization state out of browser code. A web Preview should request the flow through that host and receive only safe status.

The request URL, state, verifier, authorization code, client assertion, access token, and refresh token are redacted by default. Reveal them only at the browser handoff or final HTTPS serialization boundary. They must not enter model context, logs, analytics, A2UI state, or callback error text.

Redirect URIs​

The redirect URI must exactly match an entry of the agent's redirect_uris (§4.1): an HTTPS URL on the client_id's domain, or a loopback redirect URI (http://127.0.0.1:{port}/… or http://[::1]:{port}/…) for an app on the user's computer, as RFC 8252 §7.3 describes. A loopback redirect matches whatever its port (redirect_uri_matches, is_loopback_redirect).

Follow the lifecycle​

The lifecycle has two asynchronous halves separated by user action:

StageHost responsibilitySafe application-visible state
1. BeginCreate random state and S256 PKCE; persist the pending request with an expirywaiting_for_user, requested scopes, expiry
2. HandoffOpen the authorization URL in a user-owned browserBrowser opened
3. CallbackAtomically consume state; verify the exact redirect and issuercallback_accepted or a safe error code
4. ExchangeRedeem the code with a fresh client assertion and the PKCE verifierGranted and missing scopes
5. ContinueStore credentials behind opaque references and resume the same conversationsigned_in, the same sid

Stage 3 consumes the state, so a second callback cannot start another exchange.

Two paired runnable examples exercise these invariants against a deterministic local company endpoint without contacting an identity provider:

Both examples intentionally return booleans and scope outcomes rather than request payloads. Their mock transport is pedagogical; production deployments must use protected key and token stores, durable expiring authorization state, and an HTTPS callback owned by the host.

See upstream Direct Sign-In for the complete normative flow.