Update error type of /register route
This commit is contained in:
		
							parent
							
								
									c60402bf0d
								
							
						
					
					
						commit
						38ab7c843e
					
				
					 4 changed files with 40 additions and 32 deletions
				
			
		
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							|  | @ -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", | ||||
|  |  | |||
|  | @ -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" | ||||
|  |  | |||
							
								
								
									
										43
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								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<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 { | ||||
|             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 { | ||||
|         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
 | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue