libauth_rs/
lib.rs

1//! # libauth-rs
2//!
3//! A unified authentication and authorization library with support for multiple providers:
4//! - Clerk (feature: `clerk`)
5//! - Stytch (feature: `stytch`)
6//! - Microsoft Azure AD / MSAL (feature: `msal`)
7//!
8//! ## Features
9//!
10//! - **Feature-gated providers**: Only include the authentication providers you need
11//! - **Axum middleware**: Drop-in authentication middleware for Axum web applications
12//! - **Type-safe**: Strongly typed authentication and authorization primitives
13//! - **Async-first**: Built on async/await for high performance
14//!
15//! ## Quick Start
16//!
17//! ```toml
18//! [dependencies]
19//! libauth-rs = { version = "0.1", features = ["clerk", "axum"] }
20//! ```
21//!
22//! ```rust,ignore
23//! use libauth_rs::providers::{AuthConfig, ClerkProvider};
24//! use libauth_rs::middleware::AuthLayer;
25//! use axum::{Router, routing::get};
26//!
27//! #[tokio::main]
28//! async fn main() {
29//!     let config = AuthConfig::default();
30//!     let clerk = ClerkProvider::new(&config).await.unwrap();
31//!
32//!     let auth_layer = AuthLayer::new(vec![Box::new(clerk)]);
33//!
34//!     let app = Router::new()
35//!         .route("/", get(|| async { "Hello, World!" }))
36//!         .layer(auth_layer);
37//!
38//!     // Run your server...
39//! }
40//! ```
41
42// Core modules (always available)
43pub mod error;
44pub mod types;
45pub mod vendor;
46
47// Optional modules based on features
48// TODO: authn module needs refactoring - disabled for now
49// #[cfg(feature = "authentication")]
50// pub mod authn;
51
52#[cfg(feature = "authentication")]
53pub mod providers;
54
55#[cfg(feature = "axum")]
56pub mod middleware;
57
58#[cfg(feature = "authorization")]
59pub mod authz;
60
61// Re-export commonly used types
62pub use error::{AuthError, AuthResult};
63pub use types::{AuthProvider, AuthToken, TokenType, UserContext};
64
65#[cfg(feature = "authentication")]
66pub use providers::{AuthConfig, AuthProvider as AuthProviderTrait};
67
68#[cfg(feature = "clerk")]
69pub use providers::ClerkProvider;
70
71#[cfg(feature = "stytch")]
72pub use providers::StytchProvider;
73
74#[cfg(feature = "msal")]
75pub use providers::MsalProvider;
76
77#[cfg(feature = "axum")]
78pub use middleware::{AuthContext, AuthExtension, AuthLayer, AuthMiddleware, OptionalAuth};
79
80#[cfg(feature = "authorization")]
81pub use authz::{AuthorizationProvider, AuthorizationRouterBuilder};
82
83#[cfg(feature = "scim")]
84pub use scim::{
85    ScimAuthMethod, ScimConfig, ScimError, ScimGroup, ScimListResponse, ScimPatchOp, ScimProvider,
86    ScimServer, ScimServerBuilder, ScimUser,
87};
88
89/// Library version
90pub const VERSION: &str = env!("CARGO_PKG_VERSION");
91
92/// Prelude module for convenient imports
93pub mod prelude {
94    pub use crate::error::{AuthError, AuthResult};
95    pub use crate::types::{AuthProvider, AuthToken, TokenType, UserContext};
96
97    #[cfg(feature = "authentication")]
98    pub use crate::providers::{AuthConfig, AuthProvider as AuthProviderTrait};
99
100    #[cfg(feature = "clerk")]
101    pub use crate::providers::ClerkProvider;
102
103    #[cfg(feature = "stytch")]
104    pub use crate::providers::StytchProvider;
105
106    #[cfg(feature = "msal")]
107    pub use crate::providers::MsalProvider;
108
109    #[cfg(feature = "axum")]
110    pub use crate::middleware::{AuthContext, AuthExtension, AuthLayer, OptionalAuth};
111
112    #[cfg(feature = "authorization")]
113    pub use crate::authz::{AuthorizationProvider, AuthorizationRouterBuilder};
114
115    #[cfg(feature = "scim")]
116    pub use crate::scim::{
117        ScimAuthMethod, ScimConfig, ScimGroup, ScimProvider, ScimServer, ScimServerBuilder,
118        ScimUser,
119    };
120}