aegis_vault_utils/
lib.rs

1//! Utility crate for interacting and generating OTP codes from a backup vault from the Android app
2//! [Aegis Authenticator](https://github.com/beemdevelopment/Aegis).
3//!
4//! # Example
5//! ```no_run
6//! use aegis_vault_utils::{
7//!     otp::generate_otp,
8//!     vault::{parse_vault, PasswordGetter},
9//! };
10//! use color_eyre::eyre::Result;
11//!
12//! // Implement the PasswordGetter trait to get the password from the environment
13//! struct EnvPasswordGetter;
14//! impl PasswordGetter for EnvPasswordGetter {
15//!     fn get_password(&self) -> Result<String> {
16//!         Ok("test".to_string())
17//!     }
18//! }
19//!
20//! fn main() -> Result<()> {
21//!     // Read and parse the vault
22//!     let vault_backup_contents = std::fs::read_to_string("res/aegis_encrypted.json")?;
23//!     let db = parse_vault(&vault_backup_contents, &EnvPasswordGetter)?;
24//!
25//!     // Get the first entry and generate the OTP code
26//!     let entry = db.entries.iter().next().unwrap();
27//!     let otp = generate_otp(&entry.info)?;
28//!
29//!     // Print e.g.: "Deno (Mason): 591295"
30//!     println!("{} ({}): {}", entry.issuer, entry.name, otp);
31//!
32//!     Ok(())
33//! }
34//! ```
35
36/// The [vault][`vault::Vault`] is parsed from a JSON file exported from the Aegis app containing
37/// [database][`vault::Database`] of OTP entries. The database inside the vault can be either
38/// [plain text or encrypted][`vault::VaultDatabase`].
39///
40/// To decrypt the vault, a [`PasswordGetter`][`vault::PasswordGetter`] trait is used to get the
41/// password and the [`parse_vault`][`vault::parse_vault`] function is used to parse the vault.
42///
43/// Example:
44/// ```no_run
45// NOTE: Enable the test once all vault versions are supported
46/// # use aegis_vault_utils::vault::{parse_vault, PasswordGetter};
47/// # use color_eyre::eyre::Result;
48/// struct EnvPasswordGetter;
49/// impl PasswordGetter for EnvPasswordGetter {
50///     fn get_password(&self) -> Result<String> {
51///         Ok("test".to_string())
52///     }
53/// }
54///
55/// # fn main() -> Result<()> {
56/// let vault_backup_contents = std::fs::read_to_string("res/aegis_encrypted.json")?;
57/// let db = parse_vault(&vault_backup_contents, &EnvPasswordGetter)?;
58/// db.entries.iter().for_each(|entry| {
59///     println!("{:?}: {:?}", entry.name, entry.issuer);
60/// });
61/// # Ok(())
62/// # }
63/// ```
64pub mod vault;
65
66/// Module for generating OTP (One Time Pad) codes
67///
68/// The official Aegis documentation for code generation can be found
69/// [here](https://github.com/beemdevelopment/Aegis/blob/master/docs/vault.md#entries).
70///
71/// Example:
72/// ```rust
73/// # use aegis_vault_utils::otp::{generate_otp, Entry, EntryInfo, EntryInfoTotp, HashAlgorithm};
74/// # use color_eyre::eyre::Result;
75/// # fn main() -> Result<()> {
76/// // Example entry from the vault
77/// let entry = Entry {
78///     info: EntryInfo::Totp(EntryInfoTotp {
79///         secret: "4SJHB4GSD43FZBAI7C2HLRJGPQ".to_string(),
80///         algo: HashAlgorithm::Sha1,
81///         digits: 6,
82///         period: 30,
83///     }),
84///     name: "Mason".to_string(),
85///     issuer: "Deno".to_string(),
86/// };
87///
88/// // Generate the OTP code
89/// let otp = generate_otp(&entry.info)?;
90///
91/// // Print e.g.: "Deno (Mason): 591295"
92/// println!("{} ({}): {}", entry.issuer, entry.name, otp);
93/// # Ok(())
94/// # }
95/// ```
96pub mod otp;