refactor: switch error handle to snafu
This commit is contained in:
@@ -6,12 +6,14 @@ use axum::{
|
||||
http::request::Parts,
|
||||
routing::get,
|
||||
};
|
||||
use snafu::prelude::*;
|
||||
|
||||
use super::core::Controller;
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
auth::{
|
||||
AuthError, AuthService, AuthServiceTrait,
|
||||
errors::OidcRequestRedirectUriSnafu,
|
||||
oidc::{OidcAuthCallbackPayload, OidcAuthCallbackQuery, OidcAuthRequest},
|
||||
},
|
||||
errors::RResult,
|
||||
@@ -47,7 +49,8 @@ async fn oidc_auth(
|
||||
if let AuthService::Oidc(oidc_auth_service) = auth_service {
|
||||
let mut redirect_uri = ForwardedRelatedInfo::from_request_parts(&parts)
|
||||
.resolved_origin()
|
||||
.ok_or_else(|| AuthError::OidcRequestRedirectUriError(url::ParseError::EmptyHost))?;
|
||||
.ok_or(url::ParseError::EmptyHost)
|
||||
.context(OidcRequestRedirectUriSnafu)?;
|
||||
|
||||
redirect_uri.set_path(&format!("{CONTROLLER_PREFIX}/callback"));
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ use axum::{
|
||||
use futures_util::future::BoxFuture;
|
||||
use ipnetwork::IpNetwork;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snafu::ResultExt;
|
||||
use tower::{Layer, Service};
|
||||
use tracing::error;
|
||||
|
||||
@@ -233,12 +234,14 @@ impl RemoteIPLayer {
|
||||
proxies
|
||||
.iter()
|
||||
.map(|proxy| {
|
||||
IpNetwork::from_str(proxy).map_err(|err| {
|
||||
RError::CustomMessageString(format!(
|
||||
"remote ip middleare cannot parse trusted proxy \
|
||||
configuration: `{proxy}`, reason: `{err}`",
|
||||
))
|
||||
})
|
||||
IpNetwork::from_str(proxy)
|
||||
.boxed()
|
||||
.with_whatever_context::<_, _, RError>(|_| {
|
||||
format!(
|
||||
"remote ip middleare cannot parse trusted proxy \
|
||||
configuration: `{proxy}`"
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<RResult<Vec<_>>>()
|
||||
})
|
||||
@@ -284,8 +287,7 @@ where
|
||||
let xff_ip = maybe_get_forwarded(req.headers(), layer.trusted_proxies.as_ref());
|
||||
let remote_ip = xff_ip.map_or_else(
|
||||
|| {
|
||||
let ip = req
|
||||
.extensions()
|
||||
req.extensions()
|
||||
.get::<ConnectInfo<SocketAddr>>()
|
||||
.map_or_else(
|
||||
|| {
|
||||
@@ -296,8 +298,7 @@ where
|
||||
RemoteIP::None
|
||||
},
|
||||
|info| RemoteIP::Socket(info.ip()),
|
||||
);
|
||||
ip
|
||||
)
|
||||
},
|
||||
RemoteIP::Forwarded,
|
||||
);
|
||||
|
||||
@@ -18,13 +18,10 @@ use axum::{
|
||||
use futures_util::future::BoxFuture;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{self, json};
|
||||
use snafu::whatever;
|
||||
use tower::{Layer, Service};
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
web::middleware::MiddlewareLayer,
|
||||
errors::{RError, RResult},
|
||||
};
|
||||
use crate::{app::AppContextTrait, errors::RResult, web::middleware::MiddlewareLayer};
|
||||
|
||||
static PRESETS: OnceLock<HashMap<String, BTreeMap<String, String>>> = OnceLock::new();
|
||||
fn get_presets() -> &'static HashMap<String, BTreeMap<String, String>> {
|
||||
@@ -115,7 +112,10 @@ impl MiddlewareLayer for SecureHeader {
|
||||
}
|
||||
|
||||
/// Applies the secure headers layer to the application router
|
||||
fn apply(&self, app: Router<Arc<dyn AppContextTrait>>) -> RResult<Router<Arc<dyn AppContextTrait>>> {
|
||||
fn apply(
|
||||
&self,
|
||||
app: Router<Arc<dyn AppContextTrait>>,
|
||||
) -> RResult<Router<Arc<dyn AppContextTrait>>> {
|
||||
Ok(app.layer(SecureHeaders::new(self)?))
|
||||
}
|
||||
}
|
||||
@@ -128,17 +128,15 @@ impl SecureHeader {
|
||||
let mut headers = vec![];
|
||||
|
||||
let preset = &self.preset;
|
||||
let p = get_presets().get(preset).ok_or_else(|| {
|
||||
RError::CustomMessageString(format!(
|
||||
"secure_headers: a preset named `{preset}` does not exist"
|
||||
))
|
||||
})?;
|
||||
|
||||
Self::push_headers(&mut headers, p)?;
|
||||
if let Some(overrides) = &self.overrides {
|
||||
Self::push_headers(&mut headers, overrides)?;
|
||||
if let Some(p) = get_presets().get(preset) {
|
||||
Self::push_headers(&mut headers, p)?;
|
||||
if let Some(overrides) = &self.overrides {
|
||||
Self::push_headers(&mut headers, overrides)?;
|
||||
}
|
||||
Ok(headers)
|
||||
} else {
|
||||
whatever!("secure_headers: a preset named `{preset}` does not exist")
|
||||
}
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
/// Helper function to push headers into a mutable vector.
|
||||
|
||||
Reference in New Issue
Block a user