Skip to content

Device sessions: account info, password change, signing devices out - #14

Open
Skyfuller1303 wants to merge 12 commits into
itrycodinganditsbad:mainfrom
Skyfuller1303:device-sessions
Open

Skyfuller1303 wants to merge 12 commits into
itrycodinganditsbad:mainfrom
Skyfuller1303:device-sessions

Conversation

@Skyfuller1303

@Skyfuller1303 Skyfuller1303 commented Nov 4, 2026 •

Copy link
Copy Markdown

An account can now see and end its device sessions, and a person can change their own password over the API. A password change or reset signs the other devices out, so a stolen token doesn't outlive it. Device tokens keep their format and still don't expire. Agents and apps keep working unchanged; the new request and response fields are optional.

Server

  • Migration 0007 (SQLite and Postgres) adds client_kind, platform, app_version, revoked_ms and revoke_reason to devices, and password_changed_ms and disabled_ms to users. disabled_ms is created for a later change; nothing reads it yet.
  • POST /auth/login and POST /join take optional client_kind (agent, desktop or cli), platform and app_version, at most 32 bytes each. They are shown in the device list and never used for a permission. A bad value is invalid, checked before the try is counted.
  • GET /account returns the signed-in account: {user_id, username, role, created_ms, password_changed_ms, two_factor_enabled}. two_factor_enabled is always false for now.
  • POST /account/password takes {current_password, new_password} and answers 204. One transaction sets the new hash, signs out every other device (reason password) and deletes the account's open invite and pairings. The device that asked stays signed in.
    • A wrong current password is 403 wrong_password, never 401: the agent reads any 401 on a signed-in route as "revoked" and signs out.
    • A new password that breaks the rules or equals the old one is 400 weak_password.
    • If the password changed while the request ran, nothing changes and the answer is 409 conflict. If the calling device was signed out meanwhile, nothing changes and the answer is 401.
    • 5 tries per account per 15 minutes, wrong passwords included. The body is read up to 4 KiB. The current password is checked under the existing server-wide password-check slots.
  • GET /devices gains client_kind, platform, app_version, revoked_ms and revoke_reason. The reason is one of user, others, password, admin, disabled, two_factor, idle or reset; only the first revoke sets the time and reason.
  • PATCH /devices/{id} renames one of your devices that isn't revoked. POST /devices/revoke-others signs out every other device and answers {revoked: n}.
  • A revoked device's live sockets end at once in the same process. A waiting change-feed long-poll answers 401 instead of changes; it is checked again after the change set is read.
  • Every transaction that revokes devices, changes a password, redeems an invite or pairing, or creates a pairing locks the account's users row first. That one lock order keeps them from deadlocking on Postgres. A join code is consumed only for the account it was found for after that lock, so a code made again for another account in between is refused as invalid_code.
  • A password sign-in creates its device only if the stored hash is still the one it checked. Otherwise it answers like a wrong password, so a device can't be added after a reset or change that signed the others out.

Command line

  • user reset-password now signs out every device of the account (reason reset) and deletes its open invite and pairings. --keep-devices keeps the devices signed in, but the codes are deleted either way. It prints how many devices were signed out.
  • New device revoke-all <user> (reason admin). device revoke now records reason admin.
  • The CLI runs in its own process, so it can't reach the server's open connections. Those end at the next ping (within about 30 s), and a long-poll ends within 60 s with a 401.

Agent

  • The agent sends client_kind agent, its operating system as platform and its version as app_version when it signs in (also in the second step of a two-factor sign-in, once Two-factor sign-in (TOTP and recovery codes) #19 is in) and when it joins. Each is cut to 32 bytes and control characters are dropped. The desktop app signs in and joins through the agent, so those devices show as agent too. login_and_join_send_the_agents_device_details in the agent's end-to-end tests checks what the server stores.

Protocol

  • codes::WRONG_PASSWORD and codes::FORBIDDEN are now in craft-cloud-proto, and the local API uses them from there.
  • New types: AccountInfo, ChangePasswordRequest (its Debug hides both passwords, with a test), RenameDeviceRequest, RevokedOthers. Optional client fields are added to LoginRequest and JoinRequest, and #[serde(default)] fields to DeviceInfo.
  • craft-cloud-api gets the matching client calls.

Docs

docs/design/sync.md covers the routes, error codes, the "never 401 except for the token" rule and device sessions. docs/server.md covers the CLI, the signing-out delays and the upgrade note: an older server refuses a database migrated to 0007, so take a backup before upgrading.

Testing

  • cargo fmt --all, cargo check --workspace --all-targets --locked and cargo clippy --workspace --all-targets --locked -- -D warnings pass.
  • SQLite: cargo test -p craft-cloud-meta -p craft-cloud-server -p craft-cloud-api -p craft-cloud-proto --locked passes. That is 141 meta tests, 99 in the server's tests/api.rs, 61 server lib, 9 server CLI, 22 api and 59 proto.
  • Postgres 17: the meta tests with CRAFT_CLOUD_REQUIRE_BACKENDS=1 (141) and the server tests with CRAFT_CLOUD_TEST_PG pass. The server tests ran without S3; a new on_sqlite_and_postgres! helper in tests/api.rs runs the account scenarios on Postgres without it.
  • Every new query runs on both backends. That includes tests in which a trigger fails a password change midway (nothing changes), a sign-in checked against an old password, and two concurrency scenarios that run password changes, sign-outs, resets, sign-ins, invite and pairing redeems at the same moment. They expect no database error and no live device after a reset. The concurrency scenarios passed 7 of 7 repeated runs on Postgres.
  • cargo +1.94 check --all-targets --locked passes for the changed crates (api, proto, meta, server, app).
  • On a Windows machine, the desktop app's lib tests passed with --test-threads=1 on an earlier commit of this branch (in parallel they crash on Windows, on main as well; Desktop tests: share one wgpu device (fixes a crash in parallel runs on Windows) #35 fixes that).

Not tested, or known

  • cargo xtask ci: fmt, licenses, icons and clippy pass. The test step fails on my machine under a 16 GiB memory cap, because an_app_that_misbehaves_is_refused_or_dropped_and_the_rest_go_on in live_documents runs out of memory (it does so alone and single-threaded on unmodified main too, see Agent test an_app_that_misbehaves_is_refused_or_dropped_and_the_rest_go_on needs more than 16 GiB #15). CI here should show whether it's only my limit.
  • The full workspace doesn't build on Rust 1.94 on main today (egui needs 1.95, and crates/sync uses if-let guards), so only the changed crates were checked on 1.94.
  • The long-poll races (a revoke during the change-set read, or between the token check and the subscription) have no deterministic test; they need a hook in production code. A comment in api.rs explains the order.

Left open (happy to change)

  • The sign-in and join bodies stay at 1 MiB; only the password and rename bodies are 4 KiB.
  • No idle expiry for devices. The idle revoke reason is reserved for an opt-in setting later.
  • An admin API, 2FA and an audit log would build on this in separate PRs, if you want them.

Ported and adapted from my own prototype (Skyfuller1303/craftcloud, MIT OR Apache-2.0).

Skyfuller1303 and others added 11 commits November 4, 2026 00:04
Migration 0007 gives a device its client kind, platform and app version
and, once revoked, the time and the reason, and gives a user the time of
the last password change (plus a disabled time nothing reads yet). All
of it is additive, BIGINT on Postgres, and the flag stays the check.

Meta gets create_device_with and redeem_with, rename_device,
revoke_device_as, revoke_other_devices, revoke_all_devices, account,
change_password and reset_password. Flag, time and reason are set
together and only by the first revoke. A password change or reset sets
the hash, revokes the devices and deletes the account's invite and
pairings in one transaction.

Tests run on both databases, including the migration from 0006.

Co-Authored-By: Claude <noreply@anthropic.com>
New: GET /account, POST /account/password, PATCH /devices/{id} and
POST /devices/revoke-others. Sign-in and join take an optional client
kind, platform and app version, checked before a try is counted. The
device list shows them with the time and reason of a revoke; all new
fields are optional on the wire.

Changing the password answers a wrong current password with 403
wrong_password, never 401, since the agent reads every 401 on a
signed-in route as a revoked device. It is limited to five tries per
account in 15 minutes, takes a password-check slot, and ends the revoked
devices' live connections at once. Password and name bodies are read up
to the new TINY_JSON_BYTES (4 KiB).

wrong_password and forbidden move into the shared error codes; the local
API refers to them. The agent, app and desktop only change where they
build the extended types.

Co-Authored-By: Claude <noreply@anthropic.com>
user reset-password now revokes every device of the account (reason
reset) and deletes its open invite and pairings, so a stolen token does
not outlive the old password. --keep-devices restores the old behaviour
for the devices. device revoke-all signs out an account's devices
without touching the password. Both print how many devices they ended;
device revoke records the admin as the reason.

Co-Authored-By: Claude <noreply@anthropic.com>
The API table and error codes in the design document, the password and
sign-out commands, the live-connection delay of the command line and the
rollout note (an older server refuses the newer database) in the
server guide.

Co-Authored-By: Claude <noreply@anthropic.com>
…evice

A /changes long-poll that was waiting when its device was signed out
through the API now ends at once, and one that wakes for any other reason
looks the device up again before it answers, so a device signed out by
the command line gets 401 instead of the next change set.

The live endpoint listens for revocations and looks the device up before
the upgrade, so a revoke between the sign-in check and the subscription
is not missed.

Co-Authored-By: Claude <noreply@anthropic.com>
change_password checks, inside its transaction, that the calling device
is still signed in (it may have been revoked while the password was
hashed) and answers 401 if not. A password that changed meanwhile is now
409 conflict instead of 400 invalid. Both are new MetaError variants
mapped in the server.

set_password_hash had no caller outside a test and is removed. A new test
breaks the pairing deletes with a trigger and checks that a password
change, an admin reset and a revoke-others undo everything on SQLite and
Postgres.

Co-Authored-By: Claude <noreply@anthropic.com>
The long-poll now listens for revocations and looks its device up before
it waits, so a revoke between the sign-in check and the wait is not
missed, and looks it up once more after the change set is fetched, as
the last step before the answer, so a revoke during the fetch is not
answered with changes.

Co-Authored-By: Claude <noreply@anthropic.com>
…changes a password

change_password locked the caller's device row and then the user's row,
while revoke_other_devices from another device locked its own device and
then the user's, and reset_password locked the user before the devices.
On Postgres two of them at once could deadlock and fail with a database
error. All of them now take the user's row first (a no-op update), then
the devices, then the pairings.

A new scenario runs password changes, sign-outs and an admin reset at the
same moment, over and over, on both databases and expects only successes,
conflicts and signed-out callers. The test trigger helper also drops its
Postgres function.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
…-in adds a device

redeem consumed the invite and then updated the user, while a password
reset locks the user and then deletes the invite, so the two could
deadlock on Postgres. redeem now finds the invite or pairing with a plain
read, locks the account, and consumes the code again under the lock; a
code that is gone by then is invalid_code. create_pairing locks the
account first too.

A password sign-in verified the password outside any transaction, so a
reset or change that committed meanwhile could be followed by a new
device. sign_in_device locks the account and compares the stored hash
again before it creates the device; if it differs, login answers as for
a wrong password.

New scenarios run redeems, a sign-in, a pairing and a reset at the same
moment on both databases and expect no database error and no live device
once the reset is done.

Co-Authored-By: Claude <noreply@anthropic.com>
The invite delete and the pairing update now name the account the plain
read found, so a code hash removed and made again for another account
between that read and the lock is refused as invalid_code instead of
adding a device to the wrong account.

Co-Authored-By: Claude <noreply@anthropic.com>
The server (device sessions) shows a device's client_kind, platform and
app_version in the device list, but the agent never sent them. Every
sign-in and join of the agent now says it is an agent, which operating
system it runs on and its own version, cut to the server's 32-byte limit.
A server-backed test checks that the device list shows them after both.
@Skyfuller1303

Copy link
Copy Markdown
Author

Update: the agent now sends its device details (client_kind agent, the OS as platform, its version as app_version, each cut to 32 bytes) when it signs in and joins, so agent devices no longer show up with empty fields. An end-to-end test checks what the server stores. The change is merged into #17, #18, #19 (including the second step of a two-factor sign-in), #20, #22, #27 and #34. The description has a new "Agent" section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant