chadtmiller.com/group-host
atmosphere group host
0
0
The approval page lists what an app would get as the PDS's consent page does
crates/group-host/src/authorization.rs+3−2
| @@ -125,10 +125,11 @@ impl Host { | ||
} | ||
} | ||
| ||
/// What each of this host's permission sets asked for says it is for. | ||
/// What each of this host's permission sets asked for is called, and | ||
/// says it is for. | ||
fn labels(requested: &[String]) -> Value { | ||
let ours = requested.iter().filter_map(|s| Some((s, set_of(s)?))); | ||
ours.map(|(s, set)| json!({"scope": s, "text": set["detail"]})) | ||
ours.map(|(s, set)| json!({"scope": s, "title": set["title"], "text": set["detail"]})) | ||
.collect() | ||
} | ||
| ||
crates/group-host/tests/authorization.rs+2−2
| @@ -101,8 +101,8 @@ async fn offers_the_groups_where_the_caller_holds_a_role_with_what_the_app_would | ||
assert_eq!( | ||
offered["labels"], | ||
json!([ | ||
{"scope": "include:fyi.opensocial.configure", "text": "Edit the group's profile, rules, and roles, and decide what each role can do."}, | ||
{"scope": "include:fyi.opensocial.moderate", "text": "Review reports and moderation history, and label members."}, | ||
{"scope": "include:fyi.opensocial.configure", "title": "Group settings", "text": "Edit the group's profile, rules, and roles, and decide what each role can do."}, | ||
{"scope": "include:fyi.opensocial.moderate", "title": "Group moderation", "text": "Review reports and moderation history, and label members."}, | ||
]) | ||
); | ||
let mut theirs = dids(&offered); | ||
docs/design/signing-in.md+7−3
| @@ -148,9 +148,13 @@ The approval page asks `getAuthorization` what the request is: the | ||
client, the scopes asked, and the groups offered. If the app's | ||
`login_hint` names a switched-on group here, that group is offered; | ||
otherwise every switched-on group where the member holds a role. For | ||
each, the page shows the member's roles and which asked scopes they | ||
would grant or withhold, and each of the host's permission sets asked | ||
for in its own words, the `detail` of its lexicon. | ||
each, the page shows what the app would get and what it would not, as an | ||
account's PDS shows a request on its consent page: the scopes read by | ||
the same parser (`@atproto/oauth-scopes`) and listed as its rows, in its | ||
order (Bluesky, chat, each permission set, the repository, calling | ||
services, spaces), each with its details a dialog away, and the scopes | ||
as asked behind "Technical details". Each of the host's permission sets | ||
is named in its own words, the `title` and `detail` of its lexicon. | ||
| ||
`approveAuthorization` requires the member to hold a role in the chosen | ||
group, and that group to be the one the app named, if it named one. The | ||
harness/e2e/flows.spec.mjs+10−1
| @@ -572,7 +572,16 @@ test('signing an app in as her group', async () => { | ||
const app = await new App('atproto repo:app.bsky.feed.post').listen() | ||
await page.goto(await app.push()) | ||
await expect(page.getByRole('heading', { name: 'Sign in an app' })).toBeVisible() | ||
await expect(page.getByText('repo:app.bsky.feed.post').first()).toBeVisible() | ||
// Said as her PDS says it: Bluesky, and in its details, the posts. | ||
const riders = page.locator('form', { hasText: '@riders.groups.test' }) | ||
await expect(riders.getByText("Use parts of the group's Bluesky account")).toBeVisible() | ||
await riders.getByRole('button', { name: 'Details' }).click() | ||
const details = page.getByRole('dialog', { name: 'Bluesky' }).filter({ visible: true }) | ||
await expect(details.getByRole('row', { name: /app\.bsky\.feed\.post/ })).toBeVisible() | ||
await details.getByRole('button', { name: 'Close' }).click() | ||
await page.getByRole('button', { name: 'Technical details' }).click() | ||
await expect(page.getByRole('dialog', { name: 'Technical details' })).toContainText('repo:app.bsky.feed.post') | ||
await page.getByRole('dialog', { name: 'Technical details' }).getByRole('button', { name: 'Close' }).click() | ||
await page.getByRole('button', { name: 'Sign it in as Riders' }).click() | ||
const back = await app.back | ||
app.close() | ||
lexicons/fyi/opensocial/getAuthorization.json+5−1
| @@ -45,7 +45,7 @@ | ||
"type": "ref", | ||
"ref": "#label" | ||
}, | ||
"description": "What each permission set the app asked for says it is for, in its own words." | ||
"description": "What each permission set the app asked for is called, and says it is for, in its own words." | ||
}, | ||
"loginHint": { | ||
"type": "string", | ||
| @@ -153,6 +153,10 @@ | ||
"scope": { | ||
"type": "string" | ||
}, | ||
"title": { | ||
"type": "string", | ||
"description": "The permission set's title, in its own words." | ||
}, | ||
"text": { | ||
"type": "string" | ||
} | ||
web/src/lib/atoms/Icon.sveltenew+32−0
| @@ -0,0 +1,32 @@ | ||
<script lang="ts"> | ||
/** | ||
* A line icon, by name: the ones an account's PDS draws beside what an | ||
* app asks for (Lucide's, ISC; Bluesky's butterfly, filled). Drawn in | ||
* the text's colour by `.icon`, `lg` beside a row's title. | ||
*/ | ||
let { name, lg = false }: { name: 'mail' | 'id-card' | 'user' | 'bluesky' | 'chat' | 'set' | 'book' | 'badge' | 'users' | 'check'; lg?: boolean } = $props(); | ||
</script> | ||
| ||
<svg class="icon" class:icon-lg={lg} class:icon-filled={name === 'bluesky'} viewBox="0 0 24 24" aria-hidden="true"> | ||
{#if name === 'mail'} | ||
<rect width="20" height="16" x="2" y="4" rx="2" /><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7" /> | ||
{:else if name === 'id-card'} | ||
<path d="M16 10h2" /><path d="M16 14h2" /><path d="M6.17 15a3 3 0 0 1 5.66 0" /><circle cx="9" cy="11" r="2" /><rect x="2" y="5" width="20" height="14" rx="2" /> | ||
{:else if name === 'user'} | ||
<path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2" /><circle cx="12" cy="7" r="4" /> | ||
{:else if name === 'bluesky'} | ||
<path d="M6.335 4.212c2.293 1.76 4.76 5.327 5.665 7.241.906-1.914 3.372-5.482 5.665-7.241C19.319 2.942 22 1.960 22 5.086c0 .624-.35 5.244-.556 5.994-.713 2.608-3.315 3.273-5.629 2.870 4.045.704 5.074 3.035 2.852 5.366-4.220 4.426-6.066-1.111-6.540-2.530-.086-.260-.126-.382-.127-.278 0-.104-.041.018-.128.278-.473 1.419-2.318 6.956-6.539 2.530-2.222-2.331-1.193-4.662 2.852-5.366-2.314.403-4.916-.262-5.630-2.870C2.350 10.330 2 5.710 2 5.086c0-3.126 2.680-2.144 4.335-.874Z" /> | ||
{:else if name === 'chat'} | ||
<path d="M7.9 20A9 9 0 1 0 4 16.1L2 22Z" /><path d="M8 12h.01" /><path d="M12 12h.01" /><path d="M16 12h.01" /> | ||
{:else if name === 'set'} | ||
<circle cx="12" cy="12" r="1" /><path d="M20.2 20.2c2.04-2.03.02-7.36-4.5-11.9-4.54-4.52-9.87-6.54-11.9-4.5-2.04 2.03-.02 7.36 4.5 11.9 4.54 4.52 9.87 6.54 11.9 4.5Z" /><path d="M15.7 15.7c4.52-4.54 6.54-9.87 4.5-11.9-2.03-2.04-7.36-.02-11.9 4.5-4.52 4.54-6.54 9.87-4.5 11.9 2.03 2.04 7.36.02 11.9-4.5Z" /> | ||
{:else if name === 'book'} | ||
<path d="M12 7v14" /><path d="M3 18a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h5a4 4 0 0 1 4 4 4 4 0 0 1 4-4h5a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1h-6a3 3 0 0 0-3 3 3 3 0 0 0-3-3z" /> | ||
{:else if name === 'badge'} | ||
<path d="M3.85 8.62a4 4 0 0 1 4.78-4.77 4 4 0 0 1 6.74 0 4 4 0 0 1 4.78 4.78 4 4 0 0 1 0 6.74 4 4 0 0 1-4.77 4.78 4 4 0 0 1-6.75 0 4 4 0 0 1-4.78-4.77 4 4 0 0 1 0-6.76Z" /><path d="m9 12 2 2 4-4" /> | ||
{:else if name === 'users'} | ||
<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="M22 21v-2a4 4 0 0 0-3-3.87" /><path d="M16 3.13a4 4 0 0 1 0 7.75" /> | ||
{:else} | ||
<path d="M20 6 9 17l-5-5" /> | ||
{/if} | ||
</svg> | ||
web/src/lib/molecules/Permissions.sveltenew+87−0
| @@ -0,0 +1,87 @@ | ||
<script lang="ts"> | ||
import Icon from '#lib/atoms/Icon.svelte'; | ||
import Dialog from '#lib/molecules/Dialog.svelte'; | ||
import type { ComponentProps } from 'svelte'; | ||
import type { Data, Kind, Permission } from '#lib/permissions.ts'; | ||
/** | ||
* What an app would be let do, a row for each kind of thing, as an | ||
* account's PDS lists it: its icon, its title, a line on it, and its | ||
* details in a dialog. `id` keeps the dialogs of two lists apart; | ||
* `withheld` draws the rows of what it would not get, quieter. | ||
*/ | ||
let { id, list, withheld = false }: { id: string; list: Permission[]; withheld?: boolean } = $props(); | ||
const icons: Record<Kind, ComponentProps<typeof Icon>['name']> = { | ||
email: 'mail', | ||
identity: 'id-card', | ||
account: 'user', | ||
bluesky: 'bluesky', | ||
chat: 'chat', | ||
set: 'set', | ||
repository: 'book', | ||
authenticate: 'badge', | ||
spaces: 'users' | ||
}; | ||
const detailed = (p: Permission) => !!(p.data?.length || p.calls?.length || p.spaces?.length); | ||
const anywhere = (owner: string) => (owner === '*' ? 'the network' : owner); | ||
</script> | ||
| ||
{#snippet nsid(name: string)} | ||
<code>{#each name.split('.') as part, i (i)}{#if i}.<wbr />{/if}{part}{/each}</code> | ||
{/snippet} | ||
| ||
{#snippet ticks(data: Data[])} | ||
<table class="ticks"> | ||
<thead><tr><th>Data</th><th>Create</th><th>Update</th><th>Delete</th></tr></thead> | ||
<tbody> | ||
{#each data as d (d.collection)} | ||
<tr> | ||
<td>{#if d.collection === '*'}<em class="mute">Any collection</em>{:else}{@render nsid(d.collection)}{/if}</td> | ||
{#each [d.ticks.create, d.ticks.update, d.ticks.delete] as on, i (i)} | ||
<td>{#if on}<Icon name="check" />{/if}</td> | ||
{/each} | ||
</tr> | ||
{/each} | ||
</tbody> | ||
</table> | ||
{/snippet} | ||
| ||
<ul class="permissions" class:withheld> | ||
{#each list as p, i (i)} | ||
<li> | ||
<Icon name={icons[p.kind]} lg /> | ||
<div class="row-main"> | ||
<span class="who">{p.title}</span> | ||
<span class="what">{p.says}</span> | ||
</div> | ||
{#if detailed(p)} | ||
<div class="row-actions"><button class="quiet" type="button" popovertarget="{id}-{i}">Details</button></div> | ||
<Dialog id="{id}-{i}" title={p.title}> | ||
{#if withheld}<p>The app asked for this, and won't get it.</p>{/if} | ||
{#if p.about}<p class="mute">{p.about}</p>{/if} | ||
{#if p.data?.length}{@render ticks(p.data)}{/if} | ||
{#if p.files}<p>The app could upload files too.</p>{/if} | ||
{#if p.calls?.length} | ||
<table class="ticks calls"> | ||
<thead><tr><th>Call</th><th>On</th></tr></thead> | ||
<tbody> | ||
{#each p.calls as c (c.method + c.service)} | ||
<tr><td>{#if c.method === '*'}<em class="mute">Any method</em>{:else}{@render nsid(c.method)}{/if}</td><td>{c.service}</td></tr> | ||
{/each} | ||
</tbody> | ||
</table> | ||
{/if} | ||
{#each p.spaces ?? [] as s (s.type + s.owner)} | ||
<div class="space-access"> | ||
<p> | ||
{#if s.type === '*'}All spaces on <strong>{anywhere(s.owner)}</strong>{:else}{@render nsid(s.type)} spaces on <strong>{anywhere(s.owner)}</strong>{/if} | ||
</p> | ||
{#if s.manage}<p class="mute small">Manage the spaces, too: create them, and add and remove members.</p>{/if} | ||
{#if !s.read && s.readSelf}<p class="mute small">Read only its own data.</p>{/if} | ||
{#if s.data.length}{@render ticks(s.data)}{:else if s.read && !s.manage}<p class="mute small">Read only.</p>{:else if !s.read && !s.readSelf && !s.manage}<p class="mute small">No access.</p>{/if} | ||
</div> | ||
{/each} | ||
</Dialog> | ||
{/if} | ||
</li> | ||
{/each} | ||
</ul> | ||
web/src/lib/molecules/ScopeList.sveltedeleted+0−16
| @@ -1,16 +0,0 @@ | ||
<script lang="ts"> | ||
/** What an app would be let do, or not, scope by scope, with each permission set's own words. */ | ||
let { title, scopes, labels = [] }: { title: string; scopes: { scope: string }[]; labels?: { scope: string; text: string }[] } = $props(); | ||
const said = (scope: string) => labels.find((l) => l.scope === scope)?.text; | ||
</script> | ||
| ||
{#if scopes.length} | ||
<div class="stack"> | ||
<h3>{title}</h3> | ||
<ul> | ||
{#each scopes as s (s.scope)} | ||
<li>{#if said(s.scope)}{said(s.scope)} {/if}<code>{s.scope}</code></li> | ||
{/each} | ||
</ul> | ||
</div> | ||
{/if} | ||
web/src/lib/organisms/AuthorizeRequest.svelte+35−10
| @@ -1,29 +1,37 @@ | ||
<script lang="ts"> | ||
import Dialog from '#lib/molecules/Dialog.svelte'; | ||
import Empty from '#lib/molecules/Empty.svelte'; | ||
import GroupRow from '#lib/molecules/GroupRow.svelte'; | ||
import ScopeList from '#lib/molecules/ScopeList.svelte'; | ||
import Permissions from '#lib/molecules/Permissions.svelte'; | ||
import { permissions, warnings, type Label } from '#lib/permissions.ts'; | ||
type Group = { did: string; handle: string; displayName?: string; roles: { role: string }[]; granted: { scope: string }[]; withheld: { scope: string }[] }; | ||
/** | ||
* An app's request to sign in as a group: who it is, and for each group | ||
* the person may sign it in as, what it would get and what it would not; | ||
* approved for one, or declined. An AI app's, an MCP client's (`mcp`), | ||
* asks to act as the person in a group instead, with what their roles | ||
* there allow. | ||
* the person may sign it in as, what it would get and what it would not, | ||
* said as an account's PDS says it; approved for one, or declined. The | ||
* scopes as asked are behind "Technical details". An AI app's, an MCP | ||
* client's (`mcp`), asks to act as the person in a group instead, with | ||
* what their roles there allow. | ||
*/ | ||
let { | ||
client, | ||
groups, | ||
labels, | ||
requested = [], | ||
requestUri, | ||
mcp = false | ||
}: { | ||
client: { clientId: string; name?: string; uri?: string }; | ||
groups: Group[]; | ||
labels: { scope: string; text: string }[]; | ||
labels: Label[]; | ||
requested?: { scope: string }[]; | ||
requestUri: string; | ||
mcp?: boolean; | ||
} = $props(); | ||
const name = $derived(client.name ?? client.clientId); | ||
const scopes = (list: { scope: string }[]) => list.map((s) => s.scope); | ||
// Only `atproto`: it would learn which group it is, and nothing more. | ||
const bare = (list: { scope: string }[]) => list.every((s) => s.scope === 'atproto'); | ||
// The request stays in the address the form is sent to, for the page | ||
// drawn after it. | ||
const action = (name: string) => `?request_uri=${encodeURIComponent(requestUri)}&/${name}`; | ||
| @@ -34,11 +42,18 @@ | ||
<p>An AI app calling itself <strong>{name}</strong> wants to act as you in one of your groups.</p> | ||
<p class="mute">The app chose this name, and it isn't checked. It came from <span class="mono">{client.clientId}</span>.</p> | ||
{:else} | ||
<p><strong>{name}</strong> wants to sign in as one of your groups.</p> | ||
<div class="app-asks"> | ||
<p><strong>{name}</strong> wants to sign in as one of your groups.</p> | ||
{#if requested.length}<button class="quiet small" type="button" popovertarget="asked">Technical details</button>{/if} | ||
</div> | ||
<Dialog id="asked" title="Technical details"> | ||
<p class="mute">The app asks for these permissions (scopes):</p> | ||
<pre class="well scopes">{scopes(requested).join('\n')}</pre> | ||
</Dialog> | ||
{/if} | ||
{#if client.uri}<p class="mute"><a href={client.uri}>{client.uri}</a></p>{/if} | ||
{#if groups.length} | ||
{#each groups as g (g.did)} | ||
{#each groups as g, n (g.did)} | ||
<form class="stack" method="post" action={action('approve')}> | ||
<input type="hidden" name="requestUri" value={requestUri} /> | ||
<input type="hidden" name="group" value={g.did} /> | ||
| @@ -47,8 +62,18 @@ | ||
<p>It could do what your roles there let you do ({g.roles.map((r) => r.role).join(', ')}).</p> | ||
<button type="submit">Connect it to {g.displayName ?? g.handle}</button> | ||
{:else if g.roles.length} | ||
<ScopeList title="It would be able to" scopes={g.granted} {labels} /> | ||
<ScopeList title="It would not get" scopes={g.withheld} {labels} /> | ||
{#if bare(g.granted)} | ||
<p>The app would only know it's signed in as {g.displayName ?? g.handle}.</p> | ||
{:else} | ||
<Permissions id="granted-{n}" list={permissions(scopes(g.granted), labels, g)} /> | ||
{/if} | ||
{#each warnings(scopes(g.granted)) as w (w)}<p class="danger">{w}</p>{/each} | ||
{#if g.withheld.length} | ||
<div class="stack withheld-part"> | ||
<h3>It would not get</h3> | ||
<Permissions id="withheld-{n}" list={permissions(scopes(g.withheld), labels, g)} withheld /> | ||
</div> | ||
{/if} | ||
<button type="submit">Sign it in as {g.displayName ?? g.handle}</button> | ||
{:else} | ||
<p class="mute">You don't have a role in this group, so you can't sign an app in as it.</p> | ||
web/src/lib/permissions.tsnew+240−0
| @@ -0,0 +1,240 @@ | ||
import { BlobPermission, IncludeScope, RepoPermission, RpcPermission, ScopePermissionsTransition, SpacePermission } from '@atproto/oauth-scopes'; | ||
| ||
/** | ||
* What an app asking for some scopes could do, said as an account's own | ||
* PDS says it on its consent page: a row for each kind of thing (email, | ||
* identity, the account, Bluesky, chat, each permission set, the | ||
* repository, calling services, spaces), in its order and with its | ||
* words, read with the same parser. Each row's details are the tables | ||
* the PDS opens under its "?". Said of a group: the app would act as it. | ||
*/ | ||
| ||
export type Kind = 'email' | 'identity' | 'account' | 'bluesky' | 'chat' | 'set' | 'repository' | 'authenticate' | 'spaces'; | ||
| ||
/** Which of a collection's writes are let. */ | ||
export type Ticks = { create: boolean; update: boolean; delete: boolean }; | ||
/** A collection, or any (`*`), and the writes to it. */ | ||
export type Data = { collection: string; ticks: Ticks }; | ||
/** The spaces one row of space scopes reaches, and what it may do in them. */ | ||
export type Spaces = { | ||
/** Its type, or any (`*`). */ | ||
type: string; | ||
/** Whose spaces: a handle (`@riders.groups.test`), a DID, or any (`*`). */ | ||
owner: string; | ||
read: boolean; | ||
readSelf: boolean; | ||
manage: boolean; | ||
data: Data[]; | ||
}; | ||
| ||
export type Permission = { | ||
kind: Kind; | ||
title: string; | ||
/** A line on what it lets the app do. */ | ||
says: string; | ||
/** What the details say first, above their tables. */ | ||
about?: string; | ||
data?: Data[]; | ||
/** Whether it may upload files. */ | ||
files?: boolean; | ||
/** The methods it may call, each with the service it calls. */ | ||
calls?: { method: string; service: string }[]; | ||
spaces?: Spaces[]; | ||
}; | ||
| ||
/** A permission set's title and what it says it is for, as the host gives them. */ | ||
export type Label = { scope: string; title?: string; text: string }; | ||
| ||
/** The PDS's rows for `scopes`, asked for as the group with `handle` and `did`. */ | ||
export function permissions(scopes: string[], labels: Label[], group: { did: string; handle: string }): Permission[] { | ||
const p = new ScopePermissionsTransition(scopes); | ||
const out: Permission[] = []; | ||
const email = (['manage', 'read'] as const).find((action) => p.allowsAccount({ attr: 'email', action })); | ||
if (email) out.push({ kind: 'email', title: 'Email', says: email === 'manage' ? "Read and update the group's email address" : "Read the group's email address" }); | ||
if (p.allowsIdentity({ attr: '*' })) out.push({ kind: 'identity', title: 'Identity', says: "Manage the group's full identity, including its @handle" }); | ||
else if (p.allowsIdentity({ attr: 'handle' })) out.push({ kind: 'identity', title: 'Identity', says: "Change the group's @handle" }); | ||
if (p.allowsAccount({ attr: 'status', action: 'manage' })) out.push({ kind: 'account', title: 'Account', says: 'Turn the group off for a while, and back on' }); | ||
| ||
const onlyBluesky = blueskyOnly(scopes, p); | ||
const broad = p.hasTransitionGeneric || (['create', 'update', 'delete'] as const).some((action) => p.allowsRepo({ collection: '*', action })); | ||
const bskyRepo = scopes.some((s) => s === 'transition:generic' || (RepoPermission.fromString(s)?.collection.some((c) => c === '*' || bluesky(c)) ?? false)); | ||
const bskyRpc = scopes.some(privateBluesky); | ||
if (bskyRepo || bskyRpc) { | ||
const details = !broad && onlyBluesky; | ||
out.push({ | ||
kind: 'bluesky', | ||
title: 'Bluesky', | ||
says: | ||
broad && bskyRpc | ||
? "Manage the group's profile, posts, likes and follows, and read its private preferences" | ||
: broad | ||
? "Manage the group's profile, posts, likes and follows" | ||
: bskyRepo && bskyRpc | ||
? "Use parts of the group's Bluesky account, and read its private preferences" | ||
: bskyRepo | ||
? "Use parts of the group's Bluesky account" | ||
: "Read the group's private Bluesky preferences", | ||
...(details && { about: 'The app asks to do this on the Bluesky account:', data: repoData(scopes), files: files(scopes, p), calls: calls(scopes) }) | ||
}); | ||
} | ||
if (p.hasTransitionChatBsky || scopes.some(chat)) out.push({ kind: 'chat', title: 'Chat', says: 'Read and send messages' }); | ||
| ||
for (const s of new Set(scopes)) { | ||
const set = IncludeScope.fromString(s); | ||
if (!set) continue; | ||
const label = labels.find((l) => l.scope === s); | ||
out.push({ kind: 'set', title: label?.title ?? set.nsid, says: label?.text ?? 'A set of permissions this host does not know' }); | ||
} | ||
| ||
if (!onlyBluesky) { | ||
const data = repoData(scopes); | ||
const all = p.hasTransitionGeneric || p.allowsAccount({ attr: 'repo', action: 'manage' }) || (['create', 'update', 'delete'] as const).every((action) => p.allowsRepo({ collection: '*', action })); | ||
if (all) out.push({ kind: 'repository', title: 'Repository', says: "Create, update and delete any of the group's public data" }); | ||
else if (data.length) | ||
out.push({ | ||
kind: 'repository', | ||
title: 'Repository', | ||
says: 'Publish changes', | ||
about: "The group's repository holds its public data, such as its posts. The app asks to make these changes to it:", | ||
data, | ||
files: files(scopes, p) | ||
}); | ||
if (p.hasTransitionGeneric) out.push({ kind: 'authenticate', title: 'Authenticate', says: 'Act as the group with any service' }); | ||
else { | ||
const c = calls(scopes); | ||
if (c.length) | ||
out.push({ | ||
kind: 'authenticate', | ||
title: 'Authenticate', | ||
says: 'Act as the group with other services', | ||
about: 'The app asks to call these methods as the group:', | ||
calls: c | ||
}); | ||
} | ||
const spaces = spaceRows(scopes, group); | ||
if (spaces.length) | ||
out.push({ | ||
kind: 'spaces', | ||
title: 'Spaces', | ||
says: 'Use permissioned spaces', | ||
about: 'Spaces, such as group chats and shared calendars, hold data only their members can see. The app asks for this access:', | ||
spaces | ||
}); | ||
} | ||
return out; | ||
} | ||
| ||
/** What the PDS warns of before an app gets so much: every space anywhere, or the whole identity. */ | ||
export function warnings(scopes: string[]): string[] { | ||
const p = new ScopePermissionsTransition(scopes); | ||
const out: string[] = []; | ||
if (scopes.some((s) => { const sp = SpacePermission.fromString(s); return sp?.type === '*' && sp.authority === '*'; })) | ||
out.push('It asks for every space on the network. Only allow this for an app you trust completely.'); | ||
if (p.allowsIdentity({ attr: '*' })) out.push('It asks for full control of the group’s identity, so it could break the group’s account or take it. Only allow this for an app you trust completely.'); | ||
return out; | ||
} | ||
| ||
const bluesky = (nsid: string) => nsid === 'com.atproto.moderation.createReport' || nsid.startsWith('app.bsky.') || nsid.startsWith('chat.bsky.'); | ||
const appview = (aud: string) => aud === 'did:web:bsky.app#bsky_appview'; | ||
const PRIVATE = ['getPreferences', 'block', 'muteActor', 'muteActorList', 'muteThread', 'unmuteActor', 'unmuteActorList', 'unmuteThread', 'getMutes']; | ||
| ||
function privateBluesky(s: string) { | ||
if (s === 'transition:generic') return true; | ||
const rpc = RpcPermission.fromString(s); | ||
return !!rpc && rpc.lxm.some((m) => m === '*' || PRIVATE.some((p) => m === `app.bsky.actor.${p}` || m === `app.bsky.graph.${p}`)); | ||
} | ||
| ||
function chat(s: string) { | ||
const rpc = RpcPermission.fromString(s); | ||
return !!rpc && !appview(rpc.aud) && rpc.lxm.some((m) => m === '*' || m.startsWith('chat.bsky.')); | ||
} | ||
| ||
/** Whether every repo and rpc scope is Bluesky's own, which the Bluesky row then says alone. */ | ||
function blueskyOnly(scopes: string[], p: ScopePermissionsTransition) { | ||
if (p.allowsAccount({ attr: 'repo', action: 'manage' })) return false; | ||
let one = false; | ||
for (const s of scopes) { | ||
const rpc = RpcPermission.fromString(s); | ||
const repo = RepoPermission.fromString(s); | ||
if (rpc) { | ||
one = true; | ||
if (!appview(rpc.aud) && !rpc.lxm.every((m) => m !== '*' && bluesky(m))) return false; | ||
} else if (repo) { | ||
one = true; | ||
if (!repo.collection.every((c) => c !== '*' && bluesky(c))) return false; | ||
} | ||
} | ||
return one; | ||
} | ||
| ||
const none = (): Ticks => ({ create: false, update: false, delete: false }); | ||
| ||
/** Each collection the repo scopes name, with the writes to it; any (`*`) lends its writes to each. */ | ||
function repoData(scopes: string[]): Data[] { | ||
const by = new Map<string, Ticks>(); | ||
for (const s of scopes) { | ||
const repo = RepoPermission.fromString(s); | ||
for (const c of repo?.collection ?? []) { | ||
const t = by.get(c) ?? none(); | ||
for (const a of repo!.action) t[a] = true; | ||
by.set(c, t); | ||
} | ||
} | ||
return sorted(by); | ||
} | ||
| ||
function sorted(by: Map<string, Ticks>): Data[] { | ||
const any = by.get('*'); | ||
return [...by] | ||
.sort(([a], [b]) => a.localeCompare(b)) | ||
.map(([collection, t]) => ({ collection, ticks: { create: t.create || !!any?.create, update: t.update || !!any?.update, delete: t.delete || !!any?.delete } })); | ||
} | ||
| ||
const files = (scopes: string[], p: ScopePermissionsTransition) => p.hasTransitionGeneric || scopes.some((s) => BlobPermission.fromString(s) !== null); | ||
| ||
/** Each method the rpc scopes name, with the service it is called on, in words. */ | ||
function calls(scopes: string[]) { | ||
const by = new Map<string, Set<string>>(); | ||
for (const s of scopes) { | ||
const rpc = RpcPermission.fromString(s); | ||
if (!rpc) continue; | ||
const lxm = by.get(rpc.aud) ?? new Set(); | ||
for (const m of rpc.lxm) lxm.add(m); | ||
by.set(rpc.aud, lxm); | ||
} | ||
return [...by] | ||
.sort(([a], [b]) => a.localeCompare(b)) | ||
.flatMap(([aud, lxm]) => (lxm.has('*') ? ['*'] : [...lxm].sort()).map((method) => ({ method, service: service(aud) }))); | ||
} | ||
| ||
function service(aud: string) { | ||
if (aud === '*') return 'Any service'; | ||
if (aud.startsWith('did:web:api.bsky.app#')) return 'Bluesky'; | ||
if (aud.startsWith('did:web:api.bsky.chat#')) return 'Bluesky chat'; | ||
if (aud.startsWith('did:web:') && aud.includes('#')) return aud.slice('did:web:'.length, aud.indexOf('#')); | ||
return aud; | ||
} | ||
| ||
/** The space scopes, one row for each type, owner and key, their reads and writes merged. */ | ||
function spaceRows(scopes: string[], group: { did: string; handle: string }): Spaces[] { | ||
const rows = new Map<string, Spaces & { writes: Map<string, Ticks> }>(); | ||
for (const s of scopes) { | ||
const sp = SpacePermission.fromString(s); | ||
if (!sp) continue; | ||
const owner = sp.authority === group.did || sp.authority === 'self' ? `@${group.handle}` : sp.authority; | ||
const key = `${sp.type}|${owner}|${sp.skey}`; | ||
const row = rows.get(key) ?? { type: sp.type, owner, read: false, readSelf: false, manage: false, data: [], writes: new Map() }; | ||
rows.set(key, row); | ||
if (sp.action.includes('read')) row.read = true; | ||
if (sp.action.includes('read_self')) row.readSelf = true; | ||
if (sp.manage) row.manage = true; | ||
for (const c of sp.collection ?? []) { | ||
const t = row.writes.get(c) ?? none(); | ||
for (const a of sp.action) if (a === 'create' || a === 'update' || a === 'delete') t[a] = true; | ||
row.writes.set(c, t); | ||
} | ||
} | ||
return [...rows] | ||
.sort(([a], [b]) => a.localeCompare(b)) | ||
.map(([, { writes, ...row }]) => ({ ...row, data: sorted(writes) })); | ||
} | ||