@sovereignfs/create 0.19.0 → 0.27.1
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 +5 -3
- package/dist/create.mjs +354 -88
- package/package.json +3 -2
- package/template/app/AGENTS.md +36 -3
- package/template/app/CONTRIBUTING.md +3 -3
- package/template/app/Dockerfile +6 -5
- package/template/app/README.md +64 -32
- package/template/app/_env.example +11 -1
- package/template/app/_githooks/pre-push +2 -2
- package/template/app/_github/workflows/ci.yml +5 -4
- package/template/app/_github/workflows/publish.yml +8 -6
- package/template/app/_gitignore +4 -0
- package/template/app/app/actions.ts +34 -8
- package/template/app/app/api/auth/[...all]/route.ts +10 -0
- package/template/app/app/api/files/[id]/route.ts +4 -6
- package/template/app/app/api/files/route.ts +8 -5
- package/template/app/app/api/push/subscription/route.ts +23 -0
- package/template/app/app/api/sign-in-salt/route.ts +9 -0
- package/template/app/app/api/sovereign/account-deleted/route.ts +10 -0
- package/template/app/app/api/sovereign/backchannel-logout/route.ts +10 -0
- package/template/app/app/api/sovereign/export/route.ts +10 -0
- package/template/app/app/notifications.tsx +42 -0
- package/template/app/app/page.tsx +21 -9
- package/template/app/app/push-actions.ts +41 -0
- package/template/app/app/security/actions.ts +45 -0
- package/template/app/app/security/page.tsx +33 -0
- package/template/app/app/security/panels.tsx +50 -0
- package/template/app/app/sign-in/page.tsx +49 -0
- package/template/app/app/sign-in/panel.tsx +71 -0
- package/template/app/app/sign-out.tsx +19 -0
- package/template/app/app/sign-up/page.tsx +39 -0
- package/template/app/app/sign-up/panel.tsx +54 -0
- package/template/app/app/sw.js/route.ts +10 -2
- package/template/app/app/ui.tsx +13 -1
- package/template/app/compose.yml +3 -2
- package/template/app/instrumentation.ts +9 -1
- package/template/app/lib/account.ts +6 -0
- package/template/app/lib/crypto.ts +16 -9
- package/template/app/lib/csp.ts +5 -3
- package/template/app/lib/notices.ts +63 -0
- package/template/app/lib/push.ts +227 -0
- package/template/app/lib/session.ts +40 -0
- package/template/app/lib/sign-in-client.ts +22 -0
- package/template/app/lib/sign-in.ts +113 -0
- package/template/app/lib/sovereign.ts +7 -6
- package/template/app/migrations/002_push.sql +24 -0
- package/template/app/sovereign.manifest.json +11 -8
- package/template/encryption/_env.example +27 -0
- package/template/encryption/_skip +18 -0
- package/template/encryption/app/api/files/[id]/route.ts +45 -0
- package/template/encryption/app/api/files/route.ts +43 -0
- package/template/{app → encryption}/app/auth/[action]/route.ts +2 -1
- package/template/encryption/app/page.tsx +7 -2
- package/template/encryption/app/sign-out.tsx +21 -0
- package/template/encryption/app/ui.tsx +1 -0
- package/template/encryption/instrumentation.ts +12 -0
- package/template/encryption/lib/account.ts +6 -0
- package/template/encryption/lib/auth.ts +36 -0
- package/template/encryption/sovereign.manifest.json +4 -3
- package/template/pm/yarn/_yarnrc.yml +3 -0
- package/template/app/lib/auth.ts +0 -18
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { auth } from '@/lib/auth';
|
|
2
|
+
import { failure, files, refuseCrossOrigin, unauthorised } from '@/lib/files';
|
|
3
|
+
|
|
4
|
+
export const dynamic = 'force-dynamic';
|
|
5
|
+
|
|
6
|
+
/** The signed-in person's own files, newest first, and how much of their space they use. */
|
|
7
|
+
export async function GET() {
|
|
8
|
+
const session = await auth().getSession();
|
|
9
|
+
if (!session) return unauthorised();
|
|
10
|
+
const [list, usage] = await Promise.all([files().list(session.sub), files().usage(session.sub)]);
|
|
11
|
+
return Response.json(
|
|
12
|
+
{ files: list.map((file) => ({ ...file, url: `/api/files/${file.id}` })), usage },
|
|
13
|
+
{ headers: { 'cache-control': 'no-store' } },
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Stores a file for the signed-in person. The body is the file itself and its name is in `X-File-Name`, percent-encoded. */
|
|
18
|
+
export async function POST(request: Request) {
|
|
19
|
+
const refused = refuseCrossOrigin(request);
|
|
20
|
+
if (refused) return refused;
|
|
21
|
+
const session = await auth().getSession();
|
|
22
|
+
if (!session) return unauthorised();
|
|
23
|
+
try {
|
|
24
|
+
const length = Number(request.headers.get('content-length') ?? Number.NaN);
|
|
25
|
+
let name: string | undefined;
|
|
26
|
+
try {
|
|
27
|
+
name = decodeURIComponent(request.headers.get('x-file-name') ?? '');
|
|
28
|
+
} catch {
|
|
29
|
+
name = undefined;
|
|
30
|
+
}
|
|
31
|
+
const record = await files().put(
|
|
32
|
+
session.sub,
|
|
33
|
+
(request.body ?? new Uint8Array(0)) as unknown as AsyncIterable<Uint8Array>,
|
|
34
|
+
// Without a length the upload reserves all the room the person has left, so uploads of unknown size run one at a time.
|
|
35
|
+
{ name, ...(Number.isSafeInteger(length) && length >= 0 ? { size: length } : {}) },
|
|
36
|
+
);
|
|
37
|
+
return Response.json({ ...record, url: `/api/files/${record.id}` }, { status: 201 });
|
|
38
|
+
} catch (error) {
|
|
39
|
+
const known = failure(error);
|
|
40
|
+
if (known) return known;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// /auth/login, /auth/callback, /auth/logout, /auth/backchannel-logout
|
|
1
|
+
// /auth/login, /auth/callback, /auth/logout, /auth/backchannel-logout, /auth/account-deleted and /auth/export: the addresses in
|
|
2
|
+
// sovereign.manifest.json.
|
|
2
3
|
import { auth } from '@/lib/auth';
|
|
3
4
|
|
|
4
5
|
export const dynamic = 'force-dynamic';
|
|
@@ -3,6 +3,8 @@ import { files } from '@/lib/files';
|
|
|
3
3
|
import { keySettings } from '@/lib/keys';
|
|
4
4
|
import { deleteFile } from './actions';
|
|
5
5
|
import { Notes } from './notes';
|
|
6
|
+
import { Notifications } from './notifications';
|
|
7
|
+
import { SignOutForm } from './sign-out';
|
|
6
8
|
import { Button, Card, PageHeader } from './ui';
|
|
7
9
|
import { Upload } from './upload';
|
|
8
10
|
|
|
@@ -60,12 +62,15 @@ export default async function Home() {
|
|
|
60
62
|
</li>
|
|
61
63
|
))}
|
|
62
64
|
</ul>
|
|
65
|
+
<Card>
|
|
66
|
+
<Notifications />
|
|
67
|
+
</Card>
|
|
63
68
|
{/* Signing out ends the session, and with it the secret this browser's kept key needs: it opens nothing afterwards. */}
|
|
64
|
-
<
|
|
69
|
+
<SignOutForm>
|
|
65
70
|
<Button type="submit" variant="secondary">
|
|
66
71
|
Sign out
|
|
67
72
|
</Button>
|
|
68
|
-
</
|
|
73
|
+
</SignOutForm>
|
|
69
74
|
</main>
|
|
70
75
|
);
|
|
71
76
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { FormEvent, ReactNode } from 'react';
|
|
4
|
+
import { turnOffPush } from '@sovereignfs/pwa/client';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Signing out turns this device's notifications off first, so the next person to use it is not sent the last one's; then the form is
|
|
8
|
+
* sent as it is, to the SDK's sign-out.
|
|
9
|
+
*/
|
|
10
|
+
export function SignOutForm({ children }: { children: ReactNode }) {
|
|
11
|
+
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
|
|
12
|
+
event.preventDefault();
|
|
13
|
+
const form = event.currentTarget;
|
|
14
|
+
void turnOffPush().finally(() => form.submit());
|
|
15
|
+
};
|
|
16
|
+
return (
|
|
17
|
+
<form action="/auth/logout" method="post" onSubmit={onSubmit}>
|
|
18
|
+
{children}
|
|
19
|
+
</form>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs once when the server starts. The app applies its own migrations before it takes a request (contracts/app-manifest.md §4), then
|
|
3
|
+
* reads the push key, so a wrong one stops it here, and starts sending waiting notifications.
|
|
4
|
+
*/
|
|
5
|
+
export async function register() {
|
|
6
|
+
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
7
|
+
const { migrate } = await import('./lib/migrate');
|
|
8
|
+
await migrate();
|
|
9
|
+
const { startPushOutbox } = await import('./lib/push');
|
|
10
|
+
startPushOutbox();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { nextAuth } from '@sovereignfs/sdk/next';
|
|
2
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
3
|
+
import { db } from './db';
|
|
2
4
|
import { files } from './files';
|
|
3
5
|
import { notes } from './notes';
|
|
6
|
+
import { pushStore } from './push';
|
|
4
7
|
import { sovereign } from './sovereign';
|
|
5
8
|
|
|
6
9
|
/**
|
|
@@ -14,6 +17,39 @@ export const auth = () =>
|
|
|
14
17
|
await notes().deleteAll(sub);
|
|
15
18
|
// And the files they uploaded: their folder, which is gone once this has run, however often it does.
|
|
16
19
|
await files().removeAll(sub);
|
|
20
|
+
// And the devices they get notifications on, with what was waiting for them.
|
|
21
|
+
await pushStore().removeAccount(sub);
|
|
17
22
|
// The SDK has already ended the person's sessions and forgotten their refresh token.
|
|
18
23
|
},
|
|
24
|
+
// A person's part of their export (core-app §4.7.2): their notes as stored, still encrypted, and the keys sealed to them, so the
|
|
25
|
+
// export opens with their own keys and nobody else's.
|
|
26
|
+
onExport: {
|
|
27
|
+
prefix: '__PREFIX_LOWER__',
|
|
28
|
+
version: pkg.version,
|
|
29
|
+
handler: async ({ sub, writer }) => {
|
|
30
|
+
const notes = await db().query(
|
|
31
|
+
"SELECT id, version, encode(meta, 'base64') AS meta, encode(body, 'base64') AS body, updated_at FROM notes WHERE owner = $1 ORDER BY id",
|
|
32
|
+
[sub],
|
|
33
|
+
);
|
|
34
|
+
await writer.addRecords('apps/__PREFIX_LOWER__/records/notes.jsonl', 'notes', notes.rows, {
|
|
35
|
+
endToEnd: true,
|
|
36
|
+
});
|
|
37
|
+
const readers = await db().query(
|
|
38
|
+
'SELECT * FROM note_readers WHERE reader = $1 ORDER BY note_id',
|
|
39
|
+
[sub],
|
|
40
|
+
);
|
|
41
|
+
await writer.addRecords(
|
|
42
|
+
'apps/__PREFIX_LOWER__/records/note_readers.jsonl',
|
|
43
|
+
'note_readers',
|
|
44
|
+
readers.rows,
|
|
45
|
+
{ endToEnd: true },
|
|
46
|
+
);
|
|
47
|
+
const kept = await files().list(sub);
|
|
48
|
+
await writer.addRecords('apps/__PREFIX_LOWER__/records/files.jsonl', 'files', kept);
|
|
49
|
+
for (const record of kept) {
|
|
50
|
+
const { body } = await files().get(sub, record.id);
|
|
51
|
+
await writer.addFile(`apps/__PREFIX_LOWER__/files/${record.id}`, body, record.size);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
},
|
|
19
55
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"manifest":
|
|
2
|
+
"manifest": 5,
|
|
3
3
|
"prefix": "__PREFIX_LOWER__",
|
|
4
4
|
"name": "__TITLE__",
|
|
5
5
|
"description": "Notes kept encrypted in the browser, as a starting point for an app on a Sovereign instance",
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
"postLogout": ["/"],
|
|
13
13
|
"backChannel": "/auth/backchannel-logout",
|
|
14
14
|
"accountDeleted": "/auth/account-deleted",
|
|
15
|
+
"export": "/auth/export",
|
|
15
16
|
"scopes": ["openid", "profile", "email", "offline_access", "directory"]
|
|
16
17
|
},
|
|
17
18
|
"encryption": { "return": "/keys/return" },
|
|
18
19
|
"database": { "pooling": "transaction" },
|
|
19
20
|
"storage": true,
|
|
20
|
-
"generate": ["__PREFIX___AUTH_SECRET"],
|
|
21
|
-
"core": ">=0.
|
|
21
|
+
"generate": ["__PREFIX___AUTH_SECRET", { "name": "__PREFIX___VAPID_PRIVATE_KEY", "bytes": 32 }],
|
|
22
|
+
"core": ">=0.126.0 <1.0.0"
|
|
22
23
|
}
|
package/template/app/lib/auth.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { nextAuth } from '@sovereignfs/sdk/next';
|
|
2
|
-
import { db } from './db';
|
|
3
|
-
import { files } from './files';
|
|
4
|
-
import { sovereign } from './sovereign';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Sign-in for this app (docs/using-the-sdk.md). When a person deletes their account on the instance, core tells the app and keeps
|
|
8
|
-
* telling it until it answers 200, which the SDK sends only after this function has finished. It must be safe to run twice.
|
|
9
|
-
*/
|
|
10
|
-
export const auth = () =>
|
|
11
|
-
nextAuth(sovereign(), {
|
|
12
|
-
onAccountDeleted: async ({ sub }) => {
|
|
13
|
-
await db().query('DELETE FROM notes WHERE owner = $1', [sub]);
|
|
14
|
-
// And the files they uploaded: their folder, which is gone once this has run, however often it does.
|
|
15
|
-
await files().removeAll(sub);
|
|
16
|
-
// The SDK has already ended the person's sessions and forgotten their refresh token.
|
|
17
|
-
},
|
|
18
|
-
});
|