-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
215 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,47 @@ pub use jwt_token::JWTToken; | |
mod register; | ||
#[cfg(feature = "internal")] | ||
pub use register::{RegisterKeyResponse, RegisterRequest}; | ||
|
||
#[cfg(feature = "internal")] | ||
use crate::{ | ||
client::kdf::Kdf, | ||
crypto::{HashPurpose, MasterKey}, | ||
error::Result, | ||
}; | ||
|
||
#[cfg(feature = "internal")] | ||
async fn determine_password_hash( | ||
email: &str, | ||
kdf: &Kdf, | ||
password: &str, | ||
purpose: HashPurpose, | ||
) -> Result<String> { | ||
let master_key = MasterKey::derive(password.as_bytes(), email.as_bytes(), kdf)?; | ||
master_key.derive_master_key_hash(password.as_bytes(), purpose) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::num::NonZeroU32; | ||
|
||
use super::*; | ||
|
||
#[cfg(feature = "internal")] | ||
#[tokio::test] | ||
async 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) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(result, "7kTqkF1pY/3JeOu73N9kR99fDDe9O1JOZaVc7KH3lsU="); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
use schemars::JsonSchema; | ||
|
||
use super::determine_password_hash; | ||
use crate::{ | ||
client::{LoginMethod, UserLoginMethod}, | ||
crypto::HashPurpose, | ||
error::{Error, Result}, | ||
Client, | ||
}; | ||
|
||
pub(super) fn password_strength( | ||
_password: String, | ||
_email: String, | ||
|
@@ -16,6 +24,33 @@ pub(super) fn satisfies_policy( | |
true | ||
} | ||
|
||
/// Validate if the provided password matches the password hash stored in the client. | ||
pub(super) async fn validate_password( | ||
client: &Client, | ||
password: String, | ||
password_hash: String, | ||
) -> Result<bool> { | ||
let login_method = client | ||
.login_method | ||
.as_ref() | ||
.ok_or(Error::NotAuthenticated)?; | ||
|
||
if let LoginMethod::User(login_method) = login_method { | ||
match login_method { | ||
UserLoginMethod::Username { email, kdf, .. } | ||
| UserLoginMethod::ApiKey { email, kdf, .. } => { | ||
let hash = | ||
determine_password_hash(email, kdf, &password, HashPurpose::LocalAuthorization) | ||
.await?; | ||
|
||
Ok(hash == password_hash) | ||
} | ||
} | ||
} else { | ||
Err(Error::NotAuthenticated) | ||
} | ||
} | ||
|
||
#[derive(Debug, JsonSchema)] | ||
#[cfg_attr(feature = "mobile", derive(uniffi::Record))] | ||
#[allow(dead_code)] | ||
|
@@ -32,3 +67,33 @@ pub struct MasterPasswordPolicyOptions { | |
/// the user will be forced to update their password. | ||
enforce_on_login: bool, | ||
} | ||
|
||
#[cfg(test)] | ||
|
||
mod tests { | ||
|
||
#[cfg(feature = "mobile")] | ||
#[tokio::test] | ||
async fn test_validate_password() { | ||
use std::num::NonZeroU32; | ||
|
||
use super::validate_password; | ||
use crate::client::{kdf::Kdf, Client, LoginMethod, UserLoginMethod}; | ||
|
||
let mut client = Client::new(None); | ||
client.set_login_method(LoginMethod::User(UserLoginMethod::Username { | ||
email: "[email protected]".to_string(), | ||
kdf: Kdf::PBKDF2 { | ||
iterations: NonZeroU32::new(100_000).unwrap(), | ||
}, | ||
client_id: "1".to_string(), | ||
})); | ||
|
||
let password = "password123".to_string(); | ||
let password_hash = "7kTqkF1pY/3JeOu73N9kR99fDDe9O1JOZaVc7KH3lsU=".to_string(); | ||
|
||
let result = validate_password(&client, password, password_hash).await; | ||
|
||
assert!(result.unwrap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.