sos_platform_authenticator/
lib.rs

1//! Platform authenticator and keyring support for the
2//! [Save Our Secrets](https://saveoursecrets.com) SDK.
3#![deny(missing_docs)]
4#![forbid(unsafe_code)]
5#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))]
6use secrecy::SecretString;
7
8mod error;
9
10pub mod keyring_password;
11pub mod local_auth;
12
13pub use error::Error;
14
15/// Result type for the library.
16pub(crate) type Result<T> = std::result::Result<T, Error>;
17
18/// Attempt to find authentication credentials for an account.
19///
20/// First verify user presence via the platform
21/// authenticator and then attempt to retrieve the account
22/// password from the platform keyring.
23pub async fn find_account_credential(
24    account_id: &str,
25) -> Result<SecretString> {
26    if local_auth::supported() {
27        if local_auth::authenticate(Default::default()) {
28            if keyring_password::supported() {
29                match keyring_password::find_account_password(account_id) {
30                    Ok(password) => Ok(SecretString::new(password.into())),
31                    Err(e) => Err(e),
32                }
33            } else {
34                Err(Error::Unauthorized)
35            }
36        } else {
37            Err(Error::Forbidden)
38        }
39    } else {
40        Err(Error::Unauthorized)
41    }
42}