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    Arc::new(LoginStore::new(path, Arc::new(encdec)).unwrap())
57}
58
59// Create a LoginStore with NSSKeyManager by passing in a db path and a PrimaryPasswordAuthenticator.
60//
61// Note this is only temporarily needed until a bug with UniFFI and JavaScript is fixed, which
62// prevents passing around traits in JS
63#[cfg(feature = "keydb")]
64#[uniffi::export]
65pub fn create_login_store_with_nss_keymanager(
66    path: String,
67    primary_password_authenticator: Arc<dyn PrimaryPasswordAuthenticator>,
68) -> Arc<LoginStore> {
69    let encdec: ManagedEncryptorDecryptor = ManagedEncryptorDecryptor::new(Arc::new(
70        NSSKeyManager::new(primary_password_authenticator),
71    ));
72    Arc::new(LoginStore::new(path, Arc::new(encdec)).unwrap())
73}