kegani-cli 0.1.4

CLI tool for Kegani framework
Documentation
//! JWT Authentication middleware
//!
//! Validates Bearer tokens on protected routes.
//! This is a placeholder — implement your JWT validation logic here.
//!
//! Usage in main.rs:
//!     App::new()
//!         .service(my_api::routes::configure_with_auth)
//!         ...
//!
//! Or use as a request guard in routes.

use actix_web::dev::ServiceRequest;

/// JWT auth middleware — validates Bearer tokens on protected routes.
/// Currently a pass-through placeholder; implement your JWT validation logic.
pub struct JwtAuth {
    _secret: String,
}

impl JwtAuth {
    pub fn new(secret: String) -> Self {
        Self { _secret: secret }
    }

    /// Validate a Bearer token. Returns Ok(()) if valid, Err(status) if not.
    pub fn validate_token(&self, _token: &str) -> Result<(), actix_web::http::StatusCode> {
        // TODO: Implement actual JWT validation using self.secret:
        //   let key = jsonwebtoken::DecodingKey::from_secret(self.secret.as_bytes());
        //   let token_data = jsonwebtoken::decode(token, &key, &Validation::default())?;
        //   Ok(())
        tracing::debug!("JWT validation placeholder called");
        Ok(())
    }
}

/// Check Authorization header for a request.
pub fn extract_bearer_token(req: &ServiceRequest) -> Option<String> {
    use actix_web::http::header::AUTHORIZATION;
    req.headers()
        .get(AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.strip_prefix("Bearer "))
        .map(|s| s.to_string())
}