From 337b6b9736c0f044133ebef1f89a918c504748b4 Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Sun, 15 Nov 2026 20:13:38 -0500 Subject: [PATCH] new migration mode --- clients/quadraticserver/auth.nix | 11 ++-- clients/quadraticserver/kanidm/default.nix | 41 +++++++++++- .../quadraticserver/kanidm/migrate-link.patch | 2 +- .../kanidm/welcome-email.patch | 65 +++++++++++++++++++ clients/quadraticserver/matrix/bot.nix | 41 +----------- .../quadraticserver/matrix/continuwuity.nix | 56 +++++++++++++++- flake.lock | 16 ++--- secrets/botToken.age | 5 ++ 8 files changed, 179 insertions(+), 58 deletions(-) create mode 100644 clients/quadraticserver/kanidm/welcome-email.patch create mode 100644 secrets/botToken.age diff --git a/clients/quadraticserver/auth.nix b/clients/quadraticserver/auth.nix index da82ecf..e64eae9 100644 --- a/clients/quadraticserver/auth.nix +++ b/clients/quadraticserver/auth.nix @@ -14,18 +14,17 @@ { matrixoidc = { enable = true; - jwtSecretFile = config.age.secrets."oidcJwtSecret.age".path; + # A snapshot from before the cutover, made by ~/Documents/fednex-password-hashes.sh + passwordHashesFile = "/var/lib/matrix-password-hashes/hashes"; args = [ "--socket" socket "--homeserver" config.services.matrix-continuwuity.settings.global.well_known.client - "--issuer" - "https://${domain}" - "--authorizeEndpoint" - "https://federated.nexus/login" + "--migrateUri" + "https://federated.nexus/migrate/" "--serviceDomain" - "federated.nexus" + config.quad.matrix.domain ]; group = "caddy"; }; diff --git a/clients/quadraticserver/kanidm/default.nix b/clients/quadraticserver/kanidm/default.nix index 275c5b4..18fb817 100644 --- a/clients/quadraticserver/kanidm/default.nix +++ b/clients/quadraticserver/kanidm/default.nix @@ -37,6 +37,7 @@ let bootstrapDir = "/var/lib/kanidm-bootstrap"; mailSenderToken = "${bootstrapDir}/mail-sender-token"; migrateToken = "${bootstrapDir}/migrate-token"; + registrationToken = "${bootstrapDir}/registration-token"; secretsDir = "/var/lib/kanidm-oauth2-secrets"; oauth2Clients = builtins.attrNames config.services.kanidm.provision.systems.oauth2; @@ -90,6 +91,8 @@ in ./hide-radius.patch # Applies dark mode before the page is drawn, rather than flashing light mode first ./theme-flash.patch + # Emails new accounts a welcome message instead of "This is your credential reset link" + ./welcome-email.patch ]; }); @@ -164,6 +167,24 @@ in ]; }; + "03-registration" = { + id = "5837871f-1810-47a5-ad43-60ecbf313462"; + assertions = [ + { + # Used by nexusbot to create approved registrations + state = "present"; + id = "662082c2-9ec4-443d-98b5-537c4d128914"; + class = [ + "account" + "service_account" + ]; + name = "registration"; + displayname = "Registration approval"; + entry_managed_by = "idm_admins"; + } + ]; + }; + # Existing built-in entries: class is omitted, as asserting it would # replace the entry's classes "10-builtin" = { @@ -192,7 +213,7 @@ in }; }; - # Its /migrate creates Kanidm accounts for legacy Matrix users + # Creates Kanidm accounts for legacy Matrix users matrixoidc = { kanidmTokenFile = migrateToken; args = [ @@ -201,6 +222,15 @@ in ]; }; + # Creates Kanidm accounts for approved registrations + nexusbot = { + kanidmTokenFile = registrationToken; + args = [ + "--kanidmUrl" + "https://${domain}" + ]; + }; + caddy.virtualHosts.${domain}.extraConfig = '' reverse_proxy https://${address} { transport http { @@ -212,11 +242,15 @@ in }; systemd.services = { - # Its token is created by kanidm-bootstrap + # Their tokens are created by kanidm-bootstrap matrixoidc = { wants = [ "kanidm-bootstrap.service" ]; after = [ "kanidm-bootstrap.service" ]; }; + nexusbot = { + wants = [ "kanidm-bootstrap.service" ]; + after = [ "kanidm-bootstrap.service" ]; + }; kanidm-tls = { description = "Generate Kanidm's loopback TLS certificate"; @@ -331,7 +365,7 @@ in # Asserting these memberships in a migration didn't persist, so they're # re-applied here on every start. Adding an existing member is a no-op. kanidm group add-members idm_message_senders mail-sender -D idm_admin >/dev/null - kanidm group add-members idm_people_admins migrate -D idm_admin >/dev/null + kanidm group add-members idm_people_admins migrate registration -D idm_admin >/dev/null # Usernames must match Matrix localparts, so people can't rename themeselves. # This also locks display and legal names, as Kanidm has no separate permission for those @@ -356,6 +390,7 @@ in token mail-sender ${mailSenderToken} token migrate ${migrateToken} + token registration ${registrationToken} ''; }; diff --git a/clients/quadraticserver/kanidm/migrate-link.patch b/clients/quadraticserver/kanidm/migrate-link.patch index 54d6b52..9cbe8ed 100644 --- a/clients/quadraticserver/kanidm/migrate-link.patch +++ b/clients/quadraticserver/kanidm/migrate-link.patch @@ -5,7 +5,7 @@ (% endif -%) +
-+ Don't have a Kanidm account yet? Migrate your legacy Federated Nexus account ++ Don't have a Kanidm account yet? Migrate your legacy Federated Nexus account +
+ diff --git a/clients/quadraticserver/kanidm/welcome-email.patch b/clients/quadraticserver/kanidm/welcome-email.patch new file mode 100644 index 0000000..19ef12f --- /dev/null +++ b/clients/quadraticserver/kanidm/welcome-email.patch @@ -0,0 +1,65 @@ +diff -ru a/proto/src/v1/message.rs b/proto/src/v1/message.rs +--- a/proto/src/v1/message.rs 2026-11-15 20:02:57.593682769 -0500 ++++ b/proto/src/v1/message.rs 2026-11-15 20:03:05.231705521 -0500 +@@ -12,6 +12,9 @@ + intent_id: String, + #[serde(with = "time::serde::timestamp")] + expiry_time: OffsetDateTime, ++ // The account has no credentials yet, so this sets them up for the first time ++ #[serde(default)] ++ first_setup: bool, + }, + AccountSignupRequestV1 { + username: String, +diff -ru a/server/lib/src/idm/credupdatesession.rs b/server/lib/src/idm/credupdatesession.rs +--- a/server/lib/src/idm/credupdatesession.rs 2026-11-15 20:02:57.602151429 -0500 ++++ b/server/lib/src/idm/credupdatesession.rs 2026-11-15 20:03:05.233821288 -0500 +@@ -1107,6 +1107,9 @@ + display_name, + intent_id, + expiry_time, ++ first_setup: account.primary.is_none() ++ && account.passkeys.is_empty() ++ && account.attested_passkeys.is_empty(), + }; + + let ident = Identity::message_queue(); +diff -ru a/tools/mail_sender/src/main.rs b/tools/mail_sender/src/main.rs +--- a/tools/mail_sender/src/main.rs 2026-11-15 20:02:57.611181240 -0500 ++++ b/tools/mail_sender/src/main.rs 2026-11-15 20:03:05.236297186 -0500 +@@ -94,6 +94,7 @@ + display_name, + intent_id, + expiry_time, ++ first_setup, + } => { + let mut reset_url = ctx.instance_url.clone(); + reset_url.set_path("/ui/reset"); +@@ -108,6 +109,27 @@ + .format(&Rfc2822) + .unwrap_or("ERROR - invalid expiration time".into()); + ++ if *first_setup { ++ return ( ++ format!("Welcome to {0}", ctx.instance_display_name), ++ format!( ++ r#"Welcome to {instance}, {display_name} ++ ++You can set up your authentication here: ++ ++{reset_url} ++ ++This link will expire at {pretty_expiry_time} ++ ++ ++msg_id: {message_id} ++ "#, ++ instance = ctx.instance_display_name, ++ message_id = ctx.message_id, ++ ), ++ ); ++ } ++ + ( + format!("{0}: Credential Reset Link", ctx.instance_display_name), + format!( diff --git a/clients/quadraticserver/matrix/bot.nix b/clients/quadraticserver/matrix/bot.nix index 65fe41e..bf9b929 100644 --- a/clients/quadraticserver/matrix/bot.nix +++ b/clients/quadraticserver/matrix/bot.nix @@ -14,56 +14,19 @@ { nexusbot = { enable = true; - botPasswordFile = config.age.secrets."botPassword.age".path; - smtpPasswordFile = config.age.secrets."smtpPassword.age".path; + # An access token from a password login, made before continuwuity switched to Kanidm logins + botTokenFile = config.age.secrets."botToken.age".path; args = [ "--socket" socket "--homeserver" config.services.matrix-continuwuity.settings.global.well_known.client - "--username" - "nexusbot" "--adminRoom" "#admins:federated.nexus" "--successUri" "https://federated.nexus/success" "--failureUri" "https://federated.nexus/failure" - "--inviteTo" - "#space:federated.nexus" - "--adminName" - "conduit" - "--email" - ("inf" + "o@f" + "edera" + "ted.n" + "exus") - "--mailDomain" - "email-smtp.us-east-1.amazonaws.com" - "--mailName" - "Federated Nexus" - "--mailUsername" - "AKIAREHCBHZLJ3FNCCSM" - "--subject" - "Your registration for Federated Nexus has been accepted!" - "--plainText" - "Your registration for Federated Nexus has been accepted! Your credentials are: -Username: @{username} -Password: @{password} - -You can change your password in your favorite Matrix client, or here: https://matrix.federated.nexus/_continuwuity/account/password/change - -If you have any questions, check out our documentation: https://federated.nexus/services/matrix/. - -If you have any issues, reply to this email." - "--markdown" - "# Your registration for Federated Nexus has been accepted! -## Your credentials are: -- ### Username: @{username} -- ### Password: @{password} - -You can change your password in your favorite Matrix client, or [here](https://matrix.federated.nexus/_continuwuity/account/password/change). - -If you have any questions, check out [our documentation](https://federated.nexus/services/matrix/). - -If you have any issues, reply to this email." ]; group = "caddy"; }; diff --git a/clients/quadraticserver/matrix/continuwuity.nix b/clients/quadraticserver/matrix/continuwuity.nix index c8651b7..82eb5c7 100644 --- a/clients/quadraticserver/matrix/continuwuity.nix +++ b/clients/quadraticserver/matrix/continuwuity.nix @@ -1,19 +1,73 @@ -{ lib, pkgs, ... }: { +{ + lib, + pkgs, + config, + inputs, + ... +}: +let + client = "matrix"; +in +{ systemd.services.continuwuity = { unitConfig.RequiresMountsFor = [ "/var/lib/private/continuwuity/media" ]; + # It fetches Kanidm's OIDC metadata once at startup, and logins fail until a restart if that fails + requires = [ "kanidm-oauth2-secrets.service" ]; + wants = [ "kanidm.service" ]; + after = [ + "kanidm-oauth2-secrets.service" + "kanidm.service" + "caddy.service" + ]; serviceConfig = { MemoryMax = "5.5G"; MemorySwapMax = "4.5G"; TimeoutStartSec = "10m"; Restart = lib.mkForce "always"; ExecStartPost = "/bin/sh -c 'until ${lib.getExe pkgs.curl} -s -f https://matrix.federated.nexus/.well-known/matrix/client; do sleep 1; done'"; + LoadCredential = [ + "oidc-secret:${config.services.kanidm.provision.systems.oauth2.${client}.basicSecretFile}" + ]; }; }; + services.kanidm.provision.systems.oauth2.${client} = { + displayName = "Matrix"; + originUrl = "${config.services.matrix-continuwuity.settings.global.well_known.client}/_continuwuity/oidc/complete"; + originLanding = "https://app.${config.quad.matrix.domain}"; + imageFile = "${ + inputs.sable.packages.${pkgs.stdenv.hostPlatform.system}.default + }/public/svg/logo.svg"; + basicSecretFile = "/var/lib/kanidm-oauth2-secrets/${client}"; + # The username claim is the Matrix localpart + preferShortUsername = true; + scopeMaps.idm_all_persons = [ + "openid" + "email" + "profile" + ]; + }; + quad.matrix = rec { enable = true; domain = "federated.nexus"; settings = { + auto_join_rooms = [ "#space:${domain}" ]; + + # Logins go through Kanidm only. Existing accounts are linked by username + # on first login, and new ones are created then + oauth.oidc = { + discovery_url = "${config.services.kanidm.server.settings.origin}/oauth2/openid/${client}"; + client_id = client; + client_secret_file = "/run/credentials/continuwuity.service/oidc-secret"; + provider_name = "Federated Nexus"; + prompt_for_localpart = false; + additional_scopes = [ + "email" + "profile" + ]; + }; + admins_list = [ "@nexusbot:federated.nexus" "@quadradical:federated.nexus" diff --git a/flake.lock b/flake.lock index 50aecbe..2fca78d 100644 --- a/flake.lock +++ b/flake.lock @@ -907,11 +907,11 @@ ] }, "locked": { - "lastModified": 1794694767, - "narHash": "sha256-OkyR28RS3gjy5eMME332vZa1atniqi1YGiLQBHVcZ5g=", + "lastModified": 1794790630, + "narHash": "sha256-Bdm9crlxdbOlzUbLudsoUlASaSMJuvZCrm6qc5jJvm0=", "ref": "kanidm", - "rev": "be6b7bdf99b78bb4b1ce998dfe99e1444c254b18", - "revCount": 52, + "rev": "08e34fd98a55f1b69a080fa1dbebe281c0473115", + "revCount": 55, "type": "git", "url": "https://git.federated.nexus/Henry-Hiles/matrixoidc" }, @@ -968,11 +968,11 @@ ] }, "locked": { - "lastModified": 1794682139, - "narHash": "sha256-+3L78fPwX2C/USrLi38B3Xd1Kmv9h8cdRfCR2TnQNpA=", + "lastModified": 1794791313, + "narHash": "sha256-+wFjQq+UVWzDS7+FWdoYURewOj7bUINj1LD/QRSnPBY=", "ref": "refs/heads/main", - "rev": "cc3320ec54a9dabf75e8ad981169b6cb569b099c", - "revCount": 45, + "rev": "3c69fa54d010c3534c9df855b2a2e469b00860bb", + "revCount": 47, "type": "git", "url": "https://git.federated.nexus/federated-nexus/nexusbot" }, diff --git a/secrets/botToken.age b/secrets/botToken.age new file mode 100644 index 0000000..a3cbe4a --- /dev/null +++ b/secrets/botToken.age @@ -0,0 +1,5 @@ +age-encryption.org/v1 +-> ssh-ed25519 VKQUdQ M5n/VTu7zsanPNj9oiQvbb4BoTWjemwuV1GoB+DUFjQ +5LosU8YQ2xuVwlC8bfyHeiuqx6NOVDElSykTsVfLDYE +--- ysAguw4CGqsJamR7jMmmYxJqBpdDdZVDN0tr6DYzdc8 +�ãÒÉòˆ¨c•3Ú4&ôÙ6ïj’þΜ ç|e;Vô^]t`F6Ý“ü‰päð¬ƒ:¸t›ätÔ \ No newline at end of file