Skip to content

Commit

Permalink
feat: added skip validtion on given endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioRodriguezRuiz authored and adrrf committed Nov 17, 2024
1 parent f5ed3d2 commit f1a9f1c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ services:
- path: "/auth/api/v1"
target_service: "http://localhost"
target_port: "3001"
endpoints_without_auth:
- endpoint: "/auth/api/v1/login"
method: "POST"
7 changes: 7 additions & 0 deletions src/config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ pub struct ServiceConfig {
pub target_port: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct NoAuthEndpoints {
pub endpoint: String,
pub method: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GatewayConfig {
pub api_gateway_url: String,
pub authorization_api_url: String,
pub services: Vec<ServiceConfig>,
pub endpoints_without_auth: Vec<NoAuthEndpoints>,
}

pub fn load_config(path: &str) -> GatewayConfig {
Expand Down
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod config;

use config::parser::{load_config, GatewayConfig, ServiceConfig};
use config::parser::{load_config, GatewayConfig, NoAuthEndpoints, ServiceConfig};
use http_body_util::{BodyExt, Full};
use hyper::body::{Bytes, Incoming};
use hyper::http::request::Parts;
Expand Down Expand Up @@ -51,11 +51,13 @@ async fn handle_request(
}
};

match authorize_user(req.headers(), &config.authorization_api_url).await {
Ok(res) if !res.status().is_success() => return Ok(res),
Ok(_) => (),
Err(_) => return service_unavailable("Failed to connect to Authorization API"),
};
if needs_auth(path, req.method().as_str(), &config.endpoints_without_auth) {
match authorize_user(req.headers(), &config.authorization_api_url).await {
Ok(res) if !res.status().is_success() => return Ok(res),
Ok(_) => (),
Err(_) => return service_unavailable("Failed to connect to Authorization API"),
};
}

let (parts, body) = req.into_parts();
let downstream_req = build_downstream_request(parts, body, service_config).await?;
Expand All @@ -70,6 +72,12 @@ fn get_service_config<'a>(path: &str, services: &'a [ServiceConfig]) -> Option<&
services.iter().find(|c| path.starts_with(&c.path))
}

fn needs_auth(path: &str, method: &str, no_auth_endpoints: &[NoAuthEndpoints]) -> bool {
!no_auth_endpoints
.iter()
.any(|e| e.endpoint == path && e.method == method)
}

async fn authorize_user(headers: &HeaderMap, auth_api_url: &str) -> Result<Response<BoxBody>, ()> {
let cookies_header_value = match headers.get(COOKIE) {
Some(value) => value.to_str().unwrap_or_default(),
Expand Down

0 comments on commit f1a9f1c

Please sign in to comment.