Skip to content

Commit

Permalink
feat: Create a default auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bush1D3v committed Jul 28, 2024
1 parent 48519cb commit 1cb07af
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/middlewares/auth_middleware.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 1cb07af

Please sign in to comment.