mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-16 05:36:50 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d312cbd7f9 | ||
|
|
10cc025dca | ||
|
|
c308b29afa | ||
|
|
04fd44b94b | ||
|
|
aa85d3b470 | ||
|
|
0c266c28f6 |
No files matched your search
@@ -392,6 +392,7 @@ features = [
|
||||
"unstable-msc4406",
|
||||
"unstable-msc4439",
|
||||
"unstable-msc4354",
|
||||
"unstable-msc4388",
|
||||
"unstable-msc4480",
|
||||
"unstable-msc4466",
|
||||
"unstable-msc4494",
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Added support for MSC4388, allowing compatible Matrix clients to log in to Continuwuity by scanning a QR code on an already logged-in client. Contributed by @ginger.
|
||||
+21
-4
@@ -812,15 +812,32 @@
|
||||
# env: CONTINUWUITY_FORGET_FORCED_UPON_LEAVE
|
||||
#forget_forced_upon_leave = false
|
||||
|
||||
# Set this to true to require authentication on the normally
|
||||
# unauthenticated profile retrieval endpoints (GET)
|
||||
# "/_matrix/client/v3/profile/{userId}".
|
||||
# Set this to true to require authentication when looking up user profiles
|
||||
# through this server.
|
||||
#
|
||||
# This can prevent profile scraping.
|
||||
# By default this is false, allowing tools such as matrix.to to access
|
||||
# profiles of users on this server and on other servers.
|
||||
# Setting this to true will block those tools, and may block automated
|
||||
# crawlers from scraping users' profiles, although it will not block
|
||||
# other homeservers from accessing users' profiles.
|
||||
#
|
||||
# env: CONTINUWUITY_REQUIRE_AUTH_FOR_PROFILE_REQUESTS
|
||||
#require_auth_for_profile_requests = false
|
||||
|
||||
# Set this to true to require authentication to create a rendezvous
|
||||
# session. Rendezvous sessions are used for QR code login.
|
||||
#
|
||||
# For QR code login, the device that shows the QR code is the one that
|
||||
# creates the rendezvous session. Therefore, this must be false to let
|
||||
# a logged-out device show a QR code.
|
||||
#
|
||||
# By default this is false, allowing logged-out desktop devices (which may
|
||||
# not have a camera) to show a QR code. Setting this to true will
|
||||
# require logged-out devices to scan the QR code instead.
|
||||
#
|
||||
# env: CONTINUWUITY_REQUIRE_AUTH_FOR_RENDEZVOUS
|
||||
#require_auth_for_rendezvous = true
|
||||
|
||||
# Set this to true to allow your server's public room directory to be
|
||||
# federated. Set this to false to protect against /publicRooms spiders,
|
||||
# but will forbid external users from viewing your server's public room
|
||||
|
||||
@@ -24,6 +24,7 @@ pub(super) mod push;
|
||||
pub(super) mod read_marker;
|
||||
pub(super) mod redact;
|
||||
pub(super) mod relations;
|
||||
pub(super) mod rendezvous;
|
||||
pub(super) mod report;
|
||||
pub(super) mod room;
|
||||
pub(super) mod search;
|
||||
@@ -71,6 +72,7 @@ pub(super) use push::*;
|
||||
pub(super) use read_marker::*;
|
||||
pub(super) use redact::*;
|
||||
pub(super) use relations::*;
|
||||
pub(super) use rendezvous::*;
|
||||
pub(super) use report::*;
|
||||
pub(super) use room::*;
|
||||
pub(super) use search::*;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
use axum::extract::State;
|
||||
use conduwuit::{Err, Result};
|
||||
use http::HeaderMap;
|
||||
use ruma::api::client::rendezvous::{
|
||||
create_rendezvous_session, delete_rendezvous_session, discover_rendezvous,
|
||||
get_rendezvous_session, update_rendezvous_session,
|
||||
};
|
||||
use service::Services;
|
||||
|
||||
use crate::{Ruma, router::ClientIdentity};
|
||||
|
||||
/// `GET /_matrix/client/msc4388/io.element.msc4388/rendezvous`
|
||||
pub(crate) async fn discover_rendezvous_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<discover_rendezvous::msc4388::Request>,
|
||||
) -> Result<discover_rendezvous::msc4388::Response> {
|
||||
Ok(discover_rendezvous::msc4388::Response::new(rendezvous_available(
|
||||
&services,
|
||||
body.identity.as_ref(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// `POST /_matrix/client/msc4388/io.element.msc4388/rendezvous/`
|
||||
pub(crate) async fn create_rendezvous_session_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<create_rendezvous_session::msc4388::Request>,
|
||||
) -> Result<create_rendezvous_session::msc4388::Response> {
|
||||
if !rendezvous_available(&services, body.identity.as_ref()) {
|
||||
return Err!(Request(Forbidden("You are not authorized to create a rendezvous session")));
|
||||
}
|
||||
|
||||
services.rendezvous.create_session(body.data.clone())
|
||||
}
|
||||
|
||||
/// `PUT /_matrix/client/msc4388/io.element.msc4388/rendezvous/{id}/{txn_id}`
|
||||
pub(crate) async fn update_rendezvous_session_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<update_rendezvous_session::msc4388::Request>,
|
||||
) -> Result<update_rendezvous_session::msc4388::Response> {
|
||||
services.rendezvous.update_session(
|
||||
&body.id,
|
||||
body.txn_id.clone(),
|
||||
body.sequence_token.clone(),
|
||||
body.data.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
/// `GET /_matrix/client/msc4388/io.element.msc4388/rendezvous/{id}`
|
||||
pub(crate) async fn get_rendezvous_session_route(
|
||||
State(services): State<crate::State>,
|
||||
headers: HeaderMap,
|
||||
body: Ruma<get_rendezvous_session::msc4388::Request>,
|
||||
) -> Result<get_rendezvous_session::msc4388::Response> {
|
||||
if let Some(mode) = headers.get("Sec-Fetch-Mode")
|
||||
&& let Ok(mode) = mode.to_str()
|
||||
&& mode == "navigate"
|
||||
{
|
||||
return Err!(Request(Forbidden(
|
||||
"Rendezvous sessions do not accept browser navigation requests."
|
||||
)));
|
||||
}
|
||||
|
||||
services.rendezvous.get_session(&body.id)
|
||||
}
|
||||
|
||||
/// `DELETE /_matrix/client/msc4388/io.element.msc4388/rendezvous/{id}`
|
||||
pub(crate) async fn delete_rendezvous_session_route(
|
||||
State(services): State<crate::State>,
|
||||
body: Ruma<delete_rendezvous_session::msc4388::Request>,
|
||||
) -> Result<delete_rendezvous_session::msc4388::Response> {
|
||||
services.rendezvous.delete_session(&body.id)
|
||||
}
|
||||
|
||||
fn rendezvous_available(services: &Services, identity: Option<&ClientIdentity>) -> bool {
|
||||
!services.config.require_auth_for_rendezvous || identity.is_some()
|
||||
}
|
||||
@@ -194,6 +194,11 @@ pub fn build(router: Router<State>, state: State) -> Router<State> {
|
||||
.ruma_route(&client::well_known_policy_server)
|
||||
.ruma_route(&client::get_rtc_transports)
|
||||
.ruma_route(&client::room_initial_sync_route)
|
||||
.ruma_route(&client::discover_rendezvous_route)
|
||||
.ruma_route(&client::create_rendezvous_session_route)
|
||||
.ruma_route(&client::update_rendezvous_session_route)
|
||||
.ruma_route(&client::get_rendezvous_session_route)
|
||||
.ruma_route(&client::delete_rendezvous_session_route)
|
||||
.ruma_route(&client::get_authorization_server_metadata_route)
|
||||
.merge(client::oauth::router(state))
|
||||
.route("/_continuwuity/server_version", get(client::continuwuity_server_version))
|
||||
|
||||
+24
-4
@@ -918,14 +918,34 @@ pub struct Config {
|
||||
#[serde(default)]
|
||||
pub forget_forced_upon_leave: bool,
|
||||
|
||||
/// Set this to true to require authentication on the normally
|
||||
/// unauthenticated profile retrieval endpoints (GET)
|
||||
/// "/_matrix/client/v3/profile/{userId}".
|
||||
/// Set this to true to require authentication when looking up user profiles
|
||||
/// through this server.
|
||||
///
|
||||
/// This can prevent profile scraping.
|
||||
/// By default this is false, allowing tools such as matrix.to to access
|
||||
/// profiles of users on this server and on other servers.
|
||||
/// Setting this to true will block those tools, and may block automated
|
||||
/// crawlers from scraping users' profiles, although it will not block
|
||||
/// other homeservers from accessing users' profiles.
|
||||
///
|
||||
/// default: false
|
||||
#[serde(default)]
|
||||
pub require_auth_for_profile_requests: bool,
|
||||
|
||||
/// Set this to true to require authentication to create a rendezvous
|
||||
/// session. Rendezvous sessions are used for QR code login.
|
||||
///
|
||||
/// For QR code login, the device that shows the QR code is the one that
|
||||
/// creates the rendezvous session. Therefore, this must be false to let
|
||||
/// a logged-out device show a QR code.
|
||||
///
|
||||
/// By default this is false, allowing logged-out desktop devices (which may
|
||||
/// not have a camera) to show a QR code. Setting this to true will
|
||||
/// require logged-out devices to scan the QR code instead.
|
||||
///
|
||||
/// default: true
|
||||
#[serde(default)]
|
||||
pub require_auth_for_rendezvous: bool,
|
||||
|
||||
/// Set this to true to allow your server's public room directory to be
|
||||
/// federated. Set this to false to protect against /publicRooms spiders,
|
||||
/// but will forbid external users from viewing your server's public room
|
||||
|
||||
+2
-1
@@ -37,7 +37,8 @@ pub use error::Error;
|
||||
pub use info::version::*;
|
||||
pub use matrix::{Event, EventTypeExt, Pdu, PduCount, PduEvent, PduId, pdu, state_res};
|
||||
pub use parking_lot::{
|
||||
Mutex as SyncMutex, RwLock as SyncRwLock, RwLockReadGuard as SyncRwLockReadGuard,
|
||||
MappedMutexGuard as SyncMappedMutexGuard, Mutex as SyncMutex, MutexGuard as SyncMutexGuard,
|
||||
RwLock as SyncRwLock, RwLockReadGuard as SyncRwLockReadGuard,
|
||||
RwLockWriteGuard as SyncRwLockWriteGuard,
|
||||
};
|
||||
pub use server::Server;
|
||||
|
||||
@@ -33,6 +33,7 @@ pub mod oidc;
|
||||
pub mod presence;
|
||||
pub mod pusher;
|
||||
pub mod registration_tokens;
|
||||
pub mod rendezvous;
|
||||
pub mod rooms;
|
||||
pub mod sending;
|
||||
pub mod server_keys;
|
||||
|
||||
@@ -106,12 +106,14 @@ impl Display for RequestedScopes {
|
||||
pub struct RawScopes(String);
|
||||
|
||||
impl RawScopes {
|
||||
const MIN_DEVICE_ID_LENGTH: usize = 5;
|
||||
|
||||
#[allow(clippy::trivial_regex)]
|
||||
pub fn to_scopes(&self) -> Result<RequestedScopes, String> {
|
||||
let client_api_token_regex =
|
||||
regex!(r"urn:matrix:(client|org.matrix.msc2967.client):api:\*");
|
||||
let device_token_regex =
|
||||
regex!(r"urn:matrix:(client|org.matrix.msc2967.client):device:([a-zA-Z0-9-._~]{5,})");
|
||||
regex!(r"urn:matrix:(client|org.matrix.msc2967.client):device:(.+)");
|
||||
let server_administration_regex =
|
||||
regex!(r"urn:matrix:client:cc.c10y.msc4484.server_administration");
|
||||
|
||||
@@ -124,10 +126,40 @@ impl RawScopes {
|
||||
} else if server_administration_regex.is_match(token) {
|
||||
scopes.scopes.insert(OAuthClientScope::ServerAdministration)
|
||||
} else if let Some(captures) = device_token_regex.captures(token) {
|
||||
scopes
|
||||
.device_id
|
||||
.replace(captures.get(2).unwrap().as_str().into())
|
||||
.is_none()
|
||||
let device_id = captures.get(2).unwrap().as_str();
|
||||
|
||||
if device_id.len() < Self::MIN_DEVICE_ID_LENGTH {
|
||||
return Err(format!(
|
||||
"Device ID is too short, it must be at least {} characters",
|
||||
Self::MIN_DEVICE_ID_LENGTH
|
||||
));
|
||||
}
|
||||
|
||||
// The Matrix specification has a "suggested" set of
|
||||
// characters to be used in the device
|
||||
// ID scope. In the past we checked against this set, but we
|
||||
// no longer do that; instead we check
|
||||
// against the _required_ set of
|
||||
// characters defined by the OAuth specification.
|
||||
// We do this because we have observed SDKs in the wild
|
||||
// (*cough* matrix-rust-sdk) that will provide device IDs
|
||||
// that are just unpadded base64, which they're allowed to
|
||||
// do, since the spec just says that
|
||||
// clients "SHOULD" only use the suggested character set and
|
||||
// servers "MAY" choose to require that they do.
|
||||
// Of course this actually means that clients will do
|
||||
// whatever the hell they want and
|
||||
// servers can never ever be stricter
|
||||
// than they absolutely have to be. What
|
||||
// a stupid provision.
|
||||
if device_id
|
||||
.chars()
|
||||
.any(|char| !matches!(char, '\x21' | '\x23'..='\x5b' | '\x5d'..='\x7e'))
|
||||
{
|
||||
return Err("Device ID contains disallowed characters".to_owned());
|
||||
}
|
||||
|
||||
scopes.device_id.replace(device_id.into()).is_none()
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use conduwuit::{Err, Result, SyncMappedMutexGuard, SyncMutex, SyncMutexGuard, at, err, utils};
|
||||
use lru_cache::LruCache;
|
||||
use ruma::{
|
||||
OwnedTransactionId,
|
||||
api::client::rendezvous::{
|
||||
create_rendezvous_session, delete_rendezvous_session, get_rendezvous_session,
|
||||
update_rendezvous_session,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct Service {
|
||||
sessions: SyncMutex<LruCache<String, Session>>,
|
||||
}
|
||||
|
||||
struct Session {
|
||||
data: String,
|
||||
created_at: SystemTime,
|
||||
last_sequence_token: String,
|
||||
transaction_sequences: LruCache<OwnedTransactionId, String>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
const SAVED_TRANSACTION_SEQUENCES: usize = 256;
|
||||
|
||||
fn new(data: String, sequence_token: String) -> Self {
|
||||
Self {
|
||||
data,
|
||||
created_at: SystemTime::now(),
|
||||
last_sequence_token: sequence_token,
|
||||
transaction_sequences: LruCache::new(Self::SAVED_TRANSACTION_SEQUENCES),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Service for Service {
|
||||
fn build(_: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
sessions: SyncMutex::new(LruCache::new(Self::MAX_SESSIONS)),
|
||||
}))
|
||||
}
|
||||
|
||||
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
||||
}
|
||||
|
||||
impl Service {
|
||||
const MAX_DATA_SIZE_BYTES: usize = 4096;
|
||||
const MAX_SESSIONS: usize = 256;
|
||||
const SESSION_MAX_AGE: Duration = Duration::from_secs(300);
|
||||
const TOKEN_LENGTH: usize = 32;
|
||||
|
||||
fn create_token() -> String { utils::random_string(Self::TOKEN_LENGTH) }
|
||||
|
||||
/// Lock and fetch an unexpired rendezvous session.
|
||||
fn lock_session(&self, id: &str) -> Result<SyncMappedMutexGuard<'_, Session>> {
|
||||
// We don't clear out expired sessions manually, the LRU cache will
|
||||
// evict them over time
|
||||
SyncMutexGuard::try_map_or_err(self.sessions.lock(), |sessions| {
|
||||
sessions
|
||||
.get_mut(id)
|
||||
.filter(|session| {
|
||||
SystemTime::now()
|
||||
.duration_since(session.created_at)
|
||||
.is_ok_and(|duration| duration < Self::SESSION_MAX_AGE)
|
||||
})
|
||||
.ok_or_else(|| err!(Request(NotFound("Unknown session"))))
|
||||
})
|
||||
.map_err(at!(1))
|
||||
}
|
||||
|
||||
/// Create a fresh rendezvous session.
|
||||
pub fn create_session(
|
||||
&self,
|
||||
data: String,
|
||||
) -> Result<create_rendezvous_session::msc4388::Response> {
|
||||
if data.len() > Self::MAX_DATA_SIZE_BYTES {
|
||||
return Err!(Request(TooLarge("Data too large")));
|
||||
}
|
||||
|
||||
let id = Self::create_token();
|
||||
let sequence_token = Self::create_token();
|
||||
|
||||
self.sessions
|
||||
.lock()
|
||||
.insert(id.clone(), Session::new(data, sequence_token.clone()));
|
||||
|
||||
Ok(create_rendezvous_session::msc4388::Response::new(
|
||||
id,
|
||||
sequence_token,
|
||||
Self::SESSION_MAX_AGE,
|
||||
))
|
||||
}
|
||||
|
||||
/// Update a rendezvous session with new data.
|
||||
pub fn update_session(
|
||||
&self,
|
||||
id: &str,
|
||||
transaction: OwnedTransactionId,
|
||||
sequence_token: String,
|
||||
data: String,
|
||||
) -> Result<update_rendezvous_session::msc4388::Response> {
|
||||
let mut session = self.lock_session(id)?;
|
||||
|
||||
if data.len() > Self::MAX_DATA_SIZE_BYTES {
|
||||
return Err!(Request(TooLarge("Data too large")));
|
||||
}
|
||||
|
||||
// If the provided transaction ID was used already, we have to return
|
||||
// the sequence token that we returned last time it was used.
|
||||
// This makes session updates with the same transaction ID idempotent.
|
||||
if let Some(sequence_token) = session.transaction_sequences.get_mut(&transaction) {
|
||||
return Ok(update_rendezvous_session::msc4388::Response::new(
|
||||
sequence_token.to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
if session.last_sequence_token != sequence_token {
|
||||
return Err!(Request(ConcurrentWrite("Sequence token mismatch")));
|
||||
}
|
||||
|
||||
drop(sequence_token);
|
||||
|
||||
let new_sequence_token = Self::create_token();
|
||||
|
||||
session.data = data;
|
||||
session.last_sequence_token.clone_from(&new_sequence_token);
|
||||
session
|
||||
.transaction_sequences
|
||||
.insert(transaction, new_sequence_token.clone());
|
||||
|
||||
Ok(update_rendezvous_session::msc4388::Response::new(new_sequence_token))
|
||||
}
|
||||
|
||||
/// Look up a rendezvous session by ID.
|
||||
pub fn get_session(&self, id: &str) -> Result<get_rendezvous_session::msc4388::Response> {
|
||||
let session = self.lock_session(id)?;
|
||||
|
||||
let session_age = SystemTime::now()
|
||||
.duration_since(session.created_at)
|
||||
.unwrap_or(Duration::MAX);
|
||||
|
||||
let session_time_remaining = Self::SESSION_MAX_AGE.saturating_sub(session_age);
|
||||
|
||||
Ok(get_rendezvous_session::msc4388::Response::new(
|
||||
session.last_sequence_token.clone(),
|
||||
session.data.clone(),
|
||||
session_time_remaining,
|
||||
))
|
||||
}
|
||||
|
||||
/// Delete a rendezvous session.
|
||||
pub fn delete_session(
|
||||
&self,
|
||||
id: &str,
|
||||
) -> Result<delete_rendezvous_session::msc4388::Response> {
|
||||
let mut sessions = self.sessions.lock();
|
||||
|
||||
if sessions.remove(id).is_some() {
|
||||
Ok(delete_rendezvous_session::msc4388::Response::new())
|
||||
} else {
|
||||
Err!(Request(NotFound("Unknown session")))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ use crate::{
|
||||
account_data, admin, announcements, antispam, appservice, client, config, emergency,
|
||||
federation, firstrun, globals, key_backups, mailer,
|
||||
manager::Manager,
|
||||
media, moderation, oauth, oidc, presence, pusher, registration_tokens, rooms, sending,
|
||||
server_keys,
|
||||
media, moderation, oauth, oidc, presence, pusher, registration_tokens, rendezvous, rooms,
|
||||
sending, server_keys,
|
||||
service::{self, Args, Map, Service},
|
||||
sync, threepid, transactions, uiaa, users,
|
||||
};
|
||||
@@ -33,6 +33,7 @@ pub struct Services {
|
||||
pub presence: Arc<presence::Service>,
|
||||
pub pusher: Arc<pusher::Service>,
|
||||
pub registration_tokens: Arc<registration_tokens::Service>,
|
||||
pub rendezvous: Arc<rendezvous::Service>,
|
||||
pub rooms: rooms::Service,
|
||||
pub federation: Arc<federation::Service>,
|
||||
pub firstrun: Arc<firstrun::Service>,
|
||||
@@ -89,6 +90,7 @@ impl Services {
|
||||
presence: build!(presence::Service),
|
||||
pusher: build!(pusher::Service),
|
||||
registration_tokens: build!(registration_tokens::Service),
|
||||
rendezvous: build!(rendezvous::Service),
|
||||
rooms: rooms::Service {
|
||||
alias: build!(rooms::alias::Service),
|
||||
auth_chain: build!(rooms::auth_chain::Service),
|
||||
|
||||
Reference in new issue
Block a user