diff --git a/src/components/ListingSpacesChip.tsx b/src/components/ListingSpacesChip.tsx new file mode 100644 index 0000000..c77758a --- /dev/null +++ b/src/components/ListingSpacesChip.tsx @@ -0,0 +1,215 @@ +"use client"; + +import * as stylex from "@stylexjs/stylex"; +import { Link as RouterLink } from "@tanstack/react-router"; +import { ArrowRight, UsersRound } from "lucide-react"; +import { Button } from "react-aria-components"; + +/** + * `` — accent pill beside Scopes that calls out ATProto Spaces + * (permissioned data) support, with a popover listing which spaces the app asks for. + */ +import type { DirectoryListingOAuthProbe } from "../integrations/tanstack-query/api-directory-listings.functions"; +import type { ListingSpaceGrant } from "../lib/oauth-space-scopes"; + +import { Flex } from "../design-system/flex"; +import { HoverCard } from "../design-system/hover-card"; +import { animationDuration } from "../design-system/theme/animations.stylex"; +import { uiColor } from "../design-system/theme/color.stylex"; +import { blue } from "../design-system/theme/colors/blue.stylex"; +import { radius } from "../design-system/theme/radius.stylex"; +import { + gap, + horizontalSpace, + size, + verticalSpace, +} from "../design-system/theme/semantic-spacing.stylex"; +import { + fontFamily, + fontSize, + fontWeight, +} from "../design-system/theme/typography.stylex"; +import { SmallBody } from "../design-system/typography"; +import { Text } from "../design-system/typography/text"; +import { + describeSpaceAuthority, + describeSpaceGrantAccess, + summarizeListingSpaces, +} from "../lib/oauth-space-scopes"; + +const styles = stylex.create({ + chip: { + borderColor: { + default: blue.border1, + ":hover": blue.border2, + }, + borderRadius: radius.full, + borderStyle: "solid", + borderWidth: 1, + paddingInline: 12, + alignItems: "center", + backgroundColor: { + default: blue.component1, + ":hover": blue.component2, + }, + boxSizing: "border-box", + color: blue.text2, + columnGap: gap.sm, + cursor: "pointer", + display: "inline-flex", + flexShrink: 0, + fontFamily: fontFamily.sans, + fontSize: 13, + fontWeight: 500, + outlineColor: blue.text2, + outlineOffset: 2, + outlineStyle: { default: "none", ":focus-visible": "solid" }, + outlineWidth: 2, + transitionDuration: animationDuration.default, + transitionProperty: "border-color, background-color", + height: 30, + }, + popoverSurface: { + maxHeight: "min(480px, 78vh)", + maxWidth: "min(432px, 94vw)", + overflowX: "hidden", + overflowY: "auto", + paddingBottom: 0, + paddingLeft: 0, + paddingRight: 0, + paddingTop: 0, + }, + header: { + alignItems: "center", + columnGap: gap.xl, + display: "flex", + justifyContent: "space-between", + borderBottomColor: uiColor.border2, + borderBottomStyle: "solid", + borderBottomWidth: 1, + height: size["4xl"], + paddingLeft: horizontalSpace["2xl"], + paddingRight: horizontalSpace["2xl"], + }, + moreLink: { + textDecoration: "none", + alignItems: "center", + color: { + default: uiColor.text1, + ":hover": uiColor.text2, + }, + columnGap: 4, + display: "inline-flex", + fontSize: fontSize.sm, + fontWeight: fontWeight.medium, + whiteSpace: "nowrap", + }, + content: { + paddingBottom: verticalSpace["2xl"], + paddingLeft: horizontalSpace["2xl"], + paddingRight: horizontalSpace["2xl"], + paddingTop: verticalSpace["2xl"], + }, + list: { + margin: 0, + padding: 0, + listStyle: "none", + display: "flex", + flexDirection: "column", + rowGap: verticalSpace["3xl"], + }, + spaceType: { + color: uiColor.text2, + fontFamily: fontFamily.mono, + fontSize: fontSize.sm, + fontWeight: fontWeight.medium, + overflowWrap: "anywhere", + }, + anyType: { + fontFamily: fontFamily.sans, + }, +}); + +/** Accent chip shown only when the app's OAuth client asks for Spaces. */ +export function ListingSpacesChip({ + oauthProbe, + productName, +}: { + oauthProbe: DirectoryListingOAuthProbe | null; + productName: string; +}) { + const summary = oauthProbe ? summarizeListingSpaces(oauthProbe) : null; + if (!summary) return null; + + return ( + + + Spaces + + } + style={styles.popoverSurface} + > + +
+ + Supports Spaces + + + More apps + + +
+ + + Spaces are the AT Protocol’s permissioned data: records shared with + just the people in a space instead of everyone. + + {summary.grants.length > 0 ? ( +
    + {summary.grants.map((grant) => ( + + ))} +
+ ) : null} + {summary.unspecifiedPermissionSets.length > 0 ? ( + + {`Through the ${summary.unspecifiedPermissionSets.join(", ")} permission ${ + summary.unspecifiedPermissionSets.length === 1 ? "set" : "sets" + }.`} + + ) : null} +
+
+
+ ); +} + +function SpaceGrantItem({ grant }: { grant: ListingSpaceGrant }) { + const isAnyType = grant.spaceType === "*"; + return ( +
  • +
    + {isAnyType ? "Any kind of space" : grant.spaceType} +
    + + {`${capitalize(describeSpaceAuthority(grant.authority))} · ${describeSpaceGrantAccess(grant)}`} + +
  • + ); +} + +function capitalize(text: string) { + return text.charAt(0).toUpperCase() + text.slice(1); +} diff --git a/src/components/shelf/listing/ListingSidebar.tsx b/src/components/shelf/listing/ListingSidebar.tsx index d61d85e..3764ffd 100644 --- a/src/components/shelf/listing/ListingSidebar.tsx +++ b/src/components/shelf/listing/ListingSidebar.tsx @@ -19,11 +19,13 @@ import { getAppTagLinkOptions, } from "../../../lib/app-tag-metadata"; import { isGameOnlyListing } from "../../../lib/game-listing"; +import { summarizeListingSpaces } from "../../../lib/oauth-space-scopes"; import { FundingPopoverChip } from "../../funding-popover-chip"; import { GameFactRows } from "../../GameDetailsPanel"; import { GermNetworkBadge } from "../../GermNetworkBadge"; import { ListingOAuthScopesPopoverChip } from "../../ListingOAuthScopesPopoverChip"; import { listingOAuthScopesPopoverChipShouldRender } from "../../ListingOAuthScopesPopoverChip.logic"; +import { ListingSpacesChip } from "../../ListingSpacesChip"; import { formatListingDate, getCategoryDisplayLabel, @@ -118,6 +120,9 @@ export function ListingSidebar({ listingOAuthScopesPopoverChipShouldRender({ oauthProbe: listing.oauthProbe, }); + const supportsSpaces = + listing.oauthProbe != null && + summarizeListingSpaces(listing.oauthProbe) != null; const germHref = listing.germDmHref?.trim() || null; const hasSource = listing.links.some((link) => link.type === "source"); const hasDonate = listing.links.some((link) => link.type === "donate"); @@ -210,14 +215,22 @@ export function ListingSidebar({ ))} ) : null} - {showScopes && website ? ( + {(showScopes && website) || supportsSpaces ? ( - + {supportsSpaces ? ( + + ) : null} + {showScopes && website ? ( + + ) : null} ) : null} {hasSource || hasDonate || showFunding || germHref ? ( diff --git a/src/lib/oauth-listing-auth-probe.ts b/src/lib/oauth-listing-auth-probe.ts index c389868..4006831 100644 --- a/src/lib/oauth-listing-auth-probe.ts +++ b/src/lib/oauth-listing-auth-probe.ts @@ -16,6 +16,7 @@ import { resolve } from "node:path"; import process from "node:process"; import type { PermissionGrantStructuredLine } from "./oauth-permission-grant-ui"; +import type { ParsedSpaceScope } from "./oauth-space-scopes"; import { PERMISSION_GRANT_ACCOUNT_RELATED_HEADING, @@ -963,6 +964,8 @@ export type PermissionLexiconLookup = title?: string; detail?: string; structuredLines: Array; + /** `resource: "space"` entries in the bundle (absent on probes before Spaces support). */ + spaceGrants?: Array; } | { resolved: false; @@ -1150,6 +1153,13 @@ async function resolveIncludedPermissionLexicon( title: bundleMain.title, detail: bundleMain.detail, structuredLines, + spaceGrants: Array.isArray(perms) + ? perms.flatMap((row) => + row && typeof row === "object" && !Array.isArray(row) + ? (parseSpacePermissionSetEntry(row as JsonRecord) ?? []) + : [], + ) + : [], }; lookupCache.set(nsid, out); return out; diff --git a/src/lib/oauth-space-scopes.test.ts b/src/lib/oauth-space-scopes.test.ts index 0a505a1..8f8478c 100644 --- a/src/lib/oauth-space-scopes.test.ts +++ b/src/lib/oauth-space-scopes.test.ts @@ -5,6 +5,7 @@ import { isSpaceScopeToken, parseSpacePermissionSetEntry, parseSpaceScopeToken, + summarizeListingSpaces, } from "./oauth-space-scopes"; describe("isSpaceScopeToken", () => { @@ -84,3 +85,77 @@ describe("describeSpaceScope", () => { ); }); }); + +type ScopeRow = Parameters< + typeof summarizeListingSpaces +>[0]["scopeHumanReadable"][number]; + +function resolvedSet(nsid: string, extra: Record): ScopeRow { + return { + token: `include:${nsid}`, + description: "", + includePermissionSet: { + resolved: true, + nsid, + sourceKind: "remote", + sourceUrl: "https://example.com", + structuredLines: [], + ...extra, + }, + }; +} + +describe("summarizeListingSpaces", () => { + it("returns null when the client asks for no spaces", () => { + expect( + summarizeListingSpaces({ + oauthClientScopesDistinct: ["atproto", "repo:app.bsky.feed.post"], + clientScopeRawLine: null, + scopeHumanReadable: [], + }), + ).toBeNull(); + }); + + it("collects direct tokens and permission-set grants, concrete types first", () => { + const summary = summarizeListingSpaces({ + oauthClientScopesDistinct: [], + clientScopeRawLine: + "atproto space:*?action=read_self include:at.secretsky.permissions", + scopeHumanReadable: [ + resolvedSet("at.secretsky.permissions", { + spaceGrants: [ + { + spaceType: "at.secretsky.feed", + authority: "*", + collections: ["at.secretsky.post"], + actions: ["read", "create"], + manage: [], + }, + ], + }), + ], + }); + expect( + summary?.grants.map((g) => [g.spaceType, g.viaPermissionSet]), + ).toEqual([ + ["at.secretsky.feed", "at.secretsky.permissions"], + ["*", null], + ]); + }); + + it("flags legacy bundles that use spaces without recorded grants", () => { + const summary = summarizeListingSpaces({ + oauthClientScopesDistinct: ["atproto", "include:com.example.perms"], + clientScopeRawLine: null, + scopeHumanReadable: [ + resolvedSet("com.example.perms", { + structuredLines: ["Technical permission", "resource: space."], + }), + ], + }); + expect(summary).toEqual({ + grants: [], + unspecifiedPermissionSets: ["com.example.perms"], + }); + }); +}); diff --git a/src/lib/oauth-space-scopes.ts b/src/lib/oauth-space-scopes.ts index d1851a5..6d62da7 100644 --- a/src/lib/oauth-space-scopes.ts +++ b/src/lib/oauth-space-scopes.ts @@ -8,7 +8,13 @@ * Client-safe (no Node imports) so the browse filter, home page, and probe can share it. */ -import { atprotoPermissionScopeResource } from "./oauth-scope-include-parse"; +import type { SummaryScopeHumanRow } from "./oauth-listing-auth-probe"; + +import { PERMISSION_GRANT_SPACES_HEADING } from "./oauth-permission-grant-ui"; +import { + atprotoPermissionScopeResource, + parseIncludeScopeToken, +} from "./oauth-scope-include-parse"; export const SPACE_SCOPE_RESOURCE = "space" as const; @@ -101,11 +107,101 @@ export function describeSpaceAuthority(authority: string): string { /** One-line storefront summary for a `space:` token. */ export function describeSpaceScope(scope: ParsedSpaceScope): string { + return `Permissioned space · ${describeSpaceType(scope.spaceType)} · ${describeSpaceAuthority(scope.authority)} · ${describeSpaceGrantAccess(scope)}`; +} + +/** A space grant plus the `include:` permission set it came from (null = direct `space:` token). */ +export interface ListingSpaceGrant extends ParsedSpaceScope { + viaPermissionSet: string | null; +} + +export interface ListingSpacesSummary { + /** Concrete space types first, then `*` grants. */ + grants: Array; + /** Permission sets that use spaces but were probed before we recorded which ones. */ + unspecifiedPermissionSets: Array; +} + +/** Bundle lines written before `spaceGrants` existed: our heading, or the generic fallback. */ +const LEGACY_SPACE_BUNDLE_LINES: ReadonlySet = new Set([ + PERMISSION_GRANT_SPACES_HEADING, + "resource: space.", +]); + +function clientScopeTokens(probe: { + oauthClientScopesDistinct: ReadonlyArray; + clientScopeRawLine: string | null; +}): Array { + if (probe.oauthClientScopesDistinct.length > 0) { + return [...probe.oauthClientScopesDistinct]; + } + return (probe.clientScopeRawLine ?? "") + .replaceAll("\u00A0", " ") + .split(/\s+/) + .filter(Boolean); +} + +/** + * How a listing's OAuth client uses ATProto Spaces, or null when it doesn't. Mirrors + * `sqlProbeRequestsSpaces` (server): direct tokens come only from the app's own client + * metadata — never the authorization server's `scopes_supported`. + */ +export function summarizeListingSpaces(probe: { + oauthClientScopesDistinct: ReadonlyArray; + clientScopeRawLine: string | null; + scopeHumanReadable: ReadonlyArray; +}): ListingSpacesSummary | null { + const grants: Array = []; + for (const token of clientScopeTokens(probe)) { + const parsed = parseSpaceScopeToken(token); + if (parsed) grants.push({ ...parsed, viaPermissionSet: null }); + } + + const unspecified = new Set(); + for (const row of probe.scopeHumanReadable) { + if (!("includePermissionSet" in row)) continue; + const set = row.includePermissionSet; + const nsid = parseIncludeScopeToken(row.token)?.nsid ?? set.nsid; + if (set.spaceGrants) { + for (const grant of set.spaceGrants) { + grants.push({ ...grant, viaPermissionSet: nsid }); + } + } else if ( + set.structuredLines.some( + (line) => + typeof line === "string" && LEGACY_SPACE_BUNDLE_LINES.has(line), + ) + ) { + unspecified.add(nsid); + } + } + + if (grants.length === 0 && unspecified.size === 0) return null; + + const seen = new Set(); + const unique = grants.filter((grant) => { + const key = JSON.stringify(grant); + if (seen.has(key)) return false; + seen.add(key); + return true; + }); + return { + grants: unique.toSorted( + (a, b) => + Number(a.spaceType === "*") - Number(b.spaceType === "*") || + a.spaceType.localeCompare(b.spaceType), + ), + unspecifiedPermissionSets: [...unspecified].toSorted(), + }; +} + +/** `read`, `create`… for display; empty `actions` means all four. */ +export function describeSpaceGrantAccess(grant: ParsedSpaceScope): string { const actions = - scope.actions.length > 0 - ? scope.actions.map((a) => (a === "read_self" ? "read own" : a)) + grant.actions.length > 0 + ? grant.actions.map((a) => (a === "read_self" ? "read own" : a)) : ["read", "create", "update", "delete"]; const manage = - scope.manage.length > 0 ? ` · manage ${scope.manage.join(", ")}` : ""; - return `Permissioned space · ${describeSpaceType(scope.spaceType)} · ${describeSpaceAuthority(scope.authority)} · ${actions.join(", ")}${manage}`; + grant.manage.length > 0 ? ` · manage ${grant.manage.join(", ")}` : ""; + return `${actions.join(", ")}${manage}`; }