prepare error responses
This commit is contained in:
parent
b045bfb321
commit
8e80f02cbc
@ -23,6 +23,8 @@ use crate::{
|
|||||||
Config,
|
Config,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::errors::{api_error::ApiError, registration_error::RegistrationError};
|
||||||
|
|
||||||
pub fn routes() -> axum::Router {
|
pub fn routes() -> axum::Router {
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.route("/r0/login", get(get_login).post(post_login))
|
.route("/r0/login", get(get_login).post(post_login))
|
||||||
@ -46,11 +48,14 @@ async fn get_username_available(
|
|||||||
Extension(config): Extension<Arc<Config>>,
|
Extension(config): Extension<Arc<Config>>,
|
||||||
Extension(db): Extension<SqlitePool>,
|
Extension(db): Extension<SqlitePool>,
|
||||||
Query(params): Query<HashMap<String, String>>,
|
Query(params): Query<HashMap<String, String>>,
|
||||||
) -> Json<UsernameAvailable> {
|
) -> Result<Json<UsernameAvailable>, ApiError> {
|
||||||
let username = params.get("username").unwrap();
|
let username = params
|
||||||
let user_id = UserId::new(&username, &config.homeserver_name).unwrap();
|
.get("username")
|
||||||
let exists = User::exists(&db, &user_id).await.unwrap();
|
.ok_or(RegistrationError::MissingUserId)?;
|
||||||
Json(UsernameAvailable::new(!exists))
|
let user_id = UserId::new(username, &config.homeserver_name)?;
|
||||||
|
let exists = User::exists(&db, &user_id).await?;
|
||||||
|
|
||||||
|
Ok(Json(UsernameAvailable::new(!exists)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
@ -59,29 +64,34 @@ async fn post_register(
|
|||||||
Extension(db): Extension<SqlitePool>,
|
Extension(db): Extension<SqlitePool>,
|
||||||
Json(body): Json<RegistrationRequest>,
|
Json(body): Json<RegistrationRequest>,
|
||||||
Query(params): Query<HashMap<String, String>>,
|
Query(params): Query<HashMap<String, String>>,
|
||||||
) -> (StatusCode, Json<RegistrationResponse>) {
|
) -> Result<(StatusCode, Json<RegistrationResponse>), ApiError> {
|
||||||
// Client tries to get available flows
|
// Client tries to get available flows
|
||||||
if *&body.auth().is_none() {
|
if body.auth().is_none() {
|
||||||
return (
|
return Ok((
|
||||||
StatusCode::UNAUTHORIZED,
|
StatusCode::UNAUTHORIZED,
|
||||||
Json(RegistrationResponse::user_interactive_authorization_info()),
|
Json(RegistrationResponse::user_interactive_authorization_info()),
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (user, device) = match &body.auth().unwrap() {
|
let (user, device) = match &body.auth().unwrap() {
|
||||||
AuthenticationData::Password(auth_data) => {
|
AuthenticationData::Password(auth_data) => {
|
||||||
let username = body.username().unwrap();
|
let username = body.username().ok_or(RegistrationError::MissingUserId)?;
|
||||||
let user_id = UserId::new(&username, &config.homeserver_name).unwrap();
|
let user_id = UserId::new(username, &config.homeserver_name)
|
||||||
|
.ok()
|
||||||
|
.ok_or(RegistrationError::InvalidUserId)?;
|
||||||
|
|
||||||
if User::exists(&db, &user_id).await.unwrap() {
|
if User::exists(&db, &user_id).await.unwrap() {
|
||||||
todo!("Error out")
|
todo!("Error out")
|
||||||
}
|
}
|
||||||
|
|
||||||
let password = auth_data.password();
|
let password = auth_data.password();
|
||||||
let display_name = match *&body.initial_device_display_name() {
|
|
||||||
|
let display_name = match body.initial_device_display_name() {
|
||||||
Some(display_name) => display_name.as_ref(),
|
Some(display_name) => display_name.as_ref(),
|
||||||
None => "Random displayname",
|
None => "Random displayname",
|
||||||
};
|
};
|
||||||
|
|
||||||
let user = User::create(&db, &user_id, &user_id.to_string(), &password)
|
let user = User::create(&db, &user_id, &user_id.to_string(), password)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let device = Device::create(&db, &user, "test", display_name)
|
let device = Device::create(&db, &user, "test", display_name)
|
||||||
@ -92,15 +102,15 @@ async fn post_register(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if *&body.inhibit_login().is_some() && *&body.inhibit_login().unwrap() {
|
if body.inhibit_login().unwrap_or(false) {
|
||||||
let resp = RegistrationSuccess::new(None, device.device_id(), user.user_id());
|
let resp = RegistrationSuccess::new(None, device.device_id(), user.user_id());
|
||||||
|
|
||||||
(StatusCode::OK, Json(RegistrationResponse::Success(resp)))
|
Ok((StatusCode::OK, Json(RegistrationResponse::Success(resp))))
|
||||||
} else {
|
} else {
|
||||||
let session = device.create_session(&db).await.unwrap();
|
let session = device.create_session(&db).await.unwrap();
|
||||||
let resp =
|
let resp =
|
||||||
RegistrationSuccess::new(Some(session.value()), device.device_id(), user.user_id());
|
RegistrationSuccess::new(Some(session.value()), device.device_id(), user.user_id());
|
||||||
|
|
||||||
(StatusCode::OK, Json(RegistrationResponse::Success(resp)))
|
Ok((StatusCode::OK, Json(RegistrationResponse::Success(resp))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod versions;
|
pub mod versions;
|
||||||
|
pub mod errors;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user