|
|
|
@ -19,7 +19,7 @@ use ruma_client_api::{ |
|
|
|
membership::join_room_by_id, |
|
|
|
membership::join_room_by_id, |
|
|
|
message::create_message_event, |
|
|
|
message::create_message_event, |
|
|
|
room::create_room, |
|
|
|
room::create_room, |
|
|
|
session::login, |
|
|
|
session::{get_login_types, login}, |
|
|
|
state::{create_state_event_for_empty_key, create_state_event_for_key}, |
|
|
|
state::{create_state_event_for_empty_key, create_state_event_for_key}, |
|
|
|
sync::sync_events, |
|
|
|
sync::sync_events, |
|
|
|
}, |
|
|
|
}, |
|
|
|
@ -31,6 +31,11 @@ use ruma_wrapper::{MatrixResult, Ruma}; |
|
|
|
use serde_json::json; |
|
|
|
use serde_json::json; |
|
|
|
use std::{collections::HashMap, convert::TryInto, path::PathBuf}; |
|
|
|
use std::{collections::HashMap, convert::TryInto, path::PathBuf}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const DEVICE_ID_LENGTH: usize = 16; |
|
|
|
|
|
|
|
const SESSION_ID_LENGTH: usize = 16; |
|
|
|
|
|
|
|
const TOKEN_LENGTH: usize = 16; |
|
|
|
|
|
|
|
const GUEST_NAME_LENGTH: usize = 16; |
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/versions")] |
|
|
|
#[get("/_matrix/client/versions")] |
|
|
|
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> { |
|
|
|
fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Response> { |
|
|
|
MatrixResult(Ok(get_supported_versions::Response { |
|
|
|
MatrixResult(Ok(get_supported_versions::Response { |
|
|
|
@ -47,14 +52,14 @@ fn register_route( |
|
|
|
if body.auth.is_none() { |
|
|
|
if body.auth.is_none() { |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
return MatrixResult(Err(Error { |
|
|
|
kind: ErrorKind::InvalidUsername, |
|
|
|
kind: ErrorKind::InvalidUsername, |
|
|
|
message: serde_json::to_string(&json!({ |
|
|
|
message: json!({ |
|
|
|
"flows": [ |
|
|
|
"flows": [ |
|
|
|
{ "stages": [ "m.login.dummy" ] }, |
|
|
|
{ "stages": [ "m.login.dummy" ] }, |
|
|
|
], |
|
|
|
], |
|
|
|
"params": {}, |
|
|
|
"params": {}, |
|
|
|
"session": "TODO:randomsessionid", |
|
|
|
"session": utils::random_string(SESSION_ID_LENGTH), |
|
|
|
})) |
|
|
|
}) |
|
|
|
.unwrap(), |
|
|
|
.to_string(), |
|
|
|
status_code: http::StatusCode::UNAUTHORIZED, |
|
|
|
status_code: http::StatusCode::UNAUTHORIZED, |
|
|
|
})); |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -62,7 +67,9 @@ fn register_route( |
|
|
|
// Validate user id
|
|
|
|
// Validate user id
|
|
|
|
let user_id: UserId = match (*format!( |
|
|
|
let user_id: UserId = match (*format!( |
|
|
|
"@{}:{}", |
|
|
|
"@{}:{}", |
|
|
|
body.username.clone().unwrap_or("randomname".to_owned()), |
|
|
|
body.username |
|
|
|
|
|
|
|
.clone() |
|
|
|
|
|
|
|
.unwrap_or_else(|| utils::random_string(GUEST_NAME_LENGTH)), |
|
|
|
data.hostname() |
|
|
|
data.hostname() |
|
|
|
)) |
|
|
|
)) |
|
|
|
.try_into() |
|
|
|
.try_into() |
|
|
|
@ -95,13 +102,13 @@ fn register_route( |
|
|
|
let device_id = body |
|
|
|
let device_id = body |
|
|
|
.device_id |
|
|
|
.device_id |
|
|
|
.clone() |
|
|
|
.clone() |
|
|
|
.unwrap_or_else(|| "TODO:randomdeviceid".to_owned()); |
|
|
|
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH)); |
|
|
|
|
|
|
|
|
|
|
|
// Add device
|
|
|
|
// Add device
|
|
|
|
data.device_add(&user_id, &device_id); |
|
|
|
data.device_add(&user_id, &device_id); |
|
|
|
|
|
|
|
|
|
|
|
// Generate new token for the device
|
|
|
|
// Generate new token for the device
|
|
|
|
let token = "TODO:randomtoken".to_owned(); |
|
|
|
let token = utils::random_string(TOKEN_LENGTH); |
|
|
|
data.token_replace(&user_id, &device_id, token.clone()); |
|
|
|
data.token_replace(&user_id, &device_id, token.clone()); |
|
|
|
|
|
|
|
|
|
|
|
MatrixResult(Ok(register::Response { |
|
|
|
MatrixResult(Ok(register::Response { |
|
|
|
@ -112,6 +119,13 @@ fn register_route( |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/_matrix/client/r0/login", data = "<_body>")] |
|
|
|
|
|
|
|
fn get_login_route(_body: Ruma<login::Request>) -> MatrixResult<get_login_types::Response> { |
|
|
|
|
|
|
|
MatrixResult(Ok(get_login_types::Response { |
|
|
|
|
|
|
|
flows: vec![get_login_types::LoginType::Password], |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")] |
|
|
|
#[post("/_matrix/client/r0/login", data = "<body>")] |
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> { |
|
|
|
fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> { |
|
|
|
// Validate login method
|
|
|
|
// Validate login method
|
|
|
|
@ -167,22 +181,22 @@ fn login_route(data: State<Data>, body: Ruma<login::Request>) -> MatrixResult<lo |
|
|
|
let device_id = body |
|
|
|
let device_id = body |
|
|
|
.device_id |
|
|
|
.device_id |
|
|
|
.clone() |
|
|
|
.clone() |
|
|
|
.unwrap_or("TODO:randomdeviceid".to_owned()); |
|
|
|
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH)); |
|
|
|
|
|
|
|
|
|
|
|
// Add device
|
|
|
|
// Add device
|
|
|
|
data.device_add(&user_id, &device_id); |
|
|
|
data.device_add(&user_id, &device_id); |
|
|
|
|
|
|
|
|
|
|
|
// Generate a new token for the device
|
|
|
|
// Generate a new token for the device
|
|
|
|
let token = "TODO:randomtoken".to_owned(); |
|
|
|
let token = utils::random_string(TOKEN_LENGTH); |
|
|
|
data.token_replace(&user_id, &device_id, token.clone()); |
|
|
|
data.token_replace(&user_id, &device_id, token.clone()); |
|
|
|
|
|
|
|
|
|
|
|
return MatrixResult(Ok(login::Response { |
|
|
|
MatrixResult(Ok(login::Response { |
|
|
|
user_id, |
|
|
|
user_id, |
|
|
|
access_token: token, |
|
|
|
access_token: token, |
|
|
|
home_server: Some(data.hostname().to_owned()), |
|
|
|
home_server: Some(data.hostname().to_owned()), |
|
|
|
device_id, |
|
|
|
device_id, |
|
|
|
well_known: None, |
|
|
|
well_known: None, |
|
|
|
})); |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[post("/_matrix/client/r0/createRoom", data = "<body>")] |
|
|
|
#[post("/_matrix/client/r0/createRoom", data = "<body>")] |
|
|
|
@ -388,6 +402,7 @@ fn main() { |
|
|
|
routes![ |
|
|
|
routes![ |
|
|
|
get_supported_versions_route, |
|
|
|
get_supported_versions_route, |
|
|
|
register_route, |
|
|
|
register_route, |
|
|
|
|
|
|
|
get_login_route, |
|
|
|
login_route, |
|
|
|
login_route, |
|
|
|
create_room_route, |
|
|
|
create_room_route, |
|
|
|
get_alias_route, |
|
|
|
get_alias_route, |
|
|
|
|