use crate::err::Error; use crate::iam::verify::{basic, token}; use crate::iam::BASIC; use crate::iam::TOKEN; use std::net::SocketAddr; use surrealdb::Session; use warp::Filter; pub fn build() -> impl Filter + Clone { // Enable on any path let conf = warp::any(); // Add remote ip address let conf = conf.and(warp::filters::addr::remote()); // Add remote ip address let conf = conf.map(|addr: Option| addr.map(|v| v.to_string())); // Add authorization header let conf = conf.and(warp::header::optional::("authorization")); // Add http origin header let conf = conf.and(warp::header::optional::("origin")); // Add session id header let conf = conf.and(warp::header::optional::("id")); // Add namespace header let conf = conf.and(warp::header::optional::("ns")); // Add database header let conf = conf.and(warp::header::optional::("db")); // Process all headers conf.and_then(process) } async fn process( ip: Option, au: Option, or: Option, id: Option, ns: Option, db: Option, ) -> Result { // Create session #[rustfmt::skip] let mut session = Session { ip, or, id, ns, db, ..Default::default() }; // Parse the authentication header match au { // Basic authentication data was supplied Some(auth) if auth.starts_with(BASIC) => basic(&mut session, auth).await, // Token authentication data was supplied Some(auth) if auth.starts_with(TOKEN) => token(&mut session, auth).await, // Wrong authentication data was supplied Some(_) => Err(Error::InvalidAuth), // No authentication data was supplied None => Ok(()), }?; // Pass the authenticated session through Ok(session) }