soaprs-auth 0.5.0

Protocol-neutral authentication and authorization contracts for soaprs
Documentation
//! Typed asynchronous authentication ports.

use soaprs_core::{BoxFuture, SoapResult};

use crate::AuthorizationName;

/// Successful authentication through one named strategy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Authentication<P> {
    strategy: AuthorizationName,
    principal: P,
}

impl<P> Authentication<P> {
    /// Wraps an authenticated principal and validated strategy identity.
    pub fn new(strategy: impl Into<String>, principal: P) -> SoapResult<Self> {
        Ok(Self {
            strategy: AuthorizationName::new(strategy)?,
            principal,
        })
    }

    /// Returns the strategy that authenticated the principal.
    pub const fn strategy(&self) -> &AuthorizationName {
        &self.strategy
    }

    /// Returns the authenticated principal.
    pub const fn principal(&self) -> &P {
        &self.principal
    }

    /// Consumes the authentication into its principal.
    pub fn into_principal(self) -> P {
        self.principal
    }
}

/// Verifies one typed credential and returns an authenticated principal.
pub trait Authenticator<C, P>: Send + Sync
where
    C: Send,
    P: Send,
{
    /// Authenticates a presented credential.
    fn authenticate(&self, credential: C) -> BoxFuture<'_, SoapResult<Authentication<P>>>;
}