rework DB Models
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-04-30 23:59:21 +02:00
parent 341c516fcb
commit 3b8c529183
20 changed files with 342 additions and 184 deletions

View File

@ -10,7 +10,14 @@ use axum::{
use rand_core::OsRng;
use sqlx::SqlitePool;
use crate::{responses::{registration::RegistrationResponse, authentication::{AuthenticationResponse, AuthenticationSuccess}}, api::client_server::errors::authentication_error::AuthenticationError};
use crate::{
api::client_server::errors::authentication_error::AuthenticationError,
models::sessions::Session,
responses::{
authentication::{AuthenticationResponse, AuthenticationSuccess},
registration::RegistrationResponse,
},
};
use crate::{
models::devices::Device,
responses::{flow::Flows, registration::RegistrationSuccess},
@ -41,10 +48,11 @@ async fn get_login() -> Result<Json<Flows>, ApiError> {
async fn post_login(
Extension(config): Extension<Arc<Config>>,
Extension(db): Extension<SqlitePool>,
Json(body): Json<AuthenticationData>
Json(body): Json<AuthenticationData>,
) -> Result<Json<AuthenticationResponse>, ApiError> {
let user = UserId::new("name", "server_name").ok().ok_or(AuthenticationError::InvalidUserId)?;
todo!("Flesh this out more");
let user = UserId::new("name", "server_name")
.ok()
.ok_or(AuthenticationError::InvalidUserId)?;
let resp = AuthenticationSuccess::new("", "", &user);
Ok(Json(AuthenticationResponse::Success(resp)))
@ -92,22 +100,29 @@ async fn post_register(
None => "Random displayname",
};
let user =
User::create(&db, &user_id, &user_id.to_string(), auth_data.password()).await?;
let device = Device::create(&db, &user, "test", display_name).await?;
let user = User::new(&user_id, &user_id.to_string(), auth_data.password())?
.create(&db)
.await?;
let device = Device::new(&user, "test", display_name)?
.create(&db)
.await?;
(user, device)
}
};
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().to_string());
Ok(Json(RegistrationResponse::Success(resp)))
} else {
let session = device.create_session(&db).await?;
let resp =
RegistrationSuccess::new(Some(session.value()), device.device_id(), user.user_id());
let session = Session::new(&device)?.create(&db).await?;
let resp = RegistrationSuccess::new(
Some(session.key()),
device.device_id(),
&user.user_id().to_string(),
);
Ok(Json(RegistrationResponse::Success(resp)))
}

View File

@ -38,8 +38,9 @@ pub enum ApiError {
impl From<anyhow::Error> for ApiError {
fn from(err: anyhow::Error) -> Self {
map_err!(err,
sqlx::Error => ApiError::DBError,
RegistrationError => ApiError::RegistrationError
sqlx::Error => ApiError::DBError,
RegistrationError => ApiError::RegistrationError,
AuthenticationError => ApiError::AuthenticationError
);
ApiError::Generic(err)

View File

@ -30,12 +30,22 @@ impl IntoResponse for AuthenticationError {
.into_response(),
Self::Forbidden => (
StatusCode::FORBIDDEN,
Json(ErrorResponse::new(ErrorCode::Forbidden, &self.to_string(), None)),
).into_response(),
Json(ErrorResponse::new(
ErrorCode::Forbidden,
&self.to_string(),
None,
)),
)
.into_response(),
Self::UserDeactivated => (
StatusCode::FORBIDDEN,
Json(ErrorResponse::new(ErrorCode::UserDeactivated, &self.to_string(), None)),
).into_response(),
Json(ErrorResponse::new(
ErrorCode::UserDeactivated,
&self.to_string(),
None,
)),
)
.into_response(),
}
}
}

View File

@ -20,4 +20,4 @@ impl ErrorResponse {
retry_after_ms,
}
}
}
}