From c60402bf0d86e851c2d0768ff32ac1fda6361075 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 8 Apr 2020 23:13:30 +0200 Subject: [PATCH 1/2] Update ruma-client-api --- Cargo.lock | 2 +- src/main.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd519ba..12c6626 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1052,7 +1052,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.7.2" -source = "git+https://github.com/ruma/ruma-client-api.git#dc582758e4f846b3751d84d21eb321e8eb4faf51" +source = "git+https://github.com/ruma/ruma-client-api.git#aeb4e237b7f13a068a92929fdb5c5adac4f346e1" dependencies = [ "http", "js_int", diff --git a/src/main.rs b/src/main.rs index a2cfc10..2102c1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,10 +118,9 @@ fn register_route( data.token_replace(&user_id, &device_id, token.clone()); MatrixResult(Ok(register::Response { - access_token: token, - home_server: data.hostname().to_owned(), + access_token: Some(token), user_id, - device_id, + device_id: Some(device_id), })) } From 38ab7c843eb50bf9d8878d51a4fa8f091c2b5d32 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 8 Apr 2020 23:25:19 +0200 Subject: [PATCH 2/2] Update error type of /register route --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 55 ++++++++++++++++++++++++--------------------- src/ruma_wrapper.rs | 13 +++++++---- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12c6626..f39a719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1052,7 +1052,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.7.2" -source = "git+https://github.com/ruma/ruma-client-api.git#aeb4e237b7f13a068a92929fdb5c5adac4f346e1" +source = "git+https://github.com/ruma/ruma-client-api.git?branch=uiaa-error-type#a7136c06285864dadcc0b0c6371d181002727c55" dependencies = [ "http", "js_int", diff --git a/Cargo.toml b/Cargo.toml index 7fda8ee..2f9d5ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } http = "0.2.1" -ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } +ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git", branch = "uiaa-error-type" } pretty_env_logger = "0.4.0" log = "0.4.8" sled = "0.31.0" diff --git a/src/main.rs b/src/main.rs index 2102c1d..09c8aac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,10 @@ use rocket::{get, options, post, put, routes, State}; use ruma_client_api::{ error::{Error, ErrorKind}, r0::{ - account::register, + account::{ + register, AuthenticationFlow, UserInteractiveAuthenticationInfo, + UserInteractiveAuthenticationResponse, + }, alias::get_alias, filter::{self, create_filter, get_filter}, keys::get_keys, @@ -52,23 +55,19 @@ fn get_supported_versions_route() -> MatrixResult, body: Ruma, -) -> MatrixResult { - /* +) -> MatrixResult { if body.auth.is_none() { - return MatrixResult(Err(Error { - kind: ErrorKind::Unknown, - message: json!({ - "flows": [ - { "stages": [ "m.login.dummy" ] }, - ], - "params": {}, - "session": utils::random_string(SESSION_ID_LENGTH), - }) - .to_string(), - status_code: http::StatusCode::UNAUTHORIZED, - })); + return MatrixResult(Err(UserInteractiveAuthenticationResponse::AuthResponse( + UserInteractiveAuthenticationInfo { + flows: vec![AuthenticationFlow { + stages: vec!["m.login.dummy".to_owned()], + }], + completed: vec![], + params: json!({}), + session: Some(utils::random_string(SESSION_ID_LENGTH)), + }, + ))); } - */ // Validate user id let user_id: UserId = match (*format!( @@ -82,11 +81,13 @@ fn register_route( { Err(_) => { debug!("Username invalid"); - return MatrixResult(Err(Error { - kind: ErrorKind::InvalidUsername, - message: "Username was invalid.".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - })); + return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError( + Error { + kind: ErrorKind::InvalidUsername, + message: "Username was invalid.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + }, + ))); } Ok(user_id) => user_id, }; @@ -94,11 +95,13 @@ fn register_route( // Check if username is creative enough if data.user_exists(&user_id) { debug!("ID already taken"); - return MatrixResult(Err(Error { - kind: ErrorKind::UserInUse, - message: "Desired user ID is already taken.".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - })); + return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError( + Error { + kind: ErrorKind::UserInUse, + message: "Desired user ID is already taken.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + }, + ))); } // Create user diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 0bdcfae..f39ef75 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -9,7 +9,6 @@ use ruma_api::{ error::{FromHttpRequestError, FromHttpResponseError}, Endpoint, Outgoing, }; -use ruma_client_api::error::Error; use ruma_identifiers::UserId; use std::{ convert::{TryFrom, TryInto}, @@ -124,9 +123,13 @@ impl Deref for Ruma { } /// This struct converts ruma responses into rocket http responses. -pub struct MatrixResult(pub std::result::Result); +pub struct MatrixResult(pub std::result::Result); -impl>>> TryInto>> for MatrixResult { +impl TryInto>> for MatrixResult +where + T: TryInto>>, + E: Into>>, +{ type Error = T::Error; fn try_into(self) -> Result>, T::Error> { @@ -138,9 +141,11 @@ impl>>> TryInto>> for M } #[rocket::async_trait] -impl<'r, T: Send + TryInto>>> Responder<'r> for MatrixResult +impl<'r, T, E> Responder<'r> for MatrixResult where + T: Send + TryInto>>, T::Error: Send, + E: Into>> + Send, { async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> { let http_response: Result, _> = self.try_into();