pub struct UserManager { /* private fields */ }Expand description
User manager for handling user operations.
§Example
use auth_framework::auth_modular::UserManager;
use auth_framework::config::PasswordHashAlgorithm;
let um = UserManager::new(storage.clone(), PasswordHashAlgorithm::Argon2);
let uid = um.register_user("alice", "alice@example.com", "Str0ng!Pass").await?;Implementations§
Source§impl UserManager
impl UserManager
Sourcepub fn new(
storage: Arc<dyn AuthStorage>,
password_hash_algorithm: PasswordHashAlgorithm,
) -> Self
pub fn new( storage: Arc<dyn AuthStorage>, password_hash_algorithm: PasswordHashAlgorithm, ) -> Self
Create a new user manager that hashes new/changed passwords with
password_hash_algorithm. Verification is unaffected by this
setting – it detects the stored hash’s own format, so existing
users keep verifying correctly across an algorithm change.
Computes a dummy password hash under password_hash_algorithm once,
synchronously, before returning – this blocks the calling thread
for roughly the same time as one real hash operation (measured:
~39ms for Argon2id with this crate’s parameters, ~366ms for
bcrypt’s default cost; Scrypt returns immediately since it has
no implementation and the dummy hash is simply absent in that
case). This is a one-time, per-instance cost, not per-request.
§Example
let um = UserManager::new(storage.clone(), PasswordHashAlgorithm::Argon2);Sourcepub async fn create_api_key(
&self,
user_id: &str,
expires_in: Option<Duration>,
) -> Result<String>
pub async fn create_api_key( &self, user_id: &str, expires_in: Option<Duration>, ) -> Result<String>
Sourcepub async fn validate_api_key(&self, api_key: &str) -> Result<UserInfo>
pub async fn validate_api_key(&self, api_key: &str) -> Result<UserInfo>
Sourcepub async fn revoke_api_key(&self, api_key: &str) -> Result<()>
pub async fn revoke_api_key(&self, api_key: &str) -> Result<()>
Sourcepub async fn validate_username(&self, username: &str) -> Result<bool>
pub async fn validate_username(&self, username: &str) -> Result<bool>
Sourcepub async fn validate_display_name(&self, display_name: &str) -> Result<bool>
pub async fn validate_display_name(&self, display_name: &str) -> Result<bool>
Sourcepub async fn validate_password_strength(&self, password: &str) -> Result<bool>
pub async fn validate_password_strength(&self, password: &str) -> Result<bool>
Sourcepub async fn validate_user_input(&self, input: &str) -> Result<bool>
pub async fn validate_user_input(&self, input: &str) -> Result<bool>
Validate user input for security.
Combines a character whitelist with pattern checks for common injection vectors. Rejects HTML tags, null bytes, path traversal sequences, template injection markers, and dangerous URI schemes.
§Example
assert!(um.validate_user_input("hello world").await?);
assert!(!um.validate_user_input("<script>alert(1)</script>").await?);Sourcepub async fn get_user_info(&self, user_id: &str) -> Result<UserInfo>
pub async fn get_user_info(&self, user_id: &str) -> Result<UserInfo>
Sourcepub async fn list_users(
&self,
limit: Option<usize>,
offset: Option<usize>,
active_only: bool,
) -> Result<Vec<UserInfo>>
pub async fn list_users( &self, limit: Option<usize>, offset: Option<usize>, active_only: bool, ) -> Result<Vec<UserInfo>>
Sourcepub async fn user_exists(&self, user_id: &str) -> Result<bool>
pub async fn user_exists(&self, user_id: &str) -> Result<bool>
Sourcepub async fn register_user(
&self,
username: &str,
email: &str,
password: &str,
) -> Result<String>
pub async fn register_user( &self, username: &str, email: &str, password: &str, ) -> Result<String>
Sourcepub async fn delete_user(&self, username: &str) -> Result<()>
pub async fn delete_user(&self, username: &str) -> Result<()>
Sourcepub async fn delete_user_by_id(&self, user_id: &str) -> Result<()>
pub async fn delete_user_by_id(&self, user_id: &str) -> Result<()>
Sourcepub async fn verify_user_password(
&self,
user_id: &str,
password: &str,
) -> Result<bool>
pub async fn verify_user_password( &self, user_id: &str, password: &str, ) -> Result<bool>
Verify a user’s password against the stored hash.
Detects whether the stored hash is bcrypt or Argon2id from its own
prefix and verifies against that format, independent of the
currently configured PasswordHashAlgorithm. This is deliberate:
changing the configured algorithm must not break verification for
users whose password was hashed under the old one.
§Example
let ok = um.verify_user_password("user-1", "secret").await?;
assert!(ok);Sourcepub async fn get_username_by_id(&self, user_id: &str) -> Result<String>
pub async fn get_username_by_id(&self, user_id: &str) -> Result<String>
Sourcepub async fn username_exists(&self, username: &str) -> Result<bool>
pub async fn username_exists(&self, username: &str) -> Result<bool>
Sourcepub async fn email_exists(&self, email: &str) -> Result<bool>
pub async fn email_exists(&self, email: &str) -> Result<bool>
Sourcepub async fn get_user_profile(&self, user_id: &str) -> Result<ProviderProfile>
pub async fn get_user_profile(&self, user_id: &str) -> Result<ProviderProfile>
Sourcepub async fn verify_login_credentials(
&self,
username: &str,
password: &str,
) -> Result<Option<CredentialCheckResult>>
pub async fn verify_login_credentials( &self, username: &str, password: &str, ) -> Result<Option<CredentialCheckResult>>
Verify username/password credentials in a timing-safe manner.
Returns Ok(None) when the credentials are invalid — a dummy
verification (under the configured algorithm, against a hash
precomputed once at construction) is always executed when the
username is not found, so callers cannot distinguish missing users
from wrong passwords via timing for a manager whose stored users
all hash under the currently configured algorithm. Returns
Ok(Some(result)) on success.
Known gap: mid-migration timing oracle. The dummy hash always follows the configured algorithm, while a real existing user verifies under whatever algorithm their own stored hash actually is. If a deployment changes its configured algorithm without rehashing existing users (this crate never rehashes on login), a legacy user’s wrong-password check costs their old algorithm’s verify time while an unknown username costs the new one’s – e.g. a Bcrypt-configured manager with legacy Argon2id users shows ~39ms for a real wrong-password check vs. ~390ms for an unknown username (and the reverse, Argon2-configured with legacy Bcrypt users). This is not fixed by this crate and is not fixable without rehashing on login (a separate, larger change): the oracle this function closes is closed only once every stored user’s hash matches the currently configured algorithm.
§Example
match um.verify_login_credentials("alice", "password123").await? {
Some(cred) => println!("user_id={}, mfa={}", cred.user_id, cred.mfa_enabled),
None => println!("invalid credentials"),
}Auto Trait Implementations§
impl !RefUnwindSafe for UserManager
impl !UnwindSafe for UserManager
impl Freeze for UserManager
impl Send for UserManager
impl Sync for UserManager
impl Unpin for UserManager
impl UnsafeUnpin for UserManager
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more