#![cfg(native)]
use super::EndpointMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthProtection {
Protected,
Optional,
Public,
None,
}
impl AuthProtection {
pub fn is_violation(&self) -> bool {
matches!(self, AuthProtection::None)
}
}
pub fn validate_endpoint_security() {
for metadata in inventory::iter::<EndpointMetadata>() {
if metadata.auth_protection.is_violation() {
panic!(
"Endpoint security violation: {} {} (fn {}) has no auth protection. \
Use `guard!()` macro or add an auth parameter to the handler.",
metadata.method, metadata.path, metadata.function_name,
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case::protected(AuthProtection::Protected, false)]
#[case::optional(AuthProtection::Optional, false)]
#[case::public(AuthProtection::Public, false)]
#[case::none(AuthProtection::None, true)]
fn test_is_violation(#[case] protection: AuthProtection, #[case] expected: bool) {
let result = protection.is_violation();
assert_eq!(result, expected);
}
}