Skip to main content

alibi_api/plugins/helpers/
credentials.rs

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