Update error type of /register route

This commit is contained in:
Jonas Platte 2020-04-08 23:25:19 +02:00
parent c60402bf0d
commit 38ab7c843e
No known key found for this signature in database
GPG key ID: 7D261D771D915378
4 changed files with 40 additions and 32 deletions

2
Cargo.lock generated
View file

@ -1052,7 +1052,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.7.2" 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 = [ dependencies = [
"http", "http",
"js_int", "js_int",

View file

@ -9,7 +9,7 @@ edition = "2018"
[dependencies] [dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] }
http = "0.2.1" 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" pretty_env_logger = "0.4.0"
log = "0.4.8" log = "0.4.8"
sled = "0.31.0" sled = "0.31.0"

View file

@ -14,7 +14,10 @@ use rocket::{get, options, post, put, routes, State};
use ruma_client_api::{ use ruma_client_api::{
error::{Error, ErrorKind}, error::{Error, ErrorKind},
r0::{ r0::{
account::register, account::{
register, AuthenticationFlow, UserInteractiveAuthenticationInfo,
UserInteractiveAuthenticationResponse,
},
alias::get_alias, alias::get_alias,
filter::{self, create_filter, get_filter}, filter::{self, create_filter, get_filter},
keys::get_keys, keys::get_keys,
@ -52,23 +55,19 @@ fn get_supported_versions_route() -> MatrixResult<get_supported_versions::Respon
fn register_route( fn register_route(
data: State<Data>, data: State<Data>,
body: Ruma<register::Request>, body: Ruma<register::Request>,
) -> MatrixResult<register::Response> { ) -> MatrixResult<register::Response, UserInteractiveAuthenticationResponse> {
/*
if body.auth.is_none() { if body.auth.is_none() {
return MatrixResult(Err(Error { return MatrixResult(Err(UserInteractiveAuthenticationResponse::AuthResponse(
kind: ErrorKind::Unknown, UserInteractiveAuthenticationInfo {
message: json!({ flows: vec![AuthenticationFlow {
"flows": [ stages: vec!["m.login.dummy".to_owned()],
{ "stages": [ "m.login.dummy" ] }, }],
], completed: vec![],
"params": {}, params: json!({}),
"session": utils::random_string(SESSION_ID_LENGTH), session: Some(utils::random_string(SESSION_ID_LENGTH)),
}) },
.to_string(), )));
status_code: http::StatusCode::UNAUTHORIZED,
}));
} }
*/
// Validate user id // Validate user id
let user_id: UserId = match (*format!( let user_id: UserId = match (*format!(
@ -82,11 +81,13 @@ fn register_route(
{ {
Err(_) => { Err(_) => {
debug!("Username invalid"); debug!("Username invalid");
return MatrixResult(Err(Error { return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError(
Error {
kind: ErrorKind::InvalidUsername, kind: ErrorKind::InvalidUsername,
message: "Username was invalid.".to_owned(), message: "Username was invalid.".to_owned(),
status_code: http::StatusCode::BAD_REQUEST, status_code: http::StatusCode::BAD_REQUEST,
})); },
)));
} }
Ok(user_id) => user_id, Ok(user_id) => user_id,
}; };
@ -94,11 +95,13 @@ fn register_route(
// Check if username is creative enough // Check if username is creative enough
if data.user_exists(&user_id) { if data.user_exists(&user_id) {
debug!("ID already taken"); debug!("ID already taken");
return MatrixResult(Err(Error { return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError(
Error {
kind: ErrorKind::UserInUse, kind: ErrorKind::UserInUse,
message: "Desired user ID is already taken.".to_owned(), message: "Desired user ID is already taken.".to_owned(),
status_code: http::StatusCode::BAD_REQUEST, status_code: http::StatusCode::BAD_REQUEST,
})); },
)));
} }
// Create user // Create user

View file

@ -9,7 +9,6 @@ use ruma_api::{
error::{FromHttpRequestError, FromHttpResponseError}, error::{FromHttpRequestError, FromHttpResponseError},
Endpoint, Outgoing, Endpoint, Outgoing,
}; };
use ruma_client_api::error::Error;
use ruma_identifiers::UserId; use ruma_identifiers::UserId;
use std::{ use std::{
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
@ -124,9 +123,13 @@ impl<T: Outgoing> Deref for Ruma<T> {
} }
/// This struct converts ruma responses into rocket http responses. /// 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; type Error = T::Error;
fn try_into(self) -> Result<http::Response<Vec<u8>>, 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] #[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 where
T: Send + TryInto<http::Response<Vec<u8>>>,
T::Error: Send, T::Error: Send,
E: Into<http::Response<Vec<u8>>> + Send,
{ {
async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> { async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
let http_response: Result<http::Response<_>, _> = self.try_into(); let http_response: Result<http::Response<_>, _> = self.try_into();