libauth_rs/
traits.rs

1use crate::error::AuthResult;
2use crate::types::{Token, UserContext};
3
4/// Main authentication trait that combines all authentication capabilities
5/// This trait is object-safe and can be used with `Box<dyn Authn>`
6#[async_trait::async_trait]
7pub trait Authn: Authorizer + Authenticator + Session + Send + Sync {}
8
9/// Trait for accessing the provider's native client
10/// This is a separate trait to allow generic client types while keeping Authn object-safe
11pub trait ClientAccess {
12    type Client;
13
14    /// Get the client used by this provider
15    ///
16    /// Returns the provider's native client.
17    /// For Stytch providers, this returns the Stytch SDK client.
18    /// For other providers (Clerk, MSAL), this returns a reqwest::Client.
19    fn client(&self) -> Self::Client;
20}
21
22#[async_trait::async_trait]
23pub trait Authorizer: Send + Sync {
24    async fn authorize(&self, user: &UserContext, action: &str) -> AuthResult<()>;
25}
26
27#[async_trait::async_trait]
28pub trait Authenticator: Send + Sync {
29    async fn authenticate(&self, token: &Token) -> AuthResult<UserContext>;
30}
31
32#[async_trait::async_trait]
33pub trait Session: Send + Sync {
34    async fn validate(&self, token: &Token) -> AuthResult<UserContext>;
35}