use crate::config::GarrisonConfig;
use crate::context::token_extract::extract_token_from_headers;
use crate::error::{GarrisonError, GarrisonResult};
use crate::stp::with_current_token;
use std::sync::Arc;
use warp::http::HeaderMap;
use warp::http::StatusCode;
use warp::reject::Reject;
use warp::reply::{Reply, Response};
use warp::Filter;
impl Reject for super::GarrisonRejection {}
impl std::fmt::Display for super::GarrisonRejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
pub(crate) fn unified_error_reply(err: &GarrisonError) -> Response {
let body = err.to_json_body();
let (status, _, _, _) = err.response_parts();
let status = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let mut response = warp::reply::with_status(warp::reply::json(&body), status).into_response();
if let Some(secs) = err.retry_after_secs() {
response.headers_mut().insert(
warp::http::header::HeaderName::from_static("retry-after"),
warp::http::HeaderValue::from(secs),
);
}
if let Some(id) = crate::context::request_id::current() {
if let Ok(value) = warp::http::HeaderValue::from_str(id.as_ref()) {
response.headers_mut().insert(
warp::http::header::HeaderName::from_static("x-request-id"),
value,
);
}
}
response
}
impl Reply for GarrisonError {
fn into_response(self) -> Response {
self.log_rejection();
unified_error_reply(&self)
}
}
impl Reply for super::GarrisonRejection {
fn into_response(self) -> Response {
self.0.into_response()
}
}
pub async fn garrison_recover(err: warp::Rejection) -> Result<Response, warp::Rejection> {
if let Some(rej) = err.find::<super::GarrisonRejection>() {
rej.0.log_rejection();
return Ok(unified_error_reply(&rej.0));
}
Err(err)
}
pub fn check_login(
config: Arc<GarrisonConfig>,
) -> impl Filter<Extract = ((),), Error = warp::Rejection> + Clone {
warp::any()
.and(warp::header::headers_cloned())
.and_then(move |headers: HeaderMap| {
let config = config.clone();
async move {
let token = extract_token_from_headers(&headers, &config)
.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?
.ok_or_else(|| {
warp::reject::custom(super::GarrisonRejection(GarrisonError::NotLogin(
"web-not-login::".to_string(),
)))
})?;
let result: GarrisonResult<()> = with_current_token(token, async {
let logged_in = crate::stp::GarrisonUtil::check_login().await?;
if !logged_in {
return Err(GarrisonError::NotLogin("web-not-login::".to_string()));
}
Ok(())
})
.await;
result.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?;
Ok::<(), warp::Rejection>(())
}
})
}
pub fn check_role(
config: Arc<GarrisonConfig>,
role: String,
) -> impl Filter<Extract = ((),), Error = warp::Rejection> + Clone {
warp::any()
.and(warp::header::headers_cloned())
.and_then(move |headers: HeaderMap| {
let config = config.clone();
let role = role.clone();
async move {
let token = extract_token_from_headers(&headers, &config)
.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?
.ok_or_else(|| {
warp::reject::custom(super::GarrisonRejection(GarrisonError::NotLogin(
"web-not-login::".to_string(),
)))
})?;
let result: GarrisonResult<()> = with_current_token(token, async move {
crate::stp::GarrisonUtil::check_role(&role).await
})
.await;
result.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?;
Ok::<(), warp::Rejection>(())
}
})
}
pub fn check_permission(
config: Arc<GarrisonConfig>,
permission: String,
) -> impl Filter<Extract = ((),), Error = warp::Rejection> + Clone {
warp::any()
.and(warp::header::headers_cloned())
.and_then(move |headers: HeaderMap| {
let config = config.clone();
let permission = permission.clone();
async move {
let token = extract_token_from_headers(&headers, &config)
.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?
.ok_or_else(|| {
warp::reject::custom(super::GarrisonRejection(GarrisonError::NotLogin(
"web-not-login::".to_string(),
)))
})?;
let result: GarrisonResult<()> = with_current_token(token, async move {
crate::stp::GarrisonUtil::check_permission(&permission).await
})
.await;
result.map_err(|e| warp::reject::custom(super::GarrisonRejection(e)))?;
Ok::<(), warp::Rejection>(())
}
})
}