2020-08-06 12:29:59 +00:00
|
|
|
use super::{State, DEVICE_ID_LENGTH, TOKEN_LENGTH};
|
2020-07-30 16:14:47 +00:00
|
|
|
use crate::{utils, ConduitResult, Database, Error, Ruma};
|
2020-11-15 11:17:21 +00:00
|
|
|
use log::info;
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::session::{get_login_types, login, logout, logout_all},
|
|
|
|
},
|
|
|
|
UserId,
|
|
|
|
};
|
2021-02-07 16:38:45 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct Claims {
|
|
|
|
sub: String,
|
|
|
|
exp: usize,
|
|
|
|
}
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, post};
|
|
|
|
|
2020-07-31 12:40:28 +00:00
|
|
|
/// # `GET /_matrix/client/r0/login`
|
|
|
|
///
|
|
|
|
/// Get the homeserver's supported login types. One of these should be used as the `type` field
|
|
|
|
/// when logging in.
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/login"))]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_login_types_route() -> ConduitResult<get_login_types::Response> {
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(get_login_types::Response::new(vec![get_login_types::LoginType::Password]).into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 12:40:28 +00:00
|
|
|
/// # `POST /_matrix/client/r0/login`
|
|
|
|
///
|
|
|
|
/// Authenticates the user and returns an access token it can use in subsequent requests.
|
|
|
|
///
|
|
|
|
/// - The returned access token is associated with the user and device
|
|
|
|
/// - Old access tokens of that device should be invalidated
|
|
|
|
/// - If `device_id` is unknown, a new device will be created
|
|
|
|
///
|
|
|
|
/// Note: You can use [`GET /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see
|
|
|
|
/// supported login types.
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/login", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn login_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<login::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<login::Response> {
|
|
|
|
// Validate login method
|
2021-02-07 16:38:45 +00:00
|
|
|
// TODO: Other login methods
|
|
|
|
let user_id = match &body.login_info {
|
2021-03-18 18:38:08 +00:00
|
|
|
login::IncomingLoginInfo::Password {
|
|
|
|
identifier,
|
|
|
|
password,
|
|
|
|
} => {
|
|
|
|
let username = if let login::IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
|
2021-02-07 16:38:45 +00:00
|
|
|
matrix_id
|
|
|
|
} else {
|
|
|
|
return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type."));
|
|
|
|
};
|
|
|
|
let user_id =
|
|
|
|
UserId::parse_with_server_name(username.to_owned(), db.globals.server_name())
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::BadRequest(ErrorKind::InvalidUsername, "Username is invalid.")
|
|
|
|
})?;
|
|
|
|
let hash = db.users.password_hash(&user_id)?.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"Wrong username or password.",
|
|
|
|
))?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
if hash.is_empty() {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::UserDeactivated,
|
2021-02-07 16:38:45 +00:00
|
|
|
"The user has been deactivated",
|
2020-07-30 16:14:47 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:38:45 +00:00
|
|
|
let hash_matches = argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false);
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
if !hash_matches {
|
2021-02-07 16:38:45 +00:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"Wrong username or password.",
|
|
|
|
));
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user_id
|
2021-02-07 16:38:45 +00:00
|
|
|
}
|
|
|
|
login::IncomingLoginInfo::Token { token } => {
|
|
|
|
if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() {
|
|
|
|
let token = jsonwebtoken::decode::<Claims>(
|
|
|
|
&token,
|
|
|
|
&jwt_decoding_key,
|
|
|
|
&jsonwebtoken::Validation::default(),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidUsername, "Token is invalid."))?;
|
|
|
|
let username = token.claims.sub;
|
|
|
|
UserId::parse_with_server_name(username, db.globals.server_name()).map_err(
|
|
|
|
|_| Error::BadRequest(ErrorKind::InvalidUsername, "Username is invalid."),
|
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Unknown,
|
|
|
|
"Token login is not supported (server has no jwt decoding key).",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
|
|
// Generate a new token for the device
|
|
|
|
let token = utils::random_string(TOKEN_LENGTH);
|
|
|
|
|
2021-01-17 15:39:47 +00:00
|
|
|
// Determine if device_id was provided and exists in the db for this user
|
|
|
|
let device_exists = body.device_id.as_ref().map_or(false, |device_id| {
|
|
|
|
db.users
|
|
|
|
.all_device_ids(&user_id)
|
2021-03-04 12:35:12 +00:00
|
|
|
.any(|x| x.as_ref().map_or(false, |v| v == device_id))
|
2021-01-17 15:39:47 +00:00
|
|
|
});
|
2021-01-17 05:15:45 +00:00
|
|
|
|
2021-01-17 15:39:47 +00:00
|
|
|
if device_exists {
|
|
|
|
db.users.set_token(&user_id, &device_id, &token)?;
|
|
|
|
} else {
|
2021-01-17 05:15:45 +00:00
|
|
|
db.users.create_device(
|
|
|
|
&user_id,
|
|
|
|
&device_id,
|
|
|
|
&token,
|
|
|
|
body.initial_device_display_name.clone(),
|
|
|
|
)?;
|
|
|
|
}
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-11-15 11:17:21 +00:00
|
|
|
info!("{} logged in", user_id);
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(login::Response {
|
|
|
|
user_id,
|
|
|
|
access_token: token,
|
|
|
|
home_server: Some(db.globals.server_name().to_owned()),
|
|
|
|
device_id,
|
|
|
|
well_known: None,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:40:28 +00:00
|
|
|
/// # `POST /_matrix/client/r0/logout`
|
|
|
|
///
|
|
|
|
/// Log out the current device.
|
|
|
|
///
|
|
|
|
/// - Invalidates the access token
|
|
|
|
/// - Deletes the device and most of it's data (to-device events, last seen, etc.)
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/logout", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn logout_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<logout::Request>,
|
|
|
|
) -> ConduitResult<logout::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-18 18:33:12 +00:00
|
|
|
db.users.remove_device(&sender_user, sender_device)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(logout::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 12:40:28 +00:00
|
|
|
/// # `POST /_matrix/client/r0/logout/all`
|
|
|
|
///
|
|
|
|
/// Log out all devices of this user.
|
|
|
|
///
|
|
|
|
/// - Invalidates all access tokens
|
|
|
|
/// - Deletes devices and most of their data (to-device events, last seen, etc.)
|
|
|
|
///
|
|
|
|
/// Note: This is equivalent to calling [`GET /_matrix/client/r0/logout`](fn.logout_route.html)
|
|
|
|
/// from each device of this user.
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/logout/all", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn logout_all_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<logout_all::Request>,
|
|
|
|
) -> ConduitResult<logout_all::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-18 18:33:12 +00:00
|
|
|
for device_id in db.users.all_device_ids(sender_user) {
|
2020-07-30 16:14:47 +00:00
|
|
|
if let Ok(device_id) = device_id {
|
2020-10-18 18:33:12 +00:00
|
|
|
db.users.remove_device(&sender_user, &device_id)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(logout_all::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|