Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
API: a paired phone deletes a passwordless account by its pairing
Duarte Santos30732290365f
diff --git a/api/openapi.yaml b/api/openapi.yamlindex 9c732048a..00bdac8a4 100644--- a/api/openapi.yaml+++ b/api/openapi.yaml@@ -578,12 +578,16 @@ paths:stored for them. Not reversible. The body carries the account's name, typed backby its owner, and the same proof as setting a password (see OwnerProof): apasskey assertion made for this request, or the current password while- `PASSWORD_LOGIN` is on. The name and the last-admin rule are checked before the- proof is asked. The last admin is refused while any other profile remains (the+ `PASSWORD_LOGIN` is on. A paired phone (Bearer token) cannot hold a passkey for+ the server, so for a profile with no password that counts the pairing itself is+ the proof (audited as `device`; the app asks the phone's own screen lock first);+ a profile with one sends the current password from the phone too. Cookie+ sessions always need the proof. The name and the last-admin rule are checked+ before the proof is asked. The last admin is refused while any other profile remains (theinstance would be left with nobody to run it); the only profile on an instancemay delete itself. Clears the caller's cookie; every other session and Bearertoken of the account stops working with it. Audited as `auth.account.delete`,- with the name and the proof it was made with; a refused proof as+ with the name and the proof it was made with (`passkey`, `password`, `device`); a refused proof as`auth.proof.fail` with `act: account-delete`.requestBody:required: truediff --git a/api/server.js b/api/server.jsindex 3dd0b1298..a09900c46 100644--- a/api/server.js+++ b/api/server.js@@ -2150,21 +2150,27 @@ const routes = {// nothing; and the same proof the other changes to a way in ask (proveOwner), since a session// on its own may be a cookie someone walked off with. The proof is asked only once the delete// could go ahead, so a refusal the request already decides spends no passkey prompt and no- // password check. The last admin is refused while anyone else is left, since nobody could then- // run the instance from the app; alone on it, they may go. The session ends with the account:- // its cookie is cleared here, and any other session stops verifying since the user is gone.+ // password check. One exception: the paired phone (a Bearer token) cannot hold a passkey for+ // this server, its origin being the app's own, so for a profile with no password that counts+ // the pairing itself is the proof ('device'). The app asks the phone's own lock (Face ID, a+ // fingerprint, the passcode) before it sends. With a password, the phone sends it like+ // everyone else. A browser's cookie never gets this. The last admin is refused while anyone+ // else is left, since nobody could then run the instance from the app; alone on it, they may+ // go. The session ends with the account: its cookie is cleared here, and any other session+ // stops verifying since the user is gone.'POST /api/account/delete': async (req, res) => {- const user = readSession(req);- if (!user) return json(res, 401, { error: 'not signed in' });+ const s = sessionOf(req);+ if (!s) return json(res, 401, { error: 'not signed in' });+ const { user } = s;const body = await readBody(req);- if (readSession(req) !== user) return json(res, 401, { error: 'not signed in' });+ if (sessionOf(req)?.user !== user) return json(res, 401, { error: 'not signed in' });if (typeof body.name !== 'string' || body.name.trim() !== user.name) {return json(res, 400, { error: 'type the account name to confirm', code: 'confirm-name' });}const lastAdmin = () => isAdmin(user) && db.users.length > 1 && db.users.filter(isAdmin).length <= 1;const refuseLastAdmin = () => json(res, 409, { error: 'the last admin cannot leave while other profiles remain', code: 'last-admin' });if (lastAdmin()) return refuseLastAdmin();- const proof = await proveOwner(req, res, user, body, 'account-delete');+ const proof = s.bearer && !passwordWayIn(user) ? 'device' : await proveOwner(req, res, user, body, 'account-delete');if (!proof) return;// Everything above awaited: the session may have ended, and another admin may have left.if (readSession(req) !== user) return json(res, 401, { error: 'not signed in' });diff --git a/api/test/server-account-delete.test.js b/api/test/server-account-delete.test.jsindex 0c47c0324..0543eced0 100644--- a/api/test/server-account-delete.test.js+++ b/api/test/server-account-delete.test.js@@ -1,7 +1,8 @@/* The account's own delete (App Store rule 5.1.1(v)): a signed-in profile removes itself withthe same clean-up the admin's delete runs (removeAccount in server.js), typing its name backand proving it is the owner the way the other changes to a way in do (proveOwner): here with- the current password, the passkey half is in server-passkeys.test.js. The last admin cannot+ the current password, the passkey half is in server-passkeys.test.js. A paired phone, which+ can hold no passkey for the server, proves a profile without a password by its pairing. The last admin cannotleave an instance others still use. Real server.js in a child, same harness asserver-admin-delete.test.js. */import { test, before } from 'node:test';@@ -133,16 +134,39 @@ test('the only profile on the instance can delete itself, admin or not', async tassert.equal(fs.existsSync(path.join(h.dataDir, `state-${ADMIN}.json`)), false);});-test('a paired phone deletes with its Bearer token', async t => {- const h = await startServer(t);- const r = await fetch(`${h.api}/api/account/delete`, {- method: 'POST', headers: { Authorization: `Bearer ${mintSession(USER)}`, 'Content-Type': 'application/json' },- body: JSON.stringify({ name: 'Mallory', current: GOOD })- });- assert.equal(r.status, 200);+test('a paired phone: the pairing is the proof without a password, the password with one', async t => {+ const h = await startServer(t, { users: ['admin', 'user', 'nopw'] });+ const phone = (uid, body) => fetch(`${h.api}/api/account/delete`, {+ method: 'POST', headers: { Authorization: `Bearer ${mintSession(uid)}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body)+ }).then(async r => ({ status: r.status, body: await r.json() }));++ // A profile with a password sends it from the phone like from anywhere else.+ const without = await phone(USER, { name: 'Mallory' });+ assert.equal(without.status, 403);+ assert.equal(without.body.code, 'current-required');+ assert.equal((await phone(USER, { name: 'Mallory', current: 'not the password at all' })).body.code, 'current-wrong');+ assert.equal(h.db().users.length, 3, 'nothing was removed');+ // The name and the last admin still come first.+ assert.equal((await phone('u_nopw', { name: 'keyonly' })).body.code, 'confirm-name');++ // Passkey-only: the phone cannot hold a passkey for the server, so the pairing proves it.+ const keyOnly = await phone('u_nopw', { name: 'Keyonly' });+ assert.equal(keyOnly.status, 200);+ assert.deepEqual(h.db().users.map(u => u.id).sort(), [ADMIN, USER].sort());+ assert.equal(h.audit().find(r => r.ev === 'auth.account.delete')?.msg, 'device');++ assert.equal((await phone(USER, { name: 'Mallory', current: GOOD })).status, 200);assert.deepEqual(h.db().users.map(u => u.id), [ADMIN]);});+test('a browser never gets the phone’s pass: no proof, no delete', async t => {+ const h = await startServer(t, { users: ['admin', 'nopw'] });+ const res = await h.del('u_nopw', { name: 'Keyonly' });+ assert.equal(res.status, 403);+ assert.equal(res.body.code, 'passkey-required');+ assert.equal(h.db().users.length, 2);+});+test('needs proof that the owner is here: missing or wrong, nothing is removed', async t => {const h = await startServer(t, { users: ['admin', 'user', 'nopw'] });const none = await h.del(USER, { name: 'Mallory' });diff --git a/website/api.html b/website/api.htmlindex 8919fbec9..ae15f2ffe 100644--- a/website/api.html+++ b/website/api.html@@ -600,7 +600,7 @@ main.api-main .lead { color: var(--fg-2); max-width: 58ch; margin: 14px 0 0; fon<span class="ep-sum">Delete your own account and everything of yours</span></summary><div class="ep-body">-<p>The signed-in account removes itself for good, with the same clean-up as <code>/api/admin/user/delete</code>: the user record, their passkeys, their push subscriptions, their live presence, their training history file, their uploaded photos and videos (<code>DATA_DIR/uploads/<uid>/</code>) and any Coach credential stored for them. Not reversible. The body carries the account's name, typed back by its owner, and the same proof as setting a password (see OwnerProof): a passkey assertion made for this request, or the current password while <code>PASSWORD_LOGIN</code> is on. The name and the last-admin rule are checked before the proof is asked. The last admin is refused while any other profile remains (the instance would be left with nobody to run it); the only profile on an instance may delete itself. Clears the caller's cookie; every other session and Bearer token of the account stops working with it. Audited as <code>auth.account.delete</code>, with the name and the proof it was made with; a refused proof as <code>auth.proof.fail</code> with <code>act: account-delete</code>.</p>+<p>The signed-in account removes itself for good, with the same clean-up as <code>/api/admin/user/delete</code>: the user record, their passkeys, their push subscriptions, their live presence, their training history file, their uploaded photos and videos (<code>DATA_DIR/uploads/<uid>/</code>) and any Coach credential stored for them. Not reversible. The body carries the account's name, typed back by its owner, and the same proof as setting a password (see OwnerProof): a passkey assertion made for this request, or the current password while <code>PASSWORD_LOGIN</code> is on. A paired phone (Bearer token) cannot hold a passkey for the server, so for a profile with no password that counts the pairing itself is the proof (audited as <code>device</code>; the app asks the phone's own screen lock first); a profile with one sends the current password from the phone too. Cookie sessions always need the proof. The name and the last-admin rule are checked before the proof is asked. The last admin is refused while any other profile remains (the instance would be left with nobody to run it); the only profile on an instance may delete itself. Clears the caller's cookie; every other session and Bearer token of the account stops working with it. Audited as <code>auth.account.delete</code>, with the name and the proof it was made with (<code>passkey</code>, <code>password</code>, <code>device</code>); a refused proof as <code>auth.proof.fail</code> with <code>act: account-delete</code>.</p><p class="ep-auth"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="11" width="16" height="10" rx="2.5"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg><span>Requires a session: the cookie set at sign-in, or <code>Authorization: Bearer <token></code> from mobile-app pairing.</span></p><p class="ep-h">Request body <span class="p-hint">(JSON)</span></p>