From dae2b0b9eb7ad68f8947b79f620ecb0211685b42 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 23 Jun 2026 14:53:29 +0530 Subject: [PATCH] (feat): group OAuth2 consent scopes into a collapsible section The console OAuth2 consent screen rendered every requested scope as a flat list. For broad requests (e.g. account.admin plus dozens of granular console scopes) this produced an unusably long list. Group granular scopes by resource and tuck them into a single collapsible "App permissions" accordion, with read/write shown as compact tags. The account.admin full-access row and the OIDC identity scopes stay as always-visible rows above it. Also bump @appwrite.io/console to 15.1.0, let the consent layout scroll a tall card, and forward the resource indicator param through authorize. --- bun.lock | 4 +- package.json | 2 +- src/lib/helpers/oauth2-scopes.ts | 92 +++++++++++++++ src/routes/(public)/oauth2/+layout.svelte | 16 +-- .../(public)/oauth2/consent-card.svelte | 109 +++++++++++++++--- .../(public)/oauth2/consent/+page.svelte | 3 +- 6 files changed, 196 insertions(+), 30 deletions(-) diff --git a/bun.lock b/bun.lock index 15f8662ce..e9bda03c1 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@1a5604f", + "@appwrite.io/console": "15.1.0", "@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@1a5604f", { "dependencies": { "json-bigint": "1.0.0" } }], + "@appwrite.io/console": ["@appwrite.io/console@15.1.0", "", { "dependencies": { "json-bigint": "1.0.0" } }, "sha512-uQ8can9vDKoP6zxqhp8JzOYma/w40eKuYzZzsI9ATCRHSzRig0WQ6fue8rZEgOuD3eF3S2GbtHZK4drotAT8Ug=="], "@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 643ba8db4..8f78eca1b 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@1a5604f", + "@appwrite.io/console": "15.1.0", "@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/lib/helpers/oauth2-scopes.ts b/src/lib/helpers/oauth2-scopes.ts index a2a765454..373d1da5d 100644 --- a/src/lib/helpers/oauth2-scopes.ts +++ b/src/lib/helpers/oauth2-scopes.ts @@ -98,3 +98,95 @@ export function describeConsentScopes(scopes: string[]): ScopeDescriptor[] { return described; } + +export interface ScopeAction { + /** Full scope id, e.g. `teams.write`. */ + id: string; + /** Trailing verb, e.g. `read` / `write`. */ + action: string; + /** Display label for the verb, e.g. `Read` / `Write`. */ + label: string; +} + +export interface ScopeResourceGroup { + /** Resource prefix, e.g. `teams` or `backups.policies`. */ + resource: string; + /** Display title for the resource, e.g. `Teams`. */ + title: string; + icon: ComponentType; + actions: ScopeAction[]; +} + +export interface GroupedConsentScopes { + /** `account.admin` full-access descriptor, when requested. */ + admin: ScopeDescriptor | null; + /** OIDC identity scopes (openid/profile/email), always shown in full. */ + identity: ScopeDescriptor[]; + /** Every other granular scope, bucketed by resource for collapsing. */ + groups: ScopeResourceGroup[]; + /** Total count of granular scopes across all groups. */ + granularCount: number; +} + +/** + * Split a granular scope into its resource prefix and trailing action verb. + * `teams.read` -> { resource: 'teams', action: 'read' }, and + * `backups.policies.write` -> { resource: 'backups.policies', action: 'write' }. + * Scopes without a `.` fall back to the whole scope as the resource. + */ +function splitScope(scope: string): { resource: string; action: string } { + const lastDot = scope.lastIndexOf('.'); + if (lastDot <= 0) { + return { resource: scope, action: '' }; + } + return { resource: scope.slice(0, lastDot), action: scope.slice(lastDot + 1) }; +} + +/** + * Group the consent scopes for the OAuth2 consent screen. `account.admin` and the + * OIDC identity scopes stay as full, always-visible rows; everything else is + * bucketed by resource (`teams`, `projects`, …) so the granular console scopes can + * be tucked into a single collapsible section rather than a long flat list. + * Resource and action order follow first appearance in the requested scopes. + */ +export function groupConsentScopes(scopes: string[]): GroupedConsentScopes { + const requested = new Set(scopes); + const admin = requested.has(ACCOUNT_ADMIN_SCOPE) ? ACCOUNT_ADMIN_DESCRIPTOR : null; + const identity = CONSENT_IDENTITY_SCOPES.filter((scope) => requested.has(scope)).map( + describeScope + ); + + const remaining = scopes.filter( + (scope) => + scope !== 'openid' && + scope !== ACCOUNT_ADMIN_SCOPE && + !(CONSENT_IDENTITY_SCOPES as readonly string[]).includes(scope) + ); + + const byResource = new Map(); + for (const scope of remaining) { + const { resource, action } = splitScope(scope); + let group = byResource.get(resource); + if (!group) { + group = { + resource, + title: titleizeScope(resource), + icon: IconKey, + actions: [] + }; + byResource.set(resource, group); + } + if (!group.actions.some((existing) => existing.id === scope)) { + group.actions.push({ id: scope, action, label: titleizeScope(action || scope) }); + } + } + + const groups = [...byResource.values()]; + + // Keep a non-empty consent screen for a minimal OIDC request. + if (!admin && identity.length === 0 && groups.length === 0 && requested.has('openid')) { + return { admin: null, identity: [describeScope('openid')], groups: [], granularCount: 0 }; + } + + return { admin, identity, groups, granularCount: remaining.length }; +} diff --git a/src/routes/(public)/oauth2/+layout.svelte b/src/routes/(public)/oauth2/+layout.svelte index 39bc61b82..5486d5be3 100644 --- a/src/routes/(public)/oauth2/+layout.svelte +++ b/src/routes/(public)/oauth2/+layout.svelte @@ -34,23 +34,22 @@