@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,42 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState, useTransition } from 'react';
|
|
4
|
+
import { usePush } from '@sovereignfs/pwa/client';
|
|
5
|
+
import { Button, PushControl } from './ui';
|
|
6
|
+
import { sendTestNotification } from './push-actions';
|
|
7
|
+
|
|
8
|
+
/** Notifications on this device (@sovereignfs/pwa's usePush and @sovereignfs/ui's PushControl), and a test to see them arrive. */
|
|
9
|
+
export function Notifications() {
|
|
10
|
+
const push = usePush();
|
|
11
|
+
const [sending, start] = useTransition();
|
|
12
|
+
const [result, setResult] = useState<string | null>(null);
|
|
13
|
+
return (
|
|
14
|
+
<PushControl
|
|
15
|
+
appName="__TITLE__"
|
|
16
|
+
state={push.state}
|
|
17
|
+
working={push.working}
|
|
18
|
+
error={push.error}
|
|
19
|
+
onTurnOn={() => void push.turnOn()}
|
|
20
|
+
onTurnOff={() => void push.turnOff()}
|
|
21
|
+
>
|
|
22
|
+
{push.state === 'on' ? (
|
|
23
|
+
<>
|
|
24
|
+
<Button
|
|
25
|
+
type="button"
|
|
26
|
+
size="sm"
|
|
27
|
+
variant="secondary"
|
|
28
|
+
disabled={sending}
|
|
29
|
+
onClick={() =>
|
|
30
|
+
start(async () => {
|
|
31
|
+
setResult((await sendTestNotification()).message);
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
>
|
|
35
|
+
{sending ? 'Sending…' : 'Send me a test notification'}
|
|
36
|
+
</Button>
|
|
37
|
+
{result ? <span role="status">{result}</span> : null}
|
|
38
|
+
</>
|
|
39
|
+
) : null}
|
|
40
|
+
</PushControl>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { requireSignedIn, userOfSession } from '@/lib/session';
|
|
2
2
|
import { db } from '@/lib/db';
|
|
3
3
|
import { files } from '@/lib/files';
|
|
4
4
|
import { addNote, deleteFile, deleteNote } from './actions';
|
|
5
|
-
import {
|
|
5
|
+
import { Notifications } from './notifications';
|
|
6
|
+
import { SignOutForm } from './sign-out';
|
|
7
|
+
import { Button, ButtonLink, Card, Input, PageHeader } from './ui';
|
|
6
8
|
import { Upload } from './upload';
|
|
7
9
|
|
|
8
10
|
/** A size as a person reads it: 512 KB, 3.4 MB, 200 GB. */
|
|
@@ -21,15 +23,19 @@ function size(bytes: number): string {
|
|
|
21
23
|
export const dynamic = 'force-dynamic';
|
|
22
24
|
|
|
23
25
|
export default async function Home() {
|
|
24
|
-
// Sends a person to sign in
|
|
25
|
-
const session = await
|
|
26
|
+
// Sends a person to sign in, and back here.
|
|
27
|
+
const session = await requireSignedIn('/');
|
|
26
28
|
const { rows } = await db().query<{ id: string; body: string }>(
|
|
27
29
|
'SELECT id, body FROM notes WHERE owner = $1 ORDER BY id DESC LIMIT 100',
|
|
28
|
-
[session.
|
|
30
|
+
[session.user.id],
|
|
29
31
|
);
|
|
30
32
|
// Their files, kept the suite's way: a folder for this person and a quota for this person (lib/files.ts).
|
|
31
|
-
const [kept, usage] = await Promise.all([
|
|
32
|
-
|
|
33
|
+
const [kept, usage] = await Promise.all([
|
|
34
|
+
files().list(session.user.id),
|
|
35
|
+
files().usage(session.user.id),
|
|
36
|
+
]);
|
|
37
|
+
const user = await userOfSession(session);
|
|
38
|
+
const name = user.name ?? user.email;
|
|
33
39
|
|
|
34
40
|
return (
|
|
35
41
|
<main>
|
|
@@ -82,11 +88,17 @@ export default async function Home() {
|
|
|
82
88
|
</li>
|
|
83
89
|
))}
|
|
84
90
|
</ul>
|
|
85
|
-
<
|
|
91
|
+
<Card>
|
|
92
|
+
<Notifications />
|
|
93
|
+
</Card>
|
|
94
|
+
<SignOutForm>
|
|
95
|
+
<ButtonLink href="/security" variant="secondary">
|
|
96
|
+
Security
|
|
97
|
+
</ButtonLink>{' '}
|
|
86
98
|
<Button type="submit" variant="secondary">
|
|
87
99
|
Sign out
|
|
88
100
|
</Button>
|
|
89
|
-
</
|
|
101
|
+
</SignOutForm>
|
|
90
102
|
</main>
|
|
91
103
|
);
|
|
92
104
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import { enqueuePush } from '@sovereignfs/push';
|
|
4
|
+
import { currentAccount } from '@/lib/account';
|
|
5
|
+
import { db } from '@/lib/db';
|
|
6
|
+
import { appPush, pushStore, sendDuePushes } from '@/lib/push';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Sends the signed-in person a test notification on every device they turned notifications on in. It shows the whole path: the push is
|
|
10
|
+
* queued in a transaction, as a push raised by a change would be, and sent from the outbox after it commits.
|
|
11
|
+
*/
|
|
12
|
+
export async function sendTestNotification(): Promise<{ ok: boolean; message: string }> {
|
|
13
|
+
const account = await currentAccount();
|
|
14
|
+
if (!account) return { ok: false, message: 'Sign in again to send a test notification.' };
|
|
15
|
+
if (!appPush()) return { ok: false, message: 'This app doesn’t send notifications.' };
|
|
16
|
+
const client = await db().connect();
|
|
17
|
+
let queued = 0;
|
|
18
|
+
try {
|
|
19
|
+
await client.query('BEGIN');
|
|
20
|
+
// Your change would be written here, on the same client: if it fails, the push is never sent.
|
|
21
|
+
queued = await enqueuePush(pushStore(client), {
|
|
22
|
+
account,
|
|
23
|
+
message: {
|
|
24
|
+
title: 'Test notification',
|
|
25
|
+
body: 'Notifications from __TITLE__ work on this device.',
|
|
26
|
+
path: '/',
|
|
27
|
+
},
|
|
28
|
+
now: Date.now(),
|
|
29
|
+
ttlMs: 10 * 60_000,
|
|
30
|
+
});
|
|
31
|
+
await client.query('COMMIT');
|
|
32
|
+
} catch (error) {
|
|
33
|
+
await client.query('ROLLBACK').catch(() => undefined);
|
|
34
|
+
throw error;
|
|
35
|
+
} finally {
|
|
36
|
+
client.release();
|
|
37
|
+
}
|
|
38
|
+
if (queued === 0) return { ok: false, message: 'Turn notifications on for this device first.' };
|
|
39
|
+
await sendDuePushes();
|
|
40
|
+
return { ok: true, message: 'Sent. It should arrive in a moment.' };
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import { headers } from 'next/headers';
|
|
4
|
+
import type { ActionResult } from '@sovereignfs/ui';
|
|
5
|
+
import { endOtherSessions, endSession } from '@sovereignfs/auth/server';
|
|
6
|
+
import { getSession } from '@/lib/session';
|
|
7
|
+
import { signIn } from '@/lib/sign-in';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Ends one of the signed-in person's sessions. A server action is a public endpoint whatever the page around it checks, so it reads
|
|
11
|
+
* the session itself, and acts only on the person's own: the session is named by id, and one that is not theirs is not found.
|
|
12
|
+
*/
|
|
13
|
+
export async function endSessionAction(
|
|
14
|
+
sessionId: string,
|
|
15
|
+
_previous: ActionResult | null,
|
|
16
|
+
_form: FormData,
|
|
17
|
+
): Promise<ActionResult> {
|
|
18
|
+
const session = await getSession();
|
|
19
|
+
if (!session) return { ok: false, error: 'Sign in again to do that.' };
|
|
20
|
+
const { auth, database } = await signIn();
|
|
21
|
+
const ended = await endSession(auth, database, session.user.id, sessionId, await headers());
|
|
22
|
+
return ended ? { ok: true } : { ok: false, error: 'That session has already ended.' };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Ends every session of the person's but this one. */
|
|
26
|
+
export async function endOtherSessionsAction(
|
|
27
|
+
_previous: ActionResult | null,
|
|
28
|
+
_form: FormData,
|
|
29
|
+
): Promise<ActionResult> {
|
|
30
|
+
const session = await getSession();
|
|
31
|
+
if (!session) return { ok: false, error: 'Sign in again to do that.' };
|
|
32
|
+
const { auth, database } = await signIn();
|
|
33
|
+
const count = await endOtherSessions(
|
|
34
|
+
auth,
|
|
35
|
+
database,
|
|
36
|
+
session.user.id,
|
|
37
|
+
session.session.id,
|
|
38
|
+
await headers(),
|
|
39
|
+
);
|
|
40
|
+
return {
|
|
41
|
+
ok: true,
|
|
42
|
+
message:
|
|
43
|
+
count === 1 ? 'Signed out of 1 other session.' : `Signed out of ${count} other sessions.`,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { listSessions } from '@sovereignfs/auth/server';
|
|
2
|
+
import { requireSignedIn } from '@/lib/session';
|
|
3
|
+
import { signIn, signInSettings } from '@/lib/sign-in';
|
|
4
|
+
import { ButtonLink, PageHeader } from '../ui';
|
|
5
|
+
import { Password, Sessions } from './panels';
|
|
6
|
+
|
|
7
|
+
export const dynamic = 'force-dynamic';
|
|
8
|
+
|
|
9
|
+
/** A person's password and sessions (auth.md §7). An account that signs in through core has no password here: core is its way in. */
|
|
10
|
+
export default async function SecurityPage() {
|
|
11
|
+
const session = await requireSignedIn('/security');
|
|
12
|
+
const { auth, database } = await signIn();
|
|
13
|
+
const ctx = await auth.$context;
|
|
14
|
+
const accounts = await ctx.internalAdapter.findAccounts(session.user.id);
|
|
15
|
+
const hasPassword = accounts.some((account) => account.providerId === 'credential');
|
|
16
|
+
const sessions = await listSessions(database, session.user.id, session.session.id);
|
|
17
|
+
return (
|
|
18
|
+
<main>
|
|
19
|
+
<PageHeader
|
|
20
|
+
title="Security"
|
|
21
|
+
action={
|
|
22
|
+
<ButtonLink href="/" variant="secondary">
|
|
23
|
+
Back
|
|
24
|
+
</ButtonLink>
|
|
25
|
+
}
|
|
26
|
+
/>
|
|
27
|
+
{hasPassword ? (
|
|
28
|
+
<Password address={session.user.email} policy={signInSettings().passwordPolicy} />
|
|
29
|
+
) : null}
|
|
30
|
+
<Sessions sessions={sessions} />
|
|
31
|
+
</main>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { ChangePasswordForm, SessionsPanel, type SessionRow } from '@sovereignfs/ui';
|
|
5
|
+
import { changePassword } from '@sovereignfs/auth/client';
|
|
6
|
+
import { newPasswordProblem, type PasswordPolicy } from '@sovereignfs/auth';
|
|
7
|
+
import { authClient, deriveForSignIn } from '@/lib/sign-in-client';
|
|
8
|
+
import { endOtherSessionsAction, endSessionAction } from './actions';
|
|
9
|
+
|
|
10
|
+
/** Changing the password: both are sent as sign-in values made with the account's address, and every other session ends. */
|
|
11
|
+
export function Password({ address, policy }: { address: string; policy: PasswordPolicy }) {
|
|
12
|
+
const [error, setError] = useState<string | undefined>();
|
|
13
|
+
const [done, setDone] = useState(false);
|
|
14
|
+
const [pending, setPending] = useState(false);
|
|
15
|
+
|
|
16
|
+
async function onSubmit(values: { current: string; next: string }) {
|
|
17
|
+
setError(undefined);
|
|
18
|
+
setDone(false);
|
|
19
|
+
const problem = newPasswordProblem(values.next, undefined, policy);
|
|
20
|
+
if (problem) {
|
|
21
|
+
setError(problem);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
setPending(true);
|
|
25
|
+
const result = await changePassword(authClient, deriveForSignIn, { ...values, address });
|
|
26
|
+
setPending(false);
|
|
27
|
+
if (!result.changed) setError(result.message);
|
|
28
|
+
else setDone(true);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<ChangePasswordForm
|
|
33
|
+
error={error}
|
|
34
|
+
done={done}
|
|
35
|
+
pending={pending}
|
|
36
|
+
onSubmit={(v) => void onSubmit(v)}
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The person's sessions, each ended by this app's server action. */
|
|
42
|
+
export function Sessions({ sessions }: { sessions: SessionRow[] }) {
|
|
43
|
+
return (
|
|
44
|
+
<SessionsPanel
|
|
45
|
+
sessions={sessions}
|
|
46
|
+
endSession={endSessionAction}
|
|
47
|
+
endOthers={endOtherSessionsAction}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { redirect } from 'next/navigation';
|
|
2
|
+
import { safeReturnTo } from '@sovereignfs/auth';
|
|
3
|
+
import { sendOnIfSignedIn } from '@/lib/session';
|
|
4
|
+
import { signIn, waysIn } from '@/lib/sign-in';
|
|
5
|
+
import { AuthCard, BrandBadge } from '../ui';
|
|
6
|
+
import { SignInPanel } from './panel';
|
|
7
|
+
|
|
8
|
+
export const dynamic = 'force-dynamic';
|
|
9
|
+
|
|
10
|
+
/** What a sign-in through core that did not work comes back with (auth.md §8), in words a person can act on. */
|
|
11
|
+
const PROBLEMS: Record<string, string> = {
|
|
12
|
+
sovereign: 'Signing in through Sovereign did not work. Try again.',
|
|
13
|
+
sovereign_email: 'Sovereign did not share your email address, which this app needs.',
|
|
14
|
+
sovereign_refused: 'This account cannot sign in here.',
|
|
15
|
+
account_not_linked:
|
|
16
|
+
'An account of this app already has that email address. Sign in with its password instead.',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default async function SignInPage({
|
|
20
|
+
searchParams,
|
|
21
|
+
}: {
|
|
22
|
+
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
|
23
|
+
}) {
|
|
24
|
+
const query = await searchParams;
|
|
25
|
+
const returnTo = safeReturnTo(query.returnTo);
|
|
26
|
+
await sendOnIfSignedIn(returnTo);
|
|
27
|
+
const ways = waysIn();
|
|
28
|
+
const error = typeof query.error === 'string' ? PROBLEMS[query.error] : undefined;
|
|
29
|
+
// With core the only way in, there is nothing to choose: the person goes on to core, and comes back here only with a problem to show.
|
|
30
|
+
if (!ways.local && ways.core && error === undefined && query.error === undefined) {
|
|
31
|
+
redirect(`/api/auth/sign-in/sovereign?returnTo=${encodeURIComponent(returnTo)}`);
|
|
32
|
+
}
|
|
33
|
+
const { database } = await signIn();
|
|
34
|
+
// Sign-up is open, or closed to everyone but the first account (auth.md §5).
|
|
35
|
+
const first = (await database.run('SELECT 1 AS found FROM users LIMIT 1')).rows.length === 0;
|
|
36
|
+
return (
|
|
37
|
+
<main>
|
|
38
|
+
<AuthCard brand={<BrandBadge name="__TITLE__" />} title="Sign in to __TITLE__" legal={false}>
|
|
39
|
+
<SignInPanel
|
|
40
|
+
returnTo={returnTo}
|
|
41
|
+
local={ways.local}
|
|
42
|
+
core={ways.core !== undefined}
|
|
43
|
+
signUp={ways.local && (ways.signUp === 'open' || first)}
|
|
44
|
+
problem={error}
|
|
45
|
+
/>
|
|
46
|
+
</AuthCard>
|
|
47
|
+
</main>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { AuthError, AuthNotice, ButtonLink, SignInForm } from '@sovereignfs/ui';
|
|
5
|
+
import { signInWithPassword } from '@sovereignfs/auth/client';
|
|
6
|
+
import { authClient, deriveForSignIn } from '@/lib/sign-in-client';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The sign-in: the design system's form and the sign-in package's flow (auth.md §7), with "Continue with Sovereign" when the app signs
|
|
10
|
+
* in through core. The password never leaves the browser: what is sent is the sign-in value made from it.
|
|
11
|
+
*/
|
|
12
|
+
export function SignInPanel({
|
|
13
|
+
returnTo,
|
|
14
|
+
local,
|
|
15
|
+
core,
|
|
16
|
+
signUp,
|
|
17
|
+
problem,
|
|
18
|
+
}: {
|
|
19
|
+
returnTo: string;
|
|
20
|
+
local: boolean;
|
|
21
|
+
core: boolean;
|
|
22
|
+
signUp: boolean;
|
|
23
|
+
problem: string | undefined;
|
|
24
|
+
}) {
|
|
25
|
+
const [error, setError] = useState(problem);
|
|
26
|
+
const [pending, setPending] = useState(false);
|
|
27
|
+
const throughCore = core ? (
|
|
28
|
+
<ButtonLink
|
|
29
|
+
href={`/api/auth/sign-in/sovereign?returnTo=${encodeURIComponent(returnTo)}`}
|
|
30
|
+
variant="secondary"
|
|
31
|
+
>
|
|
32
|
+
Continue with Sovereign
|
|
33
|
+
</ButtonLink>
|
|
34
|
+
) : null;
|
|
35
|
+
|
|
36
|
+
async function onSubmit(values: { email: string; password: string }) {
|
|
37
|
+
setPending(true);
|
|
38
|
+
setError(undefined);
|
|
39
|
+
const result = await signInWithPassword(authClient, deriveForSignIn, values);
|
|
40
|
+
if (result.outcome === 'signed-in') {
|
|
41
|
+
// A full navigation, so the next page is rendered with the new session cookie.
|
|
42
|
+
window.location.assign(returnTo);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
setPending(false);
|
|
46
|
+
if (result.outcome === 'refused') setError(result.message);
|
|
47
|
+
else if (result.outcome === 'not-ready')
|
|
48
|
+
setError('This page could not get ready. Reload it and try again.');
|
|
49
|
+
// This app offers no second step and verifies no address, so neither is asked of an account of its own.
|
|
50
|
+
else setError('Signing in did not work. Try again.');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!local) {
|
|
54
|
+
return (
|
|
55
|
+
<>
|
|
56
|
+
{error ? <AuthError>{error}</AuthError> : null}
|
|
57
|
+
{throughCore ?? <AuthNotice tone="warning">This app has no way to sign in.</AuthNotice>}
|
|
58
|
+
</>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return (
|
|
62
|
+
<SignInForm
|
|
63
|
+
appName="__TITLE__"
|
|
64
|
+
error={error}
|
|
65
|
+
pending={pending}
|
|
66
|
+
onSubmit={(values) => void onSubmit(values)}
|
|
67
|
+
otherWays={throughCore}
|
|
68
|
+
signUpHref={signUp ? `/sign-up?returnTo=${encodeURIComponent(returnTo)}` : undefined}
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from 'react';
|
|
4
|
+
import { turnOffPush } from '@sovereignfs/pwa/client';
|
|
5
|
+
import { signOut } from './actions';
|
|
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. */
|
|
8
|
+
export function SignOutForm({ children }: { children: ReactNode }) {
|
|
9
|
+
return (
|
|
10
|
+
<form
|
|
11
|
+
action={async () => {
|
|
12
|
+
await turnOffPush();
|
|
13
|
+
await signOut();
|
|
14
|
+
}}
|
|
15
|
+
>
|
|
16
|
+
{children}
|
|
17
|
+
</form>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describePasswordPolicy, safeReturnTo } from '@sovereignfs/auth';
|
|
2
|
+
import { sendOnIfSignedIn } from '@/lib/session';
|
|
3
|
+
import { signIn, signInSettings, waysIn } from '@/lib/sign-in';
|
|
4
|
+
import { AuthCard, AuthFooter, AuthLink, AuthNotice, BrandBadge } from '../ui';
|
|
5
|
+
import { SignUpPanel } from './panel';
|
|
6
|
+
|
|
7
|
+
export const dynamic = 'force-dynamic';
|
|
8
|
+
|
|
9
|
+
export default async function SignUpPage({
|
|
10
|
+
searchParams,
|
|
11
|
+
}: {
|
|
12
|
+
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
|
13
|
+
}) {
|
|
14
|
+
const returnTo = safeReturnTo((await searchParams).returnTo);
|
|
15
|
+
await sendOnIfSignedIn(returnTo);
|
|
16
|
+
const ways = waysIn();
|
|
17
|
+
const { database } = await signIn();
|
|
18
|
+
const first = (await database.run('SELECT 1 AS found FROM users LIMIT 1')).rows.length === 0;
|
|
19
|
+
const open = ways.local && (ways.signUp === 'open' || first);
|
|
20
|
+
const policy = signInSettings().passwordPolicy;
|
|
21
|
+
return (
|
|
22
|
+
<main>
|
|
23
|
+
<AuthCard brand={<BrandBadge name="__TITLE__" />} title="Create your account" legal={false}>
|
|
24
|
+
{open ? (
|
|
25
|
+
<SignUpPanel returnTo={returnTo} policy={policy} hint={describePasswordPolicy(policy)} />
|
|
26
|
+
) : (
|
|
27
|
+
<>
|
|
28
|
+
<AuthNotice tone="warning">Making an account here is closed.</AuthNotice>
|
|
29
|
+
<AuthFooter>
|
|
30
|
+
<AuthLink href={`/sign-in?returnTo=${encodeURIComponent(returnTo)}`}>
|
|
31
|
+
Back to sign in
|
|
32
|
+
</AuthLink>
|
|
33
|
+
</AuthFooter>
|
|
34
|
+
</>
|
|
35
|
+
)}
|
|
36
|
+
</AuthCard>
|
|
37
|
+
</main>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { SignUpForm, type SignUpValues } from '@sovereignfs/ui';
|
|
5
|
+
import { signUp, signUpProblem } from '@sovereignfs/auth/client';
|
|
6
|
+
import type { PasswordPolicy } from '@sovereignfs/auth';
|
|
7
|
+
import { authClient, deriveForSignIn } from '@/lib/sign-in-client';
|
|
8
|
+
|
|
9
|
+
/** Making an account of the app's own: the design system's form and the sign-in package's flow (auth.md §7). */
|
|
10
|
+
export function SignUpPanel({
|
|
11
|
+
returnTo,
|
|
12
|
+
policy,
|
|
13
|
+
hint,
|
|
14
|
+
}: {
|
|
15
|
+
returnTo: string;
|
|
16
|
+
policy: PasswordPolicy;
|
|
17
|
+
hint: string;
|
|
18
|
+
}) {
|
|
19
|
+
const [error, setError] = useState<string | undefined>();
|
|
20
|
+
const [pending, setPending] = useState(false);
|
|
21
|
+
|
|
22
|
+
async function onSubmit(values: SignUpValues) {
|
|
23
|
+
setError(undefined);
|
|
24
|
+
// Said at once, before anything is derived or sent.
|
|
25
|
+
const problem = signUpProblem({ ...values, policy });
|
|
26
|
+
if (problem) {
|
|
27
|
+
setError(problem);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
setPending(true);
|
|
31
|
+
const result = await signUp(authClient, deriveForSignIn, { ...values, callbackURL: returnTo });
|
|
32
|
+
if (result.outcome === 'signed-in') {
|
|
33
|
+
window.location.assign(returnTo);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
setPending(false);
|
|
37
|
+
setError(
|
|
38
|
+
result.outcome === 'refused'
|
|
39
|
+
? result.message
|
|
40
|
+
: 'This page could not get ready. Reload it and try again.',
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<SignUpForm
|
|
46
|
+
appName="__TITLE__"
|
|
47
|
+
passwordHint={hint}
|
|
48
|
+
error={error}
|
|
49
|
+
pending={pending}
|
|
50
|
+
onSubmit={(values) => void onSubmit(values)}
|
|
51
|
+
signInHref={`/sign-in?returnTo=${encodeURIComponent(returnTo)}`}
|
|
52
|
+
/>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { serviceWorkerResponse, serviceWorkerSource } from '@sovereignfs/pwa';
|
|
4
|
+
import { appPush } from '@/lib/push';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* The service worker, served from the root so it controls the whole app (`@sovereignfs/pwa`). It keeps only what is the same for
|
|
7
|
-
* everyone, never a page of a person. Its version is the build's, so a new build is a new worker, announced to an open page.
|
|
8
|
+
* everyone, never a page of a person. Its version is the build's, so a new build is a new worker, announced to an open page. When the
|
|
9
|
+
* app has a push key, it also shows notifications, and opening one goes to a page of this app.
|
|
8
10
|
*/
|
|
9
11
|
export const dynamic = 'force-dynamic';
|
|
10
12
|
|
|
@@ -36,5 +38,11 @@ function builtFiles(): string[] {
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
export function GET() {
|
|
39
|
-
return serviceWorkerResponse(
|
|
41
|
+
return serviceWorkerResponse(
|
|
42
|
+
serviceWorkerSource({
|
|
43
|
+
version: buildId(),
|
|
44
|
+
precache: builtFiles(),
|
|
45
|
+
...(appPush() ? { push: { name: '__TITLE__', icon: '/pwa-icon/192' } } : {}),
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
40
48
|
}
|
package/template/app/app/ui.tsx
CHANGED
|
@@ -2,4 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
// `@sovereignfs/ui` is one bundle with components that use React context, so a Server Component cannot import it directly. It is
|
|
4
4
|
// re-exported from here, a Client Component module, and pages import the parts they use from "./ui". They still render on the server.
|
|
5
|
-
export {
|
|
5
|
+
export {
|
|
6
|
+
AuthCard,
|
|
7
|
+
AuthFooter,
|
|
8
|
+
AuthLink,
|
|
9
|
+
AuthNotice,
|
|
10
|
+
BrandBadge,
|
|
11
|
+
Button,
|
|
12
|
+
ButtonLink,
|
|
13
|
+
Card,
|
|
14
|
+
Input,
|
|
15
|
+
PageHeader,
|
|
16
|
+
PushControl,
|
|
17
|
+
} from '@sovereignfs/ui';
|
package/template/app/compose.yml
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
# cp .env.example .env # point it at a core you can reach, and give it its client there
|
|
4
4
|
# docker compose up --build
|
|
5
5
|
#
|
|
6
|
-
#
|
|
6
|
+
# People sign in with the app's own accounts (.env's __PREFIX___AUTH_LOCAL), or through a core too: set __PREFIX___CORE_URL,
|
|
7
|
+
# __PREFIX___CORE_CLIENT_ID and __PREFIX___CORE_CLIENT_SECRET in .env. People's
|
|
7
8
|
# files are kept in the volume `files`, mounted where an instance mounts the app's own folder, so the app's code is the same in both
|
|
8
9
|
# (contracts/app-manifest.md §2.7).
|
|
9
10
|
services:
|
|
@@ -11,7 +12,7 @@ services:
|
|
|
11
12
|
build:
|
|
12
13
|
context: .
|
|
13
14
|
args:
|
|
14
|
-
# The manifest's port, which the image listens on. `
|
|
15
|
+
# The manifest's port, which the image listens on. `__EXEC__ sovereign-manifest docker-args` prints it, with the image's label.
|
|
15
16
|
PORT: '__PORT__'
|
|
16
17
|
ports:
|
|
17
18
|
- '__PORT__:__PORT__'
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
/**
|
|
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
|
+
* the sign-in's, which `@sovereignfs/auth` keeps under their own histories. Then it reads the push key, so a wrong one stops it here,
|
|
4
|
+
* and starts sending waiting notifications.
|
|
5
|
+
*/
|
|
2
6
|
export async function register() {
|
|
3
7
|
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
4
8
|
const { migrate } = await import('./lib/migrate');
|
|
5
9
|
await migrate();
|
|
10
|
+
const { migrateSignIn } = await import('./lib/sign-in');
|
|
11
|
+
await migrateSignIn();
|
|
12
|
+
const { startPushOutbox } = await import('./lib/push');
|
|
13
|
+
startPushOutbox();
|
|
6
14
|
}
|
|
7
15
|
}
|
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
import { createCipheriv, createDecipheriv, createHash, randomBytes, scryptSync } from 'node:crypto';
|
|
2
2
|
import { required } from './config';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const keys = new Map<string, Buffer>();
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The key a secret of the app's is kept under, one for each purpose (a person's refresh token, a device's push keys): derived from
|
|
8
|
+
* `__PREFIX___AUTH_SECRET`, which the instance made once and keeps.
|
|
9
|
+
*/
|
|
10
|
+
function secretKey(purpose: string): Buffer {
|
|
11
|
+
let key = keys.get(purpose);
|
|
12
|
+
if (!key) {
|
|
13
|
+
key = scryptSync(required('__PREFIX___AUTH_SECRET'), `__NAME__ ${purpose}`, 32);
|
|
14
|
+
keys.set(purpose, key);
|
|
15
|
+
}
|
|
9
16
|
return key;
|
|
10
17
|
}
|
|
11
18
|
|
|
12
|
-
/** A
|
|
13
|
-
export function seal(plain: string): string {
|
|
19
|
+
/** A secret is kept encrypted, with AES-256-GCM and a fresh nonce each time. By default, a refresh token's key. */
|
|
20
|
+
export function seal(plain: string, purpose = 'refresh tokens'): string {
|
|
14
21
|
const nonce = randomBytes(12);
|
|
15
|
-
const cipher = createCipheriv('aes-256-gcm', secretKey(), nonce);
|
|
22
|
+
const cipher = createCipheriv('aes-256-gcm', secretKey(purpose), nonce);
|
|
16
23
|
const body = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]);
|
|
17
24
|
return Buffer.concat([nonce, cipher.getAuthTag(), body]).toString('base64');
|
|
18
25
|
}
|
|
19
26
|
|
|
20
|
-
export function open(sealed: string): string {
|
|
27
|
+
export function open(sealed: string, purpose = 'refresh tokens'): string {
|
|
21
28
|
const bytes = Buffer.from(sealed, 'base64');
|
|
22
|
-
const decipher = createDecipheriv('aes-256-gcm', secretKey(), bytes.subarray(0, 12));
|
|
29
|
+
const decipher = createDecipheriv('aes-256-gcm', secretKey(purpose), bytes.subarray(0, 12));
|
|
23
30
|
decipher.setAuthTag(bytes.subarray(12, 28));
|
|
24
31
|
return Buffer.concat([decipher.update(bytes.subarray(28)), decipher.final()]).toString('utf8');
|
|
25
32
|
}
|