From 277d7111c8ac0c4ca73235cd28aedf3396d0d8be Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Sun, 3 Jul 2022 00:08:29 +0200 Subject: [PATCH] implement more routes with default empty responses --- src/api/client_server/mod.rs | 2 +- src/api/client_server/r0/auth.rs | 10 ++--- src/api/client_server/r0/create_room.rs | 17 --------- src/api/client_server/r0/mod.rs | 4 +- src/api/client_server/r0/thirdparty.rs | 1 - src/api/client_server/versions.rs | 4 +- src/main.rs | 11 ++++-- src/types/authentication_data.rs | 50 ------------------------- src/types/identifier.rs | 22 ----------- src/types/identifier_type.rs | 44 ---------------------- src/types/mod.rs | 3 -- 11 files changed, 18 insertions(+), 150 deletions(-) delete mode 100644 src/api/client_server/r0/create_room.rs delete mode 100644 src/types/authentication_data.rs delete mode 100644 src/types/identifier.rs delete mode 100644 src/types/identifier_type.rs diff --git a/src/api/client_server/mod.rs b/src/api/client_server/mod.rs index 9d09faf..b693976 100644 --- a/src/api/client_server/mod.rs +++ b/src/api/client_server/mod.rs @@ -1,3 +1,3 @@ pub mod errors; pub mod r0; -pub mod versions; +pub mod versions; \ No newline at end of file diff --git a/src/api/client_server/r0/auth.rs b/src/api/client_server/r0/auth.rs index b74bdc6..96d9921 100644 --- a/src/api/client_server/r0/auth.rs +++ b/src/api/client_server/r0/auth.rs @@ -25,13 +25,14 @@ use ruma::api::client::{ pub fn routes() -> axum::Router { axum::Router::new() - .route("/r0/login", get(get_login).post(post_login)) + .route("/r0/login", get(get_login_types).post(login)) .route("/r0/register", post(post_register)) .route("/r0/register/available", get(get_username_available)) } #[tracing::instrument] -async fn get_login() -> Result, ApiError> { +async fn get_login_types() -> Result, ApiError> +{ use session::get_login_types::v3::*; Ok(RumaResponse(session::get_login_types::v3::Response::new( @@ -40,7 +41,7 @@ async fn get_login() -> Result>, Extension(db): Extension, RumaRequest(req): RumaRequest, @@ -118,7 +119,6 @@ async fn post_register( RumaRequest(req): RumaRequest, ) -> Result, ApiError> { use account::register::v3::*; - tracing::debug!("Register hit"); config .enable_registration() @@ -179,7 +179,7 @@ async fn post_register( let password = req .password .ok_or("password missing") - .map_err(|e| anyhow::anyhow!(e))?; + .map_err(|_| RegistrationError::AdditionalAuthenticationInformation)?; let user_id = if let Some(username) = req.username { let user_id = UserId::new(&username, config.server_name())?; diff --git a/src/api/client_server/r0/create_room.rs b/src/api/client_server/r0/create_room.rs deleted file mode 100644 index d580997..0000000 --- a/src/api/client_server/r0/create_room.rs +++ /dev/null @@ -1,17 +0,0 @@ -use axum::routing::post; - -use crate::api::client_server::errors::api_error::ApiError; -use crate::ruma_wrapper::RumaRequest; - -use ruma::api::client::room; - -pub fn routes() -> axum::Router { - axum::Router::new() - .route("/r0/createRoom", post(post_create_room)) - .layer(axum::middleware::from_fn(super::authentication_middleware)) -} -async fn post_create_room( - RumaRequest(_req): RumaRequest, -) -> Result { - Ok("".into()) -} diff --git a/src/api/client_server/r0/mod.rs b/src/api/client_server/r0/mod.rs index ee52723..973fd09 100644 --- a/src/api/client_server/r0/mod.rs +++ b/src/api/client_server/r0/mod.rs @@ -13,7 +13,9 @@ use crate::{models::sessions::Session, types::error_code::ErrorCode}; use super::errors::ErrorResponse; pub mod auth; -pub mod create_room; +pub mod room; +pub mod presence; +pub mod push; pub mod thirdparty; async fn authentication_middleware(mut req: Request, next: Next) -> impl IntoResponse { diff --git a/src/api/client_server/r0/thirdparty.rs b/src/api/client_server/r0/thirdparty.rs index 2cdbe4c..a1c62bf 100644 --- a/src/api/client_server/r0/thirdparty.rs +++ b/src/api/client_server/r0/thirdparty.rs @@ -19,7 +19,6 @@ pub fn routes() -> axum::Router { #[tracing::instrument(skip_all)] async fn get_thirdparty_protocols( Extension(_user): Extension>, - RumaRequest(_req): RumaRequest, ) -> Result, ApiError> { Ok(RumaResponse(thirdparty::get_protocols::v3::Response::new( BTreeMap::new(), diff --git a/src/api/client_server/versions.rs b/src/api/client_server/versions.rs index 7cc6822..e70104e 100644 --- a/src/api/client_server/versions.rs +++ b/src/api/client_server/versions.rs @@ -1,12 +1,12 @@ use axum::routing::get; use crate::ruma_wrapper::RumaResponse; +use ruma::api::client::discovery; pub fn routes() -> axum::Router { axum::Router::new().route("/versions", get(get_client_versions)) } -use ruma::api::client::discovery; #[tracing::instrument] async fn get_client_versions() -> RumaResponse { @@ -18,4 +18,4 @@ async fn get_client_versions() -> RumaResponse anyhow::Result<()> { if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "debug"); + std::env::set_var("RUST_LOG", "debug,hyper=info"); } tracing_subscriber::fmt::init(); @@ -28,6 +28,7 @@ async fn main() -> anyhow::Result<()> { let pool = sqlx::SqlitePool::connect(config.db_path()).await?; + // TODO: set correct CORS headers let cors = CorsLayer::new() .allow_origin(tower_http::cors::Any) .allow_methods(tower_http::cors::Any) @@ -39,15 +40,17 @@ async fn main() -> anyhow::Result<()> { .merge(api::client_server::versions::routes()) .merge(api::client_server::r0::auth::routes()) .merge(api::client_server::r0::thirdparty::routes()) - .merge(api::client_server::r0::create_room::routes()); + .merge(api::client_server::r0::room::routes()) + .merge(api::client_server::r0::presence::routes()) + .merge(api::client_server::r0::push::routes()); let router = Router::new() .nest("/_matrix/client", client_server) .layer(cors) .layer(tracing_layer) + .fallback(fallback.into_service()) .layer(Extension(pool)) - .layer(Extension(config)) - .fallback(fallback.into_service()); + .layer(Extension(config)); let _ = axum::Server::bind(&"127.0.0.1:3000".parse().unwrap()) .serve(router.into_make_service()) diff --git a/src/types/authentication_data.rs b/src/types/authentication_data.rs deleted file mode 100644 index 3d27032..0000000 --- a/src/types/authentication_data.rs +++ /dev/null @@ -1,50 +0,0 @@ -use super::{flow::Flow, identifier::Identifier}; - -#[derive(Debug, Clone, serde::Deserialize)] -#[serde(untagged)] -pub enum AuthenticationData { - Password(AuthenticationPassword), -} - -#[derive(Debug, Clone, serde::Deserialize)] -pub struct AuthenticationPassword { - #[serde(rename = "type")] - _type: Flow, - identifier: Identifier, - password: String, - user: Option, - device_id: Option, - initial_device_display_name: Option, -} - -impl AuthenticationPassword { - /// Get a reference to the authentication password's identifier. - #[must_use] - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - /// Get a reference to the authentication password's password. - #[must_use] - pub fn password(&self) -> &str { - self.password.as_ref() - } - - /// Get a reference to the authentication password's user. - #[must_use] - pub fn user(&self) -> Option<&String> { - self.user.as_ref() - } - - /// Get a reference to the authentication password's device id. - #[must_use] - pub fn device_id(&self) -> Option<&String> { - self.device_id.as_ref() - } - - /// Get a mutable reference to the authentication password's initial device display name. - #[must_use] - pub fn initial_device_display_name(&self) -> Option<&String> { - self.initial_device_display_name.as_ref() - } -} diff --git a/src/types/identifier.rs b/src/types/identifier.rs deleted file mode 100644 index f278b11..0000000 --- a/src/types/identifier.rs +++ /dev/null @@ -1,22 +0,0 @@ -use super::identifier_type::IdentifierType; - -#[derive(Debug, Clone, serde::Deserialize)] -#[serde(untagged)] -pub enum Identifier { - User(IdentifierUser), -} - -#[derive(Debug, Clone, serde::Deserialize)] -pub struct IdentifierUser { - #[serde(rename = "type")] - _type: IdentifierType, - user: Option, -} - -impl IdentifierUser { - /// Get a reference to the identifier user's user. - #[must_use] - pub fn user(&self) -> Option<&String> { - self.user.as_ref() - } -} diff --git a/src/types/identifier_type.rs b/src/types/identifier_type.rs deleted file mode 100644 index 20d79e2..0000000 --- a/src/types/identifier_type.rs +++ /dev/null @@ -1,44 +0,0 @@ -#[derive(Debug, Clone)] -pub enum IdentifierType { - User, -} - -impl serde::Serialize for IdentifierType { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(match self { - IdentifierType::User => "m.id.user", - }) - } -} - -impl<'de> serde::Deserialize<'de> for IdentifierType { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct IdentifierVisitor; - - impl<'de> serde::de::Visitor<'de> for IdentifierVisitor { - type Value = IdentifierType; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("Identifier") - } - - fn visit_borrowed_str(self, v: &'de str) -> Result - where - E: serde::de::Error, - { - match v { - "m.id.user" => Ok(IdentifierType::User), - _ => Err(serde::de::Error::custom("Unknown identifier")), - } - } - } - - deserializer.deserialize_str(IdentifierVisitor {}) - } -} diff --git a/src/types/mod.rs b/src/types/mod.rs index 7e097a7..39759fa 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,9 +1,6 @@ -pub mod authentication_data; pub mod error_code; pub mod event_type; pub mod flow; -pub mod identifier; -pub mod identifier_type; pub mod server_name; pub mod user_id; pub mod user_interactive_authorization;