Skip to main content

soaprs_http/
policy.rs

1//! Framework-independent endpoint policies.
2
3use std::{num::NonZeroU32, time::Duration};
4
5use soaprs_core::{SoapError, SoapResult};
6
7/// Declarative authorization requirement for an endpoint.
8#[derive(Debug, Clone, PartialEq, Eq, Default)]
9pub enum AuthorizationPolicy {
10    /// No authenticated identity is required.
11    #[default]
12    Public,
13    /// Any authenticated identity is allowed.
14    Authenticated,
15    /// The identity must have at least one listed role.
16    AnyRole(Vec<String>),
17    /// The identity must have every listed role.
18    AllRoles(Vec<String>),
19}
20
21/// Declarative per-key rate limit translated by an HTTP adapter.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct RateLimitPolicy {
24    /// Maximum number of requests in the period.
25    pub requests: NonZeroU32,
26    /// Length of the rate-limit period.
27    pub period: Duration,
28}
29
30impl RateLimitPolicy {
31    /// Creates a rate-limit policy with a non-zero period.
32    pub fn new(requests: NonZeroU32, period: Duration) -> SoapResult<Self> {
33        if period.is_zero() {
34            return Err(SoapError::validation(
35                "rate-limit period must be greater than zero",
36            ));
37        }
38        Ok(Self { requests, period })
39    }
40}