remove: Remove user registration body #28
3 changed files with 15 additions and 46 deletions
|
@ -36,12 +36,6 @@ pub enum ApiError {
|
||||||
/// The entered public key is already registered (400 Bad Request)
|
/// The entered public key is already registered (400 Bad Request)
|
||||||
#[error("The entered public key is already registered")]
|
#[error("The entered public key is already registered")]
|
||||||
AlreadyRegistered,
|
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}")]
|
#[error("{0}")]
|
||||||
Querys(String),
|
Querys(String),
|
||||||
/// Non registered user tried to access to registered user only endpoint
|
/// Non registered user tried to access to registered user only endpoint
|
||||||
|
@ -56,9 +50,7 @@ impl ApiError {
|
||||||
match self {
|
match self {
|
||||||
Self::Internal => StatusCode::INTERNAL_SERVER_ERROR,
|
Self::Internal => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Self::RegistrationClosed | Self::NotRegisteredUser => StatusCode::FORBIDDEN,
|
Self::RegistrationClosed | Self::NotRegisteredUser => StatusCode::FORBIDDEN,
|
||||||
Self::AlreadyRegistered | Self::TwoDifferentKeys | Self::Querys(_) => {
|
Self::AlreadyRegistered | Self::Querys(_) => StatusCode::BAD_REQUEST,
|
||||||
StatusCode::BAD_REQUEST
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,15 +17,7 @@
|
||||||
//! REST API endpoints for user management
|
//! REST API endpoints for user management
|
||||||
|
|
||||||
use oxidetalis_core::types::{PublicKey, Signature};
|
use oxidetalis_core::types::{PublicKey, Signature};
|
||||||
use salvo::{
|
use salvo::{http::StatusCode, oapi::endpoint, writing::Json, Depot, Request, Router, Writer};
|
||||||
http::StatusCode,
|
|
||||||
oapi::{endpoint, extract::JsonBody},
|
|
||||||
writing::Json,
|
|
||||||
Depot,
|
|
||||||
Request,
|
|
||||||
Router,
|
|
||||||
Writer,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{ApiError, ApiResult};
|
use super::{ApiError, ApiResult};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -33,20 +25,22 @@ use crate::{
|
||||||
extensions::DepotExt,
|
extensions::DepotExt,
|
||||||
middlewares,
|
middlewares,
|
||||||
parameters::Pagination,
|
parameters::Pagination,
|
||||||
schemas::{BlackListedUser, EmptySchema, MessageSchema, RegisterUserBody, WhiteListedUser},
|
schemas::{BlackListedUser, EmptySchema, MessageSchema, WhiteListedUser},
|
||||||
utils,
|
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(
|
#[endpoint(
|
||||||
operation_id = "register",
|
operation_id = "register",
|
||||||
tags("User"),
|
tags("User"),
|
||||||
responses(
|
responses(
|
||||||
(status_code = 201, description = "User registered"),
|
(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 = 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 signature or public key 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 = 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 = 429, description = "Too many requests", content_type = "application/json", body = MessageSchema),
|
||||||
(status_code = 500, description = "Internal server error", 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"),
|
("X-OTMP-PUBLIC" = PublicKey, Header, description = "Public key of the sender"),
|
||||||
),
|
),
|
||||||
)]
|
)]
|
||||||
pub async fn register(
|
pub async fn register(req: &Request, depot: &mut Depot) -> ApiResult<EmptySchema> {
|
||||||
body: JsonBody<RegisterUserBody>,
|
|
||||||
req: &Request,
|
|
||||||
depot: &mut Depot,
|
|
||||||
) -> ApiResult<EmptySchema> {
|
|
||||||
let body = body.into_inner();
|
|
||||||
let db = depot.db_conn();
|
let db = depot.db_conn();
|
||||||
let config = depot.config();
|
let config = depot.config();
|
||||||
|
let public_key =
|
||||||
if utils::extract_public_key(req).expect("Public key should be checked in the middleware")
|
utils::extract_public_key(req).expect("Public key should be checked in the middleware");
|
||||||
!= body.public_key
|
|
||||||
{
|
|
||||||
return Err(ApiError::TwoDifferentKeys);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !db.users_exists_in_database().await? {
|
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 {
|
} else if config.register.enable {
|
||||||
db.register_user(&body.public_key, false).await?;
|
db.register_user(&public_key, false).await?;
|
||||||
} else {
|
} else {
|
||||||
return Err(ApiError::RegistrationClosed);
|
return Err(ApiError::RegistrationClosed);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,19 +17,11 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use oxidetalis_core::{cipher::K256Secret, types::PublicKey};
|
use oxidetalis_core::types::PublicKey;
|
||||||
use oxidetalis_entities::prelude::*;
|
use oxidetalis_entities::prelude::*;
|
||||||
use salvo::oapi::ToSchema;
|
use salvo::oapi::ToSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
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)]
|
#[derive(Serialize, Deserialize, Clone, Debug, ToSchema, derive_new::new)]
|
||||||
#[salvo(schema(name = WhiteListedUser, example = json!(WhiteListedUser::default())))]
|
#[salvo(schema(name = WhiteListedUser, example = json!(WhiteListedUser::default())))]
|
||||||
pub struct WhiteListedUser {
|
pub struct WhiteListedUser {
|
||||||
|
|
Loading…
Reference in a new issue