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 traits;
45pub mod types;
46pub mod vendor;
47
48// Optional modules based on features
49// TODO: authn module needs refactoring - disabled for now
50// #[cfg(feature = "authentication")]
51// pub mod authn;
52
53#[cfg(feature = "authentication")]
54pub(crate) mod provider;
55
56#[cfg(feature = "axum")]
57pub mod middleware;
58
59#[cfg(feature = "authorization")]
60pub mod authz;
61
62// Re-export commonly used types
63pub use error::{AuthError, AuthResult};
64pub use traits::ClientAccess;
65pub use types::{AuthProvider, AuthToken, TokenType, UserContext};
66
67#[cfg(feature = "authentication")]
68pub use provider::Config;
69
70#[cfg(feature = "authentication")]
71pub use traits::Authn as AuthProviderTrait;
72
73#[cfg(feature = "clerk")]
74pub use provider::ClerkProvider;
75
76#[cfg(feature = "stytch")]
77pub use provider::StytchProvider;
78
79#[cfg(feature = "msal")]
80pub use provider::MsalProvider;
81
82#[cfg(feature = "axum")]
83pub use middleware::{AuthContext, AuthExtension, AuthLayer, AuthMiddleware, OptionalAuth};
84
85#[cfg(feature = "authorization")]
86pub use authz::{AuthorizationProvider, AuthorizationRouterBuilder};
87
88// TODO: SCIM module not yet implemented
89// #[cfg(feature = "scim")]
90// pub use scim::{
91//     ScimAuthMethod, ScimConfig, ScimError, ScimGroup, ScimListResponse, ScimPatchOp, ScimProvider,
92//     ScimServer, ScimServerBuilder, ScimUser,
93// };
94
95/// Library version
96pub const VERSION: &str = env!("CARGO_PKG_VERSION");
97
98/// Prelude module for convenient imports
99pub mod prelude {
100    pub use crate::error::{AuthError, AuthResult};
101    pub use crate::traits::ClientAccess;
102    pub use crate::types::{AuthProvider, AuthToken, TokenType, UserContext};
103
104    #[cfg(feature = "authentication")]
105    pub use crate::provider::Config;
106
107    #[cfg(feature = "authentication")]
108    pub use crate::traits::Authn as AuthProviderTrait;
109
110    #[cfg(feature = "clerk")]
111    pub use crate::provider::ClerkProvider;
112
113    #[cfg(feature = "stytch")]
114    pub use crate::provider::StytchProvider;
115
116    #[cfg(feature = "msal")]
117    pub use crate::provider::MsalProvider;
118
119    #[cfg(feature = "axum")]
120    pub use crate::middleware::{AuthContext, AuthExtension, AuthLayer, OptionalAuth};
121
122    #[cfg(feature = "authorization")]
123    pub use crate::authz::{AuthorizationProvider, AuthorizationRouterBuilder};
124
125    // TODO: SCIM module not yet implemented
126    // #[cfg(feature = "scim")]
127    // pub use crate::scim::{
128    //     ScimAuthMethod, ScimConfig, ScimGroup, ScimProvider, ScimServer, ScimServerBuilder,
129    //     ScimUser,
130    // };
131}