This commit is contained in:
Mateusz Gruszczyński committed 2026-10-27 13:22:43 +01:00
1 parent e364392659
commit f27fbf6a30
28 files changed
+2000 -60

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
use anyhow::{ensure, Result};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IdentityProvider {
Basic,
Tinyauth,
Authelia,
Authentik,
PocketId,
Custom,
}
impl IdentityProvider {
pub fn id(self) -> &'static str {
match self {
Self::Basic => "basic",
Self::Tinyauth => "tinyauth",
Self::Authelia => "authelia",
Self::Authentik => "authentik",
Self::PocketId => "pocket_id",
Self::Custom => "custom",
}
}
pub fn display_name(self) -> &'static str {
match self {
Self::Basic => "HTTP Basic Auth",
Self::Tinyauth => "Tinyauth",
Self::Authelia => "Authelia",
Self::Authentik => "authentik",
Self::PocketId => "Pocket ID",
Self::Custom => "OIDC",
}
}
pub fn supports(self, method: IntegrationMethod) -> bool {
match self {
Self::Basic => method == IntegrationMethod::Basic,
Self::Tinyauth | Self::Authelia | Self::Authentik => method != IntegrationMethod::Basic,
Self::PocketId | Self::Custom => method == IntegrationMethod::Oidc,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IntegrationMethod {
Basic,
ForwardAuth,
Oidc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Choice {
pub provider: IdentityProvider,
pub method: IntegrationMethod,
}
impl Choice {
pub fn validate(self) -> Result<()> {
ensure!(
self.provider.supports(self.method),
"unsupported authentication provider and integration method combination"
);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn supported_methods_are_explicit_and_reject_invalid_pairs() {
for provider in [
IdentityProvider::Tinyauth,
IdentityProvider::Authelia,
IdentityProvider::Authentik,
] {
assert!(provider.supports(IntegrationMethod::ForwardAuth));
assert!(provider.supports(IntegrationMethod::Oidc));
}
for provider in [IdentityProvider::PocketId, IdentityProvider::Custom] {
assert!(!provider.supports(IntegrationMethod::ForwardAuth));
assert!(provider.supports(IntegrationMethod::Oidc));
}
assert!(IdentityProvider::Basic.supports(IntegrationMethod::Basic));
assert!(!IdentityProvider::Basic.supports(IntegrationMethod::Oidc));
assert!(!IdentityProvider::Authelia.supports(IntegrationMethod::Basic));
}
#[test]
fn selection_serializes_as_independent_provider_and_method() {
let selection = Choice {
provider: IdentityProvider::Authelia,
method: IntegrationMethod::Oidc,
};
let json = serde_json::to_value(selection).unwrap();
assert_eq!(
json,
serde_json::json!({"provider":"authelia","method":"oidc"})
);
assert_eq!(serde_json::from_value::<Choice>(json).unwrap(), selection);
assert!(serde_json::from_value::<Choice>(
serde_json::json!({"provider":"pocket_id","method":"forward_auth"})
)
.unwrap()
.validate()
.is_err());
assert!(serde_json::from_value::<Choice>(
serde_json::json!({"provider":"unknown","method":"oidc"})
)
.is_err());
}
}
+158 -2
View File
@@ -1,4 +1,4 @@
use crate::{authelia, authentik, config::ValidationPolicy, forwarding, tinyauth};
use crate::{authelia, authentik, config::ValidationPolicy, forwarding, oidc, tinyauth};
use anyhow::{ensure, Result};
use axum::{body::Body, response::Response};
use http::{header, HeaderMap, HeaderValue, Method, Request};
@@ -14,7 +14,9 @@ pub type Authorization<'a> =
Pin<Box<dyn Future<Output = std::result::Result<Identity, Response>> + Send + 'a>>;
pub type Route<'a> = Pin<Box<dyn Future<Output = Response> + Send + 'a>>;
mod choice;
mod errors;
pub use choice::{Choice, IdentityProvider, IntegrationMethod};
pub use errors::{deny, gateway_failure, GatewayFailure};
#[derive(Debug, Default)]
@@ -34,6 +36,9 @@ impl Identity {
pub trait Provider: Send + Sync + std::fmt::Debug {
fn id(&self) -> &'static str;
fn method(&self) -> IntegrationMethod {
IntegrationMethod::ForwardAuth
}
fn strip_identity(&self, headers: &mut HeaderMap);
fn authorize<'a>(
&'a self,
@@ -66,6 +71,9 @@ impl Compiled {
pub fn id(&self) -> &'static str {
self.0.id()
}
pub fn method(&self) -> IntegrationMethod {
self.0.method()
}
pub fn strip_identity(&self, headers: &mut HeaderMap) {
self.0.strip_identity(headers);
}
@@ -95,26 +103,49 @@ impl Compiled {
}
pub struct Selection<'a> {
pub choice: Option<Choice>,
pub basic_enabled: bool,
pub tinyauth: &'a tinyauth::Settings,
pub authelia: &'a authelia::Settings,
pub authentik: &'a authentik::Settings,
pub oidc: &'a oidc::Settings,
}
impl Selection<'_> {
pub fn validate(&self) -> Result<()> {
self.tinyauth.validate()?;
self.authelia.validate()?;
self.authentik.validate()?;
self.oidc.validate()?;
let active = [
self.basic_enabled,
self.tinyauth.enabled,
self.authelia.enabled,
self.authentik.enabled,
self.oidc.enabled,
];
ensure!(
active.iter().filter(|enabled| **enabled).count() <= 1,
"choose only one authentication provider (Basic, Tinyauth, Authelia or authentik)"
"choose only one authentication method per Vhost"
);
if let Some(choice) = self.choice {
choice.validate()?;
let matches_configuration = match choice.method {
IntegrationMethod::Basic => {
self.basic_enabled && choice.provider == IdentityProvider::Basic
}
IntegrationMethod::ForwardAuth => match choice.provider {
IdentityProvider::Tinyauth => self.tinyauth.enabled,
IdentityProvider::Authelia => self.authelia.enabled,
IdentityProvider::Authentik => self.authentik.enabled,
_ => false,
},
IntegrationMethod::Oidc => self.oidc.enabled,
};
ensure!(
matches_configuration,
"authentication selection does not match the enabled settings"
);
}
Ok(())
}
pub fn compile(
@@ -132,6 +163,15 @@ impl Selection<'_> {
if self.authentik.enabled {
return Ok(self.authentik.compile(policy, bindings)?.map(Compiled::new));
}
if self.oidc.enabled {
let (id, label) = self.choice.map_or(("oidc", "OIDC"), |choice| {
(choice.provider.id(), choice.provider.display_name())
});
return Ok(self
.oidc
.compile(policy, bindings)?
.map(|provider| Compiled::new(provider.with_provider(id, label))));
}
Ok(None)
}
}
@@ -184,10 +224,12 @@ mod tests {
}))
.unwrap();
let selection = Selection {
choice: host.authentication,
basic_enabled: host.basic_auth.enabled,
tinyauth: &host.tinyauth,
authelia: &host.authelia,
authentik: &host.authentik,
oidc: &host.oidc,
};
assert!(selection.validate().is_ok());
}
@@ -196,16 +238,129 @@ mod tests {
let mut tinyauth = tinyauth::Settings::default();
let mut authelia = authelia::Settings::default();
let authentik = authentik::Settings::default();
let oidc = oidc::Settings::default();
tinyauth.enabled = true;
authelia.enabled = true;
let selection = Selection {
choice: None,
basic_enabled: false,
tinyauth: &tinyauth,
authelia: &authelia,
authentik: &authentik,
oidc: &oidc,
};
assert!(selection.validate().is_err());
}
#[test]
fn oidc_is_exclusive_and_missing_legacy_values_remain_disabled() {
let mut tinyauth = tinyauth::Settings::default();
tinyauth.enabled = true;
tinyauth.url = "http://127.0.0.1:3000".into();
tinyauth.login_url = "https://auth.example.net".into();
let oidc = oidc::Settings {
enabled: true,
issuer_url: "https://auth.test.com".into(),
backend_url: "http://127.0.0.1:9091".into(),
client_id: "test-client".into(),
client_secret: "test-secret".into(),
redirect_url: "https://app.example.com/_rustproxy/oidc/callback".into(),
session_secret: "long-session-secret-at-least-32-characters".into(),
..oidc::Settings::default()
};
assert!(Selection {
choice: None,
basic_enabled: false,
tinyauth: &tinyauth,
authelia: &authelia::Settings::default(),
authentik: &authentik::Settings::default(),
oidc: &oidc,
}
.validate()
.is_err());
}
#[test]
fn oidc_identity_provider_is_independent_of_integration_method() {
let mut oidc = oidc::Settings::default();
oidc.enabled = true;
oidc.issuer_url = "https://auth.test.com".into();
oidc.client_id = "rustproxy-test".into();
oidc.client_secret = "test-client-secret".into();
oidc.session_secret = "a-long-random-value-for-testing-oidc-sessions".into();
oidc.redirect_url = "https://app.example.com/_rustproxy/oidc/callback".into();
for provider in [
IdentityProvider::Tinyauth,
IdentityProvider::Authelia,
IdentityProvider::Authentik,
IdentityProvider::PocketId,
IdentityProvider::Custom,
] {
assert!(Selection {
choice: Some(Choice {
provider,
method: IntegrationMethod::Oidc
}),
basic_enabled: false,
tinyauth: &tinyauth::Settings::default(),
authelia: &authelia::Settings::default(),
authentik: &authentik::Settings::default(),
oidc: &oidc,
}
.validate()
.is_ok());
}
assert!(Selection {
choice: Some(Choice {
provider: IdentityProvider::Authelia,
method: IntegrationMethod::ForwardAuth
}),
basic_enabled: false,
tinyauth: &tinyauth::Settings::default(),
authelia: &authelia::Settings::default(),
authentik: &authentik::Settings::default(),
oidc: &oidc,
}
.validate()
.is_err());
}
#[test]
fn the_new_selection_cannot_silently_disable_or_switch_authentication() {
let mut tinyauth = tinyauth::Settings::default();
tinyauth.enabled = true;
tinyauth.url = "http://10.0.0.2:3000".into();
tinyauth.login_url = "https://auth.example.com".into();
let authelia = authelia::Settings::default();
let authentik = authentik::Settings::default();
let oidc = oidc::Settings::default();
let selection = |choice| Selection {
choice,
basic_enabled: false,
tinyauth: &tinyauth,
authelia: &authelia,
authentik: &authentik,
oidc: &oidc,
};
assert!(selection(None).validate().is_ok());
assert!(selection(Some(Choice {
provider: IdentityProvider::Tinyauth,
method: IntegrationMethod::ForwardAuth
}))
.validate()
.is_ok());
assert!(selection(Some(Choice {
provider: IdentityProvider::Authelia,
method: IntegrationMethod::ForwardAuth
}))
.validate()
.is_err());
assert!(selection(Some(Choice {
provider: IdentityProvider::Tinyauth,
method: IntegrationMethod::Oidc
}))
.validate()
.is_err());
}
#[derive(Debug)]
struct FutureProvider;
impl Provider for FutureProvider {
@@ -228,6 +383,7 @@ mod tests {
async fn future_providers_can_use_runtime_interface_without_proxy_branches() {
let provider = Compiled::new(FutureProvider);
assert_eq!(provider.id(), "future");
assert_eq!(provider.method(), IntegrationMethod::ForwardAuth);
assert!(!provider.reserved_path("/"));
let headers = HeaderMap::new();
let response = provider