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 Reply for GarrisonError {
fn into_response(self) -> Response {
tracing::error!(error = ?self, "garrison rejection");
let (status, error_code, message, ex_code) = self.response_parts_i18n();
let status = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
let body = if let Some(code) = ex_code {
serde_json::json!({
"error_code": error_code,
"message": message,
"code": code,
})
} else {
serde_json::json!({
"error_code": error_code,
"message": message,
})
};
let body = warp::reply::json(&body);
warp::reply::with_status(body, status).into_response()
}
}
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>(())
}
})
}