-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from 8shaws/middle
chore: adds middleware for verification checks
- Loading branch information
Showing
10 changed files
with
358 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
pub mod middleware; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
crates/api/src/auth/middleware.rs → .../api/src/middlewares/extract_client_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod check_verify; | ||
pub mod extract_client_id; | ||
pub mod rate_limit; | ||
pub mod un_verify_user; | ||
pub mod verify_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use actix_service::Service; | ||
use actix_web::{web, App, Error, HttpRequest, HttpResponse}; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
use std::time::Duration; | ||
use tokio::sync::Semaphore; | ||
|
||
pub struct RateLimiter { | ||
semaphore: Arc<Semaphore>, | ||
requests: Arc<Mutex<HashMap<String, usize>>>, | ||
max_requests: usize, | ||
window_duration: Duration, | ||
} | ||
|
||
impl RateLimiter { | ||
pub fn new(max_requests: usize, window_duration: Duration) -> Self { | ||
RateLimiter { | ||
semaphore: Arc::new(Semaphore::new(max_requests)), | ||
requests: Arc::new(Mutex::new(HashMap::new())), | ||
max_requests, | ||
window_duration, | ||
} | ||
} | ||
|
||
pub async fn check_rate_limit(&self, key: &str) -> Result<(), HttpResponse> { | ||
let mut requests = self.requests.lock().unwrap(); | ||
let count = requests.entry(key.to_string()).or_insert(0); | ||
if *count >= self.max_requests { | ||
Err(HttpResponse::TooManyRequests().finish()) | ||
} else { | ||
*count += 1; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub async fn handle_request( | ||
&self, | ||
key: &str, | ||
request: HttpRequest, | ||
) -> Result<HttpResponse, Error> { | ||
self.check_rate_limit(key).await; | ||
Ok(HttpResponse::Ok().finish()) | ||
} | ||
} | ||
|
||
async fn rate_limited_handler( | ||
req: HttpRequest, | ||
rate_limiter: web::Data<RateLimiter>, | ||
) -> Result<HttpResponse, Error> { | ||
let key = req.peer_addr().unwrap().to_string(); | ||
rate_limiter.handle_request(&key, req).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use crate::db::user_db_fn::is_user_verified; | ||
use crate::{middlewares::extract_client_id::IdKey, models::AppState}; | ||
use actix_service::Service; | ||
use actix_web::web::Data; | ||
use actix_web::{ | ||
body::EitherBody, | ||
dev::{ServiceRequest, ServiceResponse, Transform}, | ||
Error, HttpMessage, HttpResponse, | ||
}; | ||
use futures_util::future::{ok, LocalBoxFuture, Ready}; | ||
use serde_json::json; | ||
use std::rc::Rc; | ||
use std::task::{Context, Poll}; | ||
|
||
pub struct UnVerifyUser { | ||
app_state: Data<AppState>, | ||
} | ||
|
||
impl UnVerifyUser { | ||
pub fn new(app_state: Data<AppState>) -> Self { | ||
Self { app_state } | ||
} | ||
} | ||
|
||
impl<S, B: 'static> Transform<S, ServiceRequest> for UnVerifyUser | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<EitherBody<B>>; | ||
type Error = Error; | ||
type InitError = (); | ||
type Transform = UnVerifyUserMiddleware<S>; | ||
type Future = Ready<Result<Self::Transform, Self::InitError>>; | ||
|
||
fn new_transform(&self, service: S) -> Self::Future { | ||
ok(UnVerifyUserMiddleware { | ||
service: Rc::new(service), | ||
app_state: self.app_state.clone(), | ||
}) | ||
} | ||
} | ||
|
||
pub struct UnVerifyUserMiddleware<S> { | ||
service: Rc<S>, | ||
app_state: Data<AppState>, | ||
} | ||
|
||
impl<S, B: 'static> Service<ServiceRequest> for UnVerifyUserMiddleware<S> | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<EitherBody<B>>; | ||
type Error = Error; | ||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.service.poll_ready(ctx) | ||
} | ||
fn call(&self, req: ServiceRequest) -> Self::Future { | ||
let srv = Rc::clone(&self.service); | ||
let id_key = req.extensions().get::<IdKey>().cloned(); | ||
|
||
let db_pool = self.app_state.db_pool.clone(); | ||
|
||
let fut = async move { | ||
let mut conn = db_pool.get().unwrap(); | ||
|
||
match id_key { | ||
Some(id) => { | ||
let verified = is_user_verified(&mut conn, id.0); | ||
match verified { | ||
Ok(verified) => { | ||
if !verified { | ||
let res = srv.call(req).await?; | ||
Ok(res.map_into_left_body()) | ||
} else { | ||
let response = HttpResponse::Forbidden().json(json!({ | ||
"message": "User is already Verified!", | ||
"status": "Error" | ||
})); | ||
|
||
Ok(req.into_response(response.map_into_right_body())) | ||
} | ||
} | ||
Err(_) => { | ||
let response = HttpResponse::InternalServerError().json(json!({ | ||
"message": "User Verification Failed!", | ||
"status": "Error" | ||
})); | ||
Ok(req.into_response(response.map_into_right_body())) | ||
} | ||
} | ||
} | ||
None => { | ||
let response = HttpResponse::Forbidden().json(json!({ | ||
"msg": "No authentication token found", | ||
"status": "Error" | ||
})); | ||
Ok(req.into_response(response.map_into_right_body())) | ||
} | ||
} | ||
}; | ||
|
||
Box::pin(fut) | ||
} | ||
} |
Oops, something went wrong.