Compare commits

...
50 Commits
Author SHA1 Message Date
dasha_uwu 121aa9e39d Revert "Support mdraid hierarchies for storage topology detection."
This reverts commit d665a34f30.

The commit was causing requests to panic on some systems
2026-01-01 01:10:59 +05:00
Jason Volk 6b4a09fc81 Bump 1.4.9
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 23:48:15 +00:00
Jason Volk 5a92a00939 Fix missing validations of federation member event stubs.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk b412aafaf8 Add an infolog progress message during batch notary request.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk 63bdeb79c9 Add newtype deserialization for Json(_) for parity with Cbor(_).
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk 0bbe96f761 Bump Ruma for custom JoinRule fix.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-30 18:42:46 +00:00
Jason Volk b33e73672b Implement MSC3706 two-step join.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-29 21:55:15 +00:00
Jason Volk 59481ad28d Refactor/dedup join event preparation paths.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-29 08:08:05 +00:00
Jason Volk 977f9e2e2e Fix deactivated user check misapplied to appservice user. (11309062a2)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 21:59:29 +00:00
Jason Volk 432e6a2663 Fix serializing zero notification count in never-read room case.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 05:17:29 +00:00
Jason Volk f4ee8bc433 Simplify spaces related syntax.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 00:49:45 +00:00
Jason Volk 8aeaaea271 Fix clippy::result-large-err.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-28 00:49:45 +00:00
divideableZero 5847d8135d docs(readme): add Alpine package 2025-12-27 18:22:48 +03:00
Jason Volk 14e7a001b9 Use abstracted UIAA call for login token route.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk d56ee58a73 Fix SSO cookie deserialization for cases requiring allocated strings.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk 806ecaec7e Add demoserver badge to readme documentation section.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 14:20:07 +00:00
Jason Volk 11309062a2 Implement SSO/OIDC support. (closes #7)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 07:24:49 +00:00
Jason Volk d665a34f30 Support mdraid hierarchies for storage topology detection.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 00:51:02 +00:00
Jason Volk e019ed77d0 Add more linespace between example config sections.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-26 00:11:43 +00:00
Jason Volk 568a28220c Add stochastic string truncation utils.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-24 03:26:08 +00:00
Jason Volk 4229a1d630 Add type alias for Arc<OnceServices>
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 14:52:55 +00:00
Jason Volk 0cbdcf3b3d Add displayname argument to full_register.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 03:50:29 +00:00
Jason Volk fe57ea3354 Add functor for timepoint expiration to utils.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 422802a9cf Add serde_html_form to Error; use serde_core for greater abstraction.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk e79b888367 Improve well-known resolver tracing span.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk f91b3b6a6c Add string util for #[serde(deserialize_with = "deserialize::to_lowercase")]
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 4150b917d8 Restructure full_register arguments with names and defaults.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 5b0dfaeef6 Allow clippy::toplevel-ref-arg.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
Jason Volk 7a4d07fd3b Lock tracing to 0.1.43 until 0.1.44 breaking change resolved.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-23 02:55:32 +00:00
dasha_uwu eadc9e782d Fetch tags in CI builds to provide correct version string 2025-12-21 22:38:50 +00:00
Jason Volk af570c481e Bump 1.4.8
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 19:21:27 +00:00
Jason Volk dc9314de1f Fix missing validations for federation invites.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-21 12:34:58 +00:00
June Strawberry cf005ee537 bump ruma to fix join_rule being omitted on default value in /publicRooms
also updates complement results for now passing TestKnockRoomsInPublicRoomsDirectory

Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:22:40 -05:00
June Strawberry 6455ef72cd bump ldap3 fork, initialise aws_lc_rs with single function for ldap and direct tls
Signed-off-by: June Strawberry <june@vern.cc>
2025-12-20 22:16:27 -05:00
dasha_uwu 7115fb2796 Refactor join, alias services
Split knock, user register from api into services

Fix autojoin not working with v12 rooms

Fix 'm.login.registration_token/validity' for reloaded registration tokens

Change join servers order

Move autojoin for ldap
2025-12-18 18:41:23 +00:00
Jason Volk 959c559bd8 Log all ruma handler results at trace level.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 17:27:22 +00:00
Jason Volk a1bf188504 Update some links to project documentation.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 16:54:19 +00:00
Jason Volk 87770fefeb Bump dependencies.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 02:43:35 +00:00
dasha_uwu 0c7ba1dd5a Fix sync handling for appservices calling without device_id 2025-12-18 02:18:30 +00:00
dasha_uwu 7b2079f714 Check PDU formats 2025-12-18 01:53:09 +00:00
dasha_uwu c5508bba58 Fix appservices not receiving membership events to sender_localpart
Simplify sending PDUs to appservices
2025-12-18 01:49:54 +00:00
Jason Volk 88a6b72f0f Update config comment for default hydra backports.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:54 +00:00
Jason Volk 22c5f0207d Fix chat badge hyperlink too.
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-18 01:49:45 +00:00
Jason Volk 3af78ac851 Fix readme documentation link; tweak wording. [ci skip]
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 14:34:14 +00:00
dasha_uwu b5f50c3fda Allow using OpenID userinfo endpoint with federation disabled, fixes #240
Even though it's in SS API this is not really related to federation, so
it makes sense to allow using this endpoint with allow_federation=false
2025-12-17 08:51:29 +00:00
Jason Volk c9a26a0280 Bump rust-rocksdb, jevmalloc. (fixes #238) (fixes #239)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 08:51:26 +00:00
Jason Volk 417e9ba052 Update README; additional docs link. (closes #235)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
yefim 085cdb30f4 Fix newly created LDAP users not added to auto-join-rooms 2025-12-17 06:53:06 +00:00
Jason Volk 77474479b5 audit: Ignore unmaintained rustls-pemfile. (RUSTSEC-2025-0134)
Signed-off-by: Jason Volk <jason@zemos.net>
2025-12-17 06:53:06 +00:00
Alexandru Scvorțov d244e8027c Update README to say matrix-tuwunel is in stable NixOS [ci skip] 2025-12-17 04:39:38 +00:00
97 changed files with 4198 additions and 2131 deletions

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
[advisories]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
ignore = ["RUSTSEC-2024-0436", "RUSTSEC-2025-0014", "RUSTSEC-2025-0134"] # advisory IDs to ignore e.g. ["RUSTSEC-2019-0001", ...]
informational_warnings = [] # warn for categories of informational advisories
severity_threshold = "none" # CVSS severity ("none", "low", "medium", "high", "critical")
+2
View File
@@ -111,6 +111,8 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
fetch-tags: true
# Call docker
- name: Bake ${{matrix.bake_target}}
+2
View File
@@ -100,6 +100,8 @@ jobs:
- uses: actions/checkout@v4
with:
persist-credentials: false
fetch-depth: 0
fetch-tags: true
- name: GitHub Login
uses: docker/login-action@v3
Generated
+210 -218
View File
File diff suppressed because it is too large. Load diff
+17 -9
View File
@@ -29,7 +29,7 @@ license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/matrix-construct/tuwunel"
rust-version = "1.89.0"
version = "1.4.7"
version = "1.4.8"
[workspace.metadata.crane]
name = "tuwunel"
@@ -76,6 +76,7 @@ version = "0.7"
version = "0.10"
default-features = false
features = [
"cookie",
"typed-header",
"tracing",
]
@@ -233,7 +234,7 @@ version = "0.14"
[workspace.dependencies.jevmalloc]
git = "https://github.com/matrix-construct/jevmalloc"
rev = "29fde02533ee2c6a5e9b87095324e6f7a5d43738"
rev = "93795449913f65ab533b7fa482333eef63fc3ae0"
[workspace.dependencies.jsonwebtoken]
version = "9.3"
@@ -242,7 +243,7 @@ features = ["use_pem"]
[workspace.dependencies.ldap3]
git = "https://github.com/matrix-construct/ldap3"
rev = "7d423314b9dbc66347284e38fc2b78c3d8f3d494"
rev = "fdfbba2bf916b53e5f73cdb1a495ebb649978079"
default-features = false
features = ["sync", "tls-rustls"]
@@ -312,10 +313,12 @@ version = "1.12"
version = "0.12"
default-features = false
features = [
"rustls-tls-native-roots",
"socks",
"charset",
"hickory-dns",
"http2",
"json",
"rustls-tls-native-roots",
"socks",
]
[workspace.dependencies.ring]
@@ -324,7 +327,7 @@ default-features = false
[workspace.dependencies.ruma]
git = "https://github.com/matrix-construct/ruma"
rev = "f8aa292ce48f28ee6c82b29a811d26c6ccb5aad2"
rev = "ff66bdd9975152f0382d753120dddddf3577ad51"
features = [
"__compat",
"appservice-api-c",
@@ -364,7 +367,7 @@ features = [
[workspace.dependencies.rustls]
version = "0.23"
default-features = false
features = ["aws_lc_rs"]
features = ["aws_lc_rs", "logging", "tls12", "prefer-post-quantum"]
[workspace.dependencies.rustyline-async]
version = "0.4.6"
@@ -372,7 +375,7 @@ default-features = false
[workspace.dependencies.rust-rocksdb]
git = "https://github.com/matrix-construct/rust-rocksdb"
rev = "31e6f8dd4a531b379c132e82773efc9315b0489a"
rev = "c11395350bc1f2090a0152f2d15c8c5847821eba"
default-features = false
features = [
"bzip2",
@@ -418,6 +421,10 @@ version = "1.0"
default-features = false
features = ["rc"]
[workspace.dependencies.serde_core]
version = "1.0"
default-features = false
[workspace.dependencies.serde_html_form]
version = "0.2"
@@ -524,7 +531,7 @@ features = [
]
[workspace.dependencies.tracing]
version = "0.1"
version = "=0.1.43"
default-features = false
[workspace.dependencies.tracing-core]
@@ -982,6 +989,7 @@ assertions_on_constants = { level = "allow", priority = 1 }
module_inception = { level = "allow", priority = 1 }
obfuscated_if_else = { level = "allow", priority = 1 }
collapsible_if = { level = "allow", priority = 1 } # TODO stable v. nightly 05-21-25
toplevel-ref-arg = { level = "allow", priority = 1 }
###################
suspicious = { level = "warn", priority = -1 }
+18 -15
View File
@@ -1,11 +1,11 @@
# Tuwunel<sup>💕</sup>
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
![GitHub License](https://img.shields.io/github/license/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Created At](https://img.shields.io/github/created-at/matrix-construct/tuwunel?style=flat%2Dsquare&color=%238A2BE2)
![GitHub Commit Activity](https://img.shields.io/github/commit-activity/m/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel%2Fpulse%2Fmonthly&color=%238A2BE2)
![Docker Pulls](https://img.shields.io/docker/pulls/jevolk/tuwunel?style=flat%2Dsquare&color=8A2BE2)
![GitHub Repo Stars](https://img.shields.io/github/stars/matrix-construct/tuwunel?style=flat%2Dsquare&link=https%3A%2F%2Fgithub.com%2Fmatrix-construct%2Ftuwunel&color=%238A2BE2)
[![CI/CD](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml/badge.svg?branch=main&style=flat%2Dsquare)](https://github.com/matrix-construct/tuwunel/actions/workflows/main.yml)
<!-- ANCHOR: catchphrase -->
@@ -15,19 +15,21 @@
<!-- ANCHOR: body -->
[![Documentation](https://img.shields.io/badge/documentation%2D_?color=%238A2BE2&style=for-the-badge&logo=mdBook&logoColor=FFFFFF)](https://matrix-construct.github.io/tuwunel/)
[![Demo Server](https://img.shields.io/badge/demo%20server%2D_?color=%238A2BE2&style=for-the-badge&logo=Element&logoColor=FFFFFF)](https://try.tuwunel.chat)
[![Support Chat](https://img.shields.io/matrix/tuwunel%3Amatrix.org.svg?color=098A09&style=for-the-badge&label=Support%20Chat&labelColor=8A2BE2&logo=Matrix)](https://matrix.to/#/#tuwunel:grin.hu)
Tuwunel is a featureful [Matrix](https://matrix.org/) homeserver you can use instead of Synapse
with your favorite [client](https://matrix.org/ecosystem/clients/),
[bridge](https://matrix.org/ecosystem/bridges/) or
[bot](https://matrix.org/ecosystem/integrations/). It is written entirely in Rust to be a scalable,
lightweight, low-cost, community-driven alternative covering all but the most niche uses.
low-cost, enterprise-ready, community-driven alternative, fully implementing the
[Matrix Specification](https://spec.matrix.org/latest/) for all but the most niche uses.
This project is the official successor to conduwuit, which
was a featureful and high-performance fork of [Conduit](https://gitlab.com/famedly/conduit), all
community-lead homeservers implementing the compatible
[Matrix Specification](https://spec.matrix.org/latest/).
Tuwunel is operated by enterprise users with a vested interest in sponsoring its continued
development. It is now maintained by full-time staff.
This project is the official successor to [conduwuit](https://github.com/x86pup/conduwuit) after it
reached stability. Tuwunel is now used by many companies with a vested interest in its continued
development by full-time staff. It is primarily sponsored by the government of
Switzerland 🇨🇭 where it is currently deployed for citizens.
### Getting Started
@@ -38,7 +40,8 @@ development. It is now maintained by full-time staff.
- Static binaries available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Deb and RPM packages available as [releases](https://github.com/matrix-construct/tuwunel/releases) or [build artifacts](https://github.com/matrix-construct/tuwunel/actions?query=branch%3Amain).
- Arch package available as [tuwunel](https://aur.archlinux.org/packages/tuwunel) or [tuwunel-git](https://aur.archlinux.org/packages/tuwunel-git).
- Nix package still [needs some love](https://github.com/NixOS/nixpkgs/issues/415469).
- Nix package available as [`matrix-tuwunel`](https://search.nixos.org/packages?query=matrix-tuwunel) and NixOS module available as [`services.matrix-tuwunel`](https://search.nixos.org/options?query=services.matrix-tuwunel).
- Alpine package available as [tuwunel](https://pkgs.alpinelinux.org/package/edge/testing/x86_64/tuwunel).
**1.** [Configure](https://matrix-construct.github.io/tuwunel/configuration.html) by
copying and editing the `tuwunel-example.toml`. The `server_name` and `database_path` must be
+5 -35
View File
@@ -1,39 +1,9 @@
# Tuwunel 1.4.7
# Tuwunel 1.4.9
December 3, 2025
December 30, 2025
Tuwunel is now deployed at scale serving the citizens of Switzerland in production. Some optimizations were requested to reduce operating costs from projected growth over product lifespan: this release delivers with markedly reduced CPU usage and improves responsiveness. However, complications during routine dependency upgrades consumed valuable time planned for features and issues which could not be completed for this release.
All federating deployments must upgrade for follow-up mitigations similar to those patched by 1.4.8 now uncovered as a wider class of vulnerabilities in additional locations. This is an off-schedule coordinated security release. Full release notes will be included with the next scheduled release.
### New Features
### Security Fixes
- Upgrade Support for Room Version 12 is now available. Though this room version has been supported for the entire 1.4 series, all Tuwunel servers have been protected by Hydra Backports on all room versions. As such, other work was able to be prioritized for the preceding releases.
### Enhancements
- Recursive relations have been enabled. This is an optimization which allows the server to gather more data using fewer client requests, for example, of a thread with many reactions and replies. The implementation is now optimal and utilizes the full capabilities of Tuwunel's asynchronous database.
- Several miscellaneous but significant optimizations took place at the direction of memory profiling. This reduced load on the allocator for database queries and JSON serialization of complex objects. Heroes calculations and the joined room hot-path on sync v3 were further optimized to reduce the database query load itself.
- Jemalloc has been repackaged with platform-specific optimizations enhancing the build. The upgrade to the dev branch of libjemalloc itself was considered as too much variability for the same release, it is planned for an upcoming release.
- Thanks to element-hq/synapse#18970 by @dasha-uwu, we have very slightly turned down the amount of randomness when selecting join-servers, More retries also occur within a single request if necessary. Thanks to @gogo199432 and @lifeofguenter for reporting problems in (#128) and (#205) respectively. More opportunities are still available to make large room joins robust.
### Bug Fixes
- Special thanks to @yefimg for fixing LDAP logout in (#231) from a report kindly made by @orhtej2 in (#97); thank you for your patience waiting for domain expertise to assist here.
- Thanks to @Radiant-Xyz the example configurations have been updated to remove `allow_check_for_updates`. This fixes any warnings for the item no longer existing. (#221)
- Thanks again to @Radiant-Xyz reporting in (#219) the `/whoami` endpoint is now returns spec-compliant errors for Mautrix bridges (fe12daead9). Thanks also to @bobobo1618 for confirming the fix is working.
- Relations responses were sometimes incorrect in the forwards direction. This was fixed by (5147b541) bringing those responses into full compliance. Note the prior release had also fixed compliance issues but in the backwards direction.
- Server selection for backfill struggled sometimes for version 12 rooms. These rooms might fail to load history after join. Additional servers are now found using `creators` and `additional_creators` instead.
- Room leave compliance has been fixed for an edge-case where a room becomes empty except for a locally invited user which does not have its leave event sent down `/sync`.
- Thanks to @grinapo for a report which lead to the discovery of events acquired over backfill not being checked for whether they already exist.
### Upcoming
- As stated in the summary, several planned items could not be cut into this release. These include SSO/OIDC support (#7), Element Call setup assistance and documentation (#217)(#215), User-level Admin Room and Media deletion (#192), and any other assigned issue. These items are on the short-list for the next cycle and mean a lot to us; to all participants: your issues are not being ignored and we hear you.
- Federation responses processed from a remote server assisting in membership state transitions lacked input validation: trusting, signing, and disseminating an event crafted by the remote server. These vulnerabilities were uncovered in a classic follow-up to the initial forgery attack pattern described in patch 1.4.8 also present in additional locations.
+6 -4
View File
@@ -54,7 +54,7 @@ async fn handle_command(services: Arc<Services>, command: CommandInput) -> Proce
async fn process_command(services: Arc<Services>, input: &CommandInput) -> ProcessorResult {
let (command, args, body) = match parse(&services, input) {
| Err(error) => return Err(error),
| Err(error) => return Err(Box::new(error)),
| Ok(parsed) => parsed,
};
@@ -90,12 +90,14 @@ async fn process_command(services: Arc<Services>, input: &CommandInput) -> Proce
write!(&mut logs, "Command failed with error:\n```\n{error:#?}\n```")
.expect("output buffer");
Err(reply(RoomMessageEventContent::notice_markdown(logs), context.reply_id))
Err(Box::new(reply(
RoomMessageEventContent::notice_markdown(logs),
context.reply_id,
)))
},
}
}
#[allow(clippy::result_large_err)]
fn handle_panic(error: &Error, command: &CommandInput) -> ProcessorResult {
let link =
"Please submit a [bug report](https://github.com/matrix-construct/tuwunel/issues/new). \
@@ -103,7 +105,7 @@ fn handle_panic(error: &Error, command: &CommandInput) -> ProcessorResult {
let msg = format!("Panic occurred while processing command:\n```\n{error:#?}\n```\n{link}");
let content = RoomMessageEventContent::notice_markdown(msg);
error!("Panic while processing command: {error:?}");
Err(reply(content, command.reply_id.as_deref()))
Err(Box::new(reply(content, command.reply_id.as_deref())))
}
/// Parse and process a message from the admin room
+9 -4
View File
@@ -1,6 +1,7 @@
mod account_data;
mod appservice;
mod globals;
mod oauth;
mod presence;
mod pusher;
mod raw;
@@ -18,10 +19,10 @@ use tuwunel_core::Result;
use self::{
account_data::AccountDataCommand, appservice::AppserviceCommand, globals::GlobalsCommand,
presence::PresenceCommand, pusher::PusherCommand, raw::RawCommand, resolver::ResolverCommand,
room_alias::RoomAliasCommand, room_state_cache::RoomStateCacheCommand,
room_timeline::RoomTimelineCommand, sending::SendingCommand, short::ShortCommand,
sync::SyncCommand, users::UsersCommand,
oauth::OauthCommand, presence::PresenceCommand, pusher::PusherCommand, raw::RawCommand,
resolver::ResolverCommand, room_alias::RoomAliasCommand,
room_state_cache::RoomStateCacheCommand, room_timeline::RoomTimelineCommand,
sending::SendingCommand, short::ShortCommand, sync::SyncCommand, users::UsersCommand,
};
use crate::admin_command_dispatch;
@@ -81,6 +82,10 @@ pub(super) enum QueryCommand {
#[command(subcommand)]
Sync(SyncCommand),
/// - oauth service
#[command(subcommand)]
Oauth(OauthCommand),
/// - raw service
#[command(subcommand)]
Raw(RawCommand),
+172
View File
@@ -0,0 +1,172 @@
use clap::Subcommand;
use futures::{StreamExt, TryStreamExt};
use ruma::OwnedUserId;
use tuwunel_core::{Err, Result, utils::stream::IterStream};
use tuwunel_service::{
Services,
oauth::{Provider, Session},
};
use crate::{admin_command, admin_command_dispatch};
#[admin_command_dispatch(handler_prefix = "oauth")]
#[derive(Debug, Subcommand)]
/// Query OAuth service state
pub(crate) enum OauthCommand {
/// List configured OAuth providers.
ListProviders,
/// List users associated with an OAuth provider
ListUsers,
/// Show active configuration of a provider.
ShowProvider {
id: String,
#[arg(long)]
config: bool,
},
/// Show session state
ShowSession {
id: String,
},
/// Token introspection request to provider.
TokenInfo {
id: String,
},
/// Revoke token for user_id or sess_id.
Revoke {
id: String,
},
/// Remove oauth state (DANGER!)
Remove {
id: String,
#[arg(long)]
force: bool,
},
}
#[admin_command]
pub(super) async fn oauth_list_providers(&self) -> Result {
self.services
.config
.identity_provider
.iter()
.try_stream()
.map_ok(Provider::id)
.map_ok(|id| format!("{id}\n"))
.try_for_each(async |id| self.write_str(&id).await)
.await
}
#[admin_command]
pub(super) async fn oauth_list_users(&self) -> Result {
self.services
.oauth
.sessions
.users()
.map(|id| format!("{id}\n"))
.map(Ok)
.try_for_each(async |id: String| self.write_str(&id).await)
.await
}
#[admin_command]
pub(super) async fn oauth_show_provider(&self, id: String, config: bool) -> Result {
if config {
let config = self.services.oauth.providers.get_config(&id)?;
self.write_str(&format!("{config:#?}\n")).await?;
return Ok(());
}
let provider = self.services.oauth.providers.get(&id).await?;
self.write_str(&format!("{provider:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_show_session(&self, id: String) -> Result {
let session = find_session(self.services, &id).await?;
self.write_str(&format!("{session:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_token_info(&self, id: String) -> Result {
let session = find_session(self.services, &id).await?;
let provider = self
.services
.oauth
.sessions
.provider(&session)
.await?;
let tokeninfo = self
.services
.oauth
.request_tokeninfo((&provider, &session))
.await;
self.write_str(&format!("{tokeninfo:#?}\n")).await
}
#[admin_command]
pub(super) async fn oauth_revoke(&self, id: String) -> Result {
let session = find_session(self.services, &id).await?;
let provider = self
.services
.oauth
.sessions
.provider(&session)
.await?;
self.services
.oauth
.revoke_token((&provider, &session))
.await?;
self.write_str("done").await
}
#[admin_command]
pub(super) async fn oauth_remove(&self, id: String, force: bool) -> Result {
let session = find_session(self.services, &id).await?;
let Some(sess_id) = session.sess_id else {
return Err!("Missing sess_id in oauth Session state");
};
if !force {
return Err!(
"Deleting these records can cause registration conflicts. Use --force to be sure."
);
}
self.services
.oauth
.sessions
.delete(&sess_id)
.await;
self.write_str("done").await
}
async fn find_session(services: &Services, id: &str) -> Result<Session> {
if let Ok(user_id) = OwnedUserId::parse(id) {
services
.oauth
.sessions
.get_by_user(&user_id)
.await
} else {
services.oauth.sessions.get(id).await
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ pub(crate) enum SyncCommand {
/// Show details of sliding sync connection by ID.
ShowConnection {
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
},
@@ -43,7 +43,7 @@ pub(super) async fn list_connections(&self) -> Result {
pub(super) async fn show_connection(
&self,
user_id: OwnedUserId,
device_id: OwnedDeviceId,
device_id: Option<OwnedDeviceId>,
conn_id: Option<String>,
) -> Result {
let key = into_connection_key(user_id, device_id, conn_id);
+21 -156
View File
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, fmt::Write as _};
use std::collections::BTreeMap;
use futures::{FutureExt, StreamExt};
use ruma::{
@@ -13,12 +13,11 @@ use ruma::{
},
};
use tuwunel_core::{
Err, Result, debug, debug_warn, error, info, is_equal_to,
Err, Result, debug_warn, info,
matrix::{Event, pdu::PduBuilder},
utils::{self, ReadyExt},
warn,
};
use tuwunel_service::Services;
use tuwunel_service::{Services, users::Register};
use crate::{
admin_command, get_room_info,
@@ -62,148 +61,16 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH));
// Create user
self.services
.users
.create(&user_id, Some(password.as_str()), None)
.full_register(Register {
user_id: Some(&user_id),
password: Some(&password),
grant_first_user_admin: true,
..Default::default()
})
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !self
.services
.server
.config
.new_user_displayname_suffix
.is_empty()
{
write!(
displayname,
" {}",
self.services
.server
.config
.new_user_displayname_suffix
)?;
}
self.services
.users
.set_displayname(&user_id, Some(displayname));
// Initial account data
self.services
.account_data
.update(
None,
&user_id,
ruma::events::GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: ruma::push::Ruleset::server_default(&user_id),
},
})?,
)
.await?;
if !self
.services
.server
.config
.auto_join_rooms
.is_empty()
{
for room in &self.services.server.config.auto_join_rooms {
let Ok(room_id) = self.services.alias.maybe_resolve(room).await else {
error!(
%user_id,
"Failed to resolve room alias to room ID when attempting to auto join {room}, skipping"
);
continue;
};
if !self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
let state_lock = self.services.state.mutex.lock(&room_id).await;
if let Some(room_server_name) = room.server_name() {
match self
.services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[
self.services.globals.server_name().to_owned(),
room_server_name.to_owned(),
],
&None,
&state_lock,
)
.await
{
| Ok(_response) => {
info!("Automatically joined room {room} for user {user_id}");
},
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
self.services
.admin
.send_text(&format!(
"Failed to automatically join room {room} for user {user_id}: \
{e}"
))
.await;
},
}
drop(state_lock);
}
}
}
// we dont add a device since we're not the user, just the creator
// if this account creation is from the CLI / --execute, invite the first user
// to admin room
if let Ok(admin_room) = self.services.admin.get_admin_room().await {
if self
.services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
self.services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
} else {
debug!("create_user admin command called without an admin room being available");
}
self.write_str(&format!("Created user with user_id: {user_id} and password: `{password}`"))
.await
}
@@ -403,7 +270,7 @@ pub(super) async fn list_joined_rooms(&self, user_id: String) -> Result {
#[admin_command]
pub(super) async fn force_join_list_of_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if self.body.len() < 2
@@ -415,7 +282,7 @@ pub(super) async fn force_join_list_of_local_users(
if !yes_i_want_to_do_this {
return Err!(
"You must pass the --yes-i-want-to-do-this-flag to ensure you really want to force \
"You must pass the --yes-i-want-to-do-this flag to ensure you really want to force \
bulk join all specified local users.",
);
}
@@ -427,7 +294,7 @@ pub(super) async fn force_join_list_of_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -505,9 +372,10 @@ pub(super) async fn force_join_list_of_local_users(
.join(
&user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -534,7 +402,7 @@ pub(super) async fn force_join_list_of_local_users(
#[admin_command]
pub(super) async fn force_join_all_local_users(
&self,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
yes_i_want_to_do_this: bool,
) -> Result {
if !yes_i_want_to_do_this {
@@ -551,7 +419,7 @@ pub(super) async fn force_join_all_local_users(
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
if !self
@@ -600,9 +468,10 @@ pub(super) async fn force_join_all_local_users(
.join(
user_id,
&room_id,
Some(&room),
Some(String::from(BULK_JOIN_REASON)),
&servers,
&None,
false,
&state_lock,
)
.await
@@ -627,16 +496,12 @@ pub(super) async fn force_join_all_local_users(
}
#[admin_command]
pub(super) async fn force_join_room(
&self,
user_id: String,
room_id: OwnedRoomOrAliasId,
) -> Result {
pub(super) async fn force_join_room(&self, user_id: String, room: OwnedRoomOrAliasId) -> Result {
let user_id = parse_local_user_id(self.services, &user_id)?;
let (room_id, servers) = self
.services
.alias
.maybe_resolve_with_servers(&room_id, None)
.maybe_resolve_with_servers(&room, None)
.await?;
assert!(
@@ -648,7 +513,7 @@ pub(super) async fn force_join_room(
self.services
.membership
.join(&user_id, &room_id, None, &servers, &None, &state_lock)
.join(&user_id, &room_id, Some(&room), None, &servers, false, &state_lock)
.await?;
drop(state_lock);
+3 -3
View File
@@ -78,7 +78,7 @@ pub(super) enum UserCommand {
/// - Manually join a local user to a room.
ForceJoinRoom {
user_id: String,
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
},
/// - Manually leave a local user from a room.
@@ -148,7 +148,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinListOfLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
@@ -160,7 +160,7 @@ pub(super) enum UserCommand {
///
/// Requires the `--yes-i-want-to-do-this` flag.
ForceJoinAllLocalUsers {
room_id: OwnedRoomOrAliasId,
room: OwnedRoomOrAliasId,
#[arg(long)]
yes_i_want_to_do_this: bool,
+1
View File
@@ -101,6 +101,7 @@ tokio.workspace = true
tracing.workspace = true
tuwunel-core.workspace = true
tuwunel-service.workspace = true
url.workspace = true
[lints]
workspace = true
+7 -17
View File
@@ -121,26 +121,16 @@ async fn room_available_servers(
// insert our server as the very first choice if in list, else check if we can
// prefer the room alias server first
match servers
if let Some(server_index) = servers
.iter()
.position(|server_name| services.globals.server_is_ours(server_name))
{
| Some(server_index) => {
servers.swap_remove(server_index);
servers.insert(0, services.globals.server_name().to_owned());
},
| _ => {
match servers
.iter()
.position(|server| server == room_alias.server_name())
{
| Some(alias_server_index) => {
servers.swap_remove(alias_server_index);
servers.insert(0, room_alias.server_name().into());
},
| _ => {},
}
},
servers.swap(0, server_index);
} else if let Some(alias_server_index) = servers
.iter()
.position(|server| server == room_alias.server_name())
{
servers.swap(0, alias_server_index);
}
servers
+3 -3
View File
@@ -32,8 +32,8 @@ pub(crate) async fn get_context_route(
State(services): State<crate::State>,
body: Ruma<get_context::v3::Request>,
) -> Result<get_context::v3::Response> {
let sender = body.sender();
let (sender_user, sender_device) = sender;
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let room_id = &body.room_id;
let event_id = &body.event_id;
let filter = &body.filter;
@@ -110,7 +110,7 @@ pub(crate) async fn get_context_route(
let lazy_loading_context = lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(base_count.into_unsigned()),
options: Some(&filter.lazy_load_options),
+4 -6
View File
@@ -2,14 +2,14 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::StreamExt;
use ruma::{
MilliSecondsSinceUnixEpoch, OwnedDeviceId,
MilliSecondsSinceUnixEpoch,
api::client::device::{
self, delete_device, delete_devices, get_device, get_devices, update_device,
},
};
use tuwunel_core::{Err, Result, debug, err, utils, utils::string::to_small_string};
use tuwunel_core::{Err, Result, debug, err, utils::string::to_small_string};
use crate::{Ruma, client::DEVICE_ID_LENGTH, router::auth_uiaa};
use crate::{Ruma, router::auth_uiaa};
/// # `GET /_matrix/client/r0/devices`
///
@@ -94,13 +94,11 @@ pub(crate) async fn update_device_route(
appservice.registration.id
);
let device_id = OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH));
services
.users
.create_device(
sender_user,
&device_id,
None,
(Some(&appservice.registration.as_token), None),
None,
None,
+6 -4
View File
@@ -23,7 +23,7 @@ pub(crate) async fn events_route(
State(services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let from = body
.body
@@ -62,9 +62,11 @@ pub(crate) async fn events_route(
.expect("configuration must limit maximum timeout");
loop {
let watchers = services
.sync
.watch(sender_user, sender_device, once(room_id).stream());
let watchers = services.sync.watch(
sender_user,
body.sender_device.as_deref(),
once(room_id).stream(),
);
let next_batch = services.globals.wait_pending().await?;
+2 -1
View File
@@ -35,7 +35,8 @@ pub(crate) async fn upload_keys_route(
State(services): State<crate::State>,
body: Ruma<upload_keys::v3::Request>,
) -> Result<upload_keys::v3::Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device()?;
let one_time_keys = body
.one_time_keys
+1 -2
View File
@@ -22,8 +22,7 @@ pub(crate) async fn invite_user_route(
invite_check(&services, sender_user, room_id).await?;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let invite_user::v3::InvitationRecipient::UserId { user_id } = &body.recipient else {
return Err!(Request(ThreepidDenied("Third party identifiers are not implemented")));
+17 -17
View File
@@ -2,13 +2,13 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use ruma::{
RoomId, RoomOrAliasId,
RoomId,
api::client::membership::{join_room_by_id, join_room_by_id_or_alias},
};
use tuwunel_core::{Result, warn};
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/r0/rooms/{roomId}/join`
///
@@ -28,23 +28,20 @@ pub(crate) async fn join_room_by_id_route(
let room_id: &RoomId = &body.room_id;
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
.await?;
banned_room_check(&services, sender_user, room_id, None, client).await?;
let (room_id, servers) =
get_join_params(&services, sender_user, <&RoomOrAliasId>::from(room_id), &[]).await?;
let state_lock = services.state.mutex.lock(&room_id).await;
let state_lock = services.state.mutex.lock(room_id).await;
let mut errors = 0_usize;
while let Err(e) = services
.membership
.join(
sender_user,
&room_id,
room_id,
None,
body.reason.clone(),
&servers,
&body.appservice_info,
&[],
body.appservice_info.is_some(),
&state_lock,
)
.boxed()
@@ -62,7 +59,7 @@ pub(crate) async fn join_room_by_id_route(
drop(state_lock);
Ok(join_room_by_id::v3::Response { room_id })
Ok(join_room_by_id::v3::Response { room_id: room_id.to_owned() })
}
/// # `POST /_matrix/client/r0/join/{roomIdOrAlias}`
@@ -83,10 +80,12 @@ pub(crate) async fn join_room_by_id_or_alias_route(
let sender_user = body.sender_user();
let appservice_info = &body.appservice_info;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
let state_lock = services.state.mutex.lock(&room_id).await;
@@ -97,9 +96,10 @@ pub(crate) async fn join_room_by_id_or_alias_route(
.join(
sender_user,
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
appservice_info,
appservice_info.is_some(),
&state_lock,
)
.boxed()
@@ -117,5 +117,5 @@ pub(crate) async fn join_room_by_id_or_alias_route(
drop(state_lock);
Ok(join_room_by_id_or_alias::v3::Response { room_id })
Ok(join_room_by_id_or_alias::v3::Response { room_id: room_id.clone() })
}
+16 -563
View File
@@ -1,45 +1,10 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::{FutureExt, StreamExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, RoomId,
RoomVersionId, UserId,
api::{
client::knock::knock_room,
federation::{
membership::RawStrippedState,
{self},
},
},
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
},
};
use tuwunel_core::{
Err, Result, debug, debug_info, debug_warn, err, extract_variant, info,
matrix::{
PduCount,
event::{Event, gen_event_id},
pdu::{PduBuilder, PduEvent},
},
trace,
utils::{self},
warn,
};
use tuwunel_service::{
Services,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
},
};
use ruma::api::client::knock::knock_room;
use tuwunel_core::Result;
use super::banned_room_check;
use crate::{Ruma, client::membership::get_join_params};
use crate::Ruma;
/// # `POST /_matrix/client/*/knock/{roomIdOrAlias}`
///
@@ -51,542 +16,30 @@ pub(crate) async fn knock_room_route(
body: Ruma<knock_room::v3::Request>,
) -> Result<knock_room::v3::Response> {
let sender_user = body.sender_user();
let body = &body.body;
let (room_id, servers) =
get_join_params(&services, sender_user, &body.room_id_or_alias, &body.via).await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client)
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
knock_room_by_id_helper(&services, sender_user, &room_id, body.reason.clone(), &servers).await
}
async fn knock_room_by_id_helper(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
) -> Result<knock_room::v3::Response> {
let state_lock = services.state.mutex.lock(room_id).await;
if services
.state_cache
.is_invited(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already invited in {room_id} but attempted to knock");
return Err!(Request(Forbidden(
"You cannot knock on a room you are already invited/accepted to."
)));
}
if services
.state_cache
.is_joined(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already joined in {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are already joined in.")));
}
if services
.state_cache
.is_knocked(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already knocked in {room_id}");
return Ok(knock_room::v3::Response { room_id: room_id.into() });
}
if let Ok(membership) = services
.state_accessor
.get_member(room_id, sender_user)
.await
{
if membership.membership == MembershipState::Ban {
debug_warn!("{sender_user} is banned from {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
}
let server_in_room = services
.state_cache
.server_in_room(services.globals.server_name(), room_id)
.await;
let local_knock = server_in_room
|| servers.is_empty()
|| (servers.len() == 1 && services.globals.server_is_ours(&servers[0]));
if local_knock {
knock_room_helper_local(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
} else {
knock_room_helper_remote(services, sender_user, room_id, reason, servers, state_lock)
.boxed()
.await?;
}
Ok(knock_room::v3::Response::new(room_id.to_owned()))
}
async fn knock_room_helper_local(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
debug_info!("We can knock locally");
let room_version_id = services.state.get_room_version(room_id).await?;
if matches!(
room_version_id,
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
) {
return Err!(Request(Forbidden("This room does not support knocking.")));
}
let content = RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason: reason.clone(),
..RoomMemberEventContent::new(MembershipState::Knock)
};
// Try normal knock first
let Err(error) = services
.timeline
.build_and_append_pdu(
PduBuilder::state(sender_user.to_string(), &content),
sender_user,
room_id,
&state_lock,
)
.await
else {
return Ok(());
};
if servers.is_empty() || (servers.len() == 1 && services.globals.server_is_ours(&servers[0]))
{
return Err(error);
}
warn!("We couldn't do the knock locally, maybe federation can help to satisfy the knock");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_knock_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.federation
.execute(&remote_server, send_knock_request)
banned_room_check(&services, sender_user, &room_id, Some(&body.room_id_or_alias), client)
.await?;
info!("send_knock finished");
let state_lock = services.state.mutex.lock(&room_id).await;
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
.membership
.knock(
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&room_id,
Some(&body.room_id_or_alias),
body.reason.clone(),
&servers,
&state_lock,
)
.await?;
Ok(())
}
drop(state_lock);
async fn knock_room_helper_remote(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: RoomMutexGuard,
) -> Result {
info!("Knocking {room_id} over federation.");
let (make_knock_response, remote_server) =
make_knock_request(services, sender_user, room_id, servers).await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub: CanonicalJsonObject =
serde_json::from_str(make_knock_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(services.globals.server_name().as_str().to_owned()),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(),
blurhash: services.users.blurhash(sender_user).await.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Going through send_knock response knock state events");
let state = send_knock_response
.knock_room_state
.iter()
.map(|event| {
serde_json::from_str::<CanonicalJsonObject>(
extract_variant!(event.clone(), RawStrippedState::Stripped)
.expect("Raw<AnyStrippedStateEvent>")
.json()
.get(),
)
})
.filter_map(Result::ok);
let mut state_map: HashMap<u64, OwnedEventId> = HashMap::new();
for event in state {
let Some(state_key) = event.get("state_key") else {
debug_warn!("send_knock stripped state event missing state_key: {event:?}");
continue;
};
let Some(event_type) = event.get("type") else {
debug_warn!("send_knock stripped state event missing event type: {event:?}");
continue;
};
let Ok(state_key) = serde_json::from_value::<String>(state_key.clone().into()) else {
debug_warn!("send_knock stripped state event has invalid state_key: {event:?}");
continue;
};
let Ok(event_type) = serde_json::from_value::<StateEventType>(event_type.clone().into())
else {
debug_warn!("send_knock stripped state event has invalid event type: {event:?}");
continue;
};
let event_id = gen_event_id(&event, &room_version_id)?;
let shortstatekey = services
.short
.get_or_create_shortstatekey(&event_type, &state_key)
.await;
services
.timeline
.add_pdu_outlier(&event_id, &event);
state_map.insert(shortstatekey, event_id.clone());
}
info!("Compressing state from send_knock");
let compressed: CompressedState = services
.state_compressor
.compress_state_events(
state_map
.iter()
.map(|(ssk, eid)| (ssk, eid.borrow())),
)
.collect()
.await;
debug!("Saving compressed state");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_knock,
added,
removed,
} = services
.state_compressor
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
services
.state
.force_state(room_id, statehash_before_knock, added, removed, &state_lock)
.await?;
let statehash_after_knock = services
.state
.append_to_state(&parsed_knock_pdu)
.await?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = services.globals.next_count();
services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
&state_lock,
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
services
.state
.set_room_state(room_id, statehash_after_knock, &state_lock);
Ok(())
}
async fn make_knock_request(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
servers: &[OwnedServerName],
) -> Result<(federation::membership::prepare_knock_event::v1::Response, OwnedServerName)> {
let mut make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
let mut make_knock_counter: usize = 0;
for remote_server in servers {
if services.globals.server_is_ours(remote_server) {
continue;
}
info!("Asking {remote_server} for make_knock ({make_knock_counter})");
let make_knock_response = services
.federation
.execute(remote_server, federation::membership::prepare_knock_event::v1::Request {
room_id: room_id.to_owned(),
user_id: sender_user.to_owned(),
ver: services
.server
.supported_room_versions()
.collect(),
})
.await;
trace!("make_knock response: {make_knock_response:?}");
make_knock_counter = make_knock_counter.saturating_add(1);
make_knock_response_and_server = make_knock_response.map(|r| (r, remote_server.clone()));
if make_knock_response_and_server.is_ok() {
break;
}
if make_knock_counter > 40 {
warn!(
"50 servers failed to provide valid make_knock response, assuming no server can \
assist in knocking."
);
make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
return make_knock_response_and_server;
}
}
make_knock_response_and_server
Ok(knock_room::v3::Response::new(room_id.clone()))
}
+38 -153
View File
@@ -8,15 +8,12 @@ mod leave;
mod members;
mod unban;
use std::{cmp::Ordering, net::IpAddr};
use std::net::IpAddr;
use axum::extract::State;
use futures::{FutureExt, StreamExt};
use ruma::{
OwnedRoomId, OwnedServerName, RoomId, RoomOrAliasId, ServerName, UserId,
api::client::membership::joined_rooms,
};
use tuwunel_core::{Err, Result, result::LogErr, utils::shuffle, warn};
use ruma::{RoomId, RoomOrAliasId, UserId, api::client::membership::joined_rooms};
use tuwunel_core::{Err, Result, result::LogErr, warn};
use tuwunel_service::Services;
pub(crate) use self::{
@@ -58,57 +55,42 @@ pub(crate) async fn joined_rooms_route(
pub(crate) async fn banned_room_check(
services: &Services,
user_id: &UserId,
room_id: Option<&RoomId>,
server_name: Option<&ServerName>,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
client_ip: IpAddr,
) -> Result {
if services.users.is_admin(user_id).await {
return Ok(());
}
// TODO: weird condition
if let Some(room_id) = room_id {
if services.metadata.is_banned(room_id).await
|| (room_id.server_name().is_some()
&& services
.config
.forbidden_remote_server_names
.is_match(
room_id
.server_name()
.expect("legacy room mxid")
.host(),
)) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or \
attempted to join a banned room or banned room server name: {room_id}"
);
// room id is banned ...
if services.metadata.is_banned(room_id).await
// ... or legacy room id server is banned ...
|| room_id.server_name().is_some_and(|server_name| {
services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
})
// ... or alias server is banned
|| orig_room_id.is_some_and(|orig_room_id| {
orig_room_id.server_name().is_some_and(|orig_server_name|
services
.config
.forbidden_remote_server_names
.is_match(orig_server_name.host()))
}) {
warn!(
"User {user_id} who is not an admin attempted to send an invite for or attempted to \
join a banned room or banned room server name: {room_id}"
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
} else if let Some(server_name) = server_name {
if services
.config
.forbidden_remote_server_names
.is_match(server_name.host())
{
warn!(
"User {user_id} who is not an admin tried joining a room which has the server \
name {server_name} that is globally forbidden. Rejecting.",
);
maybe_deactivate(services, user_id, client_ip)
.await
.log_err()
.ok();
return Err!(Request(Forbidden("This remote server is banned on this homeserver.")));
}
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
Ok(())
@@ -120,16 +102,15 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
.config
.auto_deactivate_banned_room_attempts
{
warn!("Automatically deactivating user {user_id} due to attempted banned room join");
let notice = format!(
"Automatically deactivating user {user_id} due to attempted banned room join from \
IP {client_ip}"
);
warn!("{notice}");
if services.server.config.admin_room_notices {
services
.admin
.send_text(&format!(
"Automatically deactivating user {user_id} due to attempted banned room \
join from IP {client_ip}"
))
.await;
services.admin.send_text(&notice).await;
}
services
@@ -141,99 +122,3 @@ async fn maybe_deactivate(services: &Services, user_id: &UserId, client_ip: IpAd
Ok(())
}
// TODO: should this be in services? banned check would have to resolve again if
// room_id is not available at callsite
async fn get_join_params(
services: &Services,
user_id: &UserId,
room_id_or_alias: &RoomOrAliasId,
via: &[OwnedServerName],
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
// servers tried first, additional_servers shuffled then tried after
let (room_id, mut primary_servers, mut additional_servers) =
match OwnedRoomId::try_from(room_id_or_alias.to_owned()) {
// if room id, shuffle via + room_id server_name ...
| Ok(room_id) => {
let mut additional_servers = via.to_vec();
if let Some(server) = room_id.server_name() {
additional_servers.push(server.to_owned());
}
(room_id, Vec::new(), additional_servers)
},
// ... if room alias, resolve and don't shuffle ...
| Err(room_alias) => {
let (room_id, servers) = services.alias.resolve_alias(&room_alias).await?;
(room_id, servers, Vec::new())
},
};
// either way, add invited vias
additional_servers.extend(
services
.state_cache
.servers_invite_via(&room_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.await,
);
// either way, add invite senders' servers
additional_servers.extend(
services
.state_cache
.invite_state(user_id, &room_id)
.await
.unwrap_or_default()
.iter()
.filter_map(|event| event.get_field("sender").ok().flatten())
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
primary_servers.sort_unstable();
primary_servers.dedup();
shuffle(&mut primary_servers);
// shuffle additionals, append to base servers
additional_servers.sort_unstable();
additional_servers.dedup();
shuffle(&mut additional_servers);
let mut servers: Vec<_> = room_id_or_alias
.server_name()
.filter(|_| room_id_or_alias.is_room_alias_id())
.map(ToOwned::to_owned)
.into_iter()
.chain(primary_servers.into_iter())
.chain(additional_servers.into_iter())
.collect();
// sort deprioritized servers last
servers.sort_by(|a, b| {
let a_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(a.host());
let b_matches = services
.server
.config
.deprioritize_joins_through_servers
.is_match(b.host());
if a_matches && !b_matches {
Ordering::Greater
} else if !a_matches && b_matches {
Ordering::Less
} else {
Ordering::Equal
}
});
Ok((room_id, servers))
}
-3
View File
@@ -88,9 +88,6 @@ pub(super) use user_directory::*;
pub(super) use voip::*;
pub(super) use well_known::*;
/// generated device ID length
const DEVICE_ID_LENGTH: usize = 10;
/// generated user access token length
const TOKEN_LENGTH: usize = tuwunel_service::users::device::TOKEN_LENGTH;
+1 -1
View File
@@ -589,7 +589,7 @@ pub(crate) async fn set_pushers_route(
services
.pusher
.set_pusher(sender_user, body.sender_device(), &body.action)
.set_pusher(sender_user, body.sender_device()?, &body.action)
.await?;
Ok(set_pusher::v3::Response::new())
+61 -243
View File
@@ -2,26 +2,20 @@ use std::fmt::Write;
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use futures::FutureExt;
use register::RegistrationKind;
use ruma::{
UserId,
api::client::{
account::{
check_registration_token_validity, get_username_availability,
register::{self, LoginType},
register::{self, LoginType, RegistrationKind},
},
uiaa::{AuthFlow, AuthType, UiaaInfo},
},
events::GlobalAccountDataEventType,
push,
};
use tuwunel_core::{
Err, Error, Result, debug_info, debug_warn, error, info, is_equal_to, utils, warn,
};
use tuwunel_service::users::device::generate_refresh_token;
use tuwunel_core::{Err, Error, Result, debug_info, debug_warn, info, utils};
use tuwunel_service::users::{Register, device::generate_refresh_token};
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH};
use super::SESSION_ID_LENGTH;
use crate::Ruma;
const RANDOM_USER_ID_LENGTH: usize = 10;
@@ -48,15 +42,10 @@ pub(crate) async fn get_register_available_route(
.appservice_info
.as_ref()
.is_some_and(|appservice| {
appservice.registration.id == "irc"
|| appservice
.registration
.id
.contains("matrix-appservice-irc")
|| appservice
.registration
.id
.contains("matrix_appservice_irc")
let id = &appservice.registration.id;
id == "irc"
|| id.contains("matrix-appservice-irc")
|| id.contains("matrix_appservice_irc")
});
if services
@@ -148,71 +137,32 @@ pub(crate) async fn register_route(
let is_guest = body.kind == RegistrationKind::Guest;
let emergency_mode_enabled = services.config.emergency_password.is_some();
let user = body.username.as_deref().unwrap_or("");
let device_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if !services.config.allow_registration && body.appservice_info.is_none() {
match (body.username.as_ref(), body.initial_device_display_name.as_ref()) {
| (Some(username), Some(device_display_name)) => {
info!(
%is_guest,
user = %username,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (Some(username), _) => {
info!(
%is_guest,
user = %username,
"Rejecting registration attempt as registration is disabled"
);
},
| (_, Some(device_display_name)) => {
info!(
%is_guest,
device_name = %device_display_name,
"Rejecting registration attempt as registration is disabled"
);
},
| (None, _) => {
info!(
%is_guest,
"Rejecting registration attempt as registration is disabled"
);
},
}
info!(
%is_guest,
%user,
%device_name,
"Rejecting registration attempt as registration is disabled"
);
return Err!(Request(Forbidden("Registration has been disabled.")));
}
if is_guest && !services.config.allow_guest_registration {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
debug_warn!(
"Guest registration disabled / registration enabled with token configured, \
rejecting guest registration attempt, initial device name: \"{display_name}\""
%device_name,
"Guest registration disabled, rejecting guest registration attempt"
);
return Err!(Request(GuestAccessForbidden("Guest registration is disabled.")));
}
// forbid guests from registering if there is not a real admin user yet. give
// generic user error.
if is_guest && services.users.count().await < 2 {
let display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
warn!(
"Guest account attempted to register before a real admin user has been registered, \
rejecting registration. Guest's initial device name: \"{display_name}\""
);
return Err!(Request(Forbidden("Registration is temporarily disabled.")));
}
let user_id = match (body.username.as_ref(), is_guest) {
| (Some(username), false) => {
// workaround for https://github.com/matrix-org/matrix-appservice-irc/issues/1780 due to inactivity of fixing the issue
@@ -313,7 +263,13 @@ pub(crate) async fn register_route(
// UIAA
let mut uiaainfo;
let skip_auth = if services.globals.registration_token.is_some() && !is_guest {
let skip_auth = if !services
.globals
.get_registration_tokens()
.await
.is_empty()
&& !is_guest
{
// Registration token required
uiaainfo = UiaaInfo {
flows: vec![AuthFlow {
@@ -378,45 +334,16 @@ pub(crate) async fn register_route(
let password = if is_guest { None } else { body.password.as_deref() };
// Create user
services
.users
.create(&user_id, password, None)
.await?;
// Default to pretty displayname
let mut displayname = user_id.localpart().to_owned();
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
if !services
.config
.new_user_displayname_suffix
.is_empty()
&& body.appservice_info.is_none()
{
write!(displayname, " {}", services.server.config.new_user_displayname_suffix)?;
}
services
.users
.set_displayname(&user_id, Some(displayname.clone()));
// Initial account data
services
.account_data
.update(
None,
&user_id,
GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(&user_id),
},
})?,
)
.full_register(Register {
user_id: Some(&user_id),
password,
appservice_info: body.appservice_info.as_ref(),
is_guest,
grant_first_user_admin: true,
..Default::default()
})
.await?;
if (!is_guest && body.inhibit_login)
@@ -426,169 +353,56 @@ pub(crate) async fn register_route(
.is_some_and(|appservice| appservice.registration.device_management)
{
return Ok(register::v3::Response {
access_token: None,
user_id,
device_id: None,
access_token: None,
refresh_token: None,
expires_in: None,
});
}
// Generate new device id if the user didn't specify one
let device_id = if is_guest { None } else { body.device_id.clone() }
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
let device_id = if is_guest { None } else { body.device_id.as_deref() };
// Generate new token for the device
let (access_token, expires_in) = services
.users
.generate_access_token(body.body.refresh_token);
.generate_access_token(body.refresh_token);
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Create device for this account
services
let device_id = services
.users
.create_device(
&user_id,
&device_id,
device_id,
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?;
debug_info!(%user_id, %device_id, "User account was created");
let device_display_name = body
.initial_device_display_name
.as_deref()
.unwrap_or("");
if body.appservice_info.is_none() && (!is_guest || services.config.log_guest_registrations) {
let mut notice = String::from(if is_guest { "New guest user" } else { "New user" });
// log in conduit admin channel if a non-guest user registered
if body.appservice_info.is_none() && !is_guest {
if !device_display_name.is_empty() {
let notice = format!(
"New user \"{user_id}\" registered on this server from IP {client} and device \
display name \"{device_display_name}\""
);
write!(notice, " registered on this server from IP {client}")?;
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
} else {
let notice = format!("New user \"{user_id}\" registered on this server.");
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
if let Some(device_name) = body.initial_device_display_name.as_deref() {
write!(notice, " with device name {device_name}")?;
}
}
// log in conduit admin channel if a guest registered
if body.appservice_info.is_none() && is_guest && services.config.log_guest_registrations {
debug_info!("New guest user \"{user_id}\" registered on this server.");
if !device_display_name.is_empty() {
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with device display name \
\"{device_display_name}\" registered on this server from IP {client}"
))
.await;
}
if !is_guest {
info!("{notice}");
} else {
#[allow(clippy::collapsible_else_if)]
if services.server.config.admin_room_notices {
services
.admin
.notice(&format!(
"Guest user \"{user_id}\" with no device display name registered on \
this server from IP {client}",
))
.await;
}
debug_info!("{notice}");
}
}
// If this is the first real user, grant them admin privileges except for guest
// users
// Note: the server user is generated first
if !is_guest
&& services.config.grant_admin_to_first_user
&& let Ok(admin_room) = services.admin.get_admin_room().await
&& services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
services
.admin
.make_user_admin(&user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
if body.appservice_info.is_none()
&& !services.server.config.auto_join_rooms.is_empty()
&& (services.config.allow_guests_auto_join_rooms || !is_guest)
{
for room in &services.server.config.auto_join_rooms {
let Ok(room_id) = services.alias.maybe_resolve(room).await else {
error!(
"Failed to resolve room alias to room ID when attempting to auto join \
{room}, skipping"
);
continue;
};
if !services
.state_cache
.server_in_room(services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
if let Some(room_server_name) = room.server_name() {
let state_lock = services.state.mutex.lock(&room_id).await;
match services
.membership
.join(
&user_id,
&room_id,
Some("Automatically joining this room upon registration".to_owned()),
&[services.globals.server_name().to_owned(), room_server_name.to_owned()],
&body.appservice_info,
&state_lock,
)
.boxed()
.await
{
| Err(e) => {
// don't return this error so we don't fail registrations
error!(
"Failed to automatically join room {room} for user {user_id}: {e}"
);
},
| _ => {
info!("Automatically joined room {room} for user {user_id}");
},
}
drop(state_lock);
}
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
}
@@ -611,9 +425,13 @@ pub(crate) async fn check_registration_token_validity(
State(services): State<crate::State>,
body: Ruma<check_registration_token_validity::v1::Request>,
) -> Result<check_registration_token_validity::v1::Response> {
let Some(reg_token) = services.globals.registration_token.clone() else {
return Err!(Request(Forbidden("Server does not allow token registration")));
};
let tokens = services.globals.get_registration_tokens().await;
Ok(check_registration_token_validity::v1::Response { valid: reg_token == body.token })
if tokens.is_empty() {
return Err!(Request(Forbidden("Server does not allow token registration")));
}
let valid = tokens.contains(&body.token);
Ok(check_registration_token_validity::v1::Response { valid })
}
+1 -2
View File
@@ -113,8 +113,7 @@ pub(crate) async fn get_relating_events_route(
name = "relations",
level = "debug",
skip_all,
fields(room_id, target, from, to, dir, limit, recurse),
ret(level = "trace")
fields(room_id, target, from, to, dir, limit, recurse)
)]
async fn paginate_relations_with_filter(
services: &Services,
+1 -1
View File
@@ -55,7 +55,7 @@ pub(crate) async fn get_room_summary(
) -> Result<get_summary::v1::Response> {
let (room_id, servers) = services
.alias
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(body.via.clone()))
.maybe_resolve_with_servers(&body.room_id_or_alias, Some(&body.via))
.await?;
if services.metadata.is_banned(&room_id).await {
+7 -2
View File
@@ -1,7 +1,7 @@
use futures::FutureExt;
use ruma::{OwnedUserId, UserId};
use tuwunel_core::{Err, Result, debug};
use tuwunel_service::Services;
use tuwunel_service::{Services, users::Register};
use super::password_login;
@@ -51,7 +51,12 @@ pub(super) async fn ldap_login(
if !services.users.exists(lowercased_user_id).await {
services
.users
.create(lowercased_user_id, Some("*"), Some("ldap"))
.full_register(Register {
user_id: Some(lowercased_user_id),
password: Some("*"),
origin: Some("ldap"),
..Default::default()
})
.await?;
}
+1 -1
View File
@@ -23,7 +23,7 @@ pub(crate) async fn logout_route(
) -> Result<logout::v3::Response> {
services
.users
.remove_device(body.sender_user(), body.sender_device())
.remove_device(body.sender_user(), body.sender_device()?)
.await;
Ok(logout::v3::Response::new())
+42 -30
View File
@@ -4,6 +4,7 @@ mod ldap;
mod logout;
mod password;
mod refresh;
mod sso;
mod token;
use axum::extract::State;
@@ -12,8 +13,8 @@ use ruma::api::client::session::{
get_login_types::{
self,
v3::{
ApplicationServiceLoginType, JwtLoginType, LoginType, PasswordLoginType,
TokenLoginType,
ApplicationServiceLoginType, IdentityProvider, JwtLoginType, LoginType,
PasswordLoginType, SsoLoginType, TokenLoginType,
},
},
login::{
@@ -21,16 +22,17 @@ use ruma::api::client::session::{
v3::{DiscoveryInfo, HomeserverInfo, LoginInfo},
},
};
use tuwunel_core::{Err, Result, info, utils, utils::stream::ReadyExt};
use tuwunel_core::{Err, Result, info, utils::stream::ReadyExt};
use tuwunel_service::users::device::generate_refresh_token;
use self::{ldap::ldap_login, password::password_login};
pub(crate) use self::{
logout::{logout_all_route, logout_route},
refresh::refresh_token_route,
sso::{sso_callback_route, sso_login_route, sso_login_with_provider_route},
token::login_token_route,
};
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use super::TOKEN_LENGTH;
use crate::Ruma;
/// # `GET /_matrix/client/v3/login`
@@ -50,6 +52,20 @@ pub(crate) async fn get_login_types_route(
LoginType::Token(TokenLoginType {
get_login_token: services.config.login_via_existing_session,
}),
LoginType::Sso(SsoLoginType {
identity_providers: services
.config
.identity_provider
.iter()
.cloned()
.map(|config| IdentityProvider {
id: config.id().to_owned(),
brand: Some(config.brand.clone().into()),
icon: config.icon,
name: config.name.unwrap_or(config.brand),
})
.collect(),
}),
]))
}
@@ -97,43 +113,39 @@ pub(crate) async fn login_route(
// Generate a new refresh_token if requested by client
let refresh_token = expires_in.is_some().then(generate_refresh_token);
// Generate new device id if the user didn't specify one
let device_id = body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Determine if device_id was provided and exists in the db for this user
let device_exists = services
.users
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await;
if !device_exists {
services
let device_id = if let Some(device_id) = &body.device_id
&& services
.users
.create_device(
&user_id,
&device_id,
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.clone(),
Some(client.to_string()),
)
.await?;
} else {
.all_device_ids(&user_id)
.ready_any(|v| v == device_id)
.await
{
services
.users
.set_access_token(
&user_id,
&device_id,
device_id,
&access_token,
expires_in,
refresh_token.as_deref(),
)
.await?;
}
device_id.clone()
} else {
services
.users
.create_device(
&user_id,
body.device_id.as_deref(),
(Some(&access_token), expires_in),
refresh_token.as_deref(),
body.initial_device_display_name.as_deref(),
Some(client.to_string()),
)
.await?
};
info!("{user_id} logged in");
+601
View File
@@ -0,0 +1,601 @@
use std::{borrow::Cow, time::Duration};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use axum_extra::extract::cookie::{Cookie, SameSite};
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD as b64};
use futures::{StreamExt, TryFutureExt, future::try_join};
use itertools::Itertools;
use reqwest::header::{CONTENT_TYPE, HeaderValue};
use ruma::{
Mxc, OwnedRoomId, OwnedUserId, ServerName, UserId,
api::client::session::{sso_callback, sso_login, sso_login_with_provider},
};
use serde::{Deserialize, Serialize};
use tuwunel_core::{
Err, Result, at,
debug::INFO_SPAN_LEVEL,
debug_info, debug_warn, err, info, utils,
utils::{
content_disposition::make_content_disposition,
hash::sha256,
result::{FlatOk, LogErr},
string::{EMPTY, truncate_deterministic},
timepoint_from_now, timepoint_has_passed,
},
warn,
};
use tuwunel_service::{
Services,
media::MXC_LENGTH,
oauth::{
CODE_VERIFIER_LENGTH, Provider, SESSION_ID_LENGTH, Session, UserInfo, unique_id,
unique_id_sub,
},
users::Register,
};
use url::Url;
use super::TOKEN_LENGTH;
use crate::Ruma;
/// Grant phase query string.
#[derive(Debug, Serialize)]
struct GrantQuery<'a> {
client_id: &'a str,
state: &'a str,
nonce: &'a str,
scope: &'a str,
response_type: &'a str,
access_type: &'a str,
code_challenge_method: &'a str,
code_challenge: &'a str,
redirect_uri: Option<&'a str>,
}
#[derive(Debug, Deserialize, Serialize)]
struct GrantCookie<'a> {
client_id: Cow<'a, str>,
state: Cow<'a, str>,
nonce: Cow<'a, str>,
redirect_uri: Cow<'a, str>,
}
static GRANT_SESSION_COOKIE: &str = "tuwunel_grant_session";
#[tracing::instrument(
name = "sso_login",
level = "debug",
skip_all,
fields(%client),
)]
pub(crate) async fn sso_login_route(
State(_services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
_body: Ruma<sso_login::v3::Request>,
) -> Result<sso_login::v3::Response> {
Err!(Request(NotImplemented(
"SSO login without specific provider has not been implemented."
)))
}
#[tracing::instrument(
name = "sso_login_with_provider",
level = "info",
skip_all,
ret(level = "debug")
fields(
%client,
idp_id = body.body.idp_id,
),
)]
pub(crate) async fn sso_login_with_provider_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<sso_login_with_provider::v3::Request>,
) -> Result<sso_login_with_provider::v3::Response> {
let sso_login_with_provider::v3::Request { idp_id, redirect_url } = body.body;
let Ok(redirect_url) = redirect_url.parse::<Url>() else {
return Err!(Request(InvalidParam("Invalid redirect_url")));
};
let provider = services.oauth.providers.get(&idp_id).await?;
let sess_id = utils::random_string(SESSION_ID_LENGTH);
let query_nonce = utils::random_string(CODE_VERIFIER_LENGTH);
let cookie_nonce = utils::random_string(CODE_VERIFIER_LENGTH);
let code_verifier = utils::random_string(CODE_VERIFIER_LENGTH);
let code_challenge = b64.encode(sha256::hash(code_verifier.as_bytes()));
let callback_uri = provider.callback_url.as_ref().map(Url::as_str);
let scope = provider.scope.iter().join(" ");
let query = GrantQuery {
client_id: &provider.client_id,
state: &sess_id,
nonce: &query_nonce,
access_type: "online",
response_type: "code",
code_challenge_method: "S256",
code_challenge: &code_challenge,
redirect_uri: callback_uri,
scope: scope
.is_empty()
.then_some("openid email profile")
.unwrap_or(scope.as_str()),
};
let location = provider
.authorization_url
.clone()
.map(|mut location| {
let query = serde_html_form::to_string(&query).ok();
location.set_query(query.as_deref());
location
})
.ok_or_else(|| {
err!(Config("authorization_url", "Missing required IdentityProvider config"))
})?;
let cookie_val = GrantCookie {
client_id: query.client_id.into(),
state: query.state.into(),
nonce: cookie_nonce.as_str().into(),
redirect_uri: redirect_url.as_str().into(),
};
let cookie_path = provider
.callback_url
.as_ref()
.map(Url::path)
.unwrap_or("/");
let cookie_max_age = provider
.grant_session_duration
.map(Duration::from_secs)
.expect("Defaulted to Some value during configure_idp()")
.try_into()
.expect("std::time::Duration to time::Duration conversion failure");
let cookie = Cookie::build((GRANT_SESSION_COOKIE, serde_html_form::to_string(&cookie_val)?))
.path(cookie_path)
.max_age(cookie_max_age)
.same_site(SameSite::None)
.secure(true)
.http_only(true)
.build()
.to_string()
.into();
let session = Session {
idp_id: Some(idp_id),
sess_id: Some(sess_id.clone()),
redirect_url: Some(redirect_url),
code_verifier: Some(code_verifier),
query_nonce: Some(query_nonce),
cookie_nonce: Some(cookie_nonce),
authorize_expires_at: provider
.grant_session_duration
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?,
..Default::default()
};
services
.oauth
.sessions
.put(&sess_id, &session)
.await;
Ok(sso_login_with_provider::v3::Response {
location: location.into(),
cookie: Some(cookie),
})
}
#[tracing::instrument(
name = "sso_callback"
level = "debug",
skip_all,
fields(
%client,
cookie = ?body.cookie,
body = ?body.body,
),
)]
pub(crate) async fn sso_callback_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<sso_callback::unstable::Request>,
) -> Result<sso_callback::unstable::Response> {
let sess_id = body
.body
.state
.as_deref()
.ok_or_else(|| err!(Request(Forbidden("Missing sess_id in callback."))))?;
let code = body
.body
.code
.as_deref()
.ok_or_else(|| err!(Request(Forbidden("Missing code in callback."))))?;
let session = services
.oauth
.sessions
.get(sess_id)
.map_err(|_| err!(Request(Forbidden("Invalid state in callback"))));
let idp_id = body.body.idp_id.as_str();
let provider = services.oauth.providers.get(idp_id);
let (provider, session) = try_join(provider, session).await.log_err()?;
let client_id = &provider.client_id;
if session.idp_id.as_deref() != Some(idp_id) {
return Err!(Request(Unauthorized("Identity Provider {idp_id} session not recognized.")));
}
if session
.authorize_expires_at
.map(timepoint_has_passed)
.unwrap_or(false)
{
return Err!(Request(Unauthorized("Authorization grant session has expired.")));
}
let cookie = body
.cookie
.get(GRANT_SESSION_COOKIE)
.map(Cookie::value)
.map(serde_html_form::from_str::<GrantCookie<'_>>)
.transpose()?
.ok_or_else(|| err!(Request(Unauthorized("Missing cookie {GRANT_SESSION_COOKIE:?}"))))?;
if cookie.client_id.as_ref() != client_id.as_str() {
return Err!(Request(Unauthorized("Client ID {client_id:?} cookie mismatch.")));
}
if Some(cookie.nonce.as_ref()) != session.cookie_nonce.as_deref() {
return Err!(Request(Unauthorized("Cookie nonce does not match session state.")));
}
if cookie.state.as_ref() != sess_id {
return Err!(Request(Unauthorized("Session ID {sess_id:?} cookie mismatch.")));
}
// Request access token.
let token_response = services
.oauth
.request_token((&provider, &session), code)
.await?;
let token_expires_at = token_response
.expires_in
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?;
let refresh_token_expires_at = token_response
.refresh_token_expires_in
.map(Duration::from_secs)
.map(timepoint_from_now)
.transpose()?;
// Update the session with access token results
let session = Session {
scope: token_response.scope,
token_type: token_response.token_type,
access_token: token_response.access_token,
expires_at: token_expires_at,
refresh_token: token_response.refresh_token,
refresh_token_expires_at,
..session
};
// Request userinfo claims.
let userinfo = services
.oauth
.request_userinfo((&provider, &session))
.await?;
// Check for an existing session from this identity. We want to maintain one
// session for each identity and keep the newer one which has up-to-date state
// and access.
let (user_id, old_sess_id) = match services
.oauth
.sessions
.get_by_unique_id(&unique_id_sub((&provider, &userinfo.sub))?)
.await
{
| Ok(session) => (session.user_id, session.sess_id),
| Err(error) if !error.is_not_found() => return Err(error),
| Err(_) => (None, None),
};
// Update the session with userinfo
let session = Session {
user_info: Some(userinfo.clone()),
..session
};
// Keep the user_id from the old session as best as possible.
let user_id = match user_id {
| Some(user_id) => user_id,
| None => decide_user_id(&services, &provider, &session, &userinfo).await?,
};
// Update the session with user_id
let session = Session {
user_id: Some(user_id.clone()),
..session
};
// Attempt to register a non-existing user.
if !services.users.exists(&user_id).await {
register_user(&services, &provider, &session, &userinfo, &user_id).await?;
}
// Commit the updated session.
services
.oauth
.sessions
.put(sess_id, &session)
.await;
// Delete any old session.
if let Some(old_sess_id) = old_sess_id
&& sess_id != old_sess_id
{
services.oauth.sessions.delete(&old_sess_id).await;
}
if !services.users.is_active_local(&user_id).await {
return Err!(Request(UserDeactivated("This user has been deactivated.")));
}
// Allow the user to login to Matrix.
let login_token = utils::random_string(TOKEN_LENGTH);
let _login_token_expires_in = services
.users
.create_login_token(&user_id, &login_token);
let location = session
.redirect_url
.as_ref()
.ok_or_else(|| err!(Request(InvalidParam("Missing redirect URL in session data"))))?
.clone()
.query_pairs_mut()
.append_pair("loginToken", &login_token)
.finish()
.to_string();
let cookie = Cookie::build((GRANT_SESSION_COOKIE, EMPTY))
.removal()
.build()
.to_string()
.into();
Ok(sso_callback::unstable::Response { location, cookie: Some(cookie) })
}
#[tracing::instrument(
name = "register",
level = INFO_SPAN_LEVEL,
skip_all,
fields(user_id, userinfo)
)]
async fn register_user(
services: &Services,
provider: &Provider,
session: &Session,
userinfo: &UserInfo,
user_id: &UserId,
) -> Result {
debug_info!(%user_id, "Creating new user account...");
services
.users
.full_register(Register {
user_id: Some(user_id),
password: Some("*"),
origin: Some("sso"),
displayname: userinfo.name.as_deref(),
..Default::default()
})
.await?;
if let Some(avatar_url) = userinfo
.avatar_url
.as_deref()
.or(userinfo.picture.as_deref())
{
set_avatar(services, provider, session, userinfo, user_id, avatar_url)
.await
.ok();
}
let idp_id = provider.id();
let idp_name = provider
.name
.as_deref()
.unwrap_or(provider.brand.as_str());
// log in conduit admin channel if a non-guest user registered
let notice =
format!("New user \"{user_id}\" registered on this server via {idp_name} ({idp_id})",);
info!("{notice}");
if services.server.config.admin_room_notices {
services.admin.notice(&notice).await;
}
Ok(())
}
#[tracing::instrument(level = "debug", skip_all, fields(user_id, avatar_url))]
async fn set_avatar(
services: &Services,
_provider: &Provider,
_session: &Session,
_userinfo: &UserInfo,
user_id: &UserId,
avatar_url: &str,
) -> Result {
use reqwest::Response;
let response = services
.client
.default
.get(avatar_url)
.send()
.await
.and_then(Response::error_for_status)?;
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(HeaderValue::to_str)
.flat_ok()
.map(ToOwned::to_owned);
let mxc = Mxc {
server_name: services.globals.server_name(),
media_id: &utils::random_string(MXC_LENGTH),
};
let content_disposition = make_content_disposition(None, content_type.as_deref(), None);
let bytes = response.bytes().await?;
services
.media
.create(&mxc, Some(user_id), Some(&content_disposition), content_type.as_deref(), &bytes)
.await?;
let all_joined_rooms: Vec<OwnedRoomId> = services
.state_cache
.rooms_joined(user_id)
.map(ToOwned::to_owned)
.collect()
.await;
let mxc_uri = mxc.to_string().into();
services
.users
.update_avatar_url(user_id, Some(mxc_uri), None, &all_joined_rooms)
.await;
Ok(())
}
#[tracing::instrument(
level = "debug",
ret(level = "debug")
skip_all,
fields(user),
)]
async fn decide_user_id(
services: &Services,
provider: &Provider,
session: &Session,
userinfo: &UserInfo,
) -> Result<OwnedUserId> {
let allowed =
|claim: &str| provider.userid_claims.is_empty() || provider.userid_claims.contains(claim);
let choices = [
userinfo
.preferred_username
.as_deref()
.map(str::to_lowercase)
.filter(|_| allowed("preferred_username")),
userinfo
.nickname
.as_deref()
.map(str::to_lowercase)
.filter(|_| allowed("nickname")),
provider
.brand
.eq(&"github")
.then_some(userinfo.sub.as_str())
.map(str::to_lowercase)
.filter(|_| allowed("login")),
userinfo
.email
.as_deref()
.and_then(|email| email.split_once('@'))
.map(at!(0))
.map(str::to_lowercase)
.filter(|_| allowed("email")),
];
for choice in choices.into_iter().flatten() {
if let Some(user_id) = try_user_id(services, &choice, false).await {
return Ok(user_id);
}
}
if let Ok(infallible) = unique_id((provider, session))
.map(|h| truncate_deterministic(&h, Some(15..23)).to_lowercase())
.log_err()
{
if let Some(user_id) = try_user_id(services, &infallible, true).await {
return Ok(user_id);
}
}
Err!(Request(UserInUse("User ID is not available.")))
}
#[tracing::instrument(level = "debug", skip_all, fields(username))]
async fn try_user_id(
services: &Services,
username: &str,
may_exist: bool,
) -> Option<OwnedUserId> {
let server_name = services.globals.server_name();
let user_id = parse_user_id(server_name, username)
.inspect_err(|e| warn!(?username, "Username invalid: {e}"))
.ok()?;
if services
.config
.forbidden_usernames
.is_match(username)
{
warn!(?username, "Username forbidden.");
return None;
}
if services.users.exists(&user_id).await {
debug_warn!(?username, "Username exists.");
if services
.users
.origin(&user_id)
.await
.ok()
.is_none_or(|origin| origin != "sso")
{
debug_warn!(?username, "Username has non-sso origin.");
return None;
}
if !may_exist {
return None;
}
}
Some(user_id)
}
fn parse_user_id(server_name: &ServerName, username: &str) -> Result<OwnedUserId> {
match UserId::parse_with_server_name(username, server_name) {
| Err(e) =>
Err!(Request(InvalidUsername(debug_error!("Username {username} is not valid: {e}")))),
| Ok(user_id) => match user_id.validate_strict() {
| Ok(()) => Ok(user_id),
| Err(e) => Err!(Request(InvalidUsername(debug_error!(
"Username {username} contains disallowed characters or spaces: {e}"
)))),
},
}
}
+9 -44
View File
@@ -4,19 +4,16 @@ use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use ruma::{
OwnedUserId,
api::client::{
session::{
get_login_token,
login::v3::{Request, Token},
},
uiaa,
api::client::session::{
get_login_token,
login::v3::{Request, Token},
},
};
use tuwunel_core::{Err, Result, utils::random_string};
use tuwunel_service::{Services, uiaa::SESSION_ID_LENGTH};
use tuwunel_service::Services;
use super::TOKEN_LENGTH;
use crate::Ruma;
use crate::{Ruma, router::auth_uiaa};
pub(super) async fn handle_login(
services: &Services,
@@ -48,47 +45,15 @@ pub(crate) async fn login_token_route(
return Err!(Request(Forbidden("Login via an existing session is not enabled")));
}
// This route SHOULD have UIA
// TODO: How do we make only UIA sessions that have not been used before valid?
let (sender_user, sender_device) = body.sender();
let password_flow = uiaa::AuthFlow { stages: vec![uiaa::AuthType::Password] };
let mut uiaainfo = uiaa::UiaaInfo {
flows: vec![password_flow],
..Default::default()
};
match &body.auth {
| Some(auth) => {
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
if !worked {
return Err!(Uiaa(uiaainfo));
}
// Success!
},
| _ => match body.json_body.as_ref() {
| Some(json) => {
uiaainfo.session = Some(random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, sender_device, &uiaainfo, json);
return Err!(Uiaa(uiaainfo));
},
| _ => return Err!(Request(NotJson("No JSON body was sent when required."))),
},
let sender_user = auth_uiaa(&services, &body).await?;
if !services.users.is_active_local(&sender_user).await {
return Err!(Request(UserDeactivated("This user has been deactivated.")));
}
let login_token = random_string(TOKEN_LENGTH);
let expires_in = services
.users
.create_login_token(sender_user, &login_token);
.create_login_token(&sender_user, &login_token);
Ok(get_login_token::v1::Response {
expires_in: Duration::from_millis(expires_in),
+67 -32
View File
@@ -10,7 +10,7 @@ use futures::{
pin_mut,
};
use ruma::{
DeviceId, EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
DeviceId, EventId, OwnedRoomId, OwnedUserId, RoomId, UInt, UserId,
api::client::{
filter::FilterDefinition,
sync::sync_events::{
@@ -120,13 +120,15 @@ type PresenceUpdates = HashMap<OwnedUserId, PresenceEventContent>;
skip_all,
fields(
user_id = %body.sender_user(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
)
)]
pub(crate) async fn sync_events_route(
State(services): State<crate::State>,
body: Ruma<sync_events::v3::Request>,
) -> Result<sync_events::v3::Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let filter: OptionFuture<_> = body
.body
@@ -195,7 +197,8 @@ pub(crate) async fn sync_events_route(
if since < next_batch || full_state {
let response = build_sync_events(
&services,
body.sender(),
sender_user,
sender_device,
since,
next_batch,
full_state,
@@ -216,7 +219,8 @@ pub(crate) async fn sync_events_route(
// Wait for activity
if time::timeout_at(stop_at, watchers).await.is_err() || services.server.is_stopping() {
let response = build_empty_response(&services, body.sender(), next_batch).await;
let response =
build_empty_response(&services, sender_user, sender_device, next_batch).await;
trace!(since, next_batch, "empty response");
return Ok(response);
}
@@ -235,14 +239,22 @@ pub(crate) async fn sync_events_route(
async fn build_empty_response(
services: &Services,
(sender_user, sender_device): (&UserId, &DeviceId),
sender_user: &UserId,
sender_device: Option<&DeviceId>,
next_batch: u64,
) -> sync_events::v3::Response {
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
sync_events::v3::Response {
device_one_time_keys_count: services
.users
.count_one_time_keys(sender_user, sender_device)
.await,
device_one_time_keys_count: device_one_time_keys_count
.await
.unwrap_or_default(),
..sync_events::v3::Response::new(to_small_string(next_batch))
}
@@ -251,7 +263,6 @@ async fn build_empty_response(
#[tracing::instrument(
name = "build",
level = INFO_SPAN_LEVEL,
ret(level = "trace"),
skip_all,
fields(
%since,
@@ -261,7 +272,8 @@ async fn build_empty_response(
)]
async fn build_sync_events(
services: &Services,
(sender_user, sender_device): (&UserId, &DeviceId),
sender_user: &UserId,
sender_device: Option<&DeviceId>,
since: u64,
next_batch: u64,
full_state: bool,
@@ -387,27 +399,38 @@ async fn build_sync_events(
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let to_device_events = services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>();
let to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
.map(at!(1))
.collect::<Vec<_>>()
})
.into();
let device_one_time_keys_count = services
.users
.count_one_time_keys(sender_user, sender_device);
let device_one_time_keys_count: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.count_one_time_keys(sender_user, sender_device)
})
.into();
// Remove all to-device events the device received *last time*
let remove_to_device_events =
services
.users
.remove_to_device_events(sender_user, sender_device, since);
let remove_to_device_events: OptionFuture<_> = sender_device
.map(|sender_device| {
services
.users
.remove_to_device_events(sender_user, sender_device, since)
})
.into();
let (
account_data,
keys_changed,
device_one_time_keys_count,
((), to_device_events, presence_updates),
presence_updates,
(_, to_device_events, device_one_time_keys_count),
(
(joined_rooms, mut device_list_updates, left_encrypted_users),
left_rooms,
@@ -417,8 +440,8 @@ async fn build_sync_events(
) = join5(
account_data,
keys_changed,
device_one_time_keys_count,
join3(remove_to_device_events, to_device_events, presence_updates),
presence_updates,
join3(remove_to_device_events, to_device_events, device_one_time_keys_count),
join4(joined_rooms, left_rooms, invited_rooms, knocked_rooms),
)
.boxed()
@@ -454,7 +477,7 @@ async fn build_sync_events(
left: device_list_left,
changed: device_list_updates.into_iter().collect(),
},
device_one_time_keys_count,
device_one_time_keys_count: device_one_time_keys_count.unwrap_or_default(),
// Fallback keys are not yet supported
device_unused_fallback_key_types: None,
next_batch: to_small_string(next_batch),
@@ -465,7 +488,9 @@ async fn build_sync_events(
invite: invited_rooms,
knock: knocked_rooms,
},
to_device: ToDevice { events: to_device_events },
to_device: ToDevice {
events: to_device_events.unwrap_or_default(),
},
})
}
@@ -732,7 +757,7 @@ async fn load_left_room(
async fn load_joined_room(
services: &Services,
sender_user: &UserId,
sender_device: &DeviceId,
sender_device: Option<&DeviceId>,
ref room_id: OwnedRoomId,
since: u64,
next_batch: u64,
@@ -841,7 +866,7 @@ async fn load_joined_room(
let lazy_loading_context = &lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
device_id: sender_device,
room_id,
token: Some(since),
options: Some(&filter.room.state.lazy_load_options),
@@ -970,6 +995,13 @@ async fn load_joined_room(
.flatten()
.is_none_or(|last_count| last_count.gt(&since));
let send_notification_resets = last_notification_read
.flatten()
.is_some_and(|last_count| last_count.gt(&since));
let send_notification_count_filter =
|count: &UInt| *count != uint!(0) || send_notification_resets;
let notification_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
@@ -1144,7 +1176,10 @@ async fn load_joined_room(
.map(Event::into_format)
.collect(),
},
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
unread_notifications: UnreadNotificationsCount {
highlight_count: highlight_count.filter(send_notification_count_filter),
notification_count: notification_count.filter(send_notification_count_filter),
},
unread_thread_notifications: BTreeMap::new(),
};
+5 -4
View File
@@ -37,7 +37,7 @@ use crate::Ruma;
struct SyncInfo<'a> {
services: &'a Services,
sender_user: &'a UserId,
sender_device: &'a DeviceId,
sender_device: Option<&'a DeviceId>,
}
#[derive(Clone, Debug)]
@@ -69,7 +69,7 @@ type ListIds = SmallVec<[ListId; 1]>;
skip_all,
fields(
user_id = %body.sender_user().localpart(),
device_id = %body.sender_device(),
device_id = %body.sender_device.as_deref().map_or("<no device>", |x| x.as_str()),
conn_id = ?body.body.conn_id.clone().unwrap_or_default(),
since = ?body.body.pos.clone().unwrap_or_default(),
)
@@ -78,7 +78,8 @@ pub(crate) async fn sync_events_v5_route(
State(ref services): State<crate::State>,
body: Ruma<Request>,
) -> Result<Response> {
let (sender_user, sender_device) = body.sender();
let sender_user = body.sender_user();
let sender_device = body.sender_device.as_deref();
let request = &body.body;
let since = request
.pos
@@ -108,7 +109,7 @@ pub(crate) async fn sync_events_v5_route(
let conn = conn_val.lock();
let ping_presence = services
.presence
.maybe_ping_presence(sender_user, Some(sender_device), &request.set_presence)
.maybe_ping_presence(sender_user, sender_device, &request.set_presence)
.inspect_err(inspect_log)
.ok();
@@ -32,6 +32,9 @@ pub(super) async fn collect(
conn: &Connection,
) -> Result<response::E2EE> {
let SyncInfo { services, sender_user, sender_device, .. } = sync_info;
let Some(sender_device) = sender_device else {
return Ok(response::E2EE::default());
};
let keys_changed = services
.users
@@ -9,6 +9,10 @@ pub(super) async fn collect(
SyncInfo { services, sender_user, sender_device, .. }: SyncInfo<'_>,
conn: &Connection,
) -> Result<Option<response::ToDevice>> {
let Some(sender_device) = sender_device else {
return Ok(None);
};
services
.users
.remove_to_device_events(sender_user, sender_device, conn.globalsince)
+1 -1
View File
@@ -27,7 +27,7 @@ pub(crate) async fn turn_server_route(
let turn_secret = &services.globals.turn_secret;
let (username, password) = if !turn_secret.is_empty() {
let (username, password) = if let Some(turn_secret) = turn_secret {
let expiry = SecondsSinceUnixEpoch::from_system_time(
SystemTime::now()
.checked_add(Duration::from_secs(services.config.turn_ttl))
+6 -1
View File
@@ -38,6 +38,9 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::login_route)
.ruma_route(&client::login_token_route)
.ruma_route(&client::refresh_token_route)
.ruma_route(&client::sso_login_route)
.ruma_route(&client::sso_login_with_provider_route)
.ruma_route(&client::sso_callback_route)
.ruma_route(&client::whoami_route)
.ruma_route(&client::logout_route)
.ruma_route(&client::logout_all_route)
@@ -195,6 +198,9 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::room_initial_sync_route)
.route("/client/server.json", get(client::syncv3_client_server_json));
// SS endpoint not related to federation
router = router.ruma_route(&server::get_openid_userinfo_route);
if config.allow_federation {
router = router
.ruma_route(&server::get_server_version_route)
@@ -226,7 +232,6 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&server::get_profile_information_route)
.ruma_route(&server::get_keys_route)
.ruma_route(&server::claim_keys_route)
.ruma_route(&server::get_openid_userinfo_route)
.ruma_route(&server::get_hierarchy_route)
.ruma_route(&server::well_known_server)
.ruma_route(&server::get_content_route)
+12 -12
View File
@@ -1,6 +1,7 @@
use std::{fmt::Debug, mem, ops::Deref};
use axum::{body::Body, extract::FromRequest};
use axum_extra::extract::cookie::CookieJar;
use bytes::{BufMut, Bytes, BytesMut};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, DeviceId, OwnedDeviceId, OwnedServerName,
@@ -18,6 +19,9 @@ pub(crate) struct Args<T> {
/// Request struct body
pub(crate) body: T,
/// Cookies received from the useragent.
pub(crate) cookie: CookieJar,
/// Federation server authentication: X-Matrix origin
/// None when not a federation server.
pub(crate) origin: Option<OwnedServerName>,
@@ -40,11 +44,6 @@ pub(crate) struct Args<T> {
}
impl<T> Args<T> {
#[inline]
pub(crate) fn sender(&self) -> (&UserId, &DeviceId) {
(self.sender_user(), self.sender_device())
}
#[inline]
pub(crate) fn sender_user(&self) -> &UserId {
self.sender_user
@@ -52,19 +51,19 @@ impl<T> Args<T> {
.expect("user must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> &DeviceId {
self.sender_device
.as_deref()
.expect("user must be authenticated and device identified")
}
#[inline]
pub(crate) fn origin(&self) -> &ServerName {
self.origin
.as_deref()
.expect("server must be authenticated for this handler")
}
#[inline]
pub(crate) fn sender_device(&self) -> Result<&DeviceId> {
self.sender_device
.as_deref()
.ok_or(err!(Request(Forbidden("user must be authenticated and device identified"))))
}
}
impl<T> Deref for Args<T>
@@ -115,6 +114,7 @@ where
let auth = auth::auth(services, &mut request, json_body.as_ref(), &T::METADATA).await?;
Ok(Self {
body: make_body::<T>(services, &mut request, json_body.as_mut(), &auth)?,
cookie: request.cookie,
origin: auth.origin,
sender_user: auth.sender_user,
sender_device: auth.sender_device,
+16 -4
View File
@@ -5,7 +5,7 @@ use ruma::{
client::uiaa::{AuthData, AuthFlow, AuthType, Jwt, UiaaInfo},
},
};
use tuwunel_core::{Err, Error, Result, err, utils};
use tuwunel_core::{Err, Error, Result, err, is_equal_to, utils};
use tuwunel_service::{Services, uiaa::SESSION_ID_LENGTH};
use crate::{Ruma, client::jwt};
@@ -14,6 +14,8 @@ pub(crate) async fn auth_uiaa<T>(services: &Services, body: &Ruma<T>) -> Result<
where
T: IncomingRequest + Send + Sync,
{
let sender_device = body.sender_device()?;
let flows = [
AuthFlow::new([AuthType::Password].into()),
AuthFlow::new([AuthType::Jwt].into()),
@@ -51,7 +53,7 @@ where
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, body.sender_device(), auth, &uiaainfo)
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
if !worked {
@@ -65,13 +67,23 @@ where
| Some(ref json) => {
let sender_user = body
.sender_user
.as_ref()
.as_deref()
.ok_or_else(|| err!(Request(MissingToken("Missing access token."))))?;
// Skip UIAA for SSO/OIDC users.
if services
.users
.origin(sender_user)
.await
.is_ok_and(is_equal_to!("sso"))
{
return Ok(sender_user.to_owned());
}
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, body.sender_device(), &uiaainfo, json);
.create(sender_user, sender_device, &uiaainfo, json);
Err(Error::Uiaa(uiaainfo))
},
+8 -3
View File
@@ -9,7 +9,7 @@ use axum::{
use futures::{Future, TryFutureExt};
use http::Method;
use ruma::api::IncomingRequest;
use tuwunel_core::Result;
use tuwunel_core::{Result, trace};
use super::{Ruma, RumaResponse, State};
@@ -42,7 +42,7 @@ macro_rules! ruma_handler {
Fut: Future<Output = Result<Req::OutgoingResponse, Err>> + Send,
Req: IncomingRequest + Debug + Send + Sync + 'static,
Err: IntoResponse + Debug + Send,
<Req as IncomingRequest>::OutgoingResponse: Send,
<Req as IncomingRequest>::OutgoingResponse: Debug + Send,
$( $tx: FromRequestParts<State> + Send + Sync + 'static, )*
{
fn add_routes(&'static self, router: Router<State>) -> Router<State> {
@@ -53,8 +53,13 @@ macro_rules! ruma_handler {
}
fn add_route(&'static self, router: Router<State>, path: &str) -> Router<State> {
let action = |$($tx,)* req| self($($tx,)* req).map_ok(RumaResponse);
let method = method_to_filter(&Req::METADATA.method);
let action = |$($tx,)* req| {
self($($tx,)* req)
.inspect_ok(|result| trace!(?result))
.map_ok(RumaResponse)
};
router.route(path, on(method, action))
}
}
+4 -1
View File
@@ -1,6 +1,7 @@
use std::str;
use axum::{RequestExt, RequestPartsExt, extract::Path};
use axum_extra::extract::cookie::CookieJar;
use bytes::Bytes;
use http::request::Parts;
use serde::Deserialize;
@@ -17,6 +18,7 @@ pub(super) type UserId = SmallString<[u8; 48]>;
#[derive(Debug)]
pub(super) struct Request {
pub(super) cookie: CookieJar,
pub(super) path: Path<PathParams>,
pub(super) query: QueryParams,
pub(super) body: Bytes,
@@ -33,6 +35,7 @@ pub(super) async fn from(
let limited = request.with_limited_body();
let (mut parts, body) = limited.into_parts();
let cookie: CookieJar = parts.extract().await?;
let path: Path<PathParams> = parts.extract().await?;
let query = parts.uri.query().unwrap_or_default();
let query = serde_html_form::from_str(query)
@@ -44,5 +47,5 @@ pub(super) async fn from(
.await
.map_err(|e| err!(Request(TooLarge("Request body too large: {e}"))))?;
Ok(Request { path, query, body, parts })
Ok(Request { cookie, path, query, body, parts })
}
+51 -2
View File
@@ -3,13 +3,13 @@ use axum_client_ip::InsecureClientIp;
use base64::{Engine as _, engine::general_purpose};
use futures::StreamExt;
use ruma::{
CanonicalJsonValue, OwnedUserId, UserId,
CanonicalJsonValue, OwnedRoomId, OwnedUserId, RoomId, UserId,
api::{
client::error::ErrorKind,
federation::membership::{RawStrippedState, create_invite},
},
events::{
GlobalAccountDataEventType,
GlobalAccountDataEventType, StateEventType,
push_rules::PushRulesEvent,
room::member::{MembershipState, RoomMemberEventContent},
},
@@ -78,6 +78,26 @@ pub(crate) async fn create_invite_route(
let mut signed_event = utils::to_canonical_object(&body.event)
.map_err(|_| err!(Request(InvalidParam("Invite event is invalid."))))?;
let room_id: OwnedRoomId = signed_event
.get("room_id")
.try_into()
.map(RoomId::to_owned)
.map_err(|e| err!(Request(InvalidParam("Invalid room_id property: {e}"))))?;
if body.room_id != room_id {
return Err!(Request(InvalidParam("Event room_id does not match the request path.")));
}
let kind: StateEventType = signed_event
.get("type")
.and_then(CanonicalJsonValue::as_str)
.ok_or_else(|| err!(Request(BadJson("Missing type in event."))))?
.into();
if kind != StateEventType::RoomMember {
return Err!(Request(InvalidParam("Event must be m.room.member type.")));
}
let invited_user: OwnedUserId = signed_event
.get("state_key")
.try_into()
@@ -91,6 +111,19 @@ pub(crate) async fn create_invite_route(
return Err!(Request(InvalidParam("User does not belong to this homeserver.")));
}
let content: RoomMemberEventContent = signed_event
.get("content")
.cloned()
.map(Into::into)
.map(serde_json::from_value)
.transpose()
.map_err(|e| err!(Request(InvalidParam("Invalid content object in event: {e}"))))?
.ok_or_else(|| err!(Request(BadJson("Missing content in event."))))?;
if content.membership != MembershipState::Invite {
return Err!(Request(InvalidParam("Event membership must be invite.")));
}
// Make sure we're not ACL'ed from their room.
services
.event_handler
@@ -108,11 +141,27 @@ pub(crate) async fn create_invite_route(
// Add event_id back
signed_event.insert("event_id".into(), CanonicalJsonValue::String(event_id.to_string()));
let origin: Option<&str> = signed_event
.get("origin")
.and_then(CanonicalJsonValue::as_str);
let sender: &UserId = signed_event
.get("sender")
.try_into()
.map_err(|e| err!(Request(InvalidParam("Invalid sender property: {e}"))))?;
if sender.server_name() != body.origin() {
return Err!(Request(Forbidden("Can only send invites on behalf of your users.")));
}
if origin.is_some_and(|origin| origin != sender.server_name()) {
return Err!(Request(Forbidden("Your users can only be from your origin.")));
}
if origin.is_some_and(|origin| origin != body.origin()) {
return Err!(Request(Forbidden("Can only send events from your origin.")));
}
if services.metadata.is_banned(&body.room_id).await
&& !services.users.is_admin(&invited_user).await
{
+2
View File
@@ -85,6 +85,8 @@ reqwest.workspace = true
ring.workspace = true
ruma.workspace = true
sanitize-filename.workspace = true
serde_core.workspace = true
serde_html_form.workspace = true
serde_json.workspace = true
serde_regex.workspace = true
serde_yaml.workspace = true
+143 -9
View File
@@ -3,7 +3,8 @@ pub mod manager;
pub mod proxy;
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashSet},
hash::{Hash, Hasher},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
path::{Path, PathBuf},
};
@@ -16,7 +17,7 @@ use figment::providers::{Env, Format, Toml};
pub use figment::{Figment, value::Value as FigmentValue};
use regex::RegexSet;
use ruma::{
OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomVersionId,
OwnedMxcUri, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomVersionId,
api::client::discovery::discover_support::ContactRole,
};
use serde::{Deserialize, de::IgnoredAny};
@@ -28,6 +29,7 @@ pub use self::{check::check, manager::Manager};
use crate::{
Result, err,
error::Error,
utils,
utils::{string::EMPTY, sys},
};
@@ -57,7 +59,7 @@ use crate::{
### https://tuwunel.chat/configuration.html
"#,
ignore = "catchall well_known tls blurhashing allow_invalid_tls_certificates ldap jwt \
appservice"
appservice identity_provider"
)]
pub struct Config {
/// The server_name is the pretty name of this server. It is used as a
@@ -1006,7 +1008,7 @@ pub struct Config {
///
/// display: sensitive
#[serde(default)]
pub turn_secret: String,
pub turn_secret: Option<String>,
/// TURN secret to use that's read from the file path specified.
///
@@ -2111,11 +2113,6 @@ pub struct Config {
///
/// This option exists for developer and debug use, and as a failsafe in
/// lieu of hardcoding it.
///
/// This currently defaults to false as a matter of development until
/// real-world testing can shake out any implementation issues rather than
/// jeopardize existing rooms, but otherwise will default to true at the
/// next point release or patch.
#[serde(default = "true_fn")]
pub hydra_backports: bool,
@@ -2158,6 +2155,13 @@ pub struct Config {
#[serde(default)]
pub appservice: BTreeMap<String, AppService>,
// external structure; separate sections
#[serde(default)]
pub identity_provider: HashSet<IdentityProvider>,
#[serde(default)]
pub sso_aware_preferred: bool,
#[serde(flatten)]
#[allow(clippy::zero_sized_map_values)]
// this is a catchall, the map shouldn't be zero at runtime
@@ -2473,6 +2477,134 @@ pub struct JwtConfig {
pub validate_signature: bool,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[config_example_generator(
filename = "tuwunel-example.toml",
section = "[global.identity_provider]"
)]
pub struct IdentityProvider {
/// The brand-name of the service (e.g. Apple, Facebook, GitHub, GitLab,
/// Google) or the software (e.g. keycloak, MAS) providing the identity.
/// When a brand is recognized we apply certain defaults to this config
/// for your convenience. For certain brands we apply essential internal
/// workarounds specific to that provider; it is important to configure this
/// field properly when a provider needs to be recognized (like GitHub for
/// example). Several configured providers can share the same brand name. It
/// is not case-sensitive.
#[serde(deserialize_with = "utils::string::de::to_lowercase")]
pub brand: String,
/// The ID of your OAuth application which the provider generates upon
/// registration. This ID then uniquely identifies this configuration
/// instance itself, becoming the identity provider's ID and must be unique
/// and remain unchanged.
pub client_id: String,
/// Secret key the provider generated for you along with the `client_id`
/// above. Unlike the `client_id`, the `client_secret` can be changed here
/// whenever the provider regenerates one for you.
pub client_secret: String,
/// The callback URL configured when registering the OAuth application with
/// the provider. Tuwunel's callback URL must be strictly formatted exactly
/// as instructed. The URL host must point directly at the matrix server and
/// use the following path:
/// `/_matrix/client/unstable/login/sso/callback/<client_id>` where
/// `<client_id>` is the same one configured for this provider above.
pub callback_url: Option<Url>,
/// Optional display-name for this provider instance seen on the login page
/// by users. It defaults to `brand`. When configuring multiple providers
/// using the same `brand` this can be set to distinguish them.
pub name: Option<String>,
/// Optional icon for the provider. The canonical providers have a default
/// icon based on the `brand` supplied above when this is not supplied. Note
/// that it uses an MXC url which is curious in the auth-media era and may
/// not be reliable.
pub icon: Option<OwnedMxcUri>,
/// Optional list of scopes to authorize. An empty array does not impose any
/// restrictions from here, effectively defaulting to all scopes you
/// configured for the OAuth application at the provider. This setting
/// allows for restricting to a subset of those scopes for this instance.
/// Note the user can further restrict scopes during their authorization.
///
/// default: []
#[serde(default)]
pub scope: BTreeSet<String>,
/// List of userinfo claims which shape and restrict the way we compute a
/// Matrix UserId for new registrations. Reviewing Tuwunel's documentation
/// will be necessary for a complete description in detail. An empty array
/// imposes no restriction here, avoiding generated fallbacks as much as
/// possible. For simplicity we reserve a claim called "unique" which can be
/// listed alone to ensure *only* generated ID's are used for registrations.
///
/// default: []
#[serde(default)]
pub userid_claims: BTreeSet<String>,
/// Issuer URL the provider publishes for you. We have pre-supplied default
/// values for some of the canonical providers, making this field optional
/// based on the `brand` set above. Otherwise it is required for OIDC
/// discovery to acquire additional provider configuration, and it must be
/// correct to pass validations during various interactions.
pub issuer_url: Option<Url>,
/// Extra path components after the issuer_url leading to the location of
/// the `.well-known` directory used for discovery. This will be empty for
/// specification-compliant providers. We have supplied any known values
/// based on `brand` (e.g. `/login/oauth` for GitHub).
pub base_path: Option<String>,
/// Overrides the `.well-known` location where the provider's OIDC
/// configuration is found. It is very unlikely you will need to set this;
/// available for developers or special purposes only.
pub discovery_url: Option<Url>,
/// Overrides the authorize URL requested during the grant phase. This is
/// generally discovered or derived automatically, but may be required as a
/// workaround for any non-standard or undiscoverable provider.
pub authorization_url: Option<Url>,
/// Overrides the access token URL; the same caveats apply as with the other
/// URL overrides.
pub token_url: Option<Url>,
/// Overrides the revocation URL; the same caveats apply as with the other
/// URL overrides.
pub revocation_url: Option<Url>,
/// Overrides the introspection URL; the same caveats apply as with the
/// other URL overrides.
pub introspection_url: Option<Url>,
/// Overrides the userinfo URL; the same caveats apply as with the other URL
/// overrides.
pub userinfo_url: Option<Url>,
/// Whether to perform discovery and adjust this provider's configuration
/// accordingly. This defaults to true. When true, it is an error when
/// discovery fails and authorizations will not be attempted to the
/// provider.
#[serde(default = "true_fn")]
pub discovery: bool,
/// The duration in seconds before a grant authorization session expires.
#[serde(default = "default_sso_grant_session_duration")]
pub grant_session_duration: Option<u64>,
}
impl IdentityProvider {
#[must_use]
pub fn id(&self) -> &str { self.client_id.as_str() }
}
impl Hash for IdentityProvider {
fn hash<H: Hasher>(&self, state: &mut H) { self.id().hash(state) }
}
#[derive(Clone, Debug, Default, Deserialize)]
#[config_example_generator(
filename = "tuwunel-example.toml",
@@ -3021,3 +3153,5 @@ fn default_one_time_key_limit() -> usize { 256 }
fn default_max_make_join_attempts_per_join_attempt() -> usize { 48 }
fn default_max_join_attempts_per_join_request() -> usize { 3 }
fn default_sso_grant_session_duration() -> Option<u64> { Some(180) }
+6
View File
@@ -57,6 +57,10 @@ pub enum Error {
#[error(transparent)]
Figment(#[from] figment::error::Error),
#[error(transparent)]
HtmlFormDe(#[from] serde_html_form::de::Error),
#[error(transparent)]
HtmlFormSer(#[from] serde_html_form::ser::Error),
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
HttpHeader(#[from] http::header::InvalidHeaderValue),
@@ -91,6 +95,8 @@ pub enum Error {
#[error(transparent)]
TypedHeader(#[from] axum_extra::typed_header::TypedHeaderRejection),
#[error(transparent)]
UrlParse(#[from] url::ParseError),
#[error(transparent)]
Yaml(#[from] serde_yaml::Error),
// ruma/tuwunel
+2 -4
View File
@@ -1,13 +1,11 @@
use std::fmt::Display;
use serde::{de, ser};
use crate::Error;
impl de::Error for Error {
impl serde_core::de::Error for Error {
fn custom<T: Display + ToString>(msg: T) -> Self { Self::SerdeDe(msg.to_string().into()) }
}
impl ser::Error for Error {
impl serde_core::ser::Error for Error {
fn custom<T: Display + ToString>(msg: T) -> Self { Self::SerdeSer(msg.to_string().into()) }
}
+3
View File
@@ -6,6 +6,7 @@ use ruma::{
use crate::{
Result, extract_variant, is_equal_to,
matrix::{PduEvent, room_version},
state_res::{self},
};
pub fn into_outgoing_federation(
@@ -95,6 +96,8 @@ pub fn from_incoming_federation(
pdu_json.insert("event_id".into(), CanonicalJsonValue::String(event_id.into()));
}
state_res::check_pdu_format(pdu_json, &room_rules.event_format)?;
PduEvent::from_val(pdu_json)
}
+3 -5
View File
@@ -37,9 +37,7 @@ pub fn check_pdu_format(pdu: &CanonicalJsonObject, rules: &EventFormatRules) ->
.map_err(|e| err!(Request(BadJson("Failed to serialize canonical JSON: {e}"))))?;
if json.len() > MAX_PDU_BYTES {
return Err!(Request(InvalidParam(
"PDU is larger than maximum of {MAX_PDU_BYTES} bytes"
)));
return Err!(Request(TooLarge("PDU is larger than maximum of {MAX_PDU_BYTES} bytes")));
}
// Check the presence, type and length of the `type` field.
@@ -133,7 +131,7 @@ fn extract_optional_string_field<'a>(
match object.get(field) {
| Some(CanonicalJsonValue::String(value)) =>
if value.len() > ID_MAX_BYTES {
Err!(Request(InvalidParam(
Err!(Request(TooLarge(
"invalid `{field}` field in PDU: string length is larger than maximum of \
{ID_MAX_BYTES} bytes"
)))
@@ -177,7 +175,7 @@ fn extract_required_array_field<'a>(
match object.get(field) {
| Some(CanonicalJsonValue::Array(value)) =>
if value.len() > max_len {
Err!(Request(InvalidParam(
Err!(Request(TooLarge(
"invalid `{field}` field in PDU: array length is larger than maximum of \
{max_len}"
)))
+1
View File
@@ -39,6 +39,7 @@ pub use self::{
time::{
exponential_backoff::{continue_exponential_backoff, continue_exponential_backoff_secs},
now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now,
timepoint_has_passed,
},
};
+28
View File
@@ -31,6 +31,34 @@ pub fn string_array<const LENGTH: usize>() -> ArrayString<LENGTH> {
ret
}
#[must_use]
pub fn truncate_string(mut str: String, range: Range<u64>) -> String {
let len = thread_rng()
.gen_range(range)
.try_into()
.unwrap_or(usize::MAX);
if let Some((i, _)) = str.char_indices().nth(len) {
str.truncate(i);
}
str
}
#[inline]
#[must_use]
pub fn truncate_str(str: &str, range: Range<u64>) -> &str {
let len = thread_rng()
.gen_range(range)
.try_into()
.unwrap_or(usize::MAX);
str.char_indices()
.nth(len)
.map(|(i, _)| str.split_at(i).0)
.unwrap_or(str)
}
#[inline]
#[must_use]
pub fn time_from_now_secs(range: Range<u64>) -> SystemTime {
+22 -1
View File
@@ -1,10 +1,11 @@
mod between;
pub mod de;
mod split;
mod tests;
mod unquote;
mod unquoted;
use std::mem::replace;
use std::{mem::replace, ops::Range};
pub use self::{between::Between, split::SplitInfallible, unquote::Unquote, unquoted::Unquoted};
use crate::{Result, smallstr::SmallString};
@@ -111,6 +112,26 @@ pub fn common_prefix<T: AsRef<str>>(choice: &[T]) -> &str {
})
}
#[inline]
#[must_use]
#[allow(clippy::arithmetic_side_effects)]
pub fn truncate_deterministic(str: &str, range: Option<Range<usize>>) -> &str {
let range = range.unwrap_or(0..str.len());
let len = str
.as_bytes()
.iter()
.copied()
.map(Into::into)
.fold(0_usize, usize::wrapping_add)
.wrapping_rem(str.len().max(1))
.clamp(range.start, range.end);
str.char_indices()
.nth(len)
.map(|(i, _)| str.split_at(i).0)
.unwrap_or(str)
}
pub fn to_small_string<const CAP: usize, T>(t: T) -> SmallString<[u8; CAP]>
where
T: std::fmt::Display,
+22
View File
@@ -0,0 +1,22 @@
use std::fmt;
use serde::de::{Deserializer, Error, Visitor};
struct ToLowercase;
#[inline]
pub fn to_lowercase<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_string(ToLowercase)
}
impl Visitor<'_> for ToLowercase {
type Value = String;
#[inline]
fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> { Ok(v.to_lowercase()) }
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("String") }
}
+8
View File
@@ -61,6 +61,14 @@ pub fn parse_duration(duration: &str) -> Result<Duration> {
.map_err(|error| err!("'{duration:?}' is not a valid duration string: {error:?}"))
}
#[inline]
#[must_use]
pub fn timepoint_has_passed(timepoint: SystemTime) -> bool {
SystemTime::now()
.duration_since(timepoint)
.is_ok()
}
#[must_use]
pub fn rfc2822_from_seconds(epoch: i64) -> String {
use chrono::{DateTime, Utc};
+6
View File
@@ -272,6 +272,12 @@ impl<'a, 'de: 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match name {
| "$serde_json::private::RawValue" => visitor.visit_map(self),
| "Json" => visitor
.visit_newtype_struct(&mut serde_json::Deserializer::from_slice(
self.record_trail(),
))
.map_err(|e| Self::Error::SerdeDe(format!("{name}: {e}").into())),
| "Cbor" => visitor
.visit_newtype_struct(&mut minicbor_serde::Deserializer::new(self.record_trail()))
.map_err(|e| Self::Error::SerdeDe(format!("{name}: {e}").into())),
+12
View File
@@ -113,6 +113,14 @@ pub(super) static MAPS: &[Descriptor] = &[
name: "mediaid_user",
..descriptor::RANDOM_SMALL
},
Descriptor {
name: "oauthid_session",
..descriptor::RANDOM_SMALL
},
Descriptor {
name: "oauthuniqid_oauthid",
..descriptor::RANDOM_SMALL
},
Descriptor {
name: "onetimekeyid_onetimekeys",
..descriptor::RANDOM_SMALL
@@ -403,6 +411,10 @@ pub(super) static MAPS: &[Descriptor] = &[
name: "userid_masterkeyid",
..descriptor::RANDOM_SMALL
},
Descriptor {
name: "userid_oauthid",
..descriptor::RANDOM_SMALL
},
Descriptor {
name: "userid_origin",
..descriptor::RANDOM
+1 -1
View File
@@ -52,7 +52,7 @@ pub(crate) struct Serializer<'a, W: Write> {
}
/// Newtype for JSON serialization.
#[derive(Debug, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Json<T>(pub T);
/// Newtype for CBOR serialization.
+2 -2
View File
@@ -73,8 +73,8 @@ fn generate_example(input: &ItemStruct, args: &[Meta], write: bool) -> Result<To
.expect("written to config file");
}
let pound = if section != "global" { "#" } else { "" };
file.write_fmt(format_args!("\n{pound}[{section}]\n"))
let pound = if section != "global" { "\n#" } else { "" };
file.write_fmt(format_args!("\n\n{pound}[{section}]\n"))
.expect("written to config file");
}
+6 -2
View File
@@ -68,9 +68,13 @@ pub(crate) fn build(services: &Arc<Services>) -> Result<(Router, Guard)> {
.layer(RequestBodyTimeoutLayer::new(Duration::from_secs(
server.config.client_receive_timeout,
)))
.layer(TimeoutLayer::new(Duration::from_secs(server.config.client_request_timeout)))
.layer(TimeoutLayer::with_status_code(
StatusCode::REQUEST_TIMEOUT,
Duration::from_secs(server.config.client_request_timeout),
))
.layer(SetResponseHeaderLayer::if_not_present(
HeaderName::from_static("origin-agent-cluster"), // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster
HeaderName::from_static("origin-agent-cluster"),
HeaderValue::from_static("?1"),
))
.layer(SetResponseHeaderLayer::if_not_present(
+2
View File
@@ -32,6 +32,8 @@ pub(super) async fn serve(
if cfg!(unix) && config.unix_socket_path.is_some() {
unix::serve(server, app, shutdown).await
} else if config.tls.certs.is_some() {
#[cfg(feature = "direct_tls")]
services.globals.init_rustls_provider()?;
#[cfg(feature = "direct_tls")]
return tls::serve(server, app, handle, addrs).await;
-6
View File
@@ -27,12 +27,6 @@ pub(super) async fn serve(
.as_ref()
.ok_or_else(|| err!(Config("tls.key", "Missing required value in tls config section")))?;
// we use ring for ruma and hashing state, but aws-lc-rs is the new default.
// without this, TLS mode will panic.
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.expect("failed to initialise aws-lc-rs rustls crypto provider");
info!(
"Note: It is strongly recommended that you use a reverse proxy instead of running \
tuwunel directly with TLS."
+2
View File
@@ -107,8 +107,10 @@ rand.workspace = true
regex.workspace = true
reqwest.workspace = true
ruma.workspace = true
rustls.workspace = true
rustyline-async.workspace = true
rustyline-async.optional = true
serde_html_form.workspace = true
serde_json.workspace = true
serde.workspace = true
serde_yaml.workspace = true
+1 -1
View File
@@ -175,7 +175,7 @@ pub async fn create_admin_room(services: &Services) -> Result {
.build_and_append_pdu(
PduBuilder::state(String::new(), &RoomTopicEventContent {
topic_block: TopicContentBlock::default(),
topic: format!("Manage {} | Run commands prefixed with `!admin` | Run `!admin -h` for help | Documentation: https://github.com/matrix-construct/tuwunel/", services.config.server_name),
topic: format!("Manage {} | Run commands prefixed with `!admin` | Run `!admin -h` for help | Documentation: https://matrix-construct.github.io/tuwunel", services.config.server_name),
}),
server_user,
&room_id,
+1 -1
View File
@@ -145,7 +145,7 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result {
if self.services.server.config.admin_room_notices {
let welcome_message = String::from(
"## Thank you for trying out tuwunel!\n\nTuwunel is a continuation of conduwuit which was technically a hard fork of Conduit.\n\nHelpful links:\n> GitHub Repo: https://github.com/matrix-construct/tuwunel\n> Documentation: https://github.com/matrix-construct/tuwunel\n> Report issues: https://github.com/matrix-construct/tuwunel/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`"
"## Thank you for trying out tuwunel!\n\nTuwunel is a continuation of conduwuit which was technically a hard fork of Conduit.\n\nHelpful links:\n> GitHub Repo: https://github.com/matrix-construct/tuwunel\n> Documentation: https://matrix-construct.github.io/tuwunel\n> Report issues: https://github.com/matrix-construct/tuwunel/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`"
);
// Send welcome message
+9 -5
View File
@@ -54,7 +54,7 @@ pub type ProcessorFuture = Pin<Box<dyn Future<Output = ProcessorResult> + Send>>
/// events which have digested any prior errors. The wrapping preserves whether
/// the command failed without interpreting the text. Ok(None) outputs are
/// dropped to produce no response.
pub type ProcessorResult = Result<Option<CommandOutput>, CommandOutput>;
pub type ProcessorResult = Result<Option<CommandOutput>, Box<CommandOutput>>;
/// Alias for the output structure.
pub type CommandOutput = RoomMessageEventContent;
@@ -199,14 +199,18 @@ impl Service {
async fn handle_command(&self, command: CommandInput) {
match self.process_command(command).await {
| Err(output) => self.handle_command_output(*output).await,
| Ok(Some(output)) => self.handle_command_output(output).await,
| Ok(None) => debug!("Command successful with no response"),
| Ok(Some(output)) | Err(output) => self
.handle_response(output)
.await
.unwrap_or_else(default_log),
}
}
async fn handle_command_output(&self, content: RoomMessageEventContent) {
self.handle_response(content)
.await
.unwrap_or_else(default_log);
}
async fn process_command(&self, command: CommandInput) -> ProcessorResult {
let handle = &self
.handle
+44 -52
View File
@@ -8,7 +8,7 @@ use tuwunel_core::{
utils::ReadyExt,
};
use super::{NamespaceRegex, RegistrationInfo};
use super::RegistrationInfo;
/// Called by timeline::append() after accepting new PDU.
#[implement(super::Service)]
@@ -43,57 +43,7 @@ async fn append_pdu_to(
pdu_id: RawPduId,
pdu: &Pdu,
) -> Result {
if self
.services
.state_cache
.appservice_in_room(pdu.room_id(), appservice)
.await
{
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
return Ok(());
}
// If the RoomMember event has a non-empty state_key, it is targeted at someone.
// If it is our appservice user, we send this PDU to it.
if *pdu.kind() == TimelineEventType::RoomMember {
if let Some(state_key_uid) = &pdu
.state_key
.as_ref()
.and_then(|state_key| UserId::parse(state_key.as_str()).ok())
{
let appservice_uid = appservice.registration.sender_localpart.as_str();
if state_key_uid == &appservice_uid {
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
return Ok(());
}
}
}
let matching_users = |users: &NamespaceRegex| {
appservice.users.is_match(pdu.sender().as_str())
|| *pdu.kind() == TimelineEventType::RoomMember
&& pdu
.state_key
.as_ref()
.is_some_and(|state_key| users.is_match(state_key))
};
let matching_aliases = |aliases: NamespaceRegex| {
self.services
.alias
.local_aliases_for_room(pdu.room_id())
.ready_any(move |room_alias| aliases.is_match(room_alias.as_str()))
};
if matching_aliases(appservice.aliases.clone()).await
|| appservice.rooms.is_match(pdu.room_id().as_str())
|| matching_users(&appservice.users)
{
if self.should_append_to(appservice, pdu).await {
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;
@@ -101,3 +51,45 @@ async fn append_pdu_to(
Ok(())
}
#[implement(super::Service)]
async fn should_append_to(&self, appservice: &RegistrationInfo, pdu: &Pdu) -> bool {
if self
.services
.state_cache
.appservice_in_room(pdu.room_id(), appservice)
.await
{
return true;
}
if appservice.is_user_match(pdu.sender()) {
return true;
}
if *pdu.kind() == TimelineEventType::RoomMember
&& pdu
.state_key
.as_ref()
.and_then(|state_key| UserId::parse(state_key.as_str()).ok())
.is_some_and(|user_id| appservice.is_user_match(user_id))
{
return true;
}
if self
.services
.alias
.local_aliases_for_room(pdu.room_id())
.ready_any(|room_alias| appservice.aliases.is_match(room_alias.as_str()))
.await
{
return true;
}
if appservice.rooms.is_match(pdu.room_id().as_str()) {
return true;
}
false
}
+6
View File
@@ -21,6 +21,7 @@ pub struct Service {
pub sender: ClientLazylock,
pub appservice: ClientLazylock,
pub pusher: ClientLazylock,
pub oauth: ClientLazylock,
pub cidr_range_denylist: Vec<IPAddress>,
}
@@ -112,6 +113,11 @@ impl crate::Service for Service {
.pool_idle_timeout(Duration::from_secs(config.pusher_idle_timeout))
.redirect(redirect::Policy::limited(2))),
oauth: create_client!(config, services; base(config)?
.dns_resolver2(Arc::clone(&services.resolver.resolver))
.redirect(redirect::Policy::limited(0))
.pool_max_idle_per_host(1)),
cidr_range_denylist: config
.ip_range_denylist
.iter()
+50 -30
View File
@@ -1,10 +1,10 @@
mod data;
use std::{ops::Range, sync::Arc};
use std::{collections::HashSet, ops::Range, sync::Arc};
use data::Data;
use ruma::{OwnedUserId, RoomAliasId, ServerName, UserId};
use tuwunel_core::{Result, Server, error};
use tuwunel_core::{Result, Server, err, error};
use crate::service;
@@ -13,8 +13,7 @@ pub struct Service {
server: Arc<Server>,
pub server_user: OwnedUserId,
pub turn_secret: String,
pub registration_token: Option<String>,
pub turn_secret: Option<String>,
}
impl crate::Service for Service {
@@ -22,32 +21,17 @@ impl crate::Service for Service {
let db = Data::new(args);
let config = &args.server.config;
let turn_secret = config.turn_secret_file.as_ref().map_or_else(
|| config.turn_secret.clone(),
|path| {
std::fs::read_to_string(path).unwrap_or_else(|e| {
error!("Failed to read the TURN secret file: {e}");
config.turn_secret.clone()
})
},
);
let registration_token = config
.registration_token_file
let turn_secret = config
.turn_secret_file
.as_ref()
.map_or_else(
|| config.registration_token.clone(),
|path| {
let Ok(token) = std::fs::read_to_string(path).inspect_err(|e| {
error!("Failed to read the registration token file: {e}");
}) else {
return config.registration_token.clone();
};
Some(token)
},
);
.and_then(|path| {
std::fs::read_to_string(path)
.inspect_err(|e| {
error!("Failed to read the TURN secret file: {e}");
})
.ok()
})
.or_else(|| config.turn_secret.clone());
Ok(Arc::new(Self {
db,
@@ -58,7 +42,6 @@ impl crate::Service for Service {
)
.expect("@conduit:server_name is valid"),
turn_secret,
registration_token,
}))
}
@@ -122,4 +105,41 @@ impl Service {
#[inline]
#[must_use]
pub fn is_read_only(&self) -> bool { self.db.db.is_read_only() }
pub async fn get_registration_tokens(&self) -> HashSet<String> {
let mut tokens = HashSet::new();
if let Some(file) = &self
.server
.config
.registration_token_file
.as_ref()
{
match std::fs::read_to_string(file) {
| Err(e) => error!("Failed to read the registration token file: {e}"),
| Ok(text) => {
text.split_ascii_whitespace().for_each(|token| {
tokens.insert(token.to_owned());
});
},
}
}
if let Some(token) = &self.server.config.registration_token {
tokens.insert(token.to_owned());
}
tokens
}
pub fn init_rustls_provider(&self) -> Result {
if rustls::crypto::CryptoProvider::get_default().is_none() {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.map_err(|_provider| {
err!(error!("Error initialising aws_lc_rs rustls crypto backend"))
})
} else {
Ok(())
}
}
}
+341 -235
View File
@@ -1,9 +1,18 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
iter::once,
mem::take,
sync::Arc,
};
use futures::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, pin_mut};
use futures::{
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
future::{OptionFuture, join3, join4},
};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedServerName, OwnedUserId, RoomId, RoomVersionId,
UserId,
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, OwnedUserId, RoomId,
RoomOrAliasId, RoomVersionId, UserId,
api::{client::error::ErrorKind, federation},
canonical_json::to_canonical_value,
events::{
@@ -14,19 +23,21 @@ use ruma::{
},
},
room::{AllowRule, JoinRule},
room_version_rules::RoomVersionRules,
};
use serde_json::value::RawValue as RawJsonValue;
use tuwunel_core::{
Err, Result, debug, debug_error, debug_info, debug_warn, err, error, implement, info,
matrix::{event::gen_event_id_canonical_json, room_version},
pdu::{PduBuilder, format::from_incoming_federation},
state_res, trace,
utils::{self, IterStream, ReadyExt},
utils::{self, IterStream, ReadyExt, future::TryExtExt, math::Expected, shuffle},
warn,
};
use super::Service;
use crate::{
appservice::RegistrationInfo,
Services,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
@@ -39,22 +50,27 @@ use crate::{
skip_all,
fields(%sender_user, %room_id)
)]
#[allow(clippy::too_many_arguments)]
pub async fn join(
&self,
sender_user: &UserId,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
reason: Option<String>,
servers: &[OwnedServerName],
appservice_info: &Option<RegistrationInfo>,
is_appservice: bool,
state_lock: &RoomMutexGuard,
) -> Result {
let servers =
get_servers_for_room(&self.services, sender_user, room_id, orig_room_id, servers).await?;
let user_is_guest = self
.services
.users
.is_deactivated(sender_user)
.await
.unwrap_or(false)
&& appservice_info.is_none();
&& !is_appservice;
if user_is_guest
&& !self
@@ -99,12 +115,12 @@ pub async fn join(
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]));
if local_join {
self.join_local(sender_user, room_id, reason, servers, state_lock)
self.join_local(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await?;
} else {
// Ask a remote server if we are not participating in this room
self.join_remote(sender_user, room_id, reason, servers, state_lock)
self.join_remote(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await?;
}
@@ -150,89 +166,21 @@ pub async fn join_remote(
}
let room_version_rules = room_version::rules(&room_version_id)?;
let mut join_event_stub: CanonicalJsonObject =
serde_json::from_str(make_join_response.event.get()).map_err(|e| {
err!(BadServerResponse(warn!(
"Invalid make_join event json received from server: {e:?}"
)))
})?;
let join_authorized_via_users_server = {
use RoomVersionId::*;
if !matches!(room_version_id, V1 | V2 | V3 | V4 | V5 | V6 | V7) {
join_event_stub
.get("content")
.map(|s| {
s.as_object()?
.get("join_authorised_via_users_server")?
.as_str()
})
.and_then(|s| OwnedUserId::try_from(s.unwrap_or_default()).ok())
} else {
None
}
};
join_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
.globals
.server_name()
.as_str()
.to_owned(),
),
);
join_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
join_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
let (mut join_event, event_id, join_authorized_via_users_server) = self
.create_join_event(
room_id,
sender_user,
&make_join_response.event,
&room_version_id,
&room_version_rules,
reason,
join_authorized_via_users_server: join_authorized_via_users_server.clone(),
..RoomMemberEventContent::new(MembershipState::Join)
})
.expect("event is valid, we just created it"),
);
)
.await?;
let event_id = self
.services
.server_keys
.gen_id_hash_and_sign_event(&mut join_event_stub, &room_version_id)?;
// It has enough fields to be called a proper event now
let mut join_event = join_event_stub;
info!("Asking {remote_server} for send_join in room {room_id}");
let send_join_request = federation::membership::create_join_event::v2::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
omit_members: false,
omit_members: true,
pdu: self
.services
.federation
@@ -249,23 +197,60 @@ pub async fn join_remote(
.lock(room_id)
.await;
let send_join_response = match self
info!("Asking {remote_server} for fast_join in room {room_id}");
let mut response = match self
.services
.federation
.execute(&remote_server, send_join_request)
.await
.inspect_err(|e| error!("send_join failed: {e}"))
{
| Ok(response) => response,
| Err(e) => {
error!("send_join failed: {e}");
return Err(e);
},
| Err(e) => return Err(e),
| Ok(response) => response.room_state,
};
info!("send_join finished");
info!(
fast_join = response.members_omitted,
auth_chain = response.auth_chain.len(),
state = response.state.len(),
servers = response
.servers_in_room
.as_ref()
.map(Vec::len)
.unwrap_or(0),
"send_join finished"
);
if response.members_omitted {
use federation::event::get_room_state::v1::{Request, Response};
info!("Asking {remote_server} for state in room {room_id}");
match self
.services
.federation
.execute(&remote_server, Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
})
.await
.inspect_err(|e| error!("state failed: {e}"))
{
| Err(e) => return Err(e),
| Ok(Response { mut auth_chain, mut pdus }) => {
response.auth_chain = take(&mut auth_chain);
response.state = take(&mut pdus);
},
}
info!(
auth_chain = response.auth_chain.len(),
state = response.state.len(),
"state finished"
);
}
if join_authorized_via_users_server.is_some() {
if let Some(signed_raw) = &send_join_response.room_state.event {
if let Some(signed_raw) = &response.event {
debug_info!(
"There is a signed event with join_authorized_via_users_server. This room is \
probably using restricted joins. Adding signature to our event"
@@ -318,28 +303,34 @@ pub async fn join_remote(
}
}
self.services
let shortroomid = self
.services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing join event");
info!(
%room_id,
%shortroomid,
"Initialized room. Parsing join event..."
);
let parsed_join_pdu =
from_incoming_federation(room_id, &event_id, &mut join_event, &room_version_rules)?;
info!("Acquiring server signing keys for response events");
let resp_events = &send_join_response.room_state;
let resp_state = &resp_events.state;
let resp_auth = &resp_events.auth_chain;
let resp_state = &response.state;
let resp_auth = &response.auth_chain;
info!(
events = resp_state.len().expected_add(resp_auth.len()),
"Acquiring server signing keys for response events..."
);
self.services
.server_keys
.acquire_events_pubkeys(resp_auth.iter().chain(resp_state.iter()))
.await;
info!("Going through send_join response room_state");
info!(events = response.state.len(), "Going through send_join response room_state...");
let cork = self.services.db.cork_and_flush();
let state = send_join_response
.room_state
let state = response
.state
.iter()
.stream()
@@ -379,10 +370,12 @@ pub async fn join_remote(
drop(cork);
info!("Going through send_join response auth_chain");
info!(
events = response.auth_chain.len(),
"Going through send_join response auth_chain..."
);
let cork = self.services.db.cork_and_flush();
send_join_response
.room_state
response
.auth_chain
.iter()
.stream()
@@ -411,7 +404,7 @@ pub async fn join_remote(
drop(cork);
debug!("Running send_join auth check");
debug!("Running send_join auth check...");
state_res::auth_check(
&room_version_rules,
&parsed_join_pdu,
@@ -434,7 +427,7 @@ pub async fn join_remote(
.boxed()
.await?;
info!("Compressing state from send_join");
info!(events = state.len(), "Compressing state from send_join...");
let compressed: CompressedState = self
.services
.state_compressor
@@ -442,7 +435,7 @@ pub async fn join_remote(
.collect()
.await;
debug!("Saving compressed state");
debug!("Saving compressed state...");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_join,
added,
@@ -453,13 +446,15 @@ pub async fn join_remote(
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
debug!(
state_hash = ?statehash_before_join,
"Forcing state for new room..."
);
self.services
.state
.force_state(room_id, statehash_before_join, added, removed, state_lock)
.await?;
info!("Updating joined counts for new room");
self.services
.state_cache
.update_joined_count(room_id)
@@ -474,7 +469,11 @@ pub async fn join_remote(
.append_to_state(&parsed_join_pdu)
.await?;
info!("Appending new room join event");
info!(
event_id = %parsed_join_pdu.event_id,
"Appending new room join event..."
);
self.services
.timeline
.append_pdu(
@@ -485,13 +484,17 @@ pub async fn join_remote(
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
self.services
.state
.set_room_state(room_id, statehash_after_join, state_lock);
info!(
statehash = %statehash_after_join,
"Set final room state for new room."
);
Ok(())
}
@@ -531,19 +534,19 @@ pub async fn join_local(
| _ => Vec::new(),
};
let join_authorized_via_users_server: Option<OwnedUserId> = {
if restriction_rooms
.iter()
.stream()
.any(|restriction_room_id| {
self.services
.state_cache
.is_joined(sender_user, restriction_room_id)
})
.await
{
let users = self
.services
let is_joined_restricted_rooms = restriction_rooms
.iter()
.stream()
.any(|restriction_room_id| {
self.services
.state_cache
.is_joined(sender_user, restriction_room_id)
})
.await;
let join_authorized_via_users_server: OptionFuture<_> = is_joined_restricted_rooms
.then(async || {
self.services
.state_cache
.local_users_in_room(room_id)
.filter(|user| {
@@ -554,34 +557,31 @@ pub async fn join_local(
state_lock,
)
})
.map(ToOwned::to_owned);
.map(ToOwned::to_owned)
.boxed()
.next()
.await
})
.into();
pin_mut!(users);
users.next().await
} else {
None
}
};
let displayname = self.services.users.displayname(sender_user).ok();
let avatar_url = self.services.users.avatar_url(sender_user).ok();
let blurhash = self.services.users.blurhash(sender_user).ok();
let (displayname, avatar_url, blurhash, join_authorized_via_users_server) = join4(
displayname,
avatar_url,
blurhash,
join_authorized_via_users_server.map(Option::flatten),
)
.await;
let content = RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
displayname,
avatar_url,
blurhash,
reason: reason.clone(),
join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
@@ -634,21 +634,107 @@ pub async fn join_local(
));
}
let mut join_event_stub: CanonicalJsonObject =
serde_json::from_str(make_join_response.event.get()).map_err(|e| {
let room_version_rules = room_version::rules(&room_version_id)?;
let (join_event, event_id, _) = self
.create_join_event(
room_id,
sender_user,
&make_join_response.event,
&room_version_id,
&room_version_rules,
reason,
)
.await?;
let send_join_request = federation::membership::create_join_event::v2::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
omit_members: true,
pdu: self
.services
.federation
.format_pdu_into(join_event.clone(), Some(&room_version_id))
.await,
};
let send_join_response = self
.services
.federation
.execute(&remote_server, send_join_request)
.await?;
let Some(signed_raw) = send_join_response.room_state.event else {
return Err(error);
};
let (signed_event_id, signed_value) =
gen_event_id_canonical_json(&signed_raw, &room_version_id).map_err(|e| {
err!(Request(BadJson(warn!("Could not convert event to canonical JSON: {e}"))))
})?;
if signed_event_id != event_id {
return Err!(Request(BadJson(warn!(
%signed_event_id, %event_id, "Server {remote_server} sent event with wrong event ID"
))));
}
self.services
.event_handler
.handle_incoming_pdu(&remote_server, room_id, &signed_event_id, signed_value, true)
.boxed()
.await?;
Ok(())
}
#[implement(Service)]
#[tracing::instrument(name = "make_join", level = "debug", skip_all)]
async fn create_join_event(
&self,
room_id: &RoomId,
sender_user: &UserId,
join_event_stub: &RawJsonValue,
room_version_id: &RoomVersionId,
room_version_rules: &RoomVersionRules,
reason: Option<String>,
) -> Result<(CanonicalJsonObject, OwnedEventId, Option<OwnedUserId>)> {
let mut event: CanonicalJsonObject =
serde_json::from_str(join_event_stub.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_join event json received from server: {e:?}"))
})?;
let join_authorized_via_users_server = join_event_stub
.get("content")
.map(|s| {
let join_authorized_via_users_server = room_version_rules
.authorization
.restricted_join_rule
.then(|| event.get("content"))
.flatten()
.and_then(|s| {
s.as_object()?
.get("join_authorised_via_users_server")?
.as_str()
.get("join_authorised_via_users_server")
})
.and_then(|s| OwnedUserId::try_from(s.unwrap_or_default()).ok());
.and_then(|s| OwnedUserId::try_from(s.as_str().unwrap_or_default()).ok());
join_event_stub.insert(
let displayname = self.services.users.displayname(sender_user).ok();
let avatar_url = self.services.users.avatar_url(sender_user).ok();
let blurhash = self.services.users.blurhash(sender_user).ok();
let (displayname, avatar_url, blurhash) = join3(displayname, avatar_url, blurhash).await;
event.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname,
avatar_url,
blurhash,
reason,
join_authorized_via_users_server: join_authorized_via_users_server.clone(),
..RoomMemberEventContent::new(MembershipState::Join)
})?,
);
event.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
@@ -658,87 +744,28 @@ pub async fn join_local(
.to_owned(),
),
);
join_event_stub.insert(
event.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
join_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason,
join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
})
.expect("event is valid, we just created it"),
CanonicalJsonValue::Integer(utils::millis_since_unix_epoch().try_into()?),
);
event.insert("room_id".into(), CanonicalJsonValue::String(room_id.as_str().into()));
event.insert("sender".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
event.insert("state_key".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
event.insert("type".into(), CanonicalJsonValue::String("m.room.member".into()));
let event_id = self
.services
.server_keys
.gen_id_hash_and_sign_event(&mut join_event_stub, &room_version_id)?;
.gen_id_hash_and_sign_event(&mut event, room_version_id)?;
// It has enough fields to be called a proper event now
let join_event = join_event_stub;
state_res::check_pdu_format(&event, &room_version_rules.event_format)?;
let send_join_response = self
.services
.federation
.execute(&remote_server, federation::membership::create_join_event::v2::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
omit_members: false,
pdu: self
.services
.federation
.format_pdu_into(join_event.clone(), Some(&room_version_id))
.await,
})
.await?;
if let Some(signed_raw) = send_join_response.room_state.event {
let (signed_event_id, signed_value) =
gen_event_id_canonical_json(&signed_raw, &room_version_id).map_err(|e| {
err!(Request(BadJson(warn!("Could not convert event to canonical JSON: {e}"))))
})?;
if signed_event_id != event_id {
return Err!(Request(BadJson(
warn!(%signed_event_id, %event_id, "Server {remote_server} sent event with wrong event ID")
)));
}
self.services
.event_handler
.handle_incoming_pdu(&remote_server, room_id, &signed_event_id, signed_value, true)
.boxed()
.await?;
} else {
return Err(error);
}
Ok(())
Ok((event, event_id, join_authorized_via_users_server))
}
#[implement(Service)]
@@ -801,8 +828,10 @@ async fn make_join_request(
M_UNSUPPORTED_ROOM_VERSION, assuming that tuwunel does not support the \
room version {room_id}: {e}"
);
make_join_response_and_server =
Err!(BadServerResponse("Room version is not supported by tuwunel"));
return make_join_response_and_server;
}
@@ -810,12 +839,14 @@ async fn make_join_request(
.services
.config
.max_make_join_attempts_per_join_attempt;
if make_join_counter >= max_attempts {
warn!(?remote_server, "last make_join failure reason: {e}");
warn!(
"{max_attempts} servers failed to provide valid make_join response, \
assuming no server can assist in joining."
);
make_join_response_and_server =
Err!(BadServerResponse("No server available to assist in joining."));
@@ -832,3 +863,78 @@ async fn make_join_request(
make_join_response_and_server
}
pub(super) async fn get_servers_for_room(
services: &Services,
user_id: &UserId,
room_id: &RoomId,
orig_room_id: Option<&RoomOrAliasId>,
via: &[OwnedServerName],
) -> Result<Vec<OwnedServerName>> {
// add invited vias
let mut additional_servers = services
.state_cache
.servers_invite_via(room_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.await;
// add invite senders' servers
additional_servers.extend(
services
.state_cache
.invite_state(user_id, room_id)
.await
.unwrap_or_default()
.iter()
.filter_map(|event| event.get_field("sender").ok().flatten())
.filter_map(|sender: &str| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned()),
);
let mut servers = Vec::from(via);
shuffle(&mut servers);
if let Some(server_name) = room_id.server_name() {
servers.insert(0, server_name.to_owned());
}
if let Some(orig_room_id) = orig_room_id
&& let Some(orig_server_name) = orig_room_id.server_name()
{
servers.insert(0, orig_server_name.to_owned());
}
shuffle(&mut additional_servers);
servers.extend_from_slice(&additional_servers);
// 1. (room alias server)?
// 2. (room id server)?
// 3. shuffle [via query + resolve servers]?
// 4. shuffle [invited via, inviters servers]?
debug!(?servers);
// dedup preserving order
let mut set = HashSet::new();
servers.retain(|x| set.insert(x.clone()));
debug!(?servers);
// sort deprioritized servers last
if !servers.is_empty() {
for i in 0..servers.len() {
if services
.server
.config
.deprioritize_joins_through_servers
.is_match(servers[i].host())
{
let server = servers.remove(i);
servers.push(server);
}
}
}
debug_info!(?servers);
Ok(servers)
}
+677
View File
@@ -0,0 +1,677 @@
use std::{borrow::Borrow, collections::HashMap, iter::once, sync::Arc};
use futures::{FutureExt, StreamExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedServerName, RoomId,
RoomOrAliasId, RoomVersionId, UserId,
api::federation::{self, membership::RawStrippedState},
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
},
};
use tuwunel_core::{
Err, Event, PduCount, Result, debug, debug_info, debug_warn, err, extract_variant, implement,
info,
matrix::event::gen_event_id,
pdu::{PduBuilder, PduEvent},
trace, utils, warn,
};
use super::Service;
use crate::{
membership::join::get_servers_for_room,
rooms::{
state::RoomMutexGuard,
state_compressor::{CompressedState, HashSetCompressStateEvent},
},
};
#[implement(Service)]
#[tracing::instrument(
level = "debug",
skip_all,
fields(%sender_user, %room_id)
)]
pub async fn knock(
&self,
sender_user: &UserId,
room_id: &RoomId,
orig_server_name: Option<&RoomOrAliasId>,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
let servers =
get_servers_for_room(&self.services, sender_user, room_id, orig_server_name, servers)
.await?;
if self
.services
.state_cache
.is_invited(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already invited in {room_id} but attempted to knock");
return Err!(Request(Forbidden(
"You cannot knock on a room you are already invited/accepted to."
)));
}
if self
.services
.state_cache
.is_joined(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already joined in {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are already joined in.")));
}
if self
.services
.state_cache
.is_knocked(sender_user, room_id)
.await
{
debug_warn!("{sender_user} is already knocked in {room_id}");
return Ok(());
}
if let Ok(membership) = self
.services
.state_accessor
.get_member(room_id, sender_user)
.await
{
if membership.membership == MembershipState::Ban {
debug_warn!("{sender_user} is banned from {room_id} but attempted to knock");
return Err!(Request(Forbidden("You cannot knock on a room you are banned from.")));
}
}
let server_in_room = self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), room_id)
.await;
let local_knock = server_in_room
|| servers.is_empty()
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]));
if local_knock {
self.knock_room_helper_local(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await
} else {
self.knock_room_helper_remote(sender_user, room_id, reason, &servers, state_lock)
.boxed()
.await
}
}
#[implement(Service)]
async fn knock_room_helper_local(
&self,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
debug_info!("We can knock locally");
let room_version_id = self
.services
.state
.get_room_version(room_id)
.await?;
if matches!(
room_version_id,
RoomVersionId::V1
| RoomVersionId::V2
| RoomVersionId::V3
| RoomVersionId::V4
| RoomVersionId::V5
| RoomVersionId::V6
) {
return Err!(Request(Forbidden("This room does not support knocking.")));
}
let content = RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason: reason.clone(),
..RoomMemberEventContent::new(MembershipState::Knock)
};
// Try normal knock first
let Err(error) = self
.services
.timeline
.build_and_append_pdu(
PduBuilder::state(sender_user.to_string(), &content),
sender_user,
room_id,
state_lock,
)
.await
else {
return Ok(());
};
if servers.is_empty()
|| (servers.len() == 1 && self.services.globals.server_is_ours(&servers[0]))
{
return Err(error);
}
warn!("We couldn't do the knock locally, maybe federation can help to satisfy the knock");
let (make_knock_response, remote_server) = self
.make_knock_request(sender_user, room_id, servers)
.await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !self
.services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_knock_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
.globals
.server_name()
.as_str()
.to_owned(),
),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
knock_event_stub
.insert("room_id".into(), CanonicalJsonValue::String(room_id.as_str().into()));
knock_event_stub
.insert("state_key".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
knock_event_stub
.insert("sender".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
knock_event_stub.insert("type".into(), CanonicalJsonValue::String("m.room.member".into()));
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
self.services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: self
.services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = self
.services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
self.services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = self.services.globals.next_count();
self.services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
self.services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
state_lock,
)
.await?;
Ok(())
}
#[implement(Service)]
async fn knock_room_helper_remote(
&self,
sender_user: &UserId,
room_id: &RoomId,
reason: Option<String>,
servers: &[OwnedServerName],
state_lock: &RoomMutexGuard,
) -> Result {
info!("Knocking {room_id} over federation.");
let (make_knock_response, remote_server) = self
.make_knock_request(sender_user, room_id, servers)
.await?;
info!("make_knock finished");
let room_version_id = make_knock_response.room_version;
if !self
.services
.server
.supported_room_version(&room_version_id)
{
return Err!(BadServerResponse(
"Remote room version {room_version_id} is not supported by tuwunel"
));
}
let mut knock_event_stub: CanonicalJsonObject =
serde_json::from_str(make_knock_response.event.get()).map_err(|e| {
err!(BadServerResponse("Invalid make_knock event json received from server: {e:?}"))
})?;
knock_event_stub.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
.globals
.server_name()
.as_str()
.to_owned(),
),
);
knock_event_stub.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
);
knock_event_stub.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname: self
.services
.users
.displayname(sender_user)
.await
.ok(),
avatar_url: self
.services
.users
.avatar_url(sender_user)
.await
.ok(),
blurhash: self
.services
.users
.blurhash(sender_user)
.await
.ok(),
reason,
..RoomMemberEventContent::new(MembershipState::Knock)
})
.expect("event is valid, we just created it"),
);
knock_event_stub
.insert("room_id".into(), CanonicalJsonValue::String(room_id.as_str().into()));
knock_event_stub
.insert("state_key".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
knock_event_stub
.insert("sender".into(), CanonicalJsonValue::String(sender_user.as_str().into()));
knock_event_stub.insert("type".into(), CanonicalJsonValue::String("m.room.member".into()));
// In order to create a compatible ref hash (EventID) the `hashes` field needs
// to be present
self.services
.server_keys
.hash_and_sign_event(&mut knock_event_stub, &room_version_id)?;
// Generate event id
let event_id = gen_event_id(&knock_event_stub, &room_version_id)?;
// Add event_id
knock_event_stub
.insert("event_id".into(), CanonicalJsonValue::String(event_id.clone().into()));
// It has enough fields to be called a proper event now
let knock_event = knock_event_stub;
info!("Asking {remote_server} for send_knock in room {room_id}");
let send_knock_request = federation::membership::create_knock_event::v1::Request {
room_id: room_id.to_owned(),
event_id: event_id.clone(),
pdu: self
.services
.federation
.format_pdu_into(knock_event.clone(), Some(&room_version_id))
.await,
};
let send_knock_response = self
.services
.federation
.execute(&remote_server, send_knock_request)
.await?;
info!("send_knock finished");
self.services
.short
.get_or_create_shortroomid(room_id)
.await;
info!("Parsing knock event");
let parsed_knock_pdu = PduEvent::from_id_val(&event_id, knock_event.clone())
.map_err(|e| err!(BadServerResponse("Invalid knock event PDU: {e:?}")))?;
info!("Going through send_knock response knock state events");
let state = send_knock_response
.knock_room_state
.iter()
.map(|event| {
serde_json::from_str::<CanonicalJsonObject>(
extract_variant!(event.clone(), RawStrippedState::Stripped)
.expect("Raw<AnyStrippedStateEvent>")
.json()
.get(),
)
})
.filter_map(Result::ok);
let mut state_map: HashMap<u64, OwnedEventId> = HashMap::new();
for event in state {
let Some(state_key) = event.get("state_key") else {
debug_warn!("send_knock stripped state event missing state_key: {event:?}");
continue;
};
let Some(event_type) = event.get("type") else {
debug_warn!("send_knock stripped state event missing event type: {event:?}");
continue;
};
let Ok(state_key) = serde_json::from_value::<String>(state_key.clone().into()) else {
debug_warn!("send_knock stripped state event has invalid state_key: {event:?}");
continue;
};
let Ok(event_type) = serde_json::from_value::<StateEventType>(event_type.clone().into())
else {
debug_warn!("send_knock stripped state event has invalid event type: {event:?}");
continue;
};
let event_id = gen_event_id(&event, &room_version_id)?;
let shortstatekey = self
.services
.short
.get_or_create_shortstatekey(&event_type, &state_key)
.await;
self.services
.timeline
.add_pdu_outlier(&event_id, &event);
state_map.insert(shortstatekey, event_id.clone());
}
info!("Compressing state from send_knock");
let compressed: CompressedState = self
.services
.state_compressor
.compress_state_events(
state_map
.iter()
.map(|(ssk, eid)| (ssk, eid.borrow())),
)
.collect()
.await;
debug!("Saving compressed state");
let HashSetCompressStateEvent {
shortstatehash: statehash_before_knock,
added,
removed,
} = self
.services
.state_compressor
.save_state(room_id, Arc::new(compressed))
.await?;
debug!("Forcing state for new room");
self.services
.state
.force_state(room_id, statehash_before_knock, added, removed, state_lock)
.await?;
let statehash_after_knock = self
.services
.state
.append_to_state(&parsed_knock_pdu)
.await?;
info!("Updating membership locally to knock state with provided stripped state events");
let count = self.services.globals.next_count();
self.services
.state_cache
.update_membership(
room_id,
sender_user,
parsed_knock_pdu
.get_content::<RoomMemberEventContent>()
.expect("we just created this"),
sender_user,
Some(
send_knock_response
.knock_room_state
.into_iter()
.filter_map(|s| extract_variant!(s, RawStrippedState::Stripped))
.collect(),
),
None,
false,
PduCount::Normal(*count),
)
.await?;
info!("Appending room knock event locally");
self.services
.timeline
.append_pdu(
&parsed_knock_pdu,
knock_event,
once(parsed_knock_pdu.event_id.borrow()),
state_lock,
)
.await?;
info!("Setting final room state for new room");
// We set the room state after inserting the pdu, so that we never have a moment
// in time where events in the current room state do not exist
self.services
.state
.set_room_state(room_id, statehash_after_knock, state_lock);
Ok(())
}
#[implement(Service)]
async fn make_knock_request(
&self,
sender_user: &UserId,
room_id: &RoomId,
servers: &[OwnedServerName],
) -> Result<(federation::membership::prepare_knock_event::v1::Response, OwnedServerName)> {
let mut make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
let mut make_knock_counter: usize = 0;
for remote_server in servers {
if self
.services
.globals
.server_is_ours(remote_server)
{
continue;
}
info!("Asking {remote_server} for make_knock ({make_knock_counter})");
let make_knock_response = self
.services
.federation
.execute(remote_server, federation::membership::prepare_knock_event::v1::Request {
room_id: room_id.to_owned(),
user_id: sender_user.to_owned(),
ver: self
.services
.server
.supported_room_versions()
.collect(),
})
.await;
trace!("make_knock response: {make_knock_response:?}");
make_knock_counter = make_knock_counter.saturating_add(1);
make_knock_response_and_server = make_knock_response.map(|r| (r, remote_server.clone()));
if make_knock_response_and_server.is_ok() {
break;
}
if make_knock_counter > 40 {
warn!(
"50 servers failed to provide valid make_knock response, assuming no server can \
assist in knocking."
);
make_knock_response_and_server =
Err!(BadServerResponse("No server available to assist in knocking."));
return make_knock_response_and_server;
}
}
make_knock_response_and_server
}
+66 -25
View File
@@ -1,9 +1,14 @@
use std::collections::HashSet;
use futures::{FutureExt, StreamExt, TryFutureExt, future::ready, pin_mut};
use futures::{
FutureExt, StreamExt, TryFutureExt,
future::{join3, ready},
pin_mut,
};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, OwnedServerName, RoomId, UserId,
api::federation,
canonical_json::to_canonical_value,
events::{
StateEventType,
room::member::{MembershipState, RoomMemberEventContent},
@@ -11,9 +16,13 @@ use ruma::{
};
use tuwunel_core::{
Err, Result, debug_info, debug_warn, err, implement,
matrix::PduCount,
matrix::{PduCount, room_version},
pdu::PduBuilder,
utils::{self, FutureBoolExt, future::ReadyBoolExt},
state_res,
utils::{
self, FutureBoolExt,
future::{ReadyBoolExt, TryExtExt},
},
warn,
};
@@ -95,7 +104,11 @@ pub async fn leave(
// Ask a remote server if we don't have this room and are not knocking on it
if remote_leave_now || dont_have_room.and(not_knocked).await {
if let Err(e) = self.remote_leave(user_id, room_id).boxed().await {
if let Err(e) = self
.remote_leave(user_id, room_id, reason)
.boxed()
.await
{
warn!(%user_id, "Failed to leave room {room_id} remotely: {e}");
// Don't tell the client about this error
}
@@ -177,7 +190,12 @@ pub async fn leave(
#[implement(Service)]
#[tracing::instrument(name = "remote", level = "debug", skip_all)]
async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
async fn remote_leave(
&self,
user_id: &UserId,
room_id: &RoomId,
reason: Option<String>,
) -> Result {
let mut make_leave_response_and_server =
Err!(BadServerResponse("No remote server available to assist in leaving {room_id}."));
@@ -278,17 +296,36 @@ async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
)));
}
let mut leave_event_stub = serde_json::from_str::<CanonicalJsonObject>(
make_leave_response.event.get(),
)
.map_err(|e| {
err!(BadServerResponse(warn!(
"Invalid make_leave event json received from {remote_server} for {room_id}: {e:?}"
)))
})?;
let room_version_rules = room_version::rules(&room_version_id)?;
// TODO: Is origin needed?
leave_event_stub.insert(
let mut event = serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.get())
.map_err(|e| {
err!(BadServerResponse(warn!(
"Invalid make_leave event json received from {remote_server} for {room_id}: \
{e:?}"
)))
})?;
let displayname = self.services.users.displayname(user_id).ok();
let avatar_url = self.services.users.avatar_url(user_id).ok();
let blurhash = self.services.users.blurhash(user_id).ok();
let (displayname, avatar_url, blurhash) = join3(displayname, avatar_url, blurhash).await;
event.insert(
"content".into(),
to_canonical_value(RoomMemberEventContent {
displayname,
avatar_url,
blurhash,
reason,
..RoomMemberEventContent::new(MembershipState::Leave)
})?,
);
event.insert(
"origin".into(),
CanonicalJsonValue::String(
self.services
@@ -298,22 +335,26 @@ async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
.to_owned(),
),
);
leave_event_stub.insert(
event.insert(
"origin_server_ts".into(),
CanonicalJsonValue::Integer(
utils::millis_since_unix_epoch()
.try_into()
.expect("Timestamp is valid js_int value"),
),
CanonicalJsonValue::Integer(utils::millis_since_unix_epoch().try_into()?),
);
event.insert("room_id".into(), CanonicalJsonValue::String(room_id.as_str().into()));
event.insert("state_key".into(), CanonicalJsonValue::String(user_id.as_str().into()));
event.insert("sender".into(), CanonicalJsonValue::String(user_id.as_str().into()));
event.insert("type".into(), CanonicalJsonValue::String("m.room.member".into()));
let event_id = self
.services
.server_keys
.gen_id_hash_and_sign_event(&mut leave_event_stub, &room_version_id)?;
.gen_id_hash_and_sign_event(&mut event, &room_version_id)?;
// It has enough fields to be called a proper event now
let leave_event = leave_event_stub;
state_res::check_pdu_format(&event, &room_version_rules.event_format)?;
self.services
.federation
@@ -323,7 +364,7 @@ async fn remote_leave(&self, user_id: &UserId, room_id: &RoomId) -> Result {
pdu: self
.services
.federation
.format_pdu_into(leave_event.clone(), Some(&room_version_id))
.format_pdu_into(event.clone(), Some(&room_version_id))
.await,
})
.await?;
+1
View File
@@ -2,6 +2,7 @@ mod ban;
mod invite;
mod join;
mod kick;
mod knock;
mod leave;
mod unban;
+4
View File
@@ -1,3 +1,4 @@
#![recursion_limit = "256"]
#![type_length_limit = "98304"]
#![allow(refining_impl_trait)]
@@ -19,6 +20,7 @@ pub mod globals;
pub mod key_backups;
pub mod media;
pub mod membership;
pub mod oauth;
pub mod presence;
pub mod pusher;
pub mod resolver;
@@ -33,6 +35,8 @@ pub mod users;
pub(crate) use once_services::OnceServices;
pub(crate) use service::{Args, Service};
pub(crate) type SelfServices = std::sync::Arc<OnceServices>;
pub use crate::services::Services;
tuwunel_core::mod_ctor! {}
+265
View File
@@ -0,0 +1,265 @@
pub mod providers;
pub mod sessions;
pub mod user_info;
use std::sync::Arc;
use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD as b64encode};
use reqwest::{Method, header::ACCEPT};
use ruma::UserId;
use serde::Serialize;
use serde_json::Value as JsonValue;
use tuwunel_core::{
Err, Result, err, implement,
utils::{hash::sha256, result::LogErr},
};
use url::Url;
pub use self::{
providers::Provider,
sessions::{CODE_VERIFIER_LENGTH, SESSION_ID_LENGTH, Session},
user_info::UserInfo,
};
use self::{providers::Providers, sessions::Sessions};
use crate::SelfServices;
pub struct Service {
services: SelfServices,
pub providers: Arc<Providers>,
pub sessions: Arc<Sessions>,
}
impl crate::Service for Service {
fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
let providers = Arc::new(Providers::build(args));
let sessions = Arc::new(Sessions::build(args, providers.clone()));
Ok(Arc::new(Self {
services: args.services.clone(),
sessions,
providers,
}))
}
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
#[implement(Service)]
#[tracing::instrument(level = "debug", skip_all, ret)]
pub async fn request_userinfo(
&self,
(provider, session): (&Provider, &Session),
) -> Result<UserInfo> {
let url = provider
.userinfo_url
.clone()
.ok_or_else(|| err!(Config("userinfo_url", "Missing userinfo URL in config")))?;
self.request((Some(provider), Some(session)), Method::GET, url)
.await
.and_then(|value| serde_json::from_value(value).map_err(Into::into))
.log_err()
}
#[implement(Service)]
#[tracing::instrument(level = "debug", skip_all, ret)]
pub async fn request_tokeninfo(
&self,
(provider, session): (&Provider, &Session),
) -> Result<UserInfo> {
let url = provider
.introspection_url
.clone()
.ok_or_else(|| {
err!(Config("introspection_url", "Missing introspection URL in config"))
})?;
self.request((Some(provider), Some(session)), Method::GET, url)
.await
.and_then(|value| serde_json::from_value(value).map_err(Into::into))
.log_err()
}
#[implement(Service)]
#[tracing::instrument(level = "debug", skip_all, ret)]
pub async fn revoke_token(&self, (provider, session): (&Provider, &Session)) -> Result {
#[derive(Debug, Serialize)]
struct RevokeQuery<'a> {
client_id: &'a str,
client_secret: &'a str,
}
let query = RevokeQuery {
client_id: &provider.client_id,
client_secret: &provider.client_secret,
};
let query = serde_html_form::to_string(&query)?;
let url = provider
.revocation_url
.clone()
.map(|mut url| {
url.set_query(Some(&query));
url
})
.ok_or_else(|| err!(Config("revocation_url", "Missing revocation URL in config")))?;
self.request((Some(provider), Some(session)), Method::POST, url)
.await
.log_err()
.map(|_| ())
}
#[implement(Service)]
#[tracing::instrument(level = "debug", skip_all, ret)]
pub async fn request_token(
&self,
(provider, session): (&Provider, &Session),
code: &str,
) -> Result<Session> {
#[derive(Debug, Serialize)]
struct TokenQuery<'a> {
client_id: &'a str,
client_secret: &'a str,
grant_type: &'a str,
code: &'a str,
code_verifier: Option<&'a str>,
redirect_uri: Option<&'a str>,
}
let query = TokenQuery {
client_id: &provider.client_id,
client_secret: &provider.client_secret,
grant_type: "authorization_code",
code,
code_verifier: session.code_verifier.as_deref(),
redirect_uri: provider.callback_url.as_ref().map(Url::as_str),
};
let query = serde_html_form::to_string(&query)?;
let url = provider
.token_url
.clone()
.map(|mut url| {
url.set_query(Some(&query));
url
})
.ok_or_else(|| err!(Config("token_url", "Missing token URL in config")))?;
self.request((Some(provider), Some(session)), Method::POST, url)
.await
.and_then(|value| serde_json::from_value(value).map_err(Into::into))
.log_err()
}
/// Send a request to a provider; this is somewhat abstract since URL's are
/// formed prior to this call and could point at anything, however this function
/// uses the oauth-specific http client and is configured for JSON with special
/// casing for an `error` property in the response.
#[implement(Service)]
#[tracing::instrument(
name = "request",
level = "debug",
ret(level = "trace"),
skip(self)
)]
pub async fn request(
&self,
(provider, session): (Option<&Provider>, Option<&Session>),
method: Method,
url: Url,
) -> Result<JsonValue> {
let mut request = self
.services
.client
.oauth
.request(method, url)
.header(ACCEPT, "application/json");
if let Some(session) = session {
if let Some(access_token) = session.access_token.clone() {
request = request.bearer_auth(access_token);
}
}
let response: JsonValue = request
.send()
.await?
.error_for_status()?
.json()
.await?;
if let Some(response) = response.as_object().as_ref()
&& let Some(error) = response.get("error").and_then(JsonValue::as_str)
{
let description = response
.get("error_description")
.and_then(JsonValue::as_str)
.unwrap_or("(no description)");
return Err!(Request(Forbidden("Error from provider: {error}: {description}",)));
}
Ok(response)
}
#[implement(Service)]
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_user(&self, user_id: &UserId) -> Result<(Provider, Session)> {
let session = self.sessions.get_by_user(user_id).await?;
let provider = self.sessions.provider(&session).await?;
Ok((provider, session))
}
#[inline]
pub fn unique_id((provider, session): (&Provider, &Session)) -> Result<String> {
unique_id_parts((provider, session)).and_then(unique_id_iss_sub)
}
#[inline]
pub fn unique_id_sub((provider, sub): (&Provider, &str)) -> Result<String> {
unique_id_sub_parts((provider, sub)).and_then(unique_id_iss_sub)
}
#[inline]
pub fn unique_id_iss((iss, session): (&str, &Session)) -> Result<String> {
unique_id_iss_parts((iss, session)).and_then(unique_id_iss_sub)
}
pub fn unique_id_iss_sub((iss, sub): (&str, &str)) -> Result<String> {
let hash = sha256::delimited([iss, sub].iter());
let b64 = b64encode.encode(hash);
Ok(b64)
}
fn unique_id_parts<'a>(
(provider, session): (&'a Provider, &'a Session),
) -> Result<(&'a str, &'a str)> {
provider
.issuer_url
.as_ref()
.map(Url::as_str)
.ok_or_else(|| err!(Config("issuer_url", "issuer_url not found for this provider.")))
.and_then(|iss| unique_id_iss_parts((iss, session)))
}
fn unique_id_sub_parts<'a>(
(provider, sub): (&'a Provider, &'a str),
) -> Result<(&'a str, &'a str)> {
provider
.issuer_url
.as_ref()
.map(Url::as_str)
.ok_or_else(|| err!(Config("issuer_url", "issuer_url not found for this provider.")))
.map(|iss| (iss, sub))
}
fn unique_id_iss_parts<'a>((iss, session): (&'a str, &'a Session)) -> Result<(&'a str, &'a str)> {
session
.user_info
.as_ref()
.map(|user_info| user_info.sub.as_str())
.ok_or_else(|| err!(Request(NotFound("user_info not found for this session."))))
.map(|sub| (iss, sub))
}
+246
View File
@@ -0,0 +1,246 @@
use std::collections::BTreeMap;
use serde_json::{Map as JsonObject, Value as JsonValue};
use tokio::sync::RwLock;
pub use tuwunel_core::config::IdentityProvider as Provider;
use tuwunel_core::{Err, Result, debug, debug::INFO_SPAN_LEVEL, err, implement};
use url::Url;
use crate::SelfServices;
#[derive(Default)]
pub struct Providers {
services: SelfServices,
providers: RwLock<BTreeMap<String, Provider>>,
}
#[implement(Providers)]
pub(super) fn build(args: &crate::Args<'_>) -> Self {
Self {
services: args.services.clone(),
..Default::default()
}
}
/// Get the Provider configuration after any discovery and adjustments
/// made on top of the admin's configuration. This incurs network-based
/// discovery on the first call but responds from cache on subsequent calls.
#[implement(Providers)]
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get(&self, id: &str) -> Result<Provider> {
if let Some(provider) = self.providers.read().await.get(id).cloned() {
return Ok(provider);
}
let config = self.get_config(id)?;
let mut map = self.providers.write().await;
let config = self.configure(config).await?;
debug!(?id, ?config);
_ = map.insert(id.into(), config.clone());
Ok(config)
}
/// Get the admin-configured Provider which exists prior to any
/// reconciliation with the well-known discovery (the server's config is
/// immutable); though it is important to note the server config can be
/// reloaded. This will Err NotFound for a non-existent idp.
#[implement(Providers)]
pub fn get_config(&self, id: &str) -> Result<Provider> {
self.services
.config
.identity_provider
.iter()
.find(|config| config.id() == id)
.cloned()
.ok_or_else(|| err!(Request(NotFound("Unrecognized Identity Provider"))))
}
/// Configure an identity provider; takes the admin-configured instance from the
/// server's config, queries the provider for discovery, and then returns an
/// updated config based on the proper reconciliation. This final config is then
/// cached in memory to avoid repeating this process.
#[implement(Providers)]
#[tracing::instrument(
level = INFO_SPAN_LEVEL,
ret(level = "debug"),
skip(self),
)]
async fn configure(&self, mut provider: Provider) -> Result<Provider> {
_ = provider
.name
.get_or_insert_with(|| provider.brand.clone());
if provider.issuer_url.is_none() {
_ = provider
.issuer_url
.replace(match provider.brand.as_str() {
| "github" => "https://github.com".try_into()?,
| "gitlab" => "https://gitlab.com".try_into()?,
| "google" => "https://accounts.google.com".try_into()?,
| _ => return Err!(Config("issuer_url", "Required for this provider.")),
});
}
if provider.base_path.is_none() {
provider.base_path = match provider.brand.as_str() {
| "github" => Some("/login/oauth".to_owned()),
| _ => None,
};
}
let response = self
.discover(&provider)
.await
.and_then(|response| {
response.as_object().cloned().ok_or_else(|| {
err!(Request(NotJson("Expecting JSON object for discovery response")))
})
})
.and_then(|response| check_issuer(response, &provider))?;
if provider.authorization_url.is_none() {
response
.get("authorization_endpoint")
.and_then(JsonValue::as_str)
.map(Url::parse)
.transpose()?
.or_else(|| make_url(&provider, "/authorize").ok())
.map(|url| provider.authorization_url.replace(url));
}
if provider.revocation_url.is_none() {
response
.get("revocation_endpoint")
.and_then(JsonValue::as_str)
.map(Url::parse)
.transpose()?
.or_else(|| make_url(&provider, "/revocation").ok())
.map(|url| provider.revocation_url.replace(url));
}
if provider.introspection_url.is_none() {
response
.get("introspection_endpoint")
.and_then(JsonValue::as_str)
.map(Url::parse)
.transpose()?
.or_else(|| make_url(&provider, "/introspection").ok())
.map(|url| provider.introspection_url.replace(url));
}
if provider.userinfo_url.is_none() {
response
.get("userinfo_endpoint")
.and_then(JsonValue::as_str)
.map(Url::parse)
.transpose()?
.or_else(|| match provider.brand.as_str() {
| "github" => "https://api.github.com/user".try_into().ok(),
| _ => make_url(&provider, "/userinfo").ok(),
})
.map(|url| provider.userinfo_url.replace(url));
}
if provider.token_url.is_none() {
response
.get("token_endpoint")
.and_then(JsonValue::as_str)
.map(Url::parse)
.transpose()?
.or_else(|| {
let path = if provider.brand == "github" {
"/access_token"
} else {
"/token"
};
make_url(&provider, path).ok()
})
.map(|url| provider.token_url.replace(url));
}
Ok(provider)
}
#[implement(Providers)]
#[tracing::instrument(level = "debug", ret(level = "trace"), skip(self))]
pub async fn discover(&self, provider: &Provider) -> Result<JsonValue> {
self.services
.client
.oauth
.get(discovery_url(provider)?)
.send()
.await?
.error_for_status()?
.json()
.await
.map_err(Into::into)
}
fn discovery_url(provider: &Provider) -> Result<Url> {
let default_url = provider
.discovery
.then(|| make_url(provider, "/.well-known/openid-configuration"))
.transpose()?;
let Some(url) = provider
.discovery_url
.clone()
.filter(|_| provider.discovery)
.or(default_url)
else {
return Err!(Config(
"discovery_url",
"Failed to determine URL for discovery of provider {}",
provider.id()
));
};
Ok(url)
}
/// Validate that the locally configured `issuer_url` matches the issuer claimed
/// in any response. todo: cryptographic validation is not yet implemented here.
fn check_issuer(
response: JsonObject<String, JsonValue>,
provider: &Provider,
) -> Result<JsonObject<String, JsonValue>> {
let expected = provider
.issuer_url
.as_ref()
.map(Url::as_str)
.map(|url| url.trim_end_matches('/'));
let responded = response
.get("issuer")
.and_then(JsonValue::as_str)
.map(|url| url.trim_end_matches('/'));
if expected != responded {
return Err!(Request(Unauthorized(
"Configured issuer_url {expected:?} does not match discovered {responded:?}",
)));
}
Ok(response)
}
/// Generate a full URL for a request to the idp based on the idp's derived
/// configuration.
fn make_url(provider: &Provider, path: &str) -> Result<Url> {
let mut suffix = provider.base_path.clone().unwrap_or_default();
suffix.push_str(path);
let url = provider
.issuer_url
.as_ref()
.ok_or_else(|| {
let id = &provider.client_id;
err!(Config("issuer_url", "Provider {id:?} required field"))
})?
.join(&suffix)?;
Ok(url)
}
+234
View File
@@ -0,0 +1,234 @@
use std::{sync::Arc, time::SystemTime};
use futures::{Stream, StreamExt, TryFutureExt};
use ruma::{OwnedUserId, UserId};
use serde::{Deserialize, Serialize};
use tuwunel_core::{Err, Result, at, implement, utils::stream::TryExpect};
use tuwunel_database::{Cbor, Deserialized, Map};
use url::Url;
use super::{Provider, Providers, UserInfo, unique_id};
use crate::SelfServices;
pub struct Sessions {
_services: SelfServices,
providers: Arc<Providers>,
db: Data,
}
struct Data {
oauthid_session: Arc<Map>,
oauthuniqid_oauthid: Arc<Map>,
userid_oauthid: Arc<Map>,
}
/// Session ultimately represents an OAuth authorization session yielding an
/// associated matrix user registration.
///
/// Mixed-use structure capable of deserializing response values, maintaining
/// the state between authorization steps, and maintaining the association to
/// the matrix user until deactivation or revocation.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Session {
/// Identity Provider ID (the `client_id` in the configuration) associated
/// with this session.
pub idp_id: Option<String>,
/// Session ID used as the index key for this session itself.
pub sess_id: Option<String>,
/// Token type (bearer, mac, etc).
pub token_type: Option<String>,
/// Access token to the provider.
pub access_token: Option<String>,
/// Duration in seconds the access_token is valid for.
pub expires_in: Option<u64>,
/// Point in time that the access_token expires.
pub expires_at: Option<SystemTime>,
/// Token used to refresh the access_token.
pub refresh_token: Option<String>,
/// Duration in seconds the refresh_token is valid for
pub refresh_token_expires_in: Option<u64>,
/// Point in time that the refresh_token expires.
pub refresh_token_expires_at: Option<SystemTime>,
/// Access scope actually granted (if supported).
pub scope: Option<String>,
/// Redirect URL
pub redirect_url: Option<Url>,
/// Challenge preimage
pub code_verifier: Option<String>,
/// Random string passed exclusively in the grant session cookie.
pub cookie_nonce: Option<String>,
/// Random single-use string passed in the provider redirect.
pub query_nonce: Option<String>,
/// Point in time the authorization grant session expires.
pub authorize_expires_at: Option<SystemTime>,
/// Associated User Id registration.
pub user_id: Option<OwnedUserId>,
/// Last userinfo response persisted here.
pub user_info: Option<UserInfo>,
}
/// Number of characters generated for our code_verifier. The code_verifier is a
/// random string which must be between 43 and 128 characters.
pub const CODE_VERIFIER_LENGTH: usize = 64;
/// Number of characters we will generate for the Session ID.
pub const SESSION_ID_LENGTH: usize = 32;
#[implement(Sessions)]
pub(super) fn build(args: &crate::Args<'_>, providers: Arc<Providers>) -> Self {
Self {
_services: args.services.clone(),
providers,
db: Data {
oauthid_session: args.db["oauthid_session"].clone(),
oauthuniqid_oauthid: args.db["oauthuniqid_oauthid"].clone(),
userid_oauthid: args.db["userid_oauthid"].clone(),
},
}
}
#[implement(Sessions)]
pub fn users(&self) -> impl Stream<Item = OwnedUserId> + Send {
self.db
.userid_oauthid
.keys()
.expect_ok()
.map(UserId::to_owned)
}
/// Delete database state for the session.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self))]
pub async fn delete(&self, sess_id: &str) {
let Ok(session) = self.get(sess_id).await else {
return;
};
// Check the user_id still points to this sess_id before deleting. If not, the
// association was updated to a newer session.
if let Some(user_id) = session.user_id.as_deref() {
if let Ok(assoc_id) = self.get_sess_id_by_user(user_id).await {
if assoc_id == sess_id {
self.db.userid_oauthid.remove(user_id);
}
}
}
// Check the unique identity still points to this sess_id before deleting. If
// not, the association was updated to a newer session.
if let Some(idp_id) = session.idp_id.as_ref() {
if let Ok(provider) = self.providers.get(idp_id).await {
if let Ok(unique_id) = unique_id((&provider, &session)) {
if let Ok(assoc_id) = self.get_sess_id_by_unique_id(&unique_id).await {
if assoc_id == sess_id {
self.db.oauthuniqid_oauthid.remove(&unique_id);
}
}
}
}
}
self.db.oauthid_session.remove(sess_id);
}
/// Create or overwrite database state for the session.
#[implement(Sessions)]
#[tracing::instrument(level = "info", skip(self))]
pub async fn put(&self, sess_id: &str, session: &Session) {
self.db
.oauthid_session
.raw_put(sess_id, Cbor(session));
if let Some(user_id) = session.user_id.as_deref() {
self.db.userid_oauthid.insert(user_id, sess_id);
}
if let Some(idp_id) = session.idp_id.as_ref() {
if let Ok(provider) = self.providers.get(idp_id).await {
if let Ok(unique_id) = unique_id((&provider, session)) {
self.db
.oauthuniqid_oauthid
.insert(&unique_id, sess_id);
}
}
}
}
/// Fetch database state for a session from its associated `sub`, in case
/// `sess_id` is not known.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self), ret(level = "debug"))]
pub async fn get_by_unique_id(&self, unique_id: &str) -> Result<Session> {
self.get_sess_id_by_unique_id(unique_id)
.and_then(async |sess_id| self.get(&sess_id).await)
.await
}
/// Fetch database state for a session from its associated `user_id`, in case
/// `sess_id` is not known.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self), ret(level = "debug"))]
pub async fn get_by_user(&self, user_id: &UserId) -> Result<Session> {
self.get_sess_id_by_user(user_id)
.and_then(async |sess_id| self.get(&sess_id).await)
.await
}
/// Fetch database state for a session from its `sess_id`.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self), ret(level = "debug"))]
pub async fn get(&self, sess_id: &str) -> Result<Session> {
self.db
.oauthid_session
.get(sess_id)
.await
.deserialized::<Cbor<_>>()
.map(at!(0))
}
/// Resolve the `sess_id` from an associated `user_id`.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self), ret(level = "debug"))]
pub async fn get_sess_id_by_user(&self, user_id: &UserId) -> Result<String> {
self.db
.userid_oauthid
.get(user_id)
.await
.deserialized()
}
/// Resolve the `sess_id` from an associated provider subject id.
#[implement(Sessions)]
#[tracing::instrument(level = "debug", skip(self), ret(level = "debug"))]
pub async fn get_sess_id_by_unique_id(&self, unique_id: &str) -> Result<String> {
self.db
.oauthuniqid_oauthid
.get(unique_id)
.await
.deserialized()
}
#[implement(Sessions)]
pub async fn provider(&self, session: &Session) -> Result<Provider> {
let Some(idp_id) = session.idp_id.as_deref() else {
return Err!(Request(NotFound("No provider for this session")));
};
self.providers.get(idp_id).await
}
+39
View File
@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};
/// Selection of userinfo response claims.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct UserInfo {
/// Unique identifier number or login username. Usually a number on most
/// services. We consider a concatenation of the `iss` and `sub` to be a
/// universally unique identifier for some user/identity; we index that in
/// `oauthidpsub_oauthid`.
///
/// Considered for user mxid only if none of the better fields are defined.
/// `login` alias intended for github.
#[serde(alias = "login")]
pub sub: String,
/// The login username we first consider when defined.
pub preferred_username: Option<String>,
/// The login username considered if none preferred.
pub nickname: Option<String>,
/// Full name.
pub name: Option<String>,
/// First name.
pub given_name: Option<String>,
/// Last name.
pub family_name: Option<String>,
/// Email address (`email` scope).
pub email: Option<String>,
/// URL to pfp (github/gitlab)
pub avatar_url: Option<String>,
/// URL to pfp (google)
pub picture: Option<String>,
}
+6 -1
View File
@@ -3,7 +3,12 @@ use tuwunel_core::{Result, debug, debug_error, debug_info, debug_warn, implement
use super::DestString;
#[implement(super::Service)]
#[tracing::instrument(name = "well-known", level = "debug", skip(self, dest))]
#[tracing::instrument(
name = "well-known",
level = "debug",
ret(level = "debug"),
skip(self)
)]
pub(super) async fn request_well_known(&self, dest: &str) -> Result<Option<DestString>> {
trace!("Requesting well known for {dest}");
let response = self
+2 -2
View File
@@ -109,10 +109,10 @@ impl Service {
pub async fn maybe_resolve_with_servers(
&self,
room: &RoomOrAliasId,
servers: Option<Vec<OwnedServerName>>,
servers: Option<&[OwnedServerName]>,
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
match <&RoomId>::try_from(room) {
| Ok(room_id) => Ok((room_id.to_owned(), servers.unwrap_or_default())),
| Ok(room_id) => Ok((room_id.to_owned(), Vec::from(servers.unwrap_or_default()))),
| Err(alias) => self.resolve_alias(alias).await,
}
}
@@ -68,6 +68,8 @@ pub(super) async fn handle_outlier_pdu(
let room_rules = room_version::rules(room_version)?;
state_res::check_pdu_format(&pdu_json, &room_rules.event_format)?;
// Now that we have checked the signature and hashes we can make mutations and
// convert to our PduEvent type.
let event = from_incoming_federation(room_id, event_id, &mut pdu_json, &room_rules)?;
@@ -52,6 +52,8 @@ pub(super) async fn upgrade_outlier_to_timeline_pdu(
let timer = Instant::now();
let room_rules = room_version::rules(room_version)?;
state_res::check_pdu_format(&val, &room_rules.event_format)?;
// 10. Fetch missing state and auth chain events by calling /state_ids at
// backwards extremities doing all the checks in this list starting at 1.
// These are not timeline events.
+26 -53
View File
@@ -91,6 +91,29 @@ impl crate::Service for Service {
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
/// Gets the summary of a space using either local or remote (federation)
/// sources
#[implement(Service)]
pub async fn get_summary_and_children_client(
&self,
current_room: &RoomId,
suggested_only: bool,
user_id: &UserId,
via: &[OwnedServerName],
) -> Result<Option<SummaryAccessibility>> {
let identifier = Identifier::UserId(user_id);
if let Ok(Some(response)) = self
.get_summary_and_children_local(current_room, &identifier)
.await
{
return Ok(Some(response));
}
self.get_summary_and_children_federation(current_room, suggested_only, user_id, via)
.await
}
/// Gets the summary of a space using solely local information
#[implement(Service)]
pub async fn get_summary_and_children_local(
@@ -225,29 +248,6 @@ async fn get_summary_and_children_federation(
Ok(Some(accessibility))
}
/// Gets the summary of a space using either local or remote (federation)
/// sources
#[implement(Service)]
pub async fn get_summary_and_children_client(
&self,
current_room: &RoomId,
suggested_only: bool,
user_id: &UserId,
via: &[OwnedServerName],
) -> Result<Option<SummaryAccessibility>> {
let identifier = Identifier::UserId(user_id);
if let Ok(Some(response)) = self
.get_summary_and_children_local(current_room, &identifier)
.await
{
return Ok(Some(response));
}
self.get_summary_and_children_federation(current_room, suggested_only, user_id, via)
.await
}
#[implement(Service)]
async fn get_room_summary(
&self,
@@ -468,41 +468,14 @@ async fn cache_insert(
current_room: &RoomId,
child: RoomSummary,
) {
let RoomSummary {
canonical_alias,
name,
num_joined_members,
room_id,
topic,
world_readable,
guest_can_join,
avatar_url,
join_rule,
room_type,
encryption,
room_version,
} = child;
let summary = SpaceHierarchyParentSummary {
summary: RoomSummary {
canonical_alias,
name,
num_joined_members,
topic,
world_readable,
guest_can_join,
avatar_url,
join_rule,
room_type,
room_id: room_id.clone(),
encryption,
room_version,
},
children_state: self
.get_space_child_events(&room_id)
.get_space_child_events(&child.room_id)
.map(Event::into_format)
.collect()
.await,
summary: child,
};
cache.insert(current_room.to_owned(), Some(CachedSpaceHierarchySummary { summary }));
+3 -11
View File
@@ -187,17 +187,7 @@ pub async fn create_hash_and_sign_event(
pdu.event_id = self
.services
.server_keys
.gen_id_hash_and_sign_event(&mut pdu_json, &room_version)
.map_err(|e| {
use Error::Signatures;
use ruma::signatures::Error::PduSize;
match e {
| Signatures(PduSize) => {
err!(Request(TooLarge("PDU exceeds 65535 bytes")))
},
| _ => err!(Request(Unknown(warn!("Signing event failed: {e}")))),
}
})?;
.gen_id_hash_and_sign_event(&mut pdu_json, &room_version)?;
// Room id is event id for V12+
if matches!(version_rules.room_id_format, RoomIdFormatVersion::V2)
@@ -207,6 +197,8 @@ pub async fn create_hash_and_sign_event(
pdu_json.insert("room_id".into(), CanonicalJsonValue::String(pdu.room_id.clone().into()));
}
state_res::check_pdu_format(&pdu_json, &version_rules.event_format)?;
// Generate short event id
let _shorteventid = self
.services
+17 -8
View File
@@ -8,7 +8,7 @@ use ruma::{
get_server_keys,
},
};
use tuwunel_core::{Err, Result, debug, implement};
use tuwunel_core::{Err, Result, debug, implement, info};
#[implement(super::Service)]
pub(super) async fn batch_notary_request<'a, S, K>(
@@ -36,18 +36,20 @@ where
batch
});
debug_assert!(!server_keys.is_empty(), "empty batch request to notary");
let total_keys = server_keys.len();
debug_assert!(total_keys > 0, "empty batch request to notary");
let batch_max = self
.services
.server
.config
.trusted_server_batch_size;
let mut results = Vec::new();
while let Some(batch) = server_keys
.keys()
.rev()
.take(
self.services
.server
.config
.trusted_server_batch_size,
)
.take(batch_max)
.next_back()
.cloned()
{
@@ -74,6 +76,13 @@ where
.filter_map(Result::ok);
results.extend(response);
info!(
"obtained {0} of {1} keys with {2} more to request",
results.len(),
total_keys,
server_keys.len(),
);
}
Ok(results)
+10 -2
View File
@@ -1,6 +1,6 @@
use ruma::{CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, RoomVersionId};
use tuwunel_core::{
Result, implement,
Result, err, implement,
matrix::{event::gen_event_id, room_version},
};
@@ -73,7 +73,15 @@ pub fn hash_and_sign_event(
object,
&room_version_rules.redaction,
)
.map_err(Into::into)
.map_err(|e| {
use ruma::signatures::Error::PduSize;
match e {
| PduSize => {
err!(Request(TooLarge("PDU exceeds 65535 bytes")))
},
| _ => err!(Request(Unknown(warn!("Signing event failed: {e}")))),
}
})
}
#[implement(super::Service)]
+4 -1
View File
@@ -12,7 +12,7 @@ use crate::{
account_data, admin, appservice, client, config, deactivate, emergency, federation, globals,
key_backups,
manager::Manager,
media, membership, presence, pusher, resolver, rooms, sending, server_keys,
media, membership, oauth, presence, pusher, resolver, rooms, sending, server_keys,
service::{Args, Service},
sync, transaction_ids, uiaa, users,
};
@@ -58,6 +58,7 @@ pub struct Services {
pub users: Arc<users::Service>,
pub membership: Arc<membership::Service>,
pub deactivate: Arc<deactivate::Service>,
pub oauth: Arc<oauth::Service>,
manager: Mutex<Option<Arc<Manager>>>,
pub server: Arc<Server>,
@@ -115,6 +116,7 @@ pub async fn build(server: Arc<Server>) -> Result<Arc<Self>> {
users: users::Service::build(&args)?,
membership: membership::Service::build(&args)?,
deactivate: deactivate::Service::build(&args)?,
oauth: oauth::Service::build(&args)?,
manager: Mutex::new(None),
server,
@@ -173,6 +175,7 @@ pub(crate) fn services(&self) -> impl Iterator<Item = Arc<dyn Service>> + Send {
cast!(self.users),
cast!(self.membership),
cast!(self.deactivate),
cast!(self.oauth),
]
.into_iter()
}
+8 -4
View File
@@ -59,7 +59,7 @@ pub struct Room {
type Connections = TokioMutex<BTreeMap<ConnectionKey, ConnectionVal>>;
pub type ConnectionVal = Arc<TokioMutex<Connection>>;
pub type ConnectionKey = (OwnedUserId, OwnedDeviceId, Option<ConnectionId>);
pub type ConnectionKey = (OwnedUserId, Option<OwnedDeviceId>, Option<ConnectionId>);
pub type Subscriptions = BTreeMap<OwnedRoomId, request::ListConfig>;
pub type Lists = BTreeMap<ListId, request::List>;
@@ -107,7 +107,7 @@ pub async fn clear_connections(
.await
.retain(|(conn_user_id, conn_device_id, conn_conn_id), _| {
let retain = user_id.is_none_or(is_equal_to!(conn_user_id))
&& device_id.is_none_or(is_equal_to!(conn_device_id))
&& (device_id.is_none() || device_id == conn_device_id.as_deref())
&& (conn_id.is_none() || conn_id == conn_conn_id.as_ref());
if !retain {
@@ -215,13 +215,17 @@ pub async fn is_connection_stored(&self, key: &ConnectionKey) -> bool {
}
#[inline]
pub fn into_connection_key<U, D, C>(user_id: U, device_id: D, conn_id: Option<C>) -> ConnectionKey
pub fn into_connection_key<U, D, C>(
user_id: U,
device_id: Option<D>,
conn_id: Option<C>,
) -> ConnectionKey
where
U: Into<OwnedUserId>,
D: Into<OwnedDeviceId>,
C: Into<ConnectionId>,
{
(user_id.into(), device_id.into(), conn_id.map(Into::into))
(user_id.into(), device_id.map(Into::into), conn_id.map(Into::into))
}
#[implement(Connection)]
+15 -10
View File
@@ -8,25 +8,18 @@ use tuwunel_database::{Interfix, Separator, serialize_key};
pub async fn watch<'a, Rooms>(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_id: Option<&DeviceId>,
rooms: Rooms,
) -> Result
where
Rooms: Stream<Item = &'a RoomId> + Send + 'a,
{
let userdeviceid_prefix = (user_id, device_id, Interfix);
let globaluserdata_prefix = (Separator, user_id, Interfix);
let roomuserdataid_prefix = (Option::<&RoomId>::None, user_id, Interfix);
let userid_prefix =
serialize_key((user_id, Interfix)).expect("failed to serialize watch prefix");
let watchers = [
// Return when *any* user changed their key
// TODO: only send for user they share a room with
self.db
.todeviceid_events
.watch_prefix(&userdeviceid_prefix)
.boxed(),
self.db
.userroomid_joined
.watch_raw_prefix(&userid_prefix)
@@ -72,8 +65,20 @@ where
.boxed(),
];
let mut futures = FuturesUnordered::new();
futures.extend(watchers.into_iter());
let device_watchers = device_id.into_iter().map(|device_id| {
// Return when *any* user changed their key
// TODO: only send for user they share a room with
let userdeviceid_prefix = (user_id, device_id, Interfix);
self.db
.todeviceid_events
.watch_prefix(&userdeviceid_prefix)
.boxed()
});
let mut futures: FuturesUnordered<_> = watchers
.into_iter()
.chain(device_watchers)
.collect();
pin_mut!(rooms);
while let Some(room_id) = rooms.next().await {
+6 -28
View File
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, HashSet},
collections::BTreeMap,
sync::{Arc, RwLock},
};
@@ -45,32 +45,6 @@ impl crate::Service for Service {
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
#[implement(Service)]
pub async fn read_tokens(&self) -> Result<HashSet<String>> {
let mut tokens = HashSet::new();
if let Some(file) = &self
.services
.config
.registration_token_file
.as_ref()
{
match std::fs::read_to_string(file) {
| Err(e) => error!("Failed to read the registration token file: {e}"),
| Ok(text) => {
text.split_ascii_whitespace().for_each(|token| {
tokens.insert(token.to_owned());
});
},
}
}
if let Some(token) = &self.services.config.registration_token {
tokens.insert(token.to_owned());
}
Ok(tokens)
}
/// Creates a new Uiaa session. Make sure the session token is unique.
#[implement(Service)]
pub fn create(
@@ -175,7 +149,11 @@ pub async fn try_auth(
uiaainfo.completed.push(AuthType::Password);
},
| AuthData::RegistrationToken(t) => {
let tokens = self.read_tokens().await?;
let tokens = self
.services
.globals
.get_registration_tokens()
.await;
if tokens.contains(t.token.trim()) {
uiaainfo
.completed
+2 -2
View File
@@ -51,10 +51,10 @@ pub async fn set_dehydrated_device(&self, user_id: &UserId, request: Request) ->
self.create_device(
user_id,
&request.device_id,
Some(&request.device_id),
(None, None),
None,
request.initial_device_display_name.clone(),
request.initial_device_display_name.as_deref(),
None,
)
.await?;
+13 -6
View File
@@ -19,6 +19,9 @@ use tuwunel_core::{
};
use tuwunel_database::{Deserialized, Ignore, Interfix, Json, Map};
/// generated device ID length
const DEVICE_ID_LENGTH: usize = 10;
/// generated user access token length
pub const TOKEN_LENGTH: usize = 32;
@@ -28,12 +31,16 @@ pub const TOKEN_LENGTH: usize = 32;
pub async fn create_device(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_id: Option<&DeviceId>,
(access_token, expires_in): (Option<&str>, Option<Duration>),
refresh_token: Option<&str>,
initial_device_display_name: Option<String>,
initial_device_display_name: Option<&str>,
client_ip: Option<String>,
) -> Result {
) -> Result<OwnedDeviceId> {
let device_id = device_id
.map(ToOwned::to_owned)
.unwrap_or_else(|| OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH)));
if !self.exists(user_id).await {
return Err!(Request(InvalidParam(error!(
"Called create_device for non-existent user {user_id}"
@@ -42,18 +49,18 @@ pub async fn create_device(
let notify = true;
self.put_device_metadata(user_id, notify, &Device {
device_id: device_id.into(),
device_id: device_id.clone(),
display_name: initial_device_display_name.map(Into::into),
last_seen_ip: client_ip.map(Into::into),
last_seen_ts: Some(MilliSecondsSinceUnixEpoch::now()),
});
if let Some(access_token) = access_token {
self.set_access_token(user_id, device_id, access_token, expires_in, refresh_token)
self.set_access_token(user_id, &device_id, access_token, expires_in, refresh_token)
.await?;
}
Ok(())
Ok(device_id)
}
/// Removes a device from a user.
+8
View File
@@ -21,6 +21,10 @@ pub async fn search_ldap(&self, user_id: &UserId) -> Result<Vec<(String, bool)>>
.as_ref()
.ok_or_else(|| err!(Ldap(error!("LDAP URI is not configured."))))?;
if uri.scheme().starts_with("ldaps") {
self.services.globals.init_rustls_provider()?;
}
debug!(?uri, "LDAP creating connection...");
let (conn, mut ldap) = LdapConnAsync::new(uri.as_str())
.await
@@ -122,6 +126,10 @@ pub async fn auth_ldap(&self, user_dn: &str, password: &str) -> Result {
.as_ref()
.ok_or_else(|| err!(Ldap(error!("LDAP URI is not configured."))))?;
if uri.scheme().starts_with("ldaps") {
self.services.globals.init_rustls_provider()?;
}
debug!(?uri, "LDAP creating connection...");
let (conn, mut ldap) = LdapConnAsync::new(uri.as_str())
.await
+23 -17
View File
@@ -3,6 +3,7 @@ pub mod device;
mod keys;
mod ldap;
mod profile;
mod register;
use std::sync::Arc;
@@ -13,15 +14,15 @@ use ruma::{
events::{GlobalAccountDataEventType, ignored_user_list::IgnoredUserListEvent},
};
use tuwunel_core::{
Err, Result, debug_warn, err, is_equal_to,
Err, Result, debug_warn, err,
pdu::PduBuilder,
trace,
utils::{self, ReadyExt, stream::TryIgnore},
utils::{self, ReadyExt, result::LogErr, stream::TryIgnore},
warn,
};
use tuwunel_database::{Deserialized, Json, Map};
pub use self::keys::parse_master_key;
pub use self::{keys::parse_master_key, register::Register};
pub struct Service {
services: Arc<crate::services::OnceServices>,
@@ -124,15 +125,23 @@ impl Service {
password: Option<&str>,
origin: Option<&str>,
) -> Result {
origin.map_or_else(
|| self.db.userid_origin.insert(user_id, "password"),
|origin| self.db.userid_origin.insert(user_id, origin),
);
let origin = origin.unwrap_or("password");
self.db.userid_origin.insert(user_id, origin);
self.set_password(user_id, password).await
}
/// Deactivate account
pub async fn deactivate_account(&self, user_id: &UserId) -> Result {
// Revoke any SSO authorizations
if let Ok((provider, session)) = self.services.oauth.get_user(user_id).await {
self.services
.oauth
.revoke_token((&provider, &session))
.await
.log_err()
.ok();
}
// Remove all associated devices
self.all_device_ids(user_id)
.for_each(|device_id| self.remove_device(user_id, device_id))
@@ -228,18 +237,15 @@ impl Service {
// Cannot change the password of a LDAP user. There are two special cases :
// - a `None` password can be used to deactivate a LDAP user
// - a "*" password is used as the default password of an active LDAP user
if cfg!(feature = "ldap")
&& password.is_some()
//
// The above now applies to all non-password origin users including SSO
// users. Note that users with no origin are also password-origin users.
if password.is_some()
&& password != Some("*")
&& self
.db
.userid_origin
.get(user_id)
.await
.deserialized::<String>()
.is_ok_and(is_equal_to!("ldap"))
&& let Ok(origin) = self.origin(user_id).await
&& origin != "password"
{
return Err!(Request(InvalidParam("Cannot change password of a LDAP user")));
return Err!(Request(InvalidParam("Cannot change password of an {origin:?} user.")));
}
password
+186
View File
@@ -0,0 +1,186 @@
use futures::FutureExt;
use ruma::{UserId, events::GlobalAccountDataEventType, push};
use tuwunel_core::{Err, Result, error, implement, info, is_equal_to, warn};
use crate::appservice::RegistrationInfo;
#[derive(Debug, Default)]
pub struct Register<'a> {
pub user_id: Option<&'a UserId>,
pub username: Option<&'a str>,
pub password: Option<&'a str>,
pub origin: Option<&'a str>,
pub appservice_info: Option<&'a RegistrationInfo>,
pub is_guest: bool,
pub grant_first_user_admin: bool,
pub displayname: Option<&'a str>,
pub omit_displayname_suffix: bool,
}
/// Fully register a local user
///
/// Returns a device id and access token for the registered user
#[implement(super::Service)]
#[tracing::instrument(level = "info", skip(self))]
pub async fn full_register(
&self,
Register {
username,
user_id,
password,
origin,
appservice_info,
is_guest,
grant_first_user_admin,
displayname,
omit_displayname_suffix,
}: Register<'_>,
) -> Result {
let ref user_id = user_id
.map(ToOwned::to_owned)
.map(Ok)
.or_else(|| {
username.map(|username| {
UserId::parse_with_server_name(username, self.services.globals.server_name())
})
})
.transpose()?
.expect("Caller failed to supply either user_id or username parameter");
if !self.services.globals.user_is_local(user_id) {
return Err!("Cannot register remote user");
}
if self.services.users.exists(user_id).await {
return Err!(Request(UserInUse("User ID is not available.")));
}
// Create user
self.services
.users
.create(user_id, password, origin)
.await?;
let displayname_suffix = self
.services
.config
.new_user_displayname_suffix
.as_str();
let add_displayname_suffix = !displayname_suffix.is_empty() && !omit_displayname_suffix;
if let Some(displayname) =
displayname.or_else(|| add_displayname_suffix.then(|| user_id.localpart()))
{
// If `new_user_displayname_suffix` is set, registration will push whatever
// content is set to the user's display name with a space before it
let displayname = format!(
"{displayname}{}{}",
add_displayname_suffix
.then_some(" ")
.unwrap_or(""),
add_displayname_suffix
.then_some(displayname_suffix)
.unwrap_or("")
);
self.services
.users
.set_displayname(user_id, Some(displayname));
}
// Initial account data
self.services
.account_data
.update(
None,
user_id,
GlobalAccountDataEventType::PushRules
.to_string()
.into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(user_id),
},
})?,
)
.await?;
// If this is the first real user, grant them admin privileges except for guest
// users
// Note: the server user is generated first
if !is_guest
&& grant_first_user_admin
&& self.services.config.grant_admin_to_first_user
&& let Ok(admin_room) = self.services.admin.get_admin_room().await
&& self
.services
.state_cache
.room_joined_count(&admin_room)
.await
.is_ok_and(is_equal_to!(1))
{
self.services
.admin
.make_user_admin(user_id)
.boxed()
.await?;
warn!("Granting {user_id} admin privileges as the first user");
}
if appservice_info.is_none()
&& (self.services.config.allow_guests_auto_join_rooms || !is_guest)
{
for room in &self.services.server.config.auto_join_rooms {
let Ok(room_id) = self.services.alias.maybe_resolve(room).await else {
error!(
"Failed to resolve room alias to room ID when attempting to auto join \
{room}, skipping"
);
continue;
};
if !self
.services
.state_cache
.server_in_room(self.services.globals.server_name(), &room_id)
.await
{
warn!(
"Skipping room {room} to automatically join as we have never joined before."
);
continue;
}
let state_lock = self.services.state.mutex.lock(&room_id).await;
match self
.services
.membership
.join(
user_id,
&room_id,
Some(room),
Some("Automatically joining this room upon registration".to_owned()),
&[],
false,
&state_lock,
)
.boxed()
.await
{
| Err(e) => {
// don't return this error so we don't fail registrations
error!("Failed to automatically join room {room} for user {user_id}: {e}");
},
| _ => {
info!("Automatically joined room {room} for user {user_id}");
},
}
drop(state_lock);
}
}
Ok(())
}
+2 -2
View File
@@ -225,8 +225,8 @@
{"Action":"fail","Test":"TestKeyChangesLocal/New_login_should_create_a_device_lists.changed_entry"}
{"Action":"fail","Test":"TestKeyClaimOrdering"}
{"Action":"pass","Test":"TestKeysQueryWithDeviceIDAsObjectFails"}
{"Action":"fail","Test":"TestKnockRoomsInPublicRoomsDirectory"}
{"Action":"fail","Test":"TestKnockRoomsInPublicRoomsDirectoryInMSC3787Room"}
{"Action":"pass","Test":"TestKnockRoomsInPublicRoomsDirectory"}
{"Action":"pass","Test":"TestKnockRoomsInPublicRoomsDirectoryInMSC3787Room"}
{"Action":"fail","Test":"TestKnocking"}
{"Action":"fail","Test":"TestKnocking/A_user_can_knock_on_a_room_without_a_reason"}
{"Action":"fail","Test":"TestKnocking/A_user_can_knock_on_a_room_without_a_reason#01"}
+144 -5
View File
@@ -15,6 +15,7 @@
### For more information, see:
### https://tuwunel.chat/configuration.html
[global]
# The server_name is the pretty name of this server. It is used as a
@@ -1812,11 +1813,6 @@
# This option exists for developer and debug use, and as a failsafe in
# lieu of hardcoding it.
#
# This currently defaults to false as a matter of development until
# real-world testing can shake out any implementation issues rather than
# jeopardize existing rooms, but otherwise will default to true at the
# next point release or patch.
#
#hydra_backports = true
# Delete rooms when the last user from this server leaves. This feature is
@@ -1838,6 +1834,12 @@
#
#one_time_key_limit = 256
# This item is undocumented. Please contribute documentation for it.
#
#sso_aware_preferred = false
#[global.tls]
# Path to a valid TLS certificate file.
@@ -1856,6 +1858,8 @@
#
#dual_protocol = false
#[global.well_known]
# The server URL that the client well-known file will serve. This should
@@ -1918,6 +1922,8 @@
#
#rtc_transports = []
#[global.blurhashing]
# blurhashing x component, 4 is recommended by https://blurha.sh/
@@ -1936,6 +1942,8 @@
#
#blurhash_max_raw_size = 33554432
#[global.ldap]
# Whether to enable LDAP login.
@@ -2023,6 +2031,8 @@
#
#admin_filter =
#[global.jwt]
# Enable JWT logins
@@ -2089,6 +2099,133 @@
#
#validate_signature = true
#[[global.identity_provider]]
# The brand-name of the service (e.g. Apple, Facebook, GitHub, GitLab,
# Google) or the software (e.g. keycloak, MAS) providing the identity.
# When a brand is recognized we apply certain defaults to this config
# for your convenience. For certain brands we apply essential internal
# workarounds specific to that provider; it is important to configure this
# field properly when a provider needs to be recognized (like GitHub for
# example). Several configured providers can share the same brand name. It
# is not case-sensitive.
#
#brand =
# The ID of your OAuth application which the provider generates upon
# registration. This ID then uniquely identifies this configuration
# instance itself, becoming the identity provider's ID and must be unique
# and remain unchanged.
#
#client_id =
# Secret key the provider generated for you along with the `client_id`
# above. Unlike the `client_id`, the `client_secret` can be changed here
# whenever the provider regenerates one for you.
#
#client_secret =
# The callback URL configured when registering the OAuth application with
# the provider. Tuwunel's callback URL must be strictly formatted exactly
# as instructed. The URL host must point directly at the matrix server and
# use the following path:
# `/_matrix/client/unstable/login/sso/callback/<client_id>` where
# `<client_id>` is the same one configured for this provider above.
#
#callback_url =
# Optional display-name for this provider instance seen on the login page
# by users. It defaults to `brand`. When configuring multiple providers
# using the same `brand` this can be set to distinguish them.
#
#name =
# Optional icon for the provider. The canonical providers have a default
# icon based on the `brand` supplied above when this is not supplied. Note
# that it uses an MXC url which is curious in the auth-media era and may
# not be reliable.
#
#icon =
# Optional list of scopes to authorize. An empty array does not impose any
# restrictions from here, effectively defaulting to all scopes you
# configured for the OAuth application at the provider. This setting
# allows for restricting to a subset of those scopes for this instance.
# Note the user can further restrict scopes during their authorization.
#
#scope = []
# List of userinfo claims which shape and restrict the way we compute a
# Matrix UserId for new registrations. Reviewing Tuwunel's documentation
# will be necessary for a complete description in detail. An empty array
# imposes no restriction here, avoiding generated fallbacks as much as
# possible. For simplicity we reserve a claim called "unique" which can be
# listed alone to ensure *only* generated ID's are used for registrations.
#
#userid_claims = []
# Issuer URL the provider publishes for you. We have pre-supplied default
# values for some of the canonical providers, making this field optional
# based on the `brand` set above. Otherwise it is required for OIDC
# discovery to acquire additional provider configuration, and it must be
# correct to pass validations during various interactions.
#
#issuer_url =
# Extra path components after the issuer_url leading to the location of
# the `.well-known` directory used for discovery. This will be empty for
# specification-compliant providers. We have supplied any known values
# based on `brand` (e.g. `/login/oauth` for GitHub).
#
#base_path =
# Overrides the `.well-known` location where the provider's OIDC
# configuration is found. It is very unlikely you will need to set this;
# available for developers or special purposes only.
#
#discovery_url =
# Overrides the authorize URL requested during the grant phase. This is
# generally discovered or derived automatically, but may be required as a
# workaround for any non-standard or undiscoverable provider.
#
#authorization_url =
# Overrides the access token URL; the same caveats apply as with the other
# URL overrides.
#
#token_url =
# Overrides the revocation URL; the same caveats apply as with the other
# URL overrides.
#
#revocation_url =
# Overrides the introspection URL; the same caveats apply as with the
# other URL overrides.
#
#introspection_url =
# Overrides the userinfo URL; the same caveats apply as with the other URL
# overrides.
#
#userinfo_url =
# Whether to perform discovery and adjust this provider's configuration
# accordingly. This defaults to true. When true, it is an error when
# discovery fails and authorizations will not be attempted to the
# provider.
#
#discovery = true
# The duration in seconds before a grant authorization session expires.
#
#grant_session_duration =
#[global.appservice.<ID>]
# The URL for the application service.
@@ -2131,6 +2268,8 @@
#
#device_management = false
#[[global.appservice.<ID>.<users|rooms|aliases>]]
# Whether this application service has exclusive access to events within