From 46eb5918c0de0d7653c7c7ac7d5751dd63ddb7d2 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Sun, 21 Jul 2024 14:22:06 +0300 Subject: [PATCH] remove: Remove user registration body Signed-off-by: Awiteb --- crates/oxidetalis/src/routes/errors.rs | 10 +------ crates/oxidetalis/src/routes/user.rs | 41 ++++++++------------------ crates/oxidetalis/src/schemas/user.rs | 10 +------ 3 files changed, 15 insertions(+), 46 deletions(-) diff --git a/crates/oxidetalis/src/routes/errors.rs b/crates/oxidetalis/src/routes/errors.rs index 6911f06..644e968 100644 --- a/crates/oxidetalis/src/routes/errors.rs +++ b/crates/oxidetalis/src/routes/errors.rs @@ -36,12 +36,6 @@ pub enum ApiError { /// The entered public key is already registered (400 Bad Request) #[error("The entered public key is already registered")] AlreadyRegistered, - /// The user entered two different public keys - /// one in the header and other in the request body - /// (400 Bad Request) - #[error("You entered two different public keys")] - TwoDifferentKeys, - /// Error in the query parameters (400 Bad Request) #[error("{0}")] Querys(String), /// Non registered user tried to access to registered user only endpoint @@ -56,9 +50,7 @@ impl ApiError { match self { Self::Internal => StatusCode::INTERNAL_SERVER_ERROR, Self::RegistrationClosed | Self::NotRegisteredUser => StatusCode::FORBIDDEN, - Self::AlreadyRegistered | Self::TwoDifferentKeys | Self::Querys(_) => { - StatusCode::BAD_REQUEST - } + Self::AlreadyRegistered | Self::Querys(_) => StatusCode::BAD_REQUEST, } } } diff --git a/crates/oxidetalis/src/routes/user.rs b/crates/oxidetalis/src/routes/user.rs index c1169c2..ec57da2 100644 --- a/crates/oxidetalis/src/routes/user.rs +++ b/crates/oxidetalis/src/routes/user.rs @@ -17,15 +17,7 @@ //! REST API endpoints for user management use oxidetalis_core::types::{PublicKey, Signature}; -use salvo::{ - http::StatusCode, - oapi::{endpoint, extract::JsonBody}, - writing::Json, - Depot, - Request, - Router, - Writer, -}; +use salvo::{http::StatusCode, oapi::endpoint, writing::Json, Depot, Request, Router, Writer}; use super::{ApiError, ApiResult}; use crate::{ @@ -33,20 +25,22 @@ use crate::{ extensions::DepotExt, middlewares, parameters::Pagination, - schemas::{BlackListedUser, EmptySchema, MessageSchema, RegisterUserBody, WhiteListedUser}, + schemas::{BlackListedUser, EmptySchema, MessageSchema, WhiteListedUser}, utils, }; +/// (🔓) Register a user +/// +/// Register the request sender as a user in the server, the server registration +/// must be open to register a user. #[endpoint( operation_id = "register", tags("User"), responses( (status_code = 201, description = "User registered"), - (status_code = 403, description = "Server registration is closed", content_type = "application/json", body = MessageSchema), - (status_code = 400, description = "The public key in the header is not the same as the key in the body", content_type = "application/json", body = MessageSchema), (status_code = 400, description = "The entered public key is already registered", content_type = "application/json", body = MessageSchema), - (status_code = 401, description = "The entered signature is invalid", content_type = "application/json", body = MessageSchema), - (status_code = 401, description = "The entered public key is invalid", content_type = "application/json", body = MessageSchema), + (status_code = 401, description = "The entered signature or public key is invalid", content_type = "application/json", body = MessageSchema), + (status_code = 403, description = "Server registration is closed", content_type = "application/json", body = MessageSchema), (status_code = 429, description = "Too many requests", content_type = "application/json", body = MessageSchema), (status_code = 500, description = "Internal server error", content_type = "application/json", body = MessageSchema), ), @@ -55,25 +49,16 @@ use crate::{ ("X-OTMP-PUBLIC" = PublicKey, Header, description = "Public key of the sender"), ), )] -pub async fn register( - body: JsonBody, - req: &Request, - depot: &mut Depot, -) -> ApiResult { - let body = body.into_inner(); +pub async fn register(req: &Request, depot: &mut Depot) -> ApiResult { let db = depot.db_conn(); let config = depot.config(); - - if utils::extract_public_key(req).expect("Public key should be checked in the middleware") - != body.public_key - { - return Err(ApiError::TwoDifferentKeys); - } + let public_key = + utils::extract_public_key(req).expect("Public key should be checked in the middleware"); if !db.users_exists_in_database().await? { - db.register_user(&body.public_key, true).await?; + db.register_user(&public_key, true).await?; } else if config.register.enable { - db.register_user(&body.public_key, false).await?; + db.register_user(&public_key, false).await?; } else { return Err(ApiError::RegistrationClosed); } diff --git a/crates/oxidetalis/src/schemas/user.rs b/crates/oxidetalis/src/schemas/user.rs index 0b58c10..e3ffa92 100644 --- a/crates/oxidetalis/src/schemas/user.rs +++ b/crates/oxidetalis/src/schemas/user.rs @@ -17,19 +17,11 @@ use std::str::FromStr; use chrono::{DateTime, Utc}; -use oxidetalis_core::{cipher::K256Secret, types::PublicKey}; +use oxidetalis_core::types::PublicKey; use oxidetalis_entities::prelude::*; use salvo::oapi::ToSchema; use serde::{Deserialize, Serialize}; -/// The schema for the user registration request -#[derive(Serialize, Deserialize, Clone, Debug, ToSchema, derive_new::new)] -#[salvo(schema(name = RegisterUserBody, example = json!(RegisterUserBody::new(K256Secret::new().pubkey()))))] -pub struct RegisterUserBody { - /// The public key of the user - pub public_key: PublicKey, -} - #[derive(Serialize, Deserialize, Clone, Debug, ToSchema, derive_new::new)] #[salvo(schema(name = WhiteListedUser, example = json!(WhiteListedUser::default())))] pub struct WhiteListedUser {