mssql_auth/lib.rs
1//! # mssql-auth
2//!
3//! Authentication strategies for SQL Server connections.
4//!
5//! This crate provides various authentication methods, isolated from
6//! connection logic for better modularity and testing.
7//!
8//! ## Supported Authentication Methods
9//!
10//! | Method | Feature Flag | Status | Description |
11//! |--------|--------------|--------|-------------|
12//! | SQL Authentication | default | ✅ Implemented | Username/password |
13//! | Azure AD Token | default | ✅ Implemented | Pre-obtained access token |
14//! | Azure Managed Identity | `azure-identity` | ✅ Implemented | VM/container identity |
15//! | Service Principal | `azure-identity` | ✅ Implemented | App credentials |
16//! | Integrated (Kerberos) | `integrated-auth` | ✅ Implemented | GSSAPI/Kerberos (Linux/macOS) |
17//! | Windows SSPI | `sspi-auth` | ✅ Implemented | Native Windows SSPI |
18//! | Certificate | `cert-auth` | ✅ Implemented | Entra service principal w/ X.509 cert |
19//! | Default chain | `azure-identity` | ✅ Implemented | Managed identity → `az`/`azd` CLI session |
20//!
21//! `CertificateAuth` acquires a token from Microsoft Entra using an X.509
22//! client certificate; `mssql-client` wires `Credentials::Certificate` through
23//! the FEDAUTH SecurityToken login. This authenticates to Entra — it is NOT
24//! TDS-level mutual TLS (SQL Server does not accept client certificates at the
25//! protocol level).
26//!
27//! The Azure AD methods use the FEDAUTH SecurityToken workflow: the token is
28//! acquired client-side and sent in the LOGIN7 FEDAUTH feature extension
29//! (see [`azure_ad::build_security_token_feature_data`]). The interactive
30//! Entra flows (`ActiveDirectoryPassword`/`Interactive`/`DeviceCodeFlow`) are
31//! not built in — `azure_identity` ships no such credentials; acquire the
32//! token yourself and pass it as a pre-acquired access token.
33//!
34//! ## Authentication Tiers
35//!
36//! Per ARCHITECTURE.md, authentication is tiered:
37//!
38//! ### Tier 1 (Core - Pure Rust, Default)
39//!
40//! - [`SqlServerAuth`] - Username/password via Login7 ✅ Implemented
41//! - [`AzureAdAuth`] - Pre-acquired access token ✅ Implemented
42//!
43//! ### Tier 2 (Azure Native - `azure-identity` feature) ✅ Implemented
44//!
45//! - `ManagedIdentityAuth` - Azure VM/Container identity
46//! - `ServicePrincipalAuth` - Client ID + Secret
47//! - `DefaultAzureAuth` - default chain (managed identity → `az`/`azd` CLI session)
48//!
49//! ### Tier 3 (Enterprise - `integrated-auth` or `sspi-auth` feature) ✅ Implemented
50//!
51//! - `IntegratedAuth` - Kerberos (Linux/macOS via GSSAPI)
52//! - `SspiAuth` - Windows SSPI (native Windows, cross-platform via sspi-rs)
53//!
54//! ### Tier 4 (Certificate - `cert-auth` feature) ✅ Implemented
55//!
56//! - `CertificateAuth` - Entra service principal authentication with an X.509
57//! certificate (authenticates to Entra, not TDS-level mTLS)
58//!
59//! ## Secure Credential Handling
60//!
61//! Enable the `zeroize` feature for secure credential handling:
62//!
63//! ```toml
64//! mssql-auth = { version = "0.1", features = ["zeroize"] }
65//! ```
66//!
67//! This enables secure credential handling that automatically zeroes
68//! sensitive data from memory when dropped.
69//!
70//! ## Example
71//!
72//! ```rust
73//! use mssql_auth::{SqlServerAuth, AzureAdAuth, AuthProvider};
74//!
75//! // SQL Server authentication
76//! let sql_auth = SqlServerAuth::new("sa", "Password123!");
77//! let auth_data = sql_auth.authenticate().unwrap();
78//!
79//! // Azure AD authentication with pre-acquired token
80//! let azure_auth = AzureAdAuth::with_token("eyJ0eXAi...");
81//! ```
82
83#![warn(missing_docs)]
84// Unsafe code is denied globally but allowed in the Windows CNG FFI module.
85// See windows_certstore.rs for detailed SAFETY comments on each unsafe block.
86#![deny(unsafe_code)]
87
88pub mod azure_ad;
89#[cfg(feature = "azure-identity")]
90pub mod azure_identity_auth;
91#[cfg(feature = "cert-auth")]
92pub mod cert_auth;
93pub mod credentials;
94pub mod encryption;
95pub mod error;
96#[cfg(feature = "integrated-auth")]
97pub mod integrated_auth;
98#[cfg(all(windows, feature = "sspi-auth"))]
99#[allow(unsafe_code)] // Windows SSPI FFI; see SAFETY comments in each unsafe block
100pub mod native_sspi;
101#[cfg(any(feature = "integrated-auth", feature = "sspi-auth"))]
102pub mod negotiator;
103pub mod provider;
104pub mod sql_auth;
105#[cfg(feature = "sspi-auth")]
106pub mod sspi_auth;
107
108// Always Encrypted cryptography
109#[cfg(feature = "always-encrypted")]
110pub mod aead;
111#[cfg(feature = "always-encrypted")]
112pub mod cek_envelope;
113#[cfg(feature = "always-encrypted")]
114pub mod key_store;
115#[cfg(feature = "always-encrypted")]
116pub mod key_unwrap;
117
118// Always Encrypted key providers
119#[cfg(feature = "azure-keyvault")]
120pub mod azure_keyvault;
121#[cfg(all(windows, feature = "windows-certstore"))]
122#[allow(unsafe_code)] // Windows CNG FFI; see SAFETY comments in each unsafe block
123pub mod windows_certstore;
124
125// Core types
126pub use credentials::Credentials;
127pub use error::AuthError;
128pub use provider::{AsyncAuthProvider, AuthData, AuthMethod, AuthProvider};
129
130// Authentication providers
131pub use azure_ad::{AzureAdAuth, FedAuthLibrary};
132pub use sql_auth::SqlServerAuth;
133
134// Secure credential types (with zeroize feature)
135#[cfg(feature = "zeroize")]
136pub use credentials::{SecretString, SecureCredentials};
137
138// Azure Identity authentication (with azure-identity feature)
139#[cfg(feature = "azure-identity")]
140pub use azure_identity_auth::{DefaultAzureAuth, ManagedIdentityAuth, ServicePrincipalAuth};
141
142// Integrated authentication (Kerberos/GSSAPI - with integrated-auth feature)
143#[cfg(feature = "integrated-auth")]
144pub use integrated_auth::IntegratedAuth;
145
146// Certificate authentication (Azure AD with X.509 certificate - with cert-auth feature)
147#[cfg(feature = "cert-auth")]
148pub use cert_auth::CertificateAuth;
149
150// Native Windows SSPI authentication (with sspi-auth feature, Windows only)
151#[cfg(all(windows, feature = "sspi-auth"))]
152pub use native_sspi::NativeSspiAuth;
153
154// Windows SSPI authentication via sspi-rs (with sspi-auth feature)
155#[cfg(feature = "sspi-auth")]
156pub use sspi_auth::SspiAuth;
157
158// SSPI/GSSAPI negotiator trait (with integrated-auth or sspi-auth feature)
159#[cfg(any(feature = "integrated-auth", feature = "sspi-auth"))]
160pub use negotiator::SspiNegotiator;
161
162// Always Encrypted infrastructure
163pub use encryption::{
164 CekMetadata, ColumnEncryptionConfig, ColumnEncryptionInfo, EncryptedValue, EncryptionError,
165 EncryptionType, KeyStoreProvider,
166};
167
168// Always Encrypted cryptography (with always-encrypted feature)
169#[cfg(feature = "always-encrypted")]
170pub use aead::AeadEncryptor;
171#[cfg(feature = "always-encrypted")]
172pub use key_store::{CekCache, CekCacheKey, InMemoryKeyStore};
173#[cfg(feature = "always-encrypted")]
174pub use key_unwrap::RsaKeyUnwrapper;
175
176// Always Encrypted key providers
177#[cfg(feature = "azure-keyvault")]
178pub use azure_keyvault::AzureKeyVaultProvider;
179#[cfg(all(windows, feature = "windows-certstore"))]
180pub use windows_certstore::WindowsCertStoreProvider;