Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkgs/bedside-and-away/design/identity-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ The `*` policy is the store read gate. A deny there denies the entire view; use

An invitation is `pending` until it is accepted, declined or revoked, and expires 14 days after it was made (`INVITE_TTL_MS` in `worker/api.mjs`). Expiry is derived from `created_at` rather than stored, because the `invitations.status` CHECK predates `expired`; the API reports `expired` and each invitation's `expiresAt`, and an expired invitation leaves the invitee's list. The invitation ID is its single-use token: only the signed-in account whose email it names can respond, every transition is a compare-and-set on `status='pending'` and an unexpired `created_at`, and a response to an invitation that is no longer pending (a replay, or one that expired) fails closed with 409 and its status. A new invitation to the same email replaces an expired one.

## Stores, roles and provisioning (BA-W01 L2)

Roles are `viewer`, `editor` and `administrator` (`worker/roles.mjs`). The database and earlier API store them as `reader`, `editor` and `admin`; requests take either name, and responses keep the stored `role` and add the contract `roleName`. Ownership is separate from roles. Provisioning gives each account one personal private store, whose ID derives from the account ID, and seeds the Site default shared store only for the account `DEFAULT_SHARED_OWNER` names (`none` for no account; unset keeps the first-creator rule for existing deployments). No account joins the shared store by provisioning; membership comes only from an invitation. The display name is a label: changing it renames the personal store while that store still has its automatic name, keeps a name the owner chose, and never changes an identifier.

## Guest/account proof design

`identity.mjs` supplies random UUID installation IDs and 256-bit guest-claim token primitives. Only the SHA-256 hash is persisted; token is returned once. Claims bind guest ID and store ID, expire in at most ten minutes, and reject consumed records, changed stores, invalid tokens and absent authenticated accounts. These primitives do not create sessions or authenticate accounts.
Expand All @@ -49,7 +53,7 @@ The future linking route must obtain guest proof through a secure HttpOnly/SameS

## Acceptance and remaining scope

Implemented pure logic: BA-088/089 evaluation and projection foundation, role negative tests for BA-078, proof-token foundation for BA-083/084. Integration is not established merely by these tests. BA-043 browser persistence/reset UI remains partial; BA-044 fingerprinting deliberately absent pending explicit transparent consent and minimization design; BA-045 display-name changes remain existing prototype behavior; BA-079 invitation expiry and complete lifecycle integration remain follow-up; BA-080 anonymous store provision absent; BA-081 second-person ingress not granted; BA-082 Google/password login absent; BA-083/084 full guest/account linking not implemented; BA-085 open-store gateway audience remains existing approved scope; BA-086 filtered API integration required; BA-087 Atlas explicitly deferred. Scoped editor commands, granular history projections, policy administration UI, typed note-target adapters and ACL-safe derived reports are not claimed complete.
Implemented pure logic: BA-088/089 evaluation and projection foundation, role negative tests for BA-078, proof-token foundation for BA-083/084. Integration is not established merely by these tests. BA-043 browser persistence/reset UI remains partial; BA-044 fingerprinting deliberately absent pending explicit transparent consent and minimization design; BA-045 display name is separate from identifiers (L2 above); BA-079 invitation expiry and complete lifecycle integration remain follow-up; BA-080 anonymous store provision absent; BA-081 second-person ingress not granted; BA-082 Google/password login absent; BA-083/084 full guest/account linking not implemented; BA-085 open-store gateway audience remains existing approved scope; BA-086 filtered API integration required; BA-087 Atlas explicitly deferred. Scoped editor commands, granular history projections, policy administration UI, typed note-target adapters and ACL-safe derived reports are not claimed complete.

## Rollback and migration

Expand Down
93 changes: 93 additions & 0 deletions pkgs/bedside-and-away/tests/stores.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// BA-W01 L2: store roles by their contract names, default store provisioning and the display name.
import {DatabaseSync} from 'node:sqlite';
import {readFileSync} from 'node:fs';
import assert from 'node:assert/strict';
import {api} from '../worker/api.mjs';
import {storedRole,roleName,roleAtLeast,personalStoreName,maySeedDefaultShared,STORED_ROLES} from '../worker/roles.mjs';

