Move invite code generator to users::utils module
This commit is contained in:
parent
aa826fd57f
commit
b1776b9520
5 changed files with 28 additions and 11 deletions
|
@ -10,7 +10,7 @@ use mitra::logger::configure_logger;
|
||||||
use mitra::models::posts::queries::delete_post;
|
use mitra::models::posts::queries::delete_post;
|
||||||
use mitra::models::profiles::queries::delete_profile;
|
use mitra::models::profiles::queries::delete_profile;
|
||||||
use mitra::models::users::queries::{
|
use mitra::models::users::queries::{
|
||||||
generate_invite_code,
|
create_invite_code,
|
||||||
get_invite_codes,
|
get_invite_codes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ async fn main() {
|
||||||
println!("post deleted");
|
println!("post deleted");
|
||||||
},
|
},
|
||||||
SubCommand::GenerateInviteCode(_) => {
|
SubCommand::GenerateInviteCode(_) => {
|
||||||
let invite_code = generate_invite_code(db_client).await.unwrap();
|
let invite_code = create_invite_code(db_client).await.unwrap();
|
||||||
println!("generated invite code: {}", invite_code);
|
println!("generated invite code: {}", invite_code);
|
||||||
},
|
},
|
||||||
SubCommand::ListInviteCodes(_) => {
|
SubCommand::ListInviteCodes(_) => {
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod queries;
|
pub mod queries;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
mod utils;
|
||||||
|
|
|
@ -4,13 +4,13 @@ use uuid::Uuid;
|
||||||
use crate::errors::DatabaseError;
|
use crate::errors::DatabaseError;
|
||||||
use crate::models::profiles::queries::create_profile;
|
use crate::models::profiles::queries::create_profile;
|
||||||
use crate::models::profiles::types::{DbActorProfile, ProfileCreateData};
|
use crate::models::profiles::types::{DbActorProfile, ProfileCreateData};
|
||||||
use crate::utils::crypto::generate_random_string;
|
|
||||||
use super::types::{DbUser, User, UserRegistrationData};
|
use super::types::{DbUser, User, UserRegistrationData};
|
||||||
|
use super::utils::generate_invite_code;
|
||||||
|
|
||||||
pub async fn generate_invite_code(
|
pub async fn create_invite_code(
|
||||||
db_client: &impl GenericClient,
|
db_client: &impl GenericClient,
|
||||||
) -> Result<String, DatabaseError> {
|
) -> Result<String, DatabaseError> {
|
||||||
let invite_code = generate_random_string();
|
let invite_code = generate_invite_code();
|
||||||
db_client.execute(
|
db_client.execute(
|
||||||
"
|
"
|
||||||
INSERT INTO user_invite_code (code)
|
INSERT INTO user_invite_code (code)
|
||||||
|
|
22
src/models/users/utils.rs
Normal file
22
src/models/users/utils.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
use hex;
|
||||||
|
use rand;
|
||||||
|
use rand::prelude::*;
|
||||||
|
|
||||||
|
const INVITE_CODE_LENGTH: usize = 32;
|
||||||
|
|
||||||
|
pub fn generate_invite_code() -> String {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let value: [u8; INVITE_CODE_LENGTH / 2] = rng.gen();
|
||||||
|
hex::encode(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_generate_invite_code() {
|
||||||
|
let invite_code = generate_invite_code();
|
||||||
|
assert_eq!(invite_code.len(), INVITE_CODE_LENGTH);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,12 +4,6 @@ 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 generate_random_string() -> String {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let value: [u8; 16] = rng.gen();
|
|
||||||
hex::encode(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash_password(password: &str) -> Result<String, argon2::Error> {
|
pub fn hash_password(password: &str) -> Result<String, argon2::Error> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let salt: [u8; 32] = rng.gen();
|
let salt: [u8; 32] = rng.gen();
|
||||||
|
|
Loading…
Reference in a new issue