Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tower's CORS middleware #206

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tracing-opentelemetry = { version = "0.24.0", features = ["metrics"], optional =
axum = { workspace = true, optional = true }
axum-extra = { version = "0.9.0", features = ["typed-header"], optional = true }
tower = { version = "0.4.13", optional = true }
tower-http = { version = "0.5.0", features = ["trace", "timeout", "request-id", "util", "normalize-path", "sensitive-headers", "catch-panic", "compression-full", "decompression-full", "limit"], optional = true }
tower-http = { version = "0.5.0", features = ["trace", "timeout", "request-id", "util", "normalize-path", "sensitive-headers", "catch-panic", "compression-full", "decompression-full", "limit", "cors"], optional = true }
aide = { workspace = true, features = ["axum", "redoc", "scalar", "macros"], optional = true }
schemars = { workspace = true, optional = true }

Expand Down
3 changes: 3 additions & 0 deletions src/config/service/http/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ timeout = 10000
priority = -9970
limit = "5 MB"

[service.http.middleware.cors]
priority = -9950

# Initializers
[service.http.initializer]
default-enable = true
Expand Down
6 changes: 5 additions & 1 deletion src/config/service/http/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::service::http::middleware::catch_panic::CatchPanicConfig;
use crate::service::http::middleware::compression::{
RequestDecompressionConfig, ResponseCompressionConfig,
};
use crate::service::http::middleware::cors::CorsConfig;
use crate::service::http::middleware::request_id::{PropagateRequestIdConfig, SetRequestIdConfig};
use crate::service::http::middleware::sensitive_headers::{
SensitiveRequestHeadersConfig, SensitiveResponseHeadersConfig,
Expand Down Expand Up @@ -44,6 +45,9 @@ pub struct Middleware {
pub timeout: MiddlewareConfig<TimeoutConfig>,

pub size_limit: MiddlewareConfig<SizeLimitConfig>,

pub cors: MiddlewareConfig<CorsConfig>,

/// Allows providing configs for custom middleware. Any configs that aren't pre-defined above
/// will be collected here.
///
Expand Down Expand Up @@ -102,7 +106,7 @@ impl CommonConfig {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MiddlewareConfig<T> {
#[serde(flatten)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ timeout = 10000
priority = -9970
limit = '5 MB'

[service.http.middleware.cors]
priority = -9950
preset = 'restrictive'
max-age = 3600000

[service.http.initializer]
default-enable = true

Expand Down
20 changes: 19 additions & 1 deletion src/error/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ use crate::error::Error;
#[derive(Debug, Error)]
pub enum AxumError {
#[error(transparent)]
InvalidHeader(#[from] axum::http::header::InvalidHeaderName),
InvalidHeaderName(#[from] axum::http::header::InvalidHeaderName),

#[error(transparent)]
InvalidHeaderValue(#[from] axum::http::header::InvalidHeaderValue),

#[error(transparent)]
InvalidMethod(#[from] axum::http::method::InvalidMethod),

#[cfg(feature = "jwt")]
#[error(transparent)]
Expand All @@ -19,6 +25,18 @@ impl From<axum::http::header::InvalidHeaderName> for Error {
}
}

impl From<axum::http::header::InvalidHeaderValue> for Error {
fn from(value: axum::http::header::InvalidHeaderValue) -> Self {
Self::Axum(AxumError::from(value))
}
}

impl From<axum::http::method::InvalidMethod> for Error {
fn from(value: axum::http::method::InvalidMethod) -> Self {
Self::Axum(AxumError::from(value))
}
}

#[cfg(feature = "jwt")]
impl From<axum_extra::typed_header::TypedHeaderRejection> for Error {
fn from(value: axum_extra::typed_header::TypedHeaderRejection) -> Self {
Expand Down
8 changes: 1 addition & 7 deletions src/middleware/http/auth/jwt/ietf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ mod tests {
use super::*;
use crate::error::RoadsterResult;
use crate::middleware::http::auth::jwt::decode_auth_token;
use crate::util::serde_util::UriOrString;
use crate::util::serde_util::{UriOrString, Wrapper};
use chrono::{TimeDelta, Utc};
use jsonwebtoken::{encode, EncodingKey, Header, TokenData};
use serde_derive::{Deserialize, Serialize};
use serde_json::from_str;
use std::ops::{Add, Sub};
use std::str::FromStr;
Expand Down Expand Up @@ -132,11 +131,6 @@ mod tests {
(claims, token)
}

#[derive(Debug, Deserialize, Serialize)]
struct Wrapper<T> {
inner: T,
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn deserialize_audience_as_vec() {
Expand Down
7 changes: 1 addition & 6 deletions src/middleware/http/auth/jwt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,11 @@ pub enum Subject {
#[cfg(test)]
mod tests {
use super::*;
use serde_derive::{Deserialize, Serialize};
use crate::util::serde_util::Wrapper;
use serde_json::from_str;
use std::str::FromStr;
use url::Url;

#[derive(Debug, Deserialize, Serialize)]
struct Wrapper<T> {
inner: T,
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn deserialize_subject_as_uri() {
Expand Down
7 changes: 1 addition & 6 deletions src/middleware/http/auth/jwt/openid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,11 @@ pub enum Acr {
#[cfg(test)]
mod tests {
use super::*;
use serde_derive::{Deserialize, Serialize};
use crate::util::serde_util::Wrapper;
use serde_json::from_str;
use std::str::FromStr;
use url::Url;

#[derive(Debug, Deserialize, Serialize)]
struct Wrapper<T> {
inner: T,
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn deserialize_acr_as_uri() {
Expand Down
3 changes: 2 additions & 1 deletion src/service/http/middleware/catch_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::service::http::middleware::Middleware;
use axum::Router;
use serde_derive::{Deserialize, Serialize};
use tower_http::catch_panic::CatchPanicLayer;
use validator::Validate;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct CatchPanicConfig {}

Expand Down
5 changes: 3 additions & 2 deletions src/service/http/middleware/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use serde_derive::{Deserialize, Serialize};
use crate::error::RoadsterResult;
use tower_http::compression::CompressionLayer;
use tower_http::decompression::RequestDecompressionLayer;
use validator::Validate;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct ResponseCompressionConfig {}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
pub struct RequestDecompressionConfig {}

Expand Down
Loading
Loading