Files
bitwarden-proton-sync/src/bw.rs
T
2026-07-20 10:41:53 +02:00

108 lines
2.7 KiB
Rust

use crate::sh;
use serde::Deserialize;
use std::io::{Error, ErrorKind};
const EXECUTABLE: &str = "bw";
#[derive(Deserialize, Clone)]
pub struct Item {
pub id: String,
pub name: String,
#[serde(rename = "passwordHistory")]
pub password_history: Vec<PasswordHistoryItem>,
#[serde(rename = "revisionDate")]
pub revision_date: String,
#[serde(rename = "creationDate")]
pub creation_date: String,
pub object: String,
#[serde(rename = "type")]
pub type_: u16,
pub notes: Option<String>,
pub favorite: bool,
pub fields: Vec<ItemField>,
pub login: Option<Login>,
}
#[derive(Deserialize, Clone)]
pub struct PasswordHistoryItem {
#[serde(rename = "lastUsedDate")]
pub last_used_date: String,
pub password: String,
}
#[derive(Deserialize, Clone)]
pub struct ItemField {
pub name: String,
pub value: String,
#[serde(rename = "type")]
pub type_: u16,
}
#[derive(Deserialize, Clone)]
pub struct Login {
pub uris: Vec<LoginUri>,
pub username: Option<String>,
pub password: Option<String>,
pub totp: Option<String>,
#[serde(rename = "passwordRevisionDate")]
pub password_revision_date: Option<String>,
#[serde(rename = "fido2Credentials")]
pub fido2_credentials: Vec<Fido2Credential>,
}
#[derive(Deserialize, Clone)]
pub struct LoginUri {
pub uri: String,
}
#[derive(Deserialize, Clone)]
pub struct Fido2Credential {
#[serde(rename = "credentialId")]
pub credential_id: String,
#[serde(rename = "keyType")]
pub key_type: String,
#[serde(rename = "keyAlgorithm")]
pub key_algorithm: String,
#[serde(rename = "keyCurve")]
pub key_curve: String,
#[serde(rename = "keyValue")]
pub key_value: String,
#[serde(rename = "rpId")]
pub rp_id: String,
#[serde(rename = "userHandle")]
pub user_handle: String,
#[serde(rename = "userName")]
pub user_name: String,
pub counter: String,
#[serde(rename = "rpName")]
pub rp_name: String,
#[serde(rename = "userDisplayName")]
pub user_display_name: Option<String>,
pub discoverable: String,
#[serde(rename = "creationDate")]
pub creation_date: String,
}
pub fn check_bw() -> Result<(), Error> {
let which = sh::sh(format!("which {}", EXECUTABLE));
if which.is_empty() {
return Err(Error::new(
ErrorKind::Other,
format!("{} is not installed", EXECUTABLE),
));
}
Ok(())
}
pub fn get_items() -> Result<Vec<Item>, Error> {
let items_raw = sh::sh(format!("{} list items --raw", EXECUTABLE));
match serde_json::from_str(items_raw.as_str()) {
Ok(val) => Ok(val),
Err(e) => Err(Error::new(
ErrorKind::Other,
format!("Couldn't get items: {e}"),
)),
}
}