logins/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#![allow(unknown_lints)]
6#![warn(rust_2018_idioms)]
7
8#[macro_use]
9mod error;
10mod login;
11
12mod db;
13pub mod encryption;
14mod schema;
15mod store;
16mod sync;
17mod util;
18
19use crate::encryption::{
20    EncryptorDecryptor, KeyManager, ManagedEncryptorDecryptor, StaticKeyManager,
21};
22uniffi::include_scaffolding!("logins");
23
24#[cfg(feature = "keydb")]
25pub use crate::encryption::{NSSKeyManager, PrimaryPasswordAuthenticator};
26
27pub use crate::db::{LoginDb, LoginsDeletionMetrics};
28use crate::encryption::{check_canary, create_canary, create_key};
29pub use crate::error::*;
30pub use crate::login::*;
31pub use crate::store::*;
32pub use crate::sync::LoginsSyncEngine;
33use std::sync::Arc;
34
35// Utility function to create a StaticKeyManager to be used for the time being until support lands
36// for [trait implementation of an UniFFI
37// interface](https://mozilla.github.io/uniffi-rs/next/proc_macro/index.html#structs-implementing-traits)
38// in UniFFI.
39pub fn create_static_key_manager(key: String) -> Arc<StaticKeyManager> {
40    Arc::new(StaticKeyManager::new(key))
41}
42
43// Similar to create_static_key_manager above, create a
44// ManagedEncryptorDecryptor by passing in a KeyManager
45pub fn create_managed_encdec(key_manager: Arc<dyn KeyManager>) -> Arc<ManagedEncryptorDecryptor> {
46    Arc::new(ManagedEncryptorDecryptor::new(key_manager))
47}
48
49// Create a LoginStore by passing in a db path and a static key
50//
51// Note this is only temporarily needed until a bug with UniFFI and JavaScript is fixed, which
52// prevents passing around traits in JS
53pub fn create_login_store_with_static_key_manager(path: String, key: String) -> Arc<LoginStore> {
54    let encdec: ManagedEncryptorDecryptor =
55        ManagedEncryptorDecryptor::new(Arc::new(StaticKeyManager::new(key)));
56    let store = LoginStore::new(path, Arc::new(encdec)).expect("error setting up LoginStore");
57    Arc::new(store)
58}
59
60// Create a LoginStore with NSSKeyManager by passing in a db path and a PrimaryPasswordAuthenticator.
61//
62// Note this is only temporarily needed until a bug with UniFFI and JavaScript is fixed, which
63// prevents passing around traits in JS
64#[cfg(feature = "keydb")]
65#[uniffi::export]
66pub fn create_login_store_with_nss_keymanager(
67    path: String,
68    primary_password_authenticator: Arc<dyn PrimaryPasswordAuthenticator>,
69) -> ApiResult<Arc<LoginStore>> {
70    let encdec: ManagedEncryptorDecryptor = ManagedEncryptorDecryptor::new(Arc::new(
71        NSSKeyManager::new(primary_password_authenticator),
72    ));
73    let store = LoginStore::new(path, Arc::new(encdec))?;
74    Ok(Arc::new(store))
75}