|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +#[cfg(feature = "wasm")] |
| 5 | +use {tsify::Tsify, wasm_bindgen::prelude::*}; |
| 6 | + |
| 7 | +use crate::CipherId; |
| 8 | + |
| 9 | +/// Minimal login cipher data needed for risk evaluation |
| 10 | +#[derive(Serialize, Deserialize, Debug, Clone)] |
| 11 | +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] |
| 12 | +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] |
| 13 | +pub struct CipherLoginDetails { |
| 14 | + /// Cipher ID to identify which cipher in results |
| 15 | + pub id: Option<CipherId>, |
| 16 | + /// The decrypted password to evaluate |
| 17 | + pub password: String, |
| 18 | + /// Username or email (login ciphers only have one field) |
| 19 | + pub username: Option<String>, |
| 20 | +} |
| 21 | + |
| 22 | +/// Password reuse map wrapper for WASM compatibility |
| 23 | +#[derive(Serialize, Deserialize, Debug, Clone)] |
| 24 | +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] |
| 25 | +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] |
| 26 | +#[serde(transparent)] |
| 27 | +pub struct PasswordReuseMap { |
| 28 | + /// Map of passwords to their occurrence count |
| 29 | + #[cfg_attr(feature = "wasm", tsify(type = "Record<string, number>"))] |
| 30 | + pub map: HashMap<String, u32>, |
| 31 | +} |
| 32 | + |
| 33 | +/// Options for configuring risk computation |
| 34 | +#[derive(Serialize, Deserialize, Debug, Clone)] |
| 35 | +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] |
| 36 | +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] |
| 37 | +#[serde(rename_all = "camelCase")] |
| 38 | +#[derive(Default)] |
| 39 | +pub struct CipherRiskOptions { |
| 40 | + /// Pre-computed password reuse map (password → count) |
| 41 | + /// If provided, enables reuse detection across ciphers |
| 42 | + pub password_map: Option<PasswordReuseMap>, |
| 43 | + /// Whether to check passwords against Have I Been Pwned API |
| 44 | + /// When true, makes network requests to check for exposed passwords |
| 45 | + pub check_exposed: bool, |
| 46 | +} |
| 47 | + |
| 48 | +/// Risk evaluation result for a single cipher |
| 49 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] |
| 50 | +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] |
| 51 | +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] |
| 52 | +pub struct CipherRisk { |
| 53 | + /// Cipher ID matching the input CipherLoginDetails |
| 54 | + pub id: Option<CipherId>, |
| 55 | + /// Password strength score from 0 (weakest) to 4 (strongest) |
| 56 | + /// Calculated using zxcvbn with cipher-specific context |
| 57 | + pub password_strength: u8, |
| 58 | + /// Number of times password appears in HIBP database |
| 59 | + /// None if check_exposed was false in options |
| 60 | + pub exposed_count: Option<u32>, |
| 61 | + /// Number of times this password appears in the provided cipher list |
| 62 | + /// Minimum value is 1 (the cipher itself) |
| 63 | + pub reuse_count: u32, |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(feature = "wasm")] |
| 67 | +impl wasm_bindgen::__rt::VectorIntoJsValue for CipherRisk { |
| 68 | + fn vector_into_jsvalue( |
| 69 | + vector: wasm_bindgen::__rt::std::boxed::Box<[Self]>, |
| 70 | + ) -> wasm_bindgen::JsValue { |
| 71 | + wasm_bindgen::__rt::js_value_vector_into_jsvalue(vector) |
| 72 | + } |
| 73 | +} |
0 commit comments