remove: Remove user registration body

Fixes: #15
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-21 14:37:34 +03:00
parent f0d1b78946
commit 0e26557128
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
3 changed files with 15 additions and 46 deletions

View file

@ -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,
}
}
}

View file

@ -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<RegisterUserBody>,
req: &Request,
depot: &mut Depot,
) -> ApiResult<EmptySchema> {
let body = body.into_inner();
pub async fn register(req: &Request, depot: &mut Depot) -> ApiResult<EmptySchema> {
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);
}

View file

@ -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 {