Files
continuwuity/src/api/client/session.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

371 lines
11 KiB
Rust
Raw Normal View History

2025-01-11 18:49:21 +00:00
use std::time::Duration;
2024-07-16 08:05:25 +00:00
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{
Err, Error, Result, debug, err, info, utils,
utils::{ReadyExt, hash},
};
use conduwuit_service::uiaa::SESSION_ID_LENGTH;
use futures::StreamExt;
2020-07-30 18:14:47 +02:00
use ruma::{
UserId,
2020-07-30 18:14:47 +02:00
api::client::{
session::{
2025-01-11 18:49:21 +00:00
get_login_token,
2024-03-02 20:55:02 -05:00
get_login_types::{
self,
2025-01-11 18:49:21 +00:00
v3::{ApplicationServiceLoginType, PasswordLoginType, TokenLoginType},
2024-03-02 20:55:02 -05:00
},
login::{
self,
v3::{DiscoveryInfo, HomeserverInfo},
},
logout, logout_all,
},
2025-01-11 18:49:21 +00:00
uiaa,
2020-07-30 18:14:47 +02:00
},
};
2021-02-07 17:38:45 +01:00
2021-07-14 07:07:08 +00:00
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use crate::Ruma;
2024-03-05 19:48:54 -05:00
/// # `GET /_matrix/client/v3/login`
2020-07-31 14:40:28 +02:00
///
2021-08-31 19:14:37 +02:00
/// Get the supported login types of this server. One of these should be used as
2020-07-31 14:40:28 +02:00
/// the `type` field when logging in.
#[tracing::instrument(skip_all, fields(%client), name = "login")]
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_login_types_route(
2025-01-11 18:49:21 +00:00
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
_body: Ruma<get_login_types::v3::Request>,
2024-04-22 23:48:57 -04:00
) -> Result<get_login_types::v3::Response> {
2022-02-18 15:33:14 +01:00
Ok(get_login_types::v3::Response::new(vec![
2024-03-02 20:55:02 -05:00
get_login_types::v3::LoginType::Password(PasswordLoginType::default()),
get_login_types::v3::LoginType::ApplicationService(ApplicationServiceLoginType::default()),
2025-01-11 18:49:21 +00:00
get_login_types::v3::LoginType::Token(TokenLoginType {
get_login_token: services.server.config.login_via_existing_session,
}),
2022-01-22 16:58:32 +01:00
]))
2020-07-30 18:14:47 +02:00
}
/// # `POST /_matrix/client/v3/login`
2020-07-31 14:40:28 +02:00
///
/// Authenticates the user and returns an access token it can use in subsequent
/// requests.
///
2021-08-31 19:14:37 +02:00
/// - The user needs to authenticate using their password (or if enabled using a
/// json web token)
/// - If `device_id` is known: invalidates old access token of that device
/// - If `device_id` is unknown: creates a new device
/// - Returns access token that is associated with the user and device
2020-07-31 14:40:28 +02:00
///
/// Note: You can use [`GET
/// /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see
/// supported login types.
#[tracing::instrument(skip_all, fields(%client), name = "login")]
2024-07-16 08:05:25 +00:00
pub(crate) async fn login_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<login::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<login::v3::Response> {
let emergency_mode_enabled = services.config.emergency_password.is_some();
2020-07-30 18:14:47 +02:00
// Validate login method
2021-02-07 17:38:45 +01:00
// TODO: Other login methods
let user_id = match &body.login_info {
#[allow(deprecated)]
| login::v3::LoginInfo::Password(login::v3::Password {
2021-03-18 19:38:08 +01:00
identifier,
password,
user,
..
2021-10-13 10:16:45 +02:00
}) => {
debug!("Got password login type");
let user_id =
if let Some(uiaa::UserIdentifier::UserIdOrLocalpart(user_id)) = identifier {
UserId::parse_with_server_name(user_id, &services.config.server_name)
} else if let Some(user) = user {
UserId::parse_with_server_name(user, &services.config.server_name)
} else {
return Err!(Request(Unknown(
debug_warn!(?body.login_info, "Valid identifier or username was not provided (invalid or unsupported login type?)")
)));
}
.map_err(|e| err!(Request(InvalidUsername(warn!("Username is invalid: {e}")))))?;
let lowercased_user_id = UserId::parse_with_server_name(
user_id.localpart().to_lowercase(),
&services.config.server_name,
)?;
if !services.globals.user_is_local(&user_id)
|| !services.globals.user_is_local(&lowercased_user_id)
{
return Err!(Request(Unknown("User ID does not belong to this homeserver")));
}
// first try the username as-is
2024-07-16 08:05:25 +00:00
let hash = services
2022-10-05 20:34:31 +02:00
.users
2024-08-08 17:18:30 +00:00
.password_hash(&user_id)
.await
.inspect_err(|e| debug!("{e}"));
match hash {
| Ok(hash) => {
if hash.is_empty() {
return Err!(Request(UserDeactivated("The user has been deactivated")));
}
hash::verify_password(password, &hash)
.inspect_err(|e| debug!("{e}"))
.map_err(|_| err!(Request(Forbidden("Wrong username or password."))))?;
user_id
},
| Err(_e) => {
let hash_lowercased_user_id = services
.users
.password_hash(&lowercased_user_id)
.await
.inspect_err(|e| debug!("{e}"))
.map_err(|_| err!(Request(Forbidden("Wrong username or password."))))?;
if hash_lowercased_user_id.is_empty() {
return Err!(Request(UserDeactivated("The user has been deactivated")));
}
hash::verify_password(password, &hash_lowercased_user_id)
.inspect_err(|e| debug!("{e}"))
.map_err(|_| err!(Request(Forbidden("Wrong username or password."))))?;
lowercased_user_id
},
2020-07-30 18:14:47 +02:00
}
2021-02-07 17:38:45 +01:00
},
2025-01-11 18:49:21 +00:00
| login::v3::LoginInfo::Token(login::v3::Token { token }) => {
debug!("Got token login type");
2025-01-11 18:49:21 +00:00
if !services.server.config.login_via_existing_session {
return Err!(Request(Unknown("Token login is not enabled.")));
}
services.users.find_from_login_token(token).await?
2021-02-07 17:38:45 +01:00
},
#[allow(deprecated)]
| login::v3::LoginInfo::ApplicationService(login::v3::ApplicationService {
identifier,
user,
}) => {
debug!("Got appservice login type");
let Some(ref info) = body.appservice_info else {
return Err!(Request(MissingToken("Missing appservice token.")));
};
2025-01-11 18:49:21 +00:00
let user_id =
if let Some(uiaa::UserIdentifier::UserIdOrLocalpart(user_id)) = identifier {
UserId::parse_with_server_name(user_id, &services.config.server_name)
2025-01-11 18:49:21 +00:00
} else if let Some(user) = user {
UserId::parse_with_server_name(user, &services.config.server_name)
2025-01-11 18:49:21 +00:00
} else {
return Err!(Request(Unknown(
debug_warn!(?body.login_info, "Valid identifier or username was not provided (invalid or unsupported login type?)")
)));
2025-01-11 18:49:21 +00:00
}
.map_err(|e| err!(Request(InvalidUsername(warn!("Username is invalid: {e}")))))?;
if !services.globals.user_is_local(&user_id) {
return Err!(Request(Unknown("User ID does not belong to this homeserver")));
}
if !info.is_user_match(&user_id) && !emergency_mode_enabled {
return Err!(Request(Exclusive("Username is not in an appservice namespace.")));
}
user_id
},
| _ => {
debug!("/login json_body: {:?}", &body.json_body);
return Err!(Request(Unknown(
debug_warn!(?body.login_info, "Invalid or unsupported login type")
)));
2021-10-13 10:16:45 +02:00
},
2021-02-07 17:38:45 +01:00
};
2020-07-30 18:14:47 +02:00
// Generate new device id if the user didn't specify one
2024-03-25 17:05:11 -04:00
let device_id = body
.device_id
.clone()
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
2020-07-30 18:14:47 +02:00
// Generate a new token for the device
let token = utils::random_string(TOKEN_LENGTH);
2021-01-17 08:39:47 -07:00
// Determine if device_id was provided and exists in the db for this user
2024-08-08 17:18:30 +00:00
let device_exists = if body.device_id.is_some() {
2024-07-16 08:05:25 +00:00
services
2024-03-25 17:05:11 -04:00
.users
.all_device_ids(&user_id)
2024-08-08 17:18:30 +00:00
.ready_any(|v| v == device_id)
.await
} else {
false
};
2021-01-17 08:39:47 -07:00
if device_exists {
2024-08-08 17:18:30 +00:00
services
.users
.set_token(&user_id, &device_id, &token)
.await?;
2021-01-17 08:39:47 -07:00
} else {
2024-08-08 17:18:30 +00:00
services
.users
.create_device(
&user_id,
&device_id,
&token,
body.initial_device_display_name.clone(),
Some(client.to_string()),
)
.await?;
}
2020-07-30 18:14:47 +02:00
// send client well-known if specified so the client knows to reconfigure itself
2024-07-16 08:05:25 +00:00
let client_discovery_info: Option<DiscoveryInfo> = services
2024-11-24 00:19:55 +00:00
.server
.config
.well_known
.client
.as_ref()
.map(|server| DiscoveryInfo::new(HomeserverInfo::new(server.to_string())));
info!("{user_id} logged in");
2020-11-15 12:17:21 +01:00
#[allow(deprecated)]
Ok(login::v3::Response {
user_id,
access_token: token,
device_id,
well_known: client_discovery_info,
expires_in: None,
home_server: Some(services.config.server_name.clone()),
refresh_token: None,
})
2020-07-30 18:14:47 +02:00
}
2025-01-11 18:49:21 +00:00
/// # `POST /_matrix/client/v1/login/get_token`
///
/// Allows a logged-in user to get a short-lived token which can be used
/// to log in with the m.login.token flow.
///
/// <https://spec.matrix.org/v1.13/client-server-api/#post_matrixclientv1loginget_token>
#[tracing::instrument(skip_all, fields(%client), name = "login_token")]
pub(crate) async fn login_token_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_login_token::v1::Request>,
) -> Result<get_login_token::v1::Response> {
if !services.server.config.login_via_existing_session {
return Err!(Request(Forbidden("Login via an existing session is not enabled")));
2025-01-11 18:49:21 +00:00
}
2025-01-11 18:49:21 +00:00
// 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();
2025-01-11 18:49:21 +00:00
let mut uiaainfo = uiaa::UiaaInfo {
flows: vec![uiaa::AuthFlow { stages: vec![uiaa::AuthType::Password] }],
completed: Vec::new(),
params: Box::default(),
session: None,
auth_error: None,
};
match &body.auth {
| Some(auth) => {
let (worked, uiaainfo) = services
.uiaa
.try_auth(sender_user, sender_device, auth, &uiaainfo)
.await?;
2025-01-11 18:49:21 +00:00
if !worked {
return Err(Error::Uiaa(uiaainfo));
}
2025-01-11 18:49:21 +00:00
// Success!
},
| _ => match body.json_body.as_ref() {
| Some(json) => {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services
.uiaa
.create(sender_user, sender_device, &uiaainfo, json);
2025-01-11 18:49:21 +00:00
return Err(Error::Uiaa(uiaainfo));
},
| _ => {
return Err!(Request(NotJson("No JSON body was sent when required.")));
},
},
2025-01-11 18:49:21 +00:00
}
let login_token = utils::random_string(TOKEN_LENGTH);
let expires_in = services.users.create_login_token(sender_user, &login_token);
2025-01-11 18:49:21 +00:00
Ok(get_login_token::v1::Response {
expires_in: Duration::from_millis(expires_in),
login_token,
})
}
/// # `POST /_matrix/client/v3/logout`
2020-07-31 14:40:28 +02:00
///
/// Log out the current device.
///
2021-08-31 19:14:37 +02:00
/// - Invalidates access token
/// - Deletes device metadata (device id, device display name, last seen ip,
/// last seen ts)
/// - Forgets to-device events
/// - Triggers device list updates
#[tracing::instrument(skip_all, fields(%client), name = "logout")]
2024-07-16 08:05:25 +00:00
pub(crate) async fn logout_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<logout::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<logout::v3::Response> {
2024-08-08 17:18:30 +00:00
services
.users
.remove_device(body.sender_user(), body.sender_device())
2024-08-08 17:18:30 +00:00
.await;
2022-02-18 15:33:14 +01:00
Ok(logout::v3::Response::new())
2020-07-30 18:14:47 +02:00
}
2020-07-31 14:40:28 +02:00
/// # `POST /_matrix/client/r0/logout/all`
///
/// Log out all devices of this user.
///
/// - Invalidates all access tokens
2021-08-31 19:14:37 +02:00
/// - Deletes all device metadata (device id, device display name, last seen ip,
/// last seen ts)
/// - Forgets all to-device events
/// - Triggers device list updates
2020-07-31 14:40:28 +02:00
///
/// Note: This is equivalent to calling [`GET
/// /_matrix/client/r0/logout`](fn.logout_route.html) from each device of this
/// user.
#[tracing::instrument(skip_all, fields(%client), name = "logout")]
2024-07-16 08:05:25 +00:00
pub(crate) async fn logout_all_route(
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<logout_all::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<logout_all::v3::Response> {
2024-08-08 17:18:30 +00:00
services
.users
.all_device_ids(body.sender_user())
.for_each(|device_id| services.users.remove_device(body.sender_user(), device_id))
2024-08-08 17:18:30 +00:00
.await;
2020-07-30 18:14:47 +02:00
2022-02-18 15:33:14 +01:00
Ok(logout_all::v3::Response::new())
2020-07-30 18:14:47 +02:00
}