for(const [asked,stored] of [['viewer','reader'],['reader','reader'],['editor','editor'],['administrator','admin'],['admin','admin']])assert.equal(storedRole(asked),stored);
for(const x of ['owner','Viewer','remove','','toString','__proto__',null,undefined,1])assert.equal(storedRole(x),null);
assert.deepEqual(STORED_ROLES.map(roleName),['viewer','editor','administrator']);
assert.equal(roleName('owner'),null);assert.equal(roleName('constructor'),null);
assert.ok(roleAtLeast('admin','editor')&&roleAtLeast('editor','editor')&&!roleAtLeast('reader','editor')&&!roleAtLeast('owner','reader'));
assert.equal(personalStoreName(''),'Personal');assert.equal(personalStoreName('Kay'),'Kay personal store');
assert.ok(maySeedDefaultShared({},'a')&&maySeedDefaultShared({DEFAULT_SHARED_OWNER:' '},'a')&&maySeedDefaultShared({DEFAULT_SHARED_OWNER:'a'},'a'));
assert.ok(!maySeedDefaultShared({DEFAULT_SHARED_OWNER:'a'},'b')&&!maySeedDefaultShared({DEFAULT_SHARED_OWNER:'none'},'a'));
console.log('PASS role names, personal store naming and default shared seeding rule');

function harness(env={}){
const sql=new DatabaseSync(':memory:');
for(const f of ['0000_tracker.sql','0001_store_defaults.sql'])sql.exec(readFileSync(new URL('../drizzle/'+f,import.meta.url),'utf8'));
const db={prepare(q){let binds=[];return {bind(...v){binds=v;return this},async first(){return sql.prepare(q).get(...binds)||null},async all(){return {results:sql.prepare(q).all(...binds)}},async run(){const r=sql.prepare(q).run(...binds);return {meta:{changes:Number(r.changes)}}}}},async batch(statements){sql.exec('BEGIN');try{const results=[];for(const s of statements)results.push(await s.run());sql.exec('COMMIT');return results}catch(e){sql.exec('ROLLBACK');throw e}}};
return async(path,{user='alice',data}={})=>{const r=await api(new Request('https://app.test/api/'+path,{method:data?'POST':'GET',headers:{'oai-authenticated-user-id':user,'oai-authenticated-user-email':user+'@example.com',...(data?{origin:'https://app.test','content-type':'application/json'}:{})},...(data?{body:JSON.stringify(data)}:{})}),{...env,DB:db});return [r.status,await r.json()]};
}
const state={items:[],tags:[],people:['Shared'],notes:[],inventory:[],prefs:{},rules:{},recs:{},overrides:{}};

// Configured owner: only that account seeds the default shared store, whoever provisions first.
{
const call=harness({DEFAULT_SHARED_OWNER:'carol'});
let [code,r]=await call('provision',{user:'bob',data:{initialState:state}});
assert.equal(code,200);assert.ok(r.personalStoreId);assert.equal(r.defaultSharedStoreId,null);
assert.deepEqual(r.stores.map(s=>s.id),[r.personalStoreId]);
[code,r]=await call('provision',{user:'carol',data:{initialState:state}});
assert.equal(code,200);assert.ok(r.defaultSharedStoreId);
const shared=r.defaultSharedStoreId;
assert.equal(r.stores.find(s=>s.id===shared).roleName,'administrator');
[code,r]=await call('provision',{user:'bob',data:{initialState:state}});
assert.equal(r.defaultSharedStoreId,null,'provisioning never joins another account to the shared store');
assert.equal((await call('stores/'+shared,{user:'bob'}))[0],404);
// Contract role names are accepted on invitations and role changes; the stored names are kept.
[code,r]=await call('stores/'+shared+'/invites',{user:'carol',data:{email:'bob@example.com',role:'viewer'}});
assert.equal(code,201);assert.equal(r.invitation.role,'reader');assert.equal(r.invitation.roleName,'viewer');
assert.equal((await call('invites/'+r.invitation.id+'/respond',{user:'bob',data:{action:'accept'}}))[0],200);
[code,r]=await call('stores/'+shared,{user:'bob'});assert.equal(code,200);assert.equal(r.store.role,'reader');assert.equal(r.store.roleName,'viewer');
assert.equal((await call('stores/'+shared+'/members/bob',{user:'carol',data:{role:'administrator'}}))[0],200);
[code,r]=await call('stores/'+shared+'/access',{user:'bob'});assert.equal(code,200);
assert.deepEqual(r.members.map(m=>[m.userId,m.role,m.roleName]).sort(),[['bob','admin','administrator'],['carol','admin','administrator']]);
assert.equal(r.invitations[0].roleName,'viewer');
for(const role of ['owner','Viewer','',null])assert.equal((await call('stores/'+shared+'/members/bob',{user:'carol',data:{role}}))[0],400);
assert.equal((await call('stores/'+shared+'/invites',{user:'carol',data:{email:'dan@example.com',role:'owner'}}))[0],400);
assert.equal((await call('stores/'+shared+'/members/bob',{user:'carol',data:{role:'remove'}}))[0],200);
assert.equal((await call('stores/'+shared,{user:'bob'}))[0],404);
}
// `none`: no account seeds a shared store; each still gets one personal store.
{
const call=harness({DEFAULT_SHARED_OWNER:'none'});
const [code,r]=await call('provision',{data:{initialState:state}});
assert.equal(code,200);assert.equal(r.defaultSharedStoreId,null);assert.equal(r.stores.length,1);assert.equal(r.stores[0].type,'private');
}
// Unset keeps the prototype rule: the first account seeds it, later accounts do not join.
{
const call=harness();
const first=(await call('provision',{data:{initialState:state}}))[1];
assert.ok(first.defaultSharedStoreId);
assert.equal((await call('provision',{user:'bob',data:{initialState:state}}))[1].defaultSharedStoreId,null);
}
console.log('PASS default shared store seeded only by its configured owner, never joined by provisioning; contract role names at the API');

// The personal store's automatic name follows the display name; a chosen name stays; the ID never changes.
{
const call=harness();
let r=(await call('provision',{data:{initialState:state}}))[1];
const personal=r.personalStoreId,named=()=>call('bootstrap').then(([,b])=>b.stores.find(s=>s.id===personal));
assert.equal((await named()).name,'Personal');
await call('settings',{data:{nickname:'Kay'}});
assert.equal((await named()).name,'Kay personal store');
await call('settings',{data:{nickname:'Kay R'}});
assert.equal((await named()).name,'Kay R personal store');
await call('settings',{data:{nickname:''}});
assert.equal((await named()).name,'Personal');
assert.equal((await call('stores/'+personal+'/settings',{data:{name:'My notes'}}))[0],200);
await call('settings',{data:{nickname:'Lee'}});
const kept=await named();assert.equal(kept.name,'My notes');assert.equal(kept.id,personal);
assert.equal((await call('bootstrap'))[1].personalStoreId,personal);
// Another account's nickname never renames this store.
await call('provision',{user:'bob',data:{initialState:state}});
await call('settings',{user:'bob',data:{nickname:'Personal'}});
assert.equal((await named()).name,'My notes');
// Settings without a nickname change leave the name alone.
await call('stores/'+personal+'/settings',{data:{name:'Lee personal store'}});
await call('settings',{data:{showHidden:true}});
assert.equal((await named()).name,'Lee personal store');
}
console.log('PASS display name renames the personal store label only, never its identifier, and never a chosen name');
2 changes: 1 addition & 1 deletion pkgs/bedside-and-away/worker/BACKEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Local checks:

## V2 contract and limits

POST /api/provision {initialState} creates an empty personal store per authenticated account and one Site default shared store seeded by its first creator, idempotently. The display name is a nickname or Personal; identity is server-derived, never a browser UUID. Subsequent callers do not join the default shared store. Bootstrap includes personalStoreId and defaultSharedStoreId only if accessible. Default audience types are locked; names remain editable.
POST /api/provision {initialState} creates an empty personal store per authenticated account and, idempotently, one Site default shared store. `DEFAULT_SHARED_OWNER` sets who seeds the shared store: unset keeps the prototype rule (the first account to provision), `none` seeds none, any other value is the one account ID that may. The personal store is named from the display name (nickname, or Personal) and follows it while it keeps that automatic name; identity is server-derived, never a browser UUID. Provisioning never joins an account to the default shared store. Bootstrap includes personalStoreId and defaultSharedStoreId only if accessible. Default audience types are locked; names remain editable.

Open is an application audience, not public site access. Open stores start empty with their own categories. Signed-in Site visitors can read current visible items/tags, but unjoined readers get no notes, inventory, research, policy state or history. Only members with editor/admin may write. Conversion into/out of open is blocked to avoid exposing historical data. Archived open stores are inaccessible to nonmembers.

Expand Down
Loading