Skip to main content

alibi_plugins/helpers/
credentials.rs

1use alibi_core::entity::{AuthAccount, AuthUser};
2use alibi_core::{AuthContext, AuthResult};
3
4/// Fetch the user's credential account, if present.
5///
6/// # Errors
7///
8/// Propagates errors from credential-account storage.
9pub async fn get_credential_account<S: alibi_core::AuthSchema>(
10    ctx: &AuthContext<S>,
11    user_id: impl AsRef<str>,
12) -> AuthResult<Option<S::Account>> {
13    Ok(ctx
14        .database
15        .get_credential_account_record(user_id.as_ref())
16        .await?
17        .map(alibi_core::AdapterRecord::into_stored))
18}
19
20/// Resolve the user's stored password hash from the credential account.
21///
22/// # Errors
23///
24/// Propagates errors from credential-account storage.
25pub async fn get_credential_password_hash(
26    ctx: &AuthContext<impl alibi_core::AuthSchema>,
27    user: &impl AuthUser,
28) -> AuthResult<Option<String>> {
29    Ok(get_credential_account(ctx, user.id())
30        .await?
31        .and_then(|account| account.password().map(str::to_owned)))
32}
33
34/// Whether the user currently has a password set.
35///
36/// # Errors
37///
38/// Propagates errors from credential-account storage.
39pub async fn user_has_password(
40    ctx: &AuthContext<impl alibi_core::AuthSchema>,
41    user: &impl AuthUser,
42) -> AuthResult<bool> {
43    Ok(get_credential_password_hash(ctx, user).await?.is_some())
44}