Skip to main content

snap_control/pg_wap/
auth.rs

1// Copyright 2026 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! PathGuard WAP authentication service.
15
16use std::{net::IpAddr, time::Instant};
17
18/// Information about an authenticated IP address.
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20pub struct AuthInfo {
21    /// The authenticated IP address.
22    pub ip: IpAddr,
23    /// The time until which the authentication is valid. After this time, the client needs to
24    /// reauthenticate.
25    pub valid_until: Instant,
26    /// The target domains a client is allowed to connect to.
27    pub targets: Vec<String>,
28}
29
30#[derive(Debug, Clone)]
31pub struct AuthService {
32    auth_duration: std::time::Duration,
33}
34
35impl AuthService {
36    pub fn new(auth_duration: std::time::Duration) -> Self {
37        Self { auth_duration }
38    }
39
40    pub fn authenticate(&self, now: Instant, ip: IpAddr, targets: &[&str]) -> AuthInfo {
41        let valid_until = now + self.auth_duration;
42        AuthInfo {
43            ip,
44            targets: targets.iter().map(|s| s.to_string()).collect(),
45            valid_until,
46        }
47    }
48}