bitwarden_core/auth/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#[cfg(feature = "internal")]
use bitwarden_crypto::{HashPurpose, Kdf, MasterKey};

mod access_token;
pub(super) mod api;
pub mod client_auth;
mod jwt_token;
pub mod login;
#[cfg(feature = "internal")]
pub mod password;
#[cfg(feature = "internal")]
pub mod pin;
pub mod renew;
pub use access_token::AccessToken;
pub use jwt_token::JWTToken;

#[cfg(feature = "internal")]
mod auth_request;
#[cfg(feature = "internal")]
pub use auth_request::AuthRequestResponse;
#[cfg(feature = "internal")]
pub(crate) use auth_request::{auth_request_decrypt_master_key, auth_request_decrypt_user_key};

#[cfg(feature = "internal")]
mod register;
#[cfg(feature = "internal")]
pub use register::{RegisterKeyResponse, RegisterRequest};

#[cfg(feature = "internal")]
mod tde;
#[cfg(feature = "internal")]
pub use tde::RegisterTdeKeyResponse;
#[cfg(feature = "internal")]
mod key_connector;
#[cfg(feature = "internal")]
pub use key_connector::KeyConnectorResponse;

#[cfg(feature = "internal")]
use crate::error::Result;

#[cfg(feature = "internal")]
fn determine_password_hash(
    email: &str,
    kdf: &Kdf,
    password: &str,
    purpose: HashPurpose,
) -> Result<String> {
    let master_key = MasterKey::derive(password, email, kdf)?;
    Ok(master_key.derive_master_key_hash(password.as_bytes(), purpose)?)
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "internal")]
    use std::num::NonZeroU32;

    #[cfg(feature = "internal")]
    use super::*;

    #[cfg(feature = "internal")]
    #[test]
    fn test_determine_password_hash() {
        use super::determine_password_hash;

        let password = "password123";
        let email = "[email protected]";
        let kdf = Kdf::PBKDF2 {
            iterations: NonZeroU32::new(100_000).unwrap(),
        };
        let purpose = HashPurpose::LocalAuthorization;

        let result = determine_password_hash(email, &kdf, password, purpose).unwrap();

        assert_eq!(result, "7kTqkF1pY/3JeOu73N9kR99fDDe9O1JOZaVc7KH3lsU=");
    }
}