Compare commits
No commits in common. "b8e12353964481de2e820aeb86cc038088f5f518" and "a789a8cc561541cb65d3d50c602105c38beda7c1" have entirely different histories.
b8e1235396
...
a789a8cc56
@ -17,7 +17,9 @@ use crate::{
|
|||||||
models::users::User,
|
models::users::User,
|
||||||
requests::registration::RegistrationRequest,
|
requests::registration::RegistrationRequest,
|
||||||
responses::username_available::UsernameAvailable,
|
responses::username_available::UsernameAvailable,
|
||||||
types::{authentication_data::AuthenticationData, matrix_user_id::UserId},
|
types::{
|
||||||
|
authentication_data::AuthenticationData, identifier::Identifier, matrix_user_id::UserId,
|
||||||
|
},
|
||||||
Config,
|
Config,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -50,9 +52,7 @@ async fn get_username_available(
|
|||||||
let username = params
|
let username = params
|
||||||
.get("username")
|
.get("username")
|
||||||
.ok_or(RegistrationError::MissingUserId)?;
|
.ok_or(RegistrationError::MissingUserId)?;
|
||||||
let user_id = UserId::new(username, &config.homeserver_name)
|
let user_id = UserId::new(username, &config.homeserver_name)?;
|
||||||
.ok()
|
|
||||||
.ok_or(RegistrationError::InvalidUserId)?;
|
|
||||||
let exists = User::exists(&db, &user_id).await?;
|
let exists = User::exists(&db, &user_id).await?;
|
||||||
|
|
||||||
Ok(Json(UsernameAvailable::new(!exists)))
|
Ok(Json(UsernameAvailable::new(!exists)))
|
||||||
@ -63,21 +63,26 @@ async fn post_register(
|
|||||||
Extension(config): Extension<Arc<Config>>,
|
Extension(config): Extension<Arc<Config>>,
|
||||||
Extension(db): Extension<SqlitePool>,
|
Extension(db): Extension<SqlitePool>,
|
||||||
Json(body): Json<RegistrationRequest>,
|
Json(body): Json<RegistrationRequest>,
|
||||||
) -> Result<Json<RegistrationResponse>, ApiError> {
|
Query(params): Query<HashMap<String, String>>,
|
||||||
body.auth()
|
) -> Result<(StatusCode, Json<RegistrationResponse>), ApiError> {
|
||||||
.ok_or(RegistrationError::AdditionalAuthenticationInformation)?;
|
// Client tries to get available flows
|
||||||
|
if body.auth().is_none() {
|
||||||
|
return Ok((
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
Json(RegistrationResponse::user_interactive_authorization_info()),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let (user, device) = match &body.auth().expect("must be Some") {
|
let (user, device) = match &body.auth().unwrap() {
|
||||||
AuthenticationData::Password(auth_data) => {
|
AuthenticationData::Password(auth_data) => {
|
||||||
let username = body.username().ok_or(RegistrationError::MissingUserId)?;
|
let username = body.username().ok_or(RegistrationError::MissingUserId)?;
|
||||||
let user_id = UserId::new(username, &config.homeserver_name)
|
let user_id = UserId::new(username, &config.homeserver_name)
|
||||||
.ok()
|
.ok()
|
||||||
.ok_or(RegistrationError::InvalidUserId)?;
|
.ok_or(RegistrationError::InvalidUserId)?;
|
||||||
|
|
||||||
User::exists(&db, &user_id)
|
if User::exists(&db, &user_id).await.unwrap() {
|
||||||
.await?
|
todo!("Error out")
|
||||||
.then(|| ())
|
}
|
||||||
.ok_or(RegistrationError::UserIdTaken)?;
|
|
||||||
|
|
||||||
let password = auth_data.password();
|
let password = auth_data.password();
|
||||||
|
|
||||||
@ -86,8 +91,12 @@ async fn post_register(
|
|||||||
None => "Random displayname",
|
None => "Random displayname",
|
||||||
};
|
};
|
||||||
|
|
||||||
let user = User::create(&db, &user_id, &user_id.to_string(), password).await?;
|
let user = User::create(&db, &user_id, &user_id.to_string(), password)
|
||||||
let device = Device::create(&db, &user, "test", display_name).await?;
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let device = Device::create(&db, &user, "test", display_name)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
(user, device)
|
(user, device)
|
||||||
}
|
}
|
||||||
@ -96,12 +105,12 @@ async fn post_register(
|
|||||||
if body.inhibit_login().unwrap_or(false) {
|
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());
|
||||||
|
|
||||||
Ok(Json(RegistrationResponse::Success(resp)))
|
Ok((StatusCode::OK, Json(RegistrationResponse::Success(resp))))
|
||||||
} else {
|
} else {
|
||||||
let session = device.create_session(&db).await?;
|
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());
|
||||||
|
|
||||||
Ok(Json(RegistrationResponse::Success(resp)))
|
Ok((StatusCode::OK, Json(RegistrationResponse::Success(resp))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use axum::Json;
|
|
||||||
use sqlx::Statement;
|
|
||||||
|
|
||||||
use crate::responses::registration::RegistrationResponse;
|
|
||||||
use crate::types::error_code::ErrorCode;
|
|
||||||
|
|
||||||
use super::registration_error::RegistrationError;
|
use super::registration_error::RegistrationError;
|
||||||
|
|
||||||
@ -18,24 +13,6 @@ macro_rules! map_err {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
|
||||||
struct ErrorResponse {
|
|
||||||
errcode: ErrorCode,
|
|
||||||
error: String,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
retry_after_ms: Option<u64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ErrorResponse {
|
|
||||||
fn new(errcode: ErrorCode, error: &str, retry_after_ms: Option<u64>) -> Self {
|
|
||||||
Self {
|
|
||||||
errcode,
|
|
||||||
error: error.to_owned(),
|
|
||||||
retry_after_ms,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
#[error("Registration Error")]
|
#[error("Registration Error")]
|
||||||
@ -63,50 +40,14 @@ impl IntoResponse for ApiError {
|
|||||||
fn into_response(self) -> axum::response::Response {
|
fn into_response(self) -> axum::response::Response {
|
||||||
match self {
|
match self {
|
||||||
ApiError::RegistrationError(registration_error) => match registration_error {
|
ApiError::RegistrationError(registration_error) => match registration_error {
|
||||||
RegistrationError::AdditionalAuthenticationInformation => (
|
RegistrationError::InvalidUserId => {
|
||||||
StatusCode::UNAUTHORIZED,
|
(StatusCode::OK, String::new()).into_response()
|
||||||
Json(RegistrationResponse::user_interactive_authorization_info()),
|
|
||||||
).into_response(),
|
|
||||||
RegistrationError::InvalidUserId => (StatusCode::OK, Json(
|
|
||||||
ErrorResponse::new(
|
|
||||||
ErrorCode::InvalidUsername,
|
|
||||||
®istration_error.to_string(),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
)).into_response(),
|
|
||||||
RegistrationError::MissingUserId => (StatusCode::OK, String::new()).into_response(),
|
|
||||||
RegistrationError::UserIdTaken => (
|
|
||||||
StatusCode::BAD_REQUEST,
|
|
||||||
Json(ErrorResponse::new(
|
|
||||||
ErrorCode::UserInUse,
|
|
||||||
®istration_error.to_string(),
|
|
||||||
None,
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
.into_response(),
|
|
||||||
},
|
|
||||||
ApiError::DBError(err) => {
|
|
||||||
tracing::error!("{}", err.to_string());
|
|
||||||
(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json(ErrorResponse::new(
|
|
||||||
ErrorCode::Unknown,
|
|
||||||
"Database error! If you are the application owner please take a look at your application logs.",
|
|
||||||
None,
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
.into_response()
|
|
||||||
}
|
}
|
||||||
ApiError::Generic(err) => (
|
RegistrationError::MissingUserId => {
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
(StatusCode::OK, String::new()).into_response()
|
||||||
Json(ErrorResponse::new(
|
}
|
||||||
ErrorCode::Unknown,
|
},
|
||||||
"Fatal error occured! If you are the application owner please take a look at your application logs.",
|
_ => StatusCode::INTERNAL_SERVER_ERROR.into_response()
|
||||||
None,
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
.into_response(),
|
|
||||||
_ => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum RegistrationError {
|
pub enum RegistrationError {
|
||||||
#[error("The homeserver requires additional authentication information")]
|
|
||||||
AdditionalAuthenticationInformation,
|
|
||||||
#[error("UserId is missing")]
|
#[error("UserId is missing")]
|
||||||
MissingUserId,
|
MissingUserId,
|
||||||
#[error("The desired user ID is not a valid user name")]
|
#[error("UserId is invalid")]
|
||||||
InvalidUserId,
|
InvalidUserId,
|
||||||
#[error("The desired user ID is already taken")]
|
|
||||||
UserIdTaken,
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
#[non_exhaustive]
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum ErrorCode {
|
|
||||||
Forbidden,
|
|
||||||
UnknownToken,
|
|
||||||
MissingToken,
|
|
||||||
BadJson,
|
|
||||||
NotJson,
|
|
||||||
NotFound,
|
|
||||||
LimitExceeded,
|
|
||||||
Unknown,
|
|
||||||
UserInUse,
|
|
||||||
InvalidUsername,
|
|
||||||
Exclusive,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl serde::Serialize for ErrorCode {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: serde::Serializer,
|
|
||||||
{
|
|
||||||
serializer.serialize_str(match self {
|
|
||||||
ErrorCode::Forbidden => "M_FORBIDDEN",
|
|
||||||
ErrorCode::UnknownToken => "M_UNKNOWN_TOKEN",
|
|
||||||
ErrorCode::MissingToken => "M_MISSING_TOKEN",
|
|
||||||
ErrorCode::BadJson => "M_BAD_JSON",
|
|
||||||
ErrorCode::NotJson => "M_NOT_JSON",
|
|
||||||
ErrorCode::NotFound => "M_NOT_FOUND",
|
|
||||||
ErrorCode::LimitExceeded => "M_LIMIT_EXCEEDED",
|
|
||||||
ErrorCode::Unknown => "M_UNKNOWN",
|
|
||||||
ErrorCode::UserInUse => "M_USER_IN_USE",
|
|
||||||
ErrorCode::InvalidUsername => "M_INVALID_USERNAME",
|
|
||||||
ErrorCode::Exclusive => "M_EXCLUSIVE",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,4 +4,3 @@ pub mod identifier;
|
|||||||
pub mod identifier_type;
|
pub mod identifier_type;
|
||||||
pub mod matrix_user_id;
|
pub mod matrix_user_id;
|
||||||
pub mod user_interactive_authorization;
|
pub mod user_interactive_authorization;
|
||||||
pub mod error_code;
|
|
Loading…
x
Reference in New Issue
Block a user