Updated files, will be switching to sessions

This commit is contained in:
Benjamin Sherriff
2024-01-30 14:19:59 -05:00
parent 40a45275d6
commit ca9270f3a7
7 changed files with 192 additions and 188 deletions

View File

@@ -1,6 +1,5 @@
use std::{future::{ready, Ready}, env};
use actix_web::{FromRequest, Error as ActixError, HttpRequest, dev::Payload, http};
use argon2::{password_hash::{rand_core::OsRng, PasswordHasher, PasswordVerifier, SaltString, Error as HashError}, Argon2, PasswordHash};
use diesel::prelude::*;
use log::error;
use redis::Commands;
@@ -9,7 +8,7 @@ use siren::ServiceError;
use crate::storage::{schema::users, connection};
use super::AccessToken;
use super::{hash, AccessToken};
#[derive(Debug, Serialize, Deserialize)]
pub struct RegisterUser {
@@ -21,10 +20,9 @@ pub struct RegisterUser {
impl RegisterUser {
pub fn convert_to_insert(self) -> Result<InsertUser, ServiceError> {
let hash = hash_password(self.password.as_bytes())?;
Ok(InsertUser {
email: self.email.to_lowercase(),
hash,
hash: hash(&self.password)?,
role: "user".to_string(),
first_name: self.first_name,
last_name: self.last_name,
@@ -36,16 +34,6 @@ impl RegisterUser {
}
}
fn hash_password(password: &[u8]) -> Result<String, HashError> {
let salt = SaltString::generate(&mut OsRng);
Ok(Argon2::default().hash_password(password, &salt)?.to_string())
}
pub fn verify_password(hash: &str, password: &[u8]) -> Result<(), HashError> {
let parsed_hash = PasswordHash::new(hash)?;
Ok(Argon2::default().verify_password(password, &parsed_hash)?)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginRequest {
pub email: String,
@@ -134,7 +122,7 @@ impl From<QueryUser> for ResponseUser {
#[derive(Debug, Serialize, Deserialize)]
pub struct JwtAuth {
pub token: uuid::Uuid,
pub id: String,
pub user: ResponseUser
}
@@ -157,7 +145,7 @@ impl FromRequest for JwtAuth {
};
let keys_dir = env::var("KEYS_DIR_PATH").expect("KEYS_DIR_PATH must be set");
let public_key = std::fs::read_to_string(format!("{}/access_public_key.pem", keys_dir)).expect("Failed to read access public key");
let public_key = std::fs::read_to_string(format!("{}/public_key.pem", keys_dir)).expect("Failed to read access public key");
let access_token = match AccessToken::decode(&access_token_string, &public_key) {
Ok(token_details) => token_details,
@@ -169,8 +157,6 @@ impl FromRequest for JwtAuth {
})))
}
};
let access_token_uuid = uuid::Uuid::parse_str(&access_token.token_uuid.to_string()).unwrap();
let mut conn = match crate::storage::redis_connection() {
Ok(conn) => conn,
@@ -182,7 +168,7 @@ impl FromRequest for JwtAuth {
})))
}
};
let user_email = match conn.get::<_, String>(access_token_uuid.clone().to_string()) {
let user_email = match conn.get::<_, String>(access_token.id.clone().to_string()) {
Ok(result) => serde_json::from_str::<AccessToken>(&result).unwrap().email,
Err(_) => {
return ready(Err(ActixError::from(ServiceError {
@@ -194,7 +180,7 @@ impl FromRequest for JwtAuth {
match QueryUser::get_by_email(&user_email) {
Ok(user) => {
ready(Ok(JwtAuth { token: access_token_uuid, user: user.into() }))
ready(Ok(JwtAuth { id: access_token.id, user: user.into() }))
}
Err(_) => return ready(Err(ActixError::from(ServiceError {
status: 401,