chore: Use PublicKey as an argument and openapi doc
All checks were successful
DCO checker / DCO checker (pull_request) Successful in 14s
Rust CI / Rust CI (pull_request) Successful in 4m39s

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-27 02:47:17 +03:00
parent 531e27ab2c
commit f4df5b26d3
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 18 additions and 36 deletions

View file

@ -17,7 +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::{http::StatusCode, oapi::endpoint, writing::Json, Depot, Request, Router, Writer}; use salvo::{http::StatusCode, oapi::endpoint, writing::Json, Depot, Router, Writer};
use super::{ApiError, ApiResult}; use super::{ApiError, ApiResult};
use crate::{ use crate::{
@ -26,7 +26,6 @@ use crate::{
middlewares, middlewares,
parameters::Pagination, parameters::Pagination,
schemas::{BlackListedUser, EmptySchema, MessageSchema, WhiteListedUser}, schemas::{BlackListedUser, EmptySchema, MessageSchema, WhiteListedUser},
utils,
}; };
/// (🔓) Register a user /// (🔓) Register a user
@ -38,22 +37,18 @@ use crate::{
tags("User"), tags("User"),
responses( responses(
(status_code = 201, description = "User registered"), (status_code = 201, description = "User registered"),
(status_code = 400, description = "The entered public key is already registered", content_type = "application/json", body = MessageSchema), (status_code = 400, description = "Invalid public key", 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 = "Invalid signature", content_type = "application/json", body = MessageSchema),
(status_code = 403, description = "Server registration is closed", content_type = "application/json", body = MessageSchema), (status_code = 403, description = "Server registration is closed", content_type = "application/json", body = MessageSchema),
(status_code = 409, description = "The entered public key is already registered", 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),
), ),
parameters( parameters(Signature),
Signature,
("X-OTMP-PUBLIC" = PublicKey, Header, description = "Public key of the sender"),
),
)] )]
pub async fn register(req: &Request, depot: &mut Depot) -> ApiResult<EmptySchema> { pub async fn register(public_key: PublicKey, depot: &mut Depot) -> ApiResult<EmptySchema> {
let db = depot.db_conn(); let db = depot.db_conn();
let config = depot.config(); let config = depot.config();
let public_key =
utils::extract_public_key(req).expect("Public key should be checked in the middleware");
if !db.users_exists_in_database().await? { if !db.users_exists_in_database().await? {
db.register_user(&public_key, true).await?; db.register_user(&public_key, true).await?;
@ -72,28 +67,22 @@ pub async fn register(req: &Request, depot: &mut Depot) -> ApiResult<EmptySchema
tags("User"), tags("User"),
responses( responses(
(status_code = 200, description = "Returns whitelisted users", content_type = "application/json", body = Vec<WhiteListedUser>), (status_code = 200, description = "Returns whitelisted users", content_type = "application/json", body = Vec<WhiteListedUser>),
(status_code = 400, description = "Wrong query parameter", content_type = "application/json", body = MessageSchema), (status_code = 400, description = "Invalid parameters or public key", 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 = "Invalid signature", content_type = "application/json", body = MessageSchema),
(status_code = 403, description = "Not registered user, must register first", content_type = "application/json", body = MessageSchema), (status_code = 403, description = "Not registered user, must register first", 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),
), ),
parameters( parameters(Signature),
("X-OTMP-PUBLIC" = PublicKey, Header, description = "Public key of the sender"),
Signature,
),
)] )]
async fn user_whitelist( async fn user_whitelist(
req: &mut Request,
depot: &mut Depot, depot: &mut Depot,
pagination: Pagination, pagination: Pagination,
public_key: PublicKey,
) -> ApiResult<Json<Vec<WhiteListedUser>>> { ) -> ApiResult<Json<Vec<WhiteListedUser>>> {
let conn = depot.db_conn(); let conn = depot.db_conn();
let user = conn let user = conn
.get_user_by_pubk( .get_user_by_pubk(&public_key)
&utils::extract_public_key(req)
.expect("Public key should be checked in the middleware"),
)
.await? .await?
.ok_or(ApiError::NotRegisteredUser)?; .ok_or(ApiError::NotRegisteredUser)?;
Ok(Json( Ok(Json(
@ -111,28 +100,22 @@ async fn user_whitelist(
tags("User"), tags("User"),
responses( responses(
(status_code = 200, description = "Returns blacklisted users", content_type = "application/json", body = Vec<BlackListedUser>), (status_code = 200, description = "Returns blacklisted users", content_type = "application/json", body = Vec<BlackListedUser>),
(status_code = 400, description = "Wrong query parameter", content_type = "application/json", body = MessageSchema), (status_code = 400, description = "Invalid parameters or public key", 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 = "Invalid signature", content_type = "application/json", body = MessageSchema),
(status_code = 403, description = "Not registered user, must register first", content_type = "application/json", body = MessageSchema), (status_code = 403, description = "Not registered user, must register first", 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),
), ),
parameters( parameters(Signature),
("X-OTMP-PUBLIC" = PublicKey, Header, description = "Public key of the sender"),
Signature,
),
)] )]
async fn user_blacklist( async fn user_blacklist(
req: &mut Request,
depot: &mut Depot, depot: &mut Depot,
pagination: Pagination, pagination: Pagination,
public_key: PublicKey,
) -> ApiResult<Json<Vec<BlackListedUser>>> { ) -> ApiResult<Json<Vec<BlackListedUser>>> {
let conn = depot.db_conn(); let conn = depot.db_conn();
let user = conn let user = conn
.get_user_by_pubk( .get_user_by_pubk(&public_key)
&utils::extract_public_key(req)
.expect("Public key should be checked in the middleware"),
)
.await? .await?
.ok_or(ApiError::NotRegisteredUser)?; .ok_or(ApiError::NotRegisteredUser)?;
Ok(Json( Ok(Json(

View file

@ -33,6 +33,7 @@ use salvo::{
Request, Request,
Response, Response,
Router, Router,
Writer,
}; };
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use tokio::{sync::RwLock, task::spawn as tokio_spawn, time::sleep as tokio_sleep}; use tokio::{sync::RwLock, task::spawn as tokio_spawn, time::sleep as tokio_sleep};
@ -49,7 +50,6 @@ use crate::{
extensions::{DepotExt, OnlineUsersExt}, extensions::{DepotExt, OnlineUsersExt},
middlewares, middlewares,
nonce::NonceCache, nonce::NonceCache,
utils,
}; };
/// Online users type /// Online users type
@ -96,12 +96,11 @@ impl SocketUserData {
pub async fn user_connected( pub async fn user_connected(
req: &mut Request, req: &mut Request,
res: &mut Response, res: &mut Response,
public_key: PublicKey,
depot: &Depot, depot: &Depot,
) -> Result<(), StatusError> { ) -> Result<(), StatusError> {
let nonce_cache = depot.nonce_cache(); let nonce_cache = depot.nonce_cache();
let db_conn = depot.db_conn(); let db_conn = depot.db_conn();
let public_key =
utils::extract_public_key(req).expect("The public key was checked in the middleware");
let shared_secret = depot.config().server.private_key.shared_secret(&public_key); let shared_secret = depot.config().server.private_key.shared_secret(&public_key);
WebSocketUpgrade::new() WebSocketUpgrade::new()