// Grain's permission set (social.grain.authFull, as its app file names it) // against the reference PDS: carol signs in asking for the set, which the // PDS resolves from lexicons.test (harness/pds.mjs) and grants expanded; // a write of a collection it grants lands, one it does not is refused by // the PDS; then the set is published again with that collection, the // PDS's copy of it aged past its five minutes, and the same write lands // with the same sign-in, its token refreshed, nobody signing in again. import Database from 'better-sqlite3'; import { readdirSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { expect, mcpCall, mcpToken, test, who, world } from './pages.mjs'; import { root } from './world.mjs'; test.describe.configure({ mode: 'serial' }); const SET = 'social.grain.authFull'; const FOLLOW = 'social.grain.graph.follow'; const published = JSON.parse(readFileSync(join(root, 'crates/hud-core/src/testdata/grain/social.grain.authFull.json'), 'utf8')); /// Grain's set published again by lexicons.test, its repo permission /// granting `collections`; the PDS's copy of it then aged past its five /// minutes, so the next to read it resolves it again. async function publish(collections) { const { did, password } = world.lexicons; const session = await fetch(`${world.pds}/xrpc/com.atproto.server.createSession`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ identifier: did, password }), }).then((r) => r.json()); const doc = structuredClone(published); doc.defs.main.permissions.find((p) => p.resource === 'repo').collection = collections; const put = await fetch(`${world.pds}/xrpc/com.atproto.repo.putRecord`, { method: 'POST', headers: { 'content-type': 'application/json', authorization: `Bearer ${session.accessJwt}` }, body: JSON.stringify({ repo: did, collection: 'com.atproto.lexicon.schema', rkey: SET, record: { $type: 'com.atproto.lexicon.schema', ...doc } }), }); expect(put.status, await put.clone().text()).toBe(200); const db = new Database(world.lexicons.db); db.prepare('UPDATE lexicon SET updatedAt = ? WHERE nsid = ?').run('1970-01-01T00:00:00.000Z', SET); db.close(); } /// HUD's MCP sessions of carol's, as kept on disk: this flow's alone, as /// the flows after it sign her in again. const sessions = () => { const dir = join(world.dataDir, 'oauth/sessions'); return readdirSync(dir) .map((f) => JSON.parse(readFileSync(join(dir, f), 'utf8'))) .filter((s) => s.did === who.carol.did && s.kind === 'mcp'); }; const all = published.defs.main.permissions.find((p) => p.resource === 'repo').collection; const now = () => new Date().toISOString(); const follow = () => ({ collection: FOLLOW, record: { subject: who.alice.did, createdAt: now() } }); const text = (result) => result.content.map((c) => c.text).join('\n'); /** @type {string} */ let token; test.afterAll(() => publish(all)); test('carol signs in asking for the set, granted it expanded', async ({ browser }) => { await publish(all.filter((c) => c !== FOLLOW)); const context = await browser.newContext(); token = await mcpToken(context, 'carol'); await context.close(); const [s] = sessions(); expect(s.asked).toMatch(/^atproto include:social\.grain\.authFull\?aud=did:web:grain\.social%23grain_appview /); expect(s.scope).not.toContain('include:'); expect(s.scope).toContain('collection=social.grain.favorite'); expect(s.scope).not.toContain(`collection=${FOLLOW}`); }); test('a write the set grants lands; one it does not is refused by the PDS', async () => { const favorite = await mcpCall(token, 'create_record', { collection: 'social.grain.favorite', record: { subject: `at://${who.alice.did}/social.grain.gallery/3kperm`, createdAt: now() }, }); expect(favorite.isError ?? false, text(favorite)).toBe(false); const refused = await mcpCall(token, 'create_record', follow()); expect(refused.isError, text(refused)).toBe(true); expect(text(refused)).toContain(`Your sign-in does not allow repo:${FOLLOW}`); }); test('the set grown reaches the same sign-in at its next refresh', async () => { await publish(all); const made = await mcpCall(token, 'create_record', follow()); expect(made.isError ?? false, text(made)).toBe(false); const kept = sessions(); expect(kept).toHaveLength(1); expect(kept[0].scope).toContain(`collection=${FOLLOW}`); });