diff --git a/bun.lock b/bun.lock index f50854238..f438c612c 100644 --- a/bun.lock +++ b/bun.lock @@ -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=="], diff --git a/package.json b/package.json index cc147590f..ae999bd91 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/routes/(console)/account/payments/addressModal.svelte b/src/routes/(console)/account/payments/addressModal.svelte index 57ea22c10..af186cb10 100644 --- a/src/routes/(console)/account/payments/addressModal.svelte +++ b/src/routes/(console)/account/payments/addressModal.svelte @@ -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; diff --git a/src/routes/(console)/account/payments/billingAddress.svelte b/src/routes/(console)/account/payments/billingAddress.svelte index 5e9b88837..703ad7e90 100644 --- a/src/routes/(console)/account/payments/billingAddress.svelte +++ b/src/routes/(console)/account/payments/billingAddress.svelte @@ -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; diff --git a/src/routes/(console)/account/payments/editAddressModal.svelte b/src/routes/(console)/account/payments/editAddressModal.svelte index 891b65103..0fc1fdad9 100644 --- a/src/routes/(console)/account/payments/editAddressModal.svelte +++ b/src/routes/(console)/account/payments/editAddressModal.svelte @@ -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; diff --git a/src/routes/(console)/organization-[organization]/billing/billingAddress.svelte b/src/routes/(console)/organization-[organization]/billing/billingAddress.svelte index ebecd5231..6dbc5bfab 100644 --- a/src/routes/(console)/organization-[organization]/billing/billingAddress.svelte +++ b/src/routes/(console)/organization-[organization]/billing/billingAddress.svelte @@ -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; diff --git a/src/routes/(console)/organization-[organization]/billing/replaceAddress.svelte b/src/routes/(console)/organization-[organization]/billing/replaceAddress.svelte index 56798f55b..926f21319 100644 --- a/src/routes/(console)/organization-[organization]/billing/replaceAddress.svelte +++ b/src/routes/(console)/organization-[organization]/billing/replaceAddress.svelte @@ -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; diff --git a/src/routes/(console)/organization-[organization]/settings/Soc2.svelte b/src/routes/(console)/organization-[organization]/settings/Soc2.svelte index 167a80261..7d71dbd60 100644 --- a/src/routes/(console)/organization-[organization]/settings/Soc2.svelte +++ b/src/routes/(console)/organization-[organization]/settings/Soc2.svelte @@ -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; diff --git a/src/routes/(console)/organization-[organization]/settings/Soc2Modal.svelte b/src/routes/(console)/organization-[organization]/settings/Soc2Modal.svelte index c4b3b645d..34331de12 100644 --- a/src/routes/(console)/organization-[organization]/settings/Soc2Modal.svelte +++ b/src/routes/(console)/organization-[organization]/settings/Soc2Modal.svelte @@ -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 = ''; diff --git a/src/routes/(public)/oauth2/consent/+page.svelte b/src/routes/(public)/oauth2/consent/+page.svelte index 799ea54d2..2dfe96de0 100644 --- a/src/routes/(public)/oauth2/consent/+page.svelte +++ b/src/routes/(public)/oauth2/consent/+page.svelte @@ -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 { - 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, + clientId: string | null, + fromRequestUri: boolean + ): Promise { + 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 | 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.';