@sovereignfs/create 0.14.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/create.mjs +50 -28
- package/package.json +1 -1
- package/template/encryption/ENCRYPTION.md +30 -0
- package/template/encryption/app/actions.ts +20 -0
- package/template/encryption/app/api/me/keys/route.ts +21 -0
- package/template/encryption/app/api/notes/[id]/route.ts +39 -0
- package/template/encryption/app/api/notes/route.ts +36 -0
- package/template/encryption/app/api/people/[sub]/keys/route.ts +26 -0
- package/template/encryption/app/api/people/route.ts +22 -0
- package/template/encryption/app/keys/return/page.tsx +11 -0
- package/template/encryption/app/keys/return/received.tsx +36 -0
- package/template/encryption/app/notes.tsx +529 -0
- package/template/encryption/app/page.tsx +71 -0
- package/template/encryption/app/ui.tsx +14 -0
- package/template/encryption/lib/auth.ts +19 -0
- package/template/encryption/lib/keys.ts +14 -0
- package/template/encryption/lib/notes.ts +201 -0
- package/template/encryption/lib/sovereign.ts +21 -0
- package/template/encryption/migrations/001_init.sql +39 -0
- package/template/encryption/sovereign.manifest.json +21 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { db } from './db';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The notes, as the browser sends them (ENCRYPTION.md): a title and a text each encrypted with the note's own key, and that key sealed
|
|
5
|
+
* to each reader. The server checks shapes and who may do what, and opens nothing: it has no key. Every statement names the person, so
|
|
6
|
+
* someone else's note is a note that is not there.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface Reader {
|
|
10
|
+
readonly sub: string;
|
|
11
|
+
/** The note's key sealed to this reader and signed by the owner, as JSON. */
|
|
12
|
+
readonly record: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NoteRow {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly owner: string;
|
|
18
|
+
readonly version: number;
|
|
19
|
+
/** base64url. */
|
|
20
|
+
readonly meta: string;
|
|
21
|
+
readonly body: string;
|
|
22
|
+
/** The note's key sealed to the person asking. */
|
|
23
|
+
readonly record: string;
|
|
24
|
+
/** Who can read it, for the owner; empty for a note shared with the person. */
|
|
25
|
+
readonly readers: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface NoteWrite {
|
|
29
|
+
readonly version: number;
|
|
30
|
+
readonly meta: string;
|
|
31
|
+
readonly body: string;
|
|
32
|
+
readonly readers: readonly Reader[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class NoteError extends Error {
|
|
36
|
+
constructor(
|
|
37
|
+
readonly status: 400 | 404,
|
|
38
|
+
message: string,
|
|
39
|
+
) {
|
|
40
|
+
super(message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const ID = /^[a-z0-9-]{1,64}$/;
|
|
45
|
+
const BASE64URL = /^[A-Za-z0-9_-]*$/;
|
|
46
|
+
const MAX_META = 8 * 1024;
|
|
47
|
+
const MAX_BODY = 512 * 1024;
|
|
48
|
+
const MAX_READERS = 50;
|
|
49
|
+
|
|
50
|
+
const bytes = (value: unknown, max: number, what: string): Buffer => {
|
|
51
|
+
if (typeof value !== 'string' || !BASE64URL.test(value) || value.length > (max * 4) / 3 + 4) {
|
|
52
|
+
throw new NoteError(400, `${what} is not what this app stores.`);
|
|
53
|
+
}
|
|
54
|
+
return Buffer.from(value, 'base64url');
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** What a write must be: an id the browser made, a version, two encrypted values, and its readers, the owner among them. */
|
|
58
|
+
export function checkWrite(owner: string, id: string, input: unknown): NoteWrite & { id: string } {
|
|
59
|
+
if (!ID.test(id)) throw new NoteError(404, 'There is no such note.');
|
|
60
|
+
const value = (input ?? {}) as Partial<Record<keyof NoteWrite, unknown>>;
|
|
61
|
+
if (!Number.isSafeInteger(value.version) || (value.version as number) < 1) {
|
|
62
|
+
throw new NoteError(400, 'A note has a version.');
|
|
63
|
+
}
|
|
64
|
+
bytes(value.meta, MAX_META, 'The title');
|
|
65
|
+
bytes(value.body, MAX_BODY, 'The note');
|
|
66
|
+
const readers = Array.isArray(value.readers) ? (value.readers as unknown[]) : [];
|
|
67
|
+
if (readers.length === 0 || readers.length > MAX_READERS) {
|
|
68
|
+
throw new NoteError(400, 'A note is shared with 1 to 50 people.');
|
|
69
|
+
}
|
|
70
|
+
const checked = readers.map((reader) => {
|
|
71
|
+
const { sub, record } = (reader ?? {}) as { sub?: unknown; record?: unknown };
|
|
72
|
+
if (typeof sub !== 'string' || !sub || sub.length > 255)
|
|
73
|
+
throw new NoteError(400, 'A reader has no id.');
|
|
74
|
+
if (typeof record !== 'string' || record.length > 8192)
|
|
75
|
+
throw new NoteError(400, 'A key is too large.');
|
|
76
|
+
try {
|
|
77
|
+
JSON.parse(record);
|
|
78
|
+
} catch {
|
|
79
|
+
throw new NoteError(400, 'A key is not what this app stores.');
|
|
80
|
+
}
|
|
81
|
+
return { sub, record };
|
|
82
|
+
});
|
|
83
|
+
if (!checked.some((reader) => reader.sub === owner)) {
|
|
84
|
+
throw new NoteError(400, 'The owner can always read their own note.');
|
|
85
|
+
}
|
|
86
|
+
if (new Set(checked.map((reader) => reader.sub)).size !== checked.length) {
|
|
87
|
+
throw new NoteError(400, 'A reader is named twice.');
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
version: value.version as number,
|
|
92
|
+
meta: value.meta as string,
|
|
93
|
+
body: value.body as string,
|
|
94
|
+
readers: checked,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const notes = () => ({
|
|
99
|
+
/** The notes the person can read, newest first: their own, and those shared with them. */
|
|
100
|
+
async list(sub: string): Promise<NoteRow[]> {
|
|
101
|
+
const { rows } = await db().query<{
|
|
102
|
+
id: string;
|
|
103
|
+
owner: string;
|
|
104
|
+
version: number;
|
|
105
|
+
meta: Buffer;
|
|
106
|
+
body: Buffer;
|
|
107
|
+
record: string;
|
|
108
|
+
}>(
|
|
109
|
+
`SELECT n.id, n.owner, n.version, n.meta, n.body, r.record
|
|
110
|
+
FROM notes n JOIN note_readers r ON r.note_id = n.id AND r.reader = $1
|
|
111
|
+
ORDER BY n.updated_at DESC LIMIT 200`,
|
|
112
|
+
[sub],
|
|
113
|
+
);
|
|
114
|
+
const owned = rows.filter((row) => row.owner === sub).map((row) => row.id);
|
|
115
|
+
const readers = owned.length
|
|
116
|
+
? (
|
|
117
|
+
await db().query<{ note_id: string; reader: string }>(
|
|
118
|
+
'SELECT note_id, reader FROM note_readers WHERE note_id = ANY($1) ORDER BY reader',
|
|
119
|
+
[owned],
|
|
120
|
+
)
|
|
121
|
+
).rows
|
|
122
|
+
: [];
|
|
123
|
+
return rows.map((row) => ({
|
|
124
|
+
id: row.id,
|
|
125
|
+
owner: row.owner,
|
|
126
|
+
version: row.version,
|
|
127
|
+
meta: row.meta.toString('base64url'),
|
|
128
|
+
body: row.body.toString('base64url'),
|
|
129
|
+
record: row.record,
|
|
130
|
+
readers:
|
|
131
|
+
row.owner === sub ? readers.filter((r) => r.note_id === row.id).map((r) => r.reader) : [],
|
|
132
|
+
}));
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Writes a note of the person's, new or changed, with all its readers, in one transaction. A note of someone else's under the same id,
|
|
137
|
+
* or an older version than the one stored, is refused as not there.
|
|
138
|
+
*/
|
|
139
|
+
async save(owner: string, note: NoteWrite & { id: string }): Promise<void> {
|
|
140
|
+
const client = await db().connect();
|
|
141
|
+
try {
|
|
142
|
+
await client.query('BEGIN');
|
|
143
|
+
const written = await client.query(
|
|
144
|
+
`INSERT INTO notes (id, owner, version, meta, body) VALUES ($1, $2, $3, $4, $5)
|
|
145
|
+
ON CONFLICT (id) DO UPDATE SET version = EXCLUDED.version, meta = EXCLUDED.meta, body = EXCLUDED.body, updated_at = now()
|
|
146
|
+
WHERE notes.owner = EXCLUDED.owner AND notes.version <= EXCLUDED.version`,
|
|
147
|
+
[
|
|
148
|
+
note.id,
|
|
149
|
+
owner,
|
|
150
|
+
note.version,
|
|
151
|
+
Buffer.from(note.meta, 'base64url'),
|
|
152
|
+
Buffer.from(note.body, 'base64url'),
|
|
153
|
+
],
|
|
154
|
+
);
|
|
155
|
+
if (written.rowCount !== 1) throw new NoteError(404, 'There is no such note.');
|
|
156
|
+
await client.query('DELETE FROM note_readers WHERE note_id = $1', [note.id]);
|
|
157
|
+
for (const reader of note.readers) {
|
|
158
|
+
await client.query(
|
|
159
|
+
'INSERT INTO note_readers (note_id, reader, record) VALUES ($1, $2, $3)',
|
|
160
|
+
[note.id, reader.sub, reader.record],
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
await client.query('COMMIT');
|
|
164
|
+
} catch (error) {
|
|
165
|
+
await client.query('ROLLBACK').catch(() => undefined);
|
|
166
|
+
throw error;
|
|
167
|
+
} finally {
|
|
168
|
+
client.release();
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
/** Deletes a note of the person's, and every key sealed for it. */
|
|
173
|
+
async remove(owner: string, id: string): Promise<boolean> {
|
|
174
|
+
const client = await db().connect();
|
|
175
|
+
try {
|
|
176
|
+
await client.query('BEGIN');
|
|
177
|
+
const deleted = await client.query('DELETE FROM notes WHERE id = $1 AND owner = $2', [
|
|
178
|
+
id,
|
|
179
|
+
owner,
|
|
180
|
+
]);
|
|
181
|
+
if (deleted.rowCount === 1)
|
|
182
|
+
await client.query('DELETE FROM note_readers WHERE note_id = $1', [id]);
|
|
183
|
+
await client.query('COMMIT');
|
|
184
|
+
return deleted.rowCount === 1;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
await client.query('ROLLBACK').catch(() => undefined);
|
|
187
|
+
throw error;
|
|
188
|
+
} finally {
|
|
189
|
+
client.release();
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
/** Everything of a person, when their account is deleted: their notes, the keys for them, and the keys sealed to them. */
|
|
194
|
+
async deleteAll(sub: string): Promise<void> {
|
|
195
|
+
await db().query(
|
|
196
|
+
'DELETE FROM note_readers WHERE note_id IN (SELECT id FROM notes WHERE owner = $1) OR reader = $1',
|
|
197
|
+
[sub],
|
|
198
|
+
);
|
|
199
|
+
await db().query('DELETE FROM notes WHERE owner = $1', [sub]);
|
|
200
|
+
},
|
|
201
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createSovereign, type Sovereign } from '@sovereignfs/sdk';
|
|
2
|
+
import { sessionStore, tokenStore } from './stores';
|
|
3
|
+
|
|
4
|
+
let client: Sovereign | undefined;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The one client for this process. It reads __PREFIX___URL, __PREFIX___CORE_URL, __PREFIX___CORE_CLIENT_ID and __PREFIX___CORE_CLIENT_SECRET,
|
|
8
|
+
* makes no call to core, and is made when first used, so building the app needs none of them.
|
|
9
|
+
*
|
|
10
|
+
* It asks for `directory`, to find people to share a note with, and `keys`, for their public keys and the person's own (sdk §8). Core
|
|
11
|
+
* grants `keys` only to an app the operator allowed to use encryption (`encryption` in the manifest).
|
|
12
|
+
*/
|
|
13
|
+
export function sovereign(): Sovereign {
|
|
14
|
+
client ??= createSovereign({
|
|
15
|
+
prefix: '__PREFIX__',
|
|
16
|
+
sessionStore,
|
|
17
|
+
tokenStore,
|
|
18
|
+
scopes: ['openid', 'profile', 'email', 'offline_access', 'directory', 'keys'],
|
|
19
|
+
});
|
|
20
|
+
return client;
|
|
21
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
-- What the SDK needs the app to keep (docs/using-the-sdk.md §2), and the notes, kept encrypted (ENCRYPTION.md).
|
|
2
|
+
|
|
3
|
+
CREATE TABLE sv_sessions (
|
|
4
|
+
id text PRIMARY KEY,
|
|
5
|
+
kind text NOT NULL,
|
|
6
|
+
sub text,
|
|
7
|
+
sid text,
|
|
8
|
+
data jsonb NOT NULL,
|
|
9
|
+
expires_at bigint NOT NULL
|
|
10
|
+
);
|
|
11
|
+
CREATE INDEX sv_sessions_sub ON sv_sessions (sub);
|
|
12
|
+
CREATE INDEX sv_sessions_sid ON sv_sessions (sid);
|
|
13
|
+
CREATE INDEX sv_sessions_expires ON sv_sessions (expires_at);
|
|
14
|
+
|
|
15
|
+
CREATE TABLE sv_tokens (
|
|
16
|
+
sub text PRIMARY KEY,
|
|
17
|
+
sealed text NOT NULL,
|
|
18
|
+
fingerprint text NOT NULL
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- A note as the browser sent it: its title and its text, each encrypted with the note's own key. The server opens neither.
|
|
22
|
+
CREATE TABLE notes (
|
|
23
|
+
id text PRIMARY KEY,
|
|
24
|
+
owner text NOT NULL,
|
|
25
|
+
version integer NOT NULL,
|
|
26
|
+
meta bytea NOT NULL,
|
|
27
|
+
body bytea NOT NULL,
|
|
28
|
+
updated_at timestamptz NOT NULL DEFAULT now()
|
|
29
|
+
);
|
|
30
|
+
CREATE INDEX notes_owner ON notes (owner);
|
|
31
|
+
|
|
32
|
+
-- The note's key, sealed to each person who may read it, the owner included, and signed by the owner.
|
|
33
|
+
CREATE TABLE note_readers (
|
|
34
|
+
note_id text NOT NULL,
|
|
35
|
+
reader text NOT NULL,
|
|
36
|
+
record text NOT NULL,
|
|
37
|
+
PRIMARY KEY (note_id, reader)
|
|
38
|
+
);
|
|
39
|
+
CREATE INDEX note_readers_reader ON note_readers (reader);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"manifest": 4,
|
|
3
|
+
"prefix": "__PREFIX_LOWER__",
|
|
4
|
+
"name": "__TITLE__",
|
|
5
|
+
"description": "Notes kept encrypted in the browser, as a starting point for an app on a Sovereign instance",
|
|
6
|
+
"port": 3100,
|
|
7
|
+
"health": "/api/health",
|
|
8
|
+
"launch": "/",
|
|
9
|
+
"signIn": {
|
|
10
|
+
"redirect": ["/auth/callback"],
|
|
11
|
+
"postLogout": ["/"],
|
|
12
|
+
"backChannel": "/auth/backchannel-logout",
|
|
13
|
+
"accountDeleted": "/auth/account-deleted",
|
|
14
|
+
"scopes": ["openid", "profile", "email", "offline_access", "directory"]
|
|
15
|
+
},
|
|
16
|
+
"encryption": { "return": "/keys/return" },
|
|
17
|
+
"database": { "pooling": "transaction" },
|
|
18
|
+
"storage": true,
|
|
19
|
+
"generate": ["__PREFIX___AUTH_SECRET"],
|
|
20
|
+
"core": ">=0.68.0 <1.0.0"
|
|
21
|
+
}
|