Skip to main content

soaprs_auth/
authentication.rs

1//! Typed asynchronous authentication ports.
2
3use soaprs_core::{BoxFuture, SoapResult};
4
5use crate::AuthorizationName;
6
7/// Successful authentication through one named strategy.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Authentication<P> {
10    strategy: AuthorizationName,
11    principal: P,
12}
13
14impl<P> Authentication<P> {
15    /// Wraps an authenticated principal and validated strategy identity.
16    pub fn new(strategy: impl Into<String>, principal: P) -> SoapResult<Self> {
17        Ok(Self {
18            strategy: AuthorizationName::new(strategy)?,
19            principal,
20        })
21    }
22
23    /// Returns the strategy that authenticated the principal.
24    pub const fn strategy(&self) -> &AuthorizationName {
25        &self.strategy
26    }
27
28    /// Returns the authenticated principal.
29    pub const fn principal(&self) -> &P {
30        &self.principal
31    }
32
33    /// Consumes the authentication into its principal.
34    pub fn into_principal(self) -> P {
35        self.principal
36    }
37}
38
39/// Verifies one typed credential and returns an authenticated principal.
40pub trait Authenticator<C, P>: Send + Sync
41where
42    C: Send,
43    P: Send,
44{
45    /// Authenticates a presented credential.
46    fn authenticate(&self, credential: C) -> BoxFuture<'_, SoapResult<Authentication<P>>>;
47}