(feat): resume OAuth2 authorize requests through PAR handles across login

When an unauthenticated user lands on the consent screen, push the authorization request server-side (RFC 9126 PAR) and round-trip only a short request_uri handle through login, instead of the full consent URL. The full URL — scope, 2KB state, PKCE and authorization_details — previously travelled inside the GitHub OAuth state during 'Sign in with GitHub' and could exceed the provider's state size limits, breaking MCP client logins.

- createPAR goes through an anonymous no-credentials client: the server serves /par (like /token) with wildcard CORS and credentials disabled, which the browser rejects for the SDK's default credentialed fetch.
- The post-login request_uri path requires a session before dereferencing (the handle is single-use and consumed even by failing authorize calls) and sends only clientId + requestUri, as the server rejects any other param alongside the handle.
- After authorize returns a grant, the URL is rewritten to ?grant_id= with replaceState so reloads resume via getGrant instead of failing on the consumed handle.
- The consent effect is keyed on the query string via $derived: login calls invalidate(ACCOUNT) right after goto(redirect), which replaces the page.url object without changing the query — an effect tracking page.url directly re-runs, cancels the in-flight authorize and re-dereferences the consumed handle.
- Expired/consumed handles render a friendly retry message; PAR failures (e.g. servers without the endpoint) fall back to the legacy full-URL redirect.
- resource params are forwarded with getAll (RFC 8707 repeats the key).

SDK pinned to the preview build of appwrite-labs/cloud#4660 (@4b3ae78); re-pin after the cloud PR merges. Models.Locale was renamed to Models.CloudLocale in that spec, updated the seven usages.
This commit is contained in:
Chirag Aggarwal committed 2026-07-08 11:50:36 +05:30
1 parent 8ddb379169
commit cfaf3e7491
10 files changed
+153 -41

No files matched your search

+2 -2
View File
@@ -6,7 +6,7 @@
"name": "@appwrite/console",
"dependencies": {
"@ai-sdk/svelte": "^1.1.24",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@0fcb4f2",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@4b3ae78",
"@appwrite.io/pink-icons": "0.25.0",
"@appwrite.io/pink-icons-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-icons-svelte@bfe7ce3",
"@appwrite.io/pink-legacy": "^1.0.3",
@@ -126,7 +126,7 @@
"@analytics/type-utils": ["@analytics/type-utils@0.6.4", "", {}, "sha512-Ou1gQxFakOWLcPnbFVsrPb8g1wLLUZYYJXDPjHkG07+5mustGs5yqACx42UAu4A6NszNN6Z5gGxhyH45zPWRxw=="],
"@appwrite.io/console": ["@appwrite.io/console@https://pkg.vc/-/@appwrite/@appwrite.io/console@0fcb4f2", { "dependencies": { "json-bigint": "1.0.0" } }],
"@appwrite.io/console": ["@appwrite.io/console@https://pkg.vc/-/@appwrite/@appwrite.io/console@4b3ae78", { "dependencies": { "json-bigint": "1.0.0" } }],
"@appwrite.io/pink-icons": ["@appwrite.io/pink-icons@0.25.0", "", {}, "sha512-0O3i2oEuh5mWvjO80i+X6rbzrWLJ1m5wmv2/M3a1p2PyBJsFxN8xQMTEmTn3Wl/D26SsM7SpzbdW6gmfgoVU9Q=="],
+1 -1
View File
@@ -20,7 +20,7 @@
},
"dependencies": {
"@ai-sdk/svelte": "^1.1.24",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@0fcb4f2",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@4b3ae78",
"@appwrite.io/pink-icons": "0.25.0",
"@appwrite.io/pink-icons-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-icons-svelte@bfe7ce3",
"@appwrite.io/pink-legacy": "^1.0.3",
@@ -10,7 +10,7 @@
import type { Models } from '@appwrite.io/console';
export let show = false;
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let organization: string = null;
export let countryList: Models.CountryList;
@@ -29,7 +29,7 @@
export let data: PageData;
const locale: Models.Locale = data.locale;
const locale: Models.CloudLocale = data.locale;
const countryList: Models.CountryList = data.countryList;
let show = false;
@@ -10,7 +10,7 @@
import type { Models } from '@appwrite.io/console';
export let show = false;
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let countryList: Models.CountryList;
export let selectedAddress: Models.BillingAddress;
@@ -22,7 +22,7 @@
} from '@appwrite.io/pink-icons-svelte';
import type { Models } from '@appwrite.io/console';
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let countryList: Models.CountryList;
export let organization: Models.Organization;
export let billingAddress: Models.BillingAddress;
@@ -13,7 +13,7 @@
import type { Models } from '@appwrite.io/console';
export let show = false;
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let countryList: Models.CountryList;
let loading = true;
@@ -5,7 +5,7 @@
import type { Models } from '@appwrite.io/console';
let show = false;
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let countryList: Models.CountryList;
</script>
@@ -11,7 +11,7 @@
import type { Models } from '@appwrite.io/console';
export let show = false;
export let locale: Models.Locale;
export let locale: Models.CloudLocale;
export let countryList: Models.CountryList;
let email = '';
+143 -31
View File
@@ -2,7 +2,7 @@
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/state';
import { AppwriteException, type Models } from '@appwrite.io/console';
import { AppwriteException, Client, Oauth2, type Models } from '@appwrite.io/console';
import { Card, Layout, Typography, Icon, Spinner } from '@appwrite.io/pink-svelte';
import { IconExclamation } from '@appwrite.io/pink-icons-svelte';
import { Button } from '$lib/elements/forms';
@@ -33,15 +33,34 @@
phase = outcome === 'approved' ? 'approved' : 'denied';
}
// PAR is a public client endpoint (like /token): the server serves it with
// wildcard CORS and credentials disabled, so the browser rejects the
// console SDK's default credentialed fetch. Call it through an anonymous
// client that omits cookies — the endpoint needs no session anyway.
function anonymousOAuth2(): Oauth2 {
const client = new Client()
.setEndpoint(sdk.forConsole.client.config.endpoint)
.setProject('console')
.setCredentials('omit');
return new Oauth2(client);
}
// The ENTIRE query string, so the flow restarts whenever any part of the
// authorize request changes — not just grant_id/client_id, but also
// redirect_uri, scope, state, nonce, PKCE fields, prompt, max_age,
// authorization_details and request_uri. A $derived string (rather than
// reading page.url in the effect) so identity-only replacements of the
// URL object — e.g. login calling invalidate(ACCOUNT) right after goto —
// do NOT re-run the flow: a rerun would cancel an in-flight authorize
// and, on the PAR path, re-dereference an already-consumed single-use
// request_uri handle.
const authorizeQuery = $derived(page.url.searchParams.toString());
// Re-runs when the authorize params change (this route can stay mounted as
// the router moves between requests). Reset to loading so a previously
// loaded grant can never be approved against a different request.
$effect(() => {
// Depend on the ENTIRE query string so the effect re-runs whenever any
// part of the authorize request changes — not just grant_id/client_id,
// but also redirect_uri, scope, state, nonce, PKCE fields, prompt,
// max_age and authorization_details.
page.url.searchParams.toString();
const query = authorizeQuery;
let cancelled = false;
phase = 'loading';
@@ -50,8 +69,9 @@
const currentRelativeUrl = () => window.location.pathname + window.location.search;
const goSignIn = () => {
void goto(`${base}/login?redirect=${encodeURIComponent(currentRelativeUrl())}`, {
const goSignIn = (resumeUrl?: string) => {
const target = resumeUrl ?? currentRelativeUrl();
void goto(`${base}/login?redirect=${encodeURIComponent(target)}`, {
replaceState: true
});
};
@@ -78,7 +98,7 @@
}
async function init(): Promise<void> {
const params = page.url.searchParams;
const params = new URLSearchParams(query);
const grantId = params.get('grant_id');
if (grantId) {
@@ -98,8 +118,86 @@
return;
}
// No grant yet: this is the pre-login entry with raw authorize params.
// Shared handling for a JSON authorize response, whether the request
// arrived as raw params or as a pushed request_uri handle.
async function handleAuthorizeResult(
result: Models.Oauth2Authorize,
loggedInAccount: Models.User<Models.Preferences>,
clientId: string | null,
fromRequestUri: boolean
): Promise<void> {
if (result.redirectUrl) {
// Already consented — go straight back to the client.
window.location.href = result.redirectUrl;
if (!isWebRedirect(result.redirectUrl)) {
completedRedirectUrl = result.redirectUrl;
account = loggedInAccount;
app = clientId
? await sdk.forConsole.apps.get({ appId: clientId }).catch(() => null)
: null;
if (cancelled) return;
phase = 'approved';
}
return;
}
if (result.grantId) {
if (fromRequestUri) {
// The pushed request handle is single-use and now consumed.
// Rewrite the URL to the grant so a reload or back/forward
// navigation resumes via getGrant instead of failing on a
// dead request_uri. The effect re-runs and loads the grant.
await goto(`${base}/oauth2/consent?grant_id=${result.grantId}`, {
replaceState: true
});
return;
}
await loadConsent(result.grantId, loggedInAccount);
return;
}
error = 'Could not start authorization.';
phase = 'error';
}
const clientId = params.get('client_id');
const requestUri = params.get('request_uri');
// Pushed authorization request (PAR) resume: we round-tripped a short
// request_uri handle through login instead of the full authorize URL.
if (requestUri) {
const loggedInAccount = (await sdk.forConsole.account
.get()
.catch(() => null)) as Models.User<Models.Preferences> | null;
if (cancelled) return;
if (!loggedInAccount) {
// The handle is single-use: never dereference it while logged
// out — the server consumes it before rejecting the call.
goSignIn();
return;
}
try {
const result = await sdk.forConsole.oauth2.authorize({
// request_uri must not be combined with any other
// authorization param; client_id is the only one the
// server accepts alongside it (and must match).
clientId: clientId ?? undefined,
requestUri
});
if (cancelled) return;
await handleAuthorizeResult(result, loggedInAccount, clientId, true);
} catch (e: unknown) {
if (cancelled) return;
const message = (e as Error)?.message ?? '';
error = message.toLowerCase().includes('request_uri')
? 'This sign-in request has expired. Return to the application and try connecting again.'
: message || 'Could not start authorization.';
phase = 'error';
}
return;
}
// No grant yet: this is the pre-login entry with raw authorize params.
if (clientId) {
const loggedInAccount = (await sdk.forConsole.account
.get()
@@ -107,7 +205,40 @@
if (cancelled) return;
if (!loggedInAccount) {
goSignIn();
// Push the authorization request server-side and carry only a
// short request_uri through login. The full consent URL rides
// inside the OAuth provider's `state` during GitHub sign-in
// and can exceed the provider's size limits.
const resources = params.getAll('resource');
try {
const par = await anonymousOAuth2().createPAR({
clientId,
redirectUri: params.get('redirect_uri') ?? '',
responseType: params.get('response_type') ?? 'code',
scope: params.get('scope') ?? '',
state: params.get('state') ?? undefined,
nonce: params.get('nonce') ?? undefined,
codeChallenge: params.get('code_challenge') ?? undefined,
codeChallengeMethod: params.get('code_challenge_method') ?? undefined,
prompt: params.get('prompt') ?? undefined,
maxAge: parseMaxAge(params.get('max_age')),
authorizationDetails: params.get('authorization_details') ?? undefined,
// The SDK types `resource` as a string but the server
// accepts a URI list (RFC 8707 allows repeated values).
resource:
resources.length > 0 ? (resources as unknown as string) : undefined
});
if (cancelled) return;
goSignIn(
`${base}/oauth2/consent?client_id=${encodeURIComponent(clientId)}&request_uri=${encodeURIComponent(par.request_uri)}`
);
} catch {
if (cancelled) return;
// PAR unavailable (older server) or the request is invalid.
// Fall back to the legacy full-URL redirect; validation
// errors resurface via authorize after login.
goSignIn();
}
return;
}
@@ -130,26 +261,7 @@
resource: params.get('resource') ?? undefined
});
if (cancelled) return;
if (result.redirectUrl) {
// Already consented — go straight back to the client.
window.location.href = result.redirectUrl;
if (!isWebRedirect(result.redirectUrl)) {
completedRedirectUrl = result.redirectUrl;
account = loggedInAccount;
app = await sdk.forConsole.apps
.get({ appId: clientId })
.catch(() => null);
if (cancelled) return;
phase = 'approved';
}
return;
}
if (result.grantId) {
await loadConsent(result.grantId, loggedInAccount);
return;
}
error = 'Could not start authorization.';
phase = 'error';
await handleAuthorizeResult(result, loggedInAccount, clientId, false);
} catch (e: unknown) {
if (cancelled) return;
error = (e as Error)?.message ?? 'Could not start authorization.';