This commit is contained in:
Mateusz Gruszczyński committed 2026-10-27 12:18:20 +01:00
1 parent 62b7574553
commit e364392659
47 files changed
+2164 -257

No files matched your search

+44 -76
View File
@@ -1,12 +1,10 @@
use crate::{
auth_provider::{self, GatewayFailure, Identity},
config::{Backend, CompiledBackend, UpstreamProtocol, ValidationPolicy},
forwarding,
};
use anyhow::{ensure, Context as _, Result};
use axum::{
body::Body,
response::{IntoResponse, Response},
};
use axum::{body::Body, response::Response};
use http::{HeaderMap, HeaderName, HeaderValue, Method, Request, StatusCode, Uri};
use http_body_util::{BodyExt, Limited};
use serde::{Deserialize, Serialize};
@@ -61,57 +59,12 @@ pub struct Compiled {
response_headers: Vec<HeaderName>,
redirect_forbidden: bool,
}
#[derive(Clone)]
pub struct AuthenticationFailure;
#[derive(Debug)]
enum CheckError {
Connection(anyhow::Error),
InvalidResponse(anyhow::Error),
}
#[derive(Debug, Clone, Copy)]
enum GatewayFailure {
Connection,
Timeout,
Verification,
}
fn gateway_failure(kind: GatewayFailure) -> Response {
let (status, html) = match kind {
GatewayFailure::Connection => (
StatusCode::BAD_GATEWAY,
include_str!("../admin/web/error_tinyauth_connection.html"),
),
GatewayFailure::Timeout => (
StatusCode::GATEWAY_TIMEOUT,
include_str!("../admin/web/error_tinyauth_timeout.html"),
),
GatewayFailure::Verification => (
StatusCode::BAD_GATEWAY,
include_str!("../admin/web/error_tinyauth_verification.html"),
),
};
let mut response = (status, html).into_response();
response.headers_mut().insert(
"content-type",
HeaderValue::from_static("text/html; charset=utf-8"),
);
response.headers_mut().insert(
"cache-control",
HeaderValue::from_static("private, no-store"),
);
response.headers_mut().insert(
"x-content-type-options",
HeaderValue::from_static("nosniff"),
);
response.extensions_mut().insert(AuthenticationFailure);
response
}
#[derive(Debug, Default)]
pub struct Identity {
headers: HeaderMap,
}
const STANDARD_IDENTITY: [&str; 6] = [
"remote-user",
"remote-email",
@@ -252,18 +205,7 @@ impl Settings {
}
}
fn failure(status: StatusCode) -> Response {
let body = format!(
"{} {}\n",
status.as_u16(),
status.canonical_reason().unwrap_or("Error")
);
let mut response = (status, body).into_response();
response.headers_mut().insert(
"cache-control",
HeaderValue::from_static("private, no-store"),
);
response.extensions_mut().insert(AuthenticationFailure);
response
auth_provider::deny(status)
}
fn apply_nginx_auth_context(
@@ -362,15 +304,24 @@ impl Compiled {
Ok(Ok(value)) => value,
Ok(Err(CheckError::Connection(error))) => {
tracing::warn!(%error, "Tinyauth backend connection failed");
return Err(gateway_failure(GatewayFailure::Connection));
return Err(auth_provider::gateway_failure(
"Tinyauth",
GatewayFailure::Connection,
));
}
Ok(Err(CheckError::InvalidResponse(error))) => {
tracing::warn!(%error, "Tinyauth returned an invalid verification response");
return Err(gateway_failure(GatewayFailure::Verification));
return Err(auth_provider::gateway_failure(
"Tinyauth",
GatewayFailure::Verification,
));
}
Err(_) => {
tracing::warn!("Tinyauth verification timed out");
return Err(gateway_failure(GatewayFailure::Timeout));
return Err(auth_provider::gateway_failure(
"Tinyauth",
GatewayFailure::Timeout,
));
}
};
if status.is_success() {
@@ -381,7 +332,10 @@ impl Compiled {
size += value.as_bytes().len();
if size > 8192 {
tracing::warn!("Tinyauth identity headers exceed the configured limit");
return Err(gateway_failure(GatewayFailure::Verification));
return Err(auth_provider::gateway_failure(
"Tinyauth",
GatewayFailure::Verification,
));
}
identity.headers.append(name.clone(), value.clone());
}
@@ -408,7 +362,10 @@ impl Compiled {
status = status.as_u16(),
"Tinyauth returned an unexpected HTTP status"
);
Err(gateway_failure(GatewayFailure::Verification))
Err(auth_provider::gateway_failure(
"Tinyauth",
GatewayFailure::Verification,
))
}
async fn check(
&self,
@@ -451,15 +408,26 @@ impl Compiled {
Ok((parts.status, parts.headers))
}
}
impl Identity {
pub fn apply(&self, headers: &mut HeaderMap) {
for name in self.headers.keys() {
headers.remove(name);
}
for (name, value) in &self.headers {
headers.append(name.clone(), value.clone());
}
}
}
#[cfg(test)]
include!("tests.rs");
impl crate::auth_provider::Provider for Compiled {
fn id(&self) -> &'static str {
"tinyauth"
}
fn strip_identity(&self, headers: &mut HeaderMap) {
Compiled::strip_identity(self, headers);
}
fn authorize<'a>(
&'a self,
incoming: &'a HeaderMap,
method: &'a Method,
context: &'a forwarding::Context,
shutdown: &'a tokio_util::sync::CancellationToken,
_cookies: &'a crate::auth_provider::CookieSink,
) -> crate::auth_provider::Authorization<'a> {
Box::pin(Compiled::authorize(
self, incoming, method, context, shutdown,
))
}
}
+2 -2
View File
@@ -208,7 +208,7 @@ mod tests {
"Tinyauth timeout",
),
] {
let response = gateway_failure(kind);
let response = auth_provider::gateway_failure("Tinyauth", kind);
assert_eq!(response.status(), status);
assert_eq!(
response.headers()["content-type"],
@@ -218,7 +218,7 @@ mod tests {
assert_eq!(response.headers()["x-content-type-options"], "nosniff");
assert!(response
.extensions()
.get::<AuthenticationFailure>()
.get::<auth_provider::AuthenticationResponse>()
.is_some());
let body = response.into_body().collect().await.unwrap().to_bytes();
let html = std::str::from_utf8(&body).unwrap();