From f675a3dfb8af889a27c2666cb91e345b362ec7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 3 Sep 2026 04:29:47 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20migrated=20=C4=B0zlek=20users=20enrol?= =?UTF-8?q?=20TOTP=20on=20first=20login;=20ship=20im-migrate=20in=20deploy?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - accounts without confirmed 2FA are routed through /enrol before any session exists — same bar as invited accounts; a migrated user mid- /authorize continues to their app after enrolling - deploy stages im-migrate alongside im-web so the import runs on the server - fix: im-migrate's duplicated schema had drifted (no admin/app_sessions/ events/settings), producing databases the server could not log into — synced to store::SCHEMA, with the drift lesson in the doc comment Verified live: fixture izlek.db -> im-migrate -> password login (hash verbatim) -> forced TOTP enrolment -> signed-in session. --- .github/workflows/main.yml | 8 +++++--- crates/im-migrate/src/main.rs | 26 +++++++++++++++++++++++++- crates/im-web/src/auth.rs | 24 +++++++++++++++++------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 157b876..b1a2687 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -88,10 +88,13 @@ jobs: run: | set -euo pipefail # One compile: this builds im-web and writes out the asset bundle - # for the binary it produced. + # for the binary it produced. im-migrate rides along for the + # one-time İzlek import (run by hand on the server). topcoat asset bundle --package im-web --profile "$PROFILE" --out stage/assets + cargo build --package im-migrate --profile "$PROFILE" cp "target/$TARGET/$PROFILE/im-web" stage/im-web - strip stage/im-web + cp "target/$TARGET/$PROFILE/im-migrate" stage/im-migrate + strip stage/im-web stage/im-migrate ls -l stage stage/assets - name: Install SSH keys @@ -99,7 +102,6 @@ jobs: mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts - - name: Upload the release run: | set -euo pipefail diff --git a/crates/im-migrate/src/main.rs b/crates/im-migrate/src/main.rs index 5f8481f..d6ac83c 100644 --- a/crates/im-migrate/src/main.rs +++ b/crates/im-migrate/src/main.rs @@ -21,7 +21,9 @@ use turso::Builder; /// im's schema, duplicated literally from im-core's `store::SCHEMA` — the /// tool stays dependency-free of im-core so a build of this binary never -/// drags the argon2/rsa tree along. +/// drags the argon2/rsa tree along. When im-core's schema changes, change +/// this in the same diff: a drifted copy once produced a database the +/// server could not read, and every login answered "bad_login". const IM_SCHEMA: &str = " CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY, @@ -30,6 +32,7 @@ CREATE TABLE IF NOT EXISTS users ( password_hash TEXT NOT NULL, totp_secret BLOB, totp_confirmed INTEGER NOT NULL DEFAULT 0, + admin INTEGER NOT NULL DEFAULT 0, disabled INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL ); @@ -37,6 +40,7 @@ CREATE TABLE IF NOT EXISTS invites ( token TEXT PRIMARY KEY, email TEXT NOT NULL, invited_by TEXT, + admin INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL, expires_at TEXT NOT NULL, accepted_at TEXT @@ -81,6 +85,26 @@ CREATE TABLE IF NOT EXISTS signing_keys ( created_at TEXT NOT NULL, active INTEGER NOT NULL DEFAULT 1 ); +CREATE TABLE IF NOT EXISTS app_sessions ( + token_hash TEXT PRIMARY KEY, + user_id TEXT NOT NULL REFERENCES users(id), + client_id TEXT NOT NULL REFERENCES oidc_clients(client_id), + session_hash TEXT NOT NULL REFERENCES sessions(token_hash), + created_at TEXT NOT NULL, + expires_at TEXT NOT NULL, + revoked_at TEXT +); +CREATE TABLE IF NOT EXISTS events ( + id TEXT PRIMARY KEY, + at TEXT NOT NULL, + kind TEXT NOT NULL, + actor TEXT, + detail TEXT +); +CREATE TABLE IF NOT EXISTS settings ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL +); "; #[derive(Debug)] diff --git a/crates/im-web/src/auth.rs b/crates/im-web/src/auth.rs index fd525bd..049ebc2 100644 --- a/crates/im-web/src/auth.rs +++ b/crates/im-web/src/auth.rs @@ -62,12 +62,16 @@ async fn login(cx: &Cx, Form(input): Form) -> Redirect { see("/login/totp".to_string()) } Ok(user) => { - // Accounts enrol TOTP at invite time, so this is the path for one - // created before that rule — sign it in, enrolment comes next. - let token = im_core::sessions::create_session(store, &user.id).await?; - im_core::events::log(store, "login_ok", Some(&user.email), None).await; - server::set_session_cookie(cx, token.expose()); - see(back) + // An account without confirmed 2FA — everyone who migrated from + // İzlek — enrols before any session exists: same bar as an + // invited account. + if im_core::totp::totp_secret(store, &user.id).await?.is_none() { + let secret = im_core::totp::generate_secret(); + im_core::totp::set_totp(store, &user.id, &secret).await?; + } + let sealed = server::mint_pending(cx, &user.id, PendingPurpose::Enroll, back); + server::set_pending_cookie(cx, sealed); + see("/enroll".to_string()) } Err(_) => { // The failure is logged against the address tried, never the @@ -199,7 +203,13 @@ async fn enroll(cx: &Cx, Form(input): Form) -> Redirect { im_core::events::log(store, "enrolled", email.as_deref(), None).await; server::clear_pending_cookie(cx); server::set_session_cookie(cx, token.expose()); - see("/?ok=enrolled".to_string()) + // A migrated user who was mid-`/authorize` continues to their app; a + // fresh invite lands on the welcome banner. + if pending.back == "/" { + see("/?ok=enrolled".to_string()) + } else { + see(pending.back) + } } /// "Sign out everywhere": the central session dies, and with it every