use crate::error::{GarrisonError, GarrisonResult};
pub mod tenant;
pub mod token_extract;
pub mod request_id;
pub use request_id::{
current, generate, inject_request_id_into_error_body, is_valid_inbound_request_id,
propagate_or_generate, scope, RequestId, MAX_INBOUND_LEN, REQUEST_ID_BODY_FIELD,
REQUEST_ID_HEADER, RETRY_AFTER_HEADER,
};
pub use token_extract::{
extract_token_from_headers, extract_token_from_request_parts, is_body_token_allowed_method,
strip_bearer_prefix, HeaderLookup,
};
pub mod cookie;
pub use cookie::{build_set_cookie_value, token_cookie_name, CookiePath, CookieScope, CookieType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GarrisonPrincipal {
pub login_id: String,
}
pub trait GarrisonContext {
fn request(&self) -> GarrisonResult<Box<dyn GarrisonRequest>>;
}
pub trait GarrisonRequest {
fn path(&self) -> GarrisonResult<String>;
fn method(&self) -> GarrisonResult<String>;
fn header(&self, name: &str) -> GarrisonResult<Option<String>>;
fn cookie(&self, name: &str) -> GarrisonResult<Option<String>>;
fn get_token(&self, config: &crate::config::GarrisonConfig) -> GarrisonResult<Option<String>>;
}
pub trait GarrisonResponse {
fn set_status(&mut self, code: u16) -> GarrisonResult<()>;
fn set_header(&mut self, name: &str, value: &str) -> GarrisonResult<()>;
fn set_cookie(&mut self, name: &str, value: &str) -> GarrisonResult<()> {
self.set_cookie_with_frontend_check(name, value, helpers::shared_default_config())
}
fn set_cookie_with_config(
&mut self,
name: &str,
value: &str,
config: &crate::config::GarrisonConfig,
) -> GarrisonResult<()> {
let cookie = CookieType::session(name, config)?;
let cookie_value = build_set_cookie_value(&cookie, value, config.cookie_secure)?;
self.set_header("Set-Cookie", &cookie_value)
}
fn set_cookie_with_frontend_check(
&mut self,
name: &str,
value: &str,
config: &crate::config::GarrisonConfig,
) -> GarrisonResult<()> {
if config.frontend_separation {
return Ok(());
}
self.set_cookie_with_config(name, value, config)
}
}
pub trait GarrisonStorage {
fn set(&mut self, key: &str, value: &str) -> GarrisonResult<()>;
fn get(&self, key: &str) -> GarrisonResult<Option<String>>;
fn delete(&mut self, key: &str) -> GarrisonResult<()>;
}
mod helpers;
pub use helpers::{effective_is_read_cookie, effective_is_read_header};
pub(crate) fn validate_cookie_name_value(name: &str, value: &str) -> GarrisonResult<()> {
let name_ok = !name.is_empty()
&& name.bytes().all(|b| {
b.is_ascii_alphanumeric()
|| matches!(
b,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
});
if !name_ok {
return Err(GarrisonError::Context(format!(
"ctx-invalid-cookie-name::{name}"
)));
}
let value_ok = !value
.bytes()
.any(|b| b < 0x21 || b == 0x7F || matches!(b, b';' | b',' | b'\\' | b'"'));
if !value_ok {
return Err(GarrisonError::Context(
"ctx-invalid-cookie-value::cookie value must not contain control chars, space, ';', ',', '\\\\' or '\"'".to_string(),
));
}
Ok(())
}
#[cfg(feature = "web-axum")]
pub mod axum_adapter;
#[cfg(feature = "web-axum")]
pub use axum_adapter::{AxumContext, AxumRequest, AxumResponse, AxumStorage};
#[cfg(feature = "web-actix")]
pub mod actix_adapter;
#[cfg(feature = "web-actix")]
pub use actix_adapter::{ActixContext, ActixRequest, ActixResponse, ActixStorage};
#[cfg(feature = "web-warp")]
pub mod warp_adapter;
#[cfg(feature = "web-warp")]
pub use warp_adapter::{WarpContext, WarpRequest, WarpResponse, WarpStorage};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;