From 1cb07aff8cf81d2f0dda8e6609a0d2634bcae04b Mon Sep 17 00:00:00 2001 From: bush1D3v Date: Sat, 27 Jul 2024 23:58:04 -0300 Subject: [PATCH] feat: Create a default auth middleware --- src/middlewares/auth_middleware.rs | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/middlewares/auth_middleware.rs diff --git a/src/middlewares/auth_middleware.rs b/src/middlewares/auth_middleware.rs new file mode 100644 index 0000000..6d60633 --- /dev/null +++ b/src/middlewares/auth_middleware.rs @@ -0,0 +1,68 @@ +use super::{ + jwt_token_middleware::jwt_token_middleware, uuid_path_middleware::uuid_path_middleware, +}; +use crate::utils::error_construct::error_construct; +use actix_web::{HttpRequest, HttpResponse}; + +/// Auth middleware. +/// +/// This function checks if the user is authenticated. +/// +/// # Parameters +/// +/// - `id`: The ID of the user. +/// - `req`: The request object. +/// - `path_name`: The name of the path. +/// +/// # Returns +/// +/// Returns a `Result` which, on success, return an empty tuple. On failure, returns an `HttpResponse` with the corresponding error. +/// +/// # Errors +/// +/// This function may return an error if: +/// +/// - The id is not a valid UUID. +/// - The JWT token is empty or invalid. +/// - The JWT token not belongs to the user. +/// +/// +/// # Example +/// +/// ```rust +/// use navarro_blog_api::middlewares::auth_middleware::auth_middleware; +/// use actix_web::{HttpRequest, HttpResponse}; +/// +/// pub async fn example(id: String, req: HttpRequest, path_name: String) -> Result<(), HttpResponse> { +/// match auth_middleware(id, req, &path_name).await { +/// Ok(_) => Ok(()), +/// Err(e) => return Err(e), +/// } +/// }; +/// ``` +pub async fn auth_middleware( + id: String, + req: HttpRequest, + path_name: &str, +) -> Result<(), HttpResponse> { + let id = match uuid_path_middleware(id, path_name) { + Ok(id) => id, + Err(e) => return Err(e), + }; + let token = match jwt_token_middleware(req.headers()) { + Ok(token) => token, + Err(e) => return Err(e), + }; + + if token.claims.sub != id { + return Err(HttpResponse::Unauthorized().json(error_construct( + String::from("bearer token"), + String::from("unauthorized"), + String::from("O token informado não pertence ao usuário."), + None, + None, + None, + ))); + } + Ok(()) +}