Move password utils to utils::passwords module
This commit is contained in:
parent
ece3dbf71c
commit
4d85638d8c
8 changed files with 38 additions and 34 deletions
|
@ -58,7 +58,7 @@ pub fn parse_identity_proof(
|
||||||
verify_minisign_signature(
|
verify_minisign_signature(
|
||||||
did_key,
|
did_key,
|
||||||
&message,
|
&message,
|
||||||
&signature,
|
signature,
|
||||||
).map_err(|_| ValidationError("invalid identity proof"))?;
|
).map_err(|_| ValidationError("invalid identity proof"))?;
|
||||||
},
|
},
|
||||||
Did::Pkh(ref did_pkh) => {
|
Did::Pkh(ref did_pkh) => {
|
||||||
|
|
12
src/cli.rs
12
src/cli.rs
|
@ -28,12 +28,14 @@ use crate::models::users::queries::{
|
||||||
set_user_password,
|
set_user_password,
|
||||||
};
|
};
|
||||||
use crate::monero::wallet::create_monero_wallet;
|
use crate::monero::wallet::create_monero_wallet;
|
||||||
use crate::utils::crypto::{
|
use crate::utils::{
|
||||||
hash_password,
|
crypto::{
|
||||||
generate_private_key,
|
generate_private_key,
|
||||||
serialize_private_key,
|
serialize_private_key,
|
||||||
|
},
|
||||||
|
files::remove_files,
|
||||||
|
passwords::hash_password,
|
||||||
};
|
};
|
||||||
use crate::utils::files::remove_files;
|
|
||||||
|
|
||||||
/// Admin CLI tool
|
/// Admin CLI tool
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
|
@ -141,7 +141,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_json_signature_eip155() {
|
fn test_get_json_signature_eip191() {
|
||||||
let signed_object = json!({
|
let signed_object = json!({
|
||||||
"type": "Test",
|
"type": "Test",
|
||||||
"id": "https://example.org/objects/1",
|
"id": "https://example.org/objects/1",
|
||||||
|
|
|
@ -71,15 +71,17 @@ use crate::models::users::queries::{
|
||||||
get_user_by_did,
|
get_user_by_did,
|
||||||
};
|
};
|
||||||
use crate::models::users::types::UserCreateData;
|
use crate::models::users::types::UserCreateData;
|
||||||
use crate::utils::caip2::ChainId;
|
use crate::utils::{
|
||||||
use crate::utils::canonicalization::canonicalize_object;
|
caip2::ChainId,
|
||||||
use crate::utils::crypto::{
|
canonicalization::canonicalize_object,
|
||||||
hash_password,
|
crypto::{
|
||||||
generate_private_key,
|
generate_private_key,
|
||||||
serialize_private_key,
|
serialize_private_key,
|
||||||
|
},
|
||||||
|
currencies::Currency,
|
||||||
|
id::new_uuid,
|
||||||
|
passwords::hash_password,
|
||||||
};
|
};
|
||||||
use crate::utils::currencies::Currency;
|
|
||||||
use crate::utils::id::new_uuid;
|
|
||||||
use super::helpers::get_relationship;
|
use super::helpers::get_relationship;
|
||||||
use super::types::{
|
use super::types::{
|
||||||
Account,
|
Account,
|
||||||
|
|
|
@ -10,8 +10,8 @@ use crate::models::users::queries::{
|
||||||
get_user_by_name,
|
get_user_by_name,
|
||||||
get_user_by_login_address,
|
get_user_by_login_address,
|
||||||
};
|
};
|
||||||
use crate::utils::crypto::verify_password;
|
|
||||||
use crate::utils::currencies::{validate_wallet_address, Currency};
|
use crate::utils::currencies::{validate_wallet_address, Currency};
|
||||||
|
use crate::utils::passwords::verify_password;
|
||||||
use super::types::{TokenRequest, TokenResponse};
|
use super::types::{TokenRequest, TokenResponse};
|
||||||
use super::utils::generate_access_token;
|
use super::utils::generate_access_token;
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,7 @@
|
||||||
use pem;
|
|
||||||
use rand;
|
|
||||||
use rand::prelude::*;
|
|
||||||
use rsa::{Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};
|
use rsa::{Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};
|
||||||
use rsa::pkcs8::{FromPrivateKey, FromPublicKey, ToPrivateKey, ToPublicKey};
|
use rsa::pkcs8::{FromPrivateKey, FromPublicKey, ToPrivateKey, ToPublicKey};
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
pub fn hash_password(password: &str) -> Result<String, argon2::Error> {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let salt: [u8; 32] = rng.gen();
|
|
||||||
let config = argon2::Config::default();
|
|
||||||
|
|
||||||
argon2::hash_encoded(password.as_bytes(), &salt, &config)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify_password(
|
|
||||||
password_hash: &str,
|
|
||||||
password: &str,
|
|
||||||
) -> Result<bool, argon2::Error> {
|
|
||||||
argon2::verify_encoded(password_hash, password.as_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
|
pub fn generate_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
|
||||||
let mut rng = rand::rngs::OsRng;
|
let mut rng = rand::rngs::OsRng;
|
||||||
let bits = 2048;
|
let bits = 2048;
|
||||||
|
@ -28,6 +10,7 @@ pub fn generate_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn generate_weak_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
|
pub fn generate_weak_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
|
||||||
|
use rand::SeedableRng;
|
||||||
let mut rng = rand::rngs::SmallRng::seed_from_u64(0);
|
let mut rng = rand::rngs::SmallRng::seed_from_u64(0);
|
||||||
let bits = 512;
|
let bits = 512;
|
||||||
RsaPrivateKey::new(&mut rng, bits)
|
RsaPrivateKey::new(&mut rng, bits)
|
||||||
|
|
|
@ -6,4 +6,5 @@ pub mod files;
|
||||||
pub mod html;
|
pub mod html;
|
||||||
pub mod id;
|
pub mod id;
|
||||||
pub mod markdown;
|
pub mod markdown;
|
||||||
|
pub mod passwords;
|
||||||
pub mod urls;
|
pub mod urls;
|
||||||
|
|
16
src/utils/passwords.rs
Normal file
16
src/utils/passwords.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
pub fn hash_password(password: &str) -> Result<String, argon2::Error> {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let salt: [u8; 32] = rng.gen();
|
||||||
|
let config = argon2::Config::default();
|
||||||
|
|
||||||
|
argon2::hash_encoded(password.as_bytes(), &salt, &config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn verify_password(
|
||||||
|
password_hash: &str,
|
||||||
|
password: &str,
|
||||||
|
) -> Result<bool, argon2::Error> {
|
||||||
|
argon2::verify_encoded(password_hash, password.as_bytes())
|
||||||
|
}
|
Loading…
Reference in a new issue