From 1ebdd0b731b45254e458d85a17ebeda7b4c21492 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 12 May 2022 16:54:55 +0200 Subject: [PATCH] WIP: Handle /login --- crates/handlers/src/compat/mod.rs | 83 +++++++++++++++++++++++++++++++ crates/handlers/src/lib.rs | 5 ++ crates/router/src/endpoints.rs | 7 +++ 3 files changed, 95 insertions(+) create mode 100644 crates/handlers/src/compat/mod.rs diff --git a/crates/handlers/src/compat/mod.rs b/crates/handlers/src/compat/mod.rs new file mode 100644 index 00000000..2e8b8460 --- /dev/null +++ b/crates/handlers/src/compat/mod.rs @@ -0,0 +1,83 @@ +// Copyright 2022 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use axum::{response::IntoResponse, Json}; +use hyper::StatusCode; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize)] +struct MatrixError { + errcode: &'static str, + error: &'static str, + #[serde(skip)] + status: StatusCode, +} + +impl IntoResponse for MatrixError { + fn into_response(self) -> axum::response::Response { + (self.status, Json(self)).into_response() + } +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] +enum LoginType { + #[serde(rename = "m.login.password")] + Password, +} + +#[derive(Debug, Serialize, Deserialize)] +struct LoginTypes { + flows: Vec, +} + +pub(crate) async fn get() -> impl IntoResponse { + let res = LoginTypes { + flows: vec![LoginType::Password], + }; + + Json(res) +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum IncomingLogin { + #[serde(rename = "m.login.password")] + Password { + identifier: LoginIdentifier, + password: String, + }, + + #[serde(other)] + Unsupported, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(tag = "type")] +pub enum LoginIdentifier { + #[serde(rename = "m.id.user")] + User { user: String }, + + #[serde(other)] + Unsupported, +} + +pub(crate) async fn post(Json(input): Json) -> impl IntoResponse { + tracing::info!(?input, "Got Matrix login"); + MatrixError { + errcode: "M_UNKNOWN", + error: "Not implemented", + status: StatusCode::NOT_IMPLEMENTED, + } +} diff --git a/crates/handlers/src/lib.rs b/crates/handlers/src/lib.rs index 5be5ad1b..2974c5c9 100644 --- a/crates/handlers/src/lib.rs +++ b/crates/handlers/src/lib.rs @@ -39,6 +39,7 @@ use sqlx::PgPool; use tower::util::ThenLayer; use tower_http::cors::{Any, CorsLayer}; +mod compat; mod health; mod oauth2; mod views; @@ -95,6 +96,10 @@ where mas_router::OAuth2RegistrationEndpoint::route(), post(self::oauth2::registration::post), ) + .route( + mas_router::CompatLogin::route(), + get(self::compat::get).post(self::compat::post), + ) .layer( CorsLayer::new() .allow_origin(Any) diff --git a/crates/router/src/endpoints.rs b/crates/router/src/endpoints.rs index d5f1bcf5..07574eb7 100644 --- a/crates/router/src/endpoints.rs +++ b/crates/router/src/endpoints.rs @@ -366,3 +366,10 @@ impl Route for Consent { format!("/consent/{}", self.0).into() } } + +/// `GET|POST /_matrix/client/v3/login` +pub struct CompatLogin; + +impl SimpleRoute for CompatLogin { + const PATH: &'static str = "/_matrix/client/:version/login"; +}