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;
13mod schema;
14mod store;
15mod sync;
16mod util;
17
18use db_crypto::{EncryptorDecryptor, KeyManager, ManagedEncryptorDecryptor, StaticKeyManager};
19uniffi::include_scaffolding!("logins");
20
21#[cfg(feature = "keydb")]
22pub use db_crypto::{NSSKeyManager, PrimaryPasswordAuthenticator};
23
24pub use crate::db::{LoginDb, LoginsDeletionMetrics};
25pub use crate::error::*;
26pub use crate::login::*;
27pub use crate::store::*;
28pub use crate::sync::{LoginsBridgedEngine, LoginsSyncEngine};
29use std::sync::Arc;
30
31/// Identifier for the logins key, under which the key is stored in NSS.
32#[cfg(feature = "keydb")]
33static KEY_NAME: &str = "as-logins-key";
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(KEY_NAME.to_string(), primary_password_authenticator),
72    ));
73    let store = LoginStore::new(path, Arc::new(encdec))?;
74    Ok(Arc::new(store))
75}
76
77#[cfg(test)]
78pub mod test_utils {
79    use super::*;
80    use serde::{de::DeserializeOwned, Serialize};
81
82    lazy_static::lazy_static! {
83        pub static ref TEST_ENCRYPTION_KEY: String = serde_json::to_string(&jwcrypto::Jwk::new_direct_key(Some("test-key".to_string())).unwrap()).unwrap();
84        pub static ref TEST_ENCDEC: Arc<ManagedEncryptorDecryptor> = Arc::new(ManagedEncryptorDecryptor::new(Arc::new(StaticKeyManager::new(TEST_ENCRYPTION_KEY.clone()))));
85    }
86
87    pub fn encrypt_struct<T: Serialize>(fields: &T) -> String {
88        let string = serde_json::to_string(fields).unwrap();
89        let cipherbytes = TEST_ENCDEC.encrypt(string.as_bytes().into()).unwrap();
90        std::str::from_utf8(&cipherbytes).unwrap().to_owned()
91    }
92    pub fn decrypt_struct<T: DeserializeOwned>(ciphertext: String) -> T {
93        let jsonbytes = TEST_ENCDEC.decrypt(ciphertext.as_bytes().into()).unwrap();
94        serde_json::from_str(std::str::from_utf8(&jsonbytes).unwrap()).unwrap()
95    }
96}