Merge pull request 'UIAA error type' (#3) from jplatte/matrixserver:master into master

This commit is contained in:
timo 2020-04-08 23:41:39 +02:00
commit 9d40981cce
4 changed files with 42 additions and 35 deletions

2
Cargo.lock generated
View file

@ -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?branch=uiaa-error-type#a7136c06285864dadcc0b0c6371d181002727c55"
dependencies = [
"http",
"js_int",

View file

@ -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"

View file

@ -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<get_supported_versions::Respon
fn register_route(
data: State<Data>,
body: Ruma<register::Request>,
) -> MatrixResult<register::Response> {
/*
) -> MatrixResult<register::Response, UserInteractiveAuthenticationResponse> {
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
@ -118,10 +121,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),
}))
}

View file

@ -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<T: Outgoing> Deref for Ruma<T> {
}
/// This struct converts ruma responses into rocket http responses.
pub struct MatrixResult<T>(pub std::result::Result<T, Error>);
pub struct MatrixResult<T, E = ruma_client_api::Error>(pub std::result::Result<T, E>);
impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for MatrixResult<T> {
impl<T, E> TryInto<http::Response<Vec<u8>>> for MatrixResult<T, E>
where
T: TryInto<http::Response<Vec<u8>>>,
E: Into<http::Response<Vec<u8>>>,
{
type Error = T::Error;
fn try_into(self) -> Result<http::Response<Vec<u8>>, T::Error> {
@ -138,9 +141,11 @@ impl<T: TryInto<http::Response<Vec<u8>>>> TryInto<http::Response<Vec<u8>>> for M
}
#[rocket::async_trait]
impl<'r, T: Send + TryInto<http::Response<Vec<u8>>>> Responder<'r> for MatrixResult<T>
impl<'r, T, E> Responder<'r> for MatrixResult<T, E>
where
T: Send + TryInto<http::Response<Vec<u8>>>,
T::Error: Send,
E: Into<http::Response<Vec<u8>>> + Send,
{
async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
let http_response: Result<http::Response<_>, _> = self.try_into();