Fixed implementation up to the point of sending the email

This commit is contained in:
Jacob Conley committed 2023-10-22 13:57:20 -04:00
1 parent 3a0983d50e
commit e15776a4fb
5 files changed
+74 -15

No files matched your search

Generated
+29 -8
View File
@@ -2183,7 +2183,7 @@ dependencies = [
"serde_json",
"sqlx",
"sqlx-core",
"strum",
"strum 0.24.1",
"toml",
]
@@ -2683,9 +2683,9 @@ dependencies = [
[[package]]
name = "heck"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
dependencies = [
"unicode-segmentation",
]
@@ -5513,7 +5513,7 @@ checksum = "b850fa514dc11f2ee85be9d055c512aa866746adfacd1cb42d867d68e6a5b0d9"
dependencies = [
"dotenvy",
"either",
"heck 0.4.0",
"heck 0.4.1",
"hex",
"once_cell",
"proc-macro2",
@@ -5795,22 +5795,41 @@ version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
dependencies = [
"strum_macros",
"strum_macros 0.24.3",
]
[[package]]
name = "strum"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125"
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck 0.4.0",
"heck 0.4.1",
"proc-macro2",
"quote",
"rustversion",
"syn 1.0.107",
]
[[package]]
name = "strum_macros"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0"
dependencies = [
"heck 0.4.1",
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.26",
]
[[package]]
name = "subprocess"
version = "0.2.7"
@@ -6283,7 +6302,7 @@ dependencies = [
"serde_json",
"sqlx",
"sqlx-core",
"strum",
"strum 0.24.1",
"toml",
]
@@ -7011,6 +7030,8 @@ dependencies = [
"sha2 0.9.5",
"sqlx",
"sqlx_mysql_helpers",
"strum 0.25.0",
"strum_macros 0.25.3",
"time 0.2.26",
"tokens",
"toml",
@@ -7265,7 +7286,7 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
"strum",
"strum 0.24.1",
"toml",
]
@@ -6,16 +6,18 @@ use tokens::tokens::password_reset::PasswordResetToken;
use super::lookup_user_for_login_result::UserRecordForLogin;
// todo: ip_address_redemption nullable
pub async fn create_password_reset(pool: &MySqlPool, user: &UserRecordForLogin, ip_address: String, secret_key: String) -> AnyhowResult<()> {
let token = PasswordResetToken::generate();
//TODO: ip_address_redemption nullable
// Definitely remove the empty string default as well!
sqlx::query!(
r#"
INSERT INTO password_resets
(token, user_token, secret_key, current_password_version, ip_address_creation, expires_at)
VALUES (?, ?, ?, ?, ?, NOW() + INTERVAL 3 hour);
(token, user_token, secret_key, current_password_version, ip_address_creation, ip_address_redemption, expires_at)
VALUES (?, ?, ?, ?, ?, '', NOW() + INTERVAL 3 hour);
"#,
token,
+2
View File
@@ -68,3 +68,5 @@ sqlx = { version = "0.6.2", features = [ "mysql", "runtime-actix-native-tls", "o
r2d2_redis = "0.14.0"
crockford = "1.1.2"
byteorder = "1.5.0"
strum = "0.25.0"
strum_macros = "0.25.3"
@@ -7,6 +7,7 @@ use actix_web::error::Error;
use crate::endpoints::create_account_handler::create_account_handler;
use crate::endpoints::login_handler::login_handler;
use crate::endpoints::logout_handler::logout_handler;
use crate::endpoints::password_reset_handler::password_reset_request_handler;
use crate::endpoints::session_info_handler::session_info_handler;
// NB: This does not include the user edit endpoints since FakeYou's API mounts them alongside other things.
@@ -64,4 +65,9 @@ pub fn add_suggested_api_v1_account_creation_and_session_routes<T, B> (app: App<
.route(web::get().to(session_info_handler))
.route(web::head().to(|| HttpResponse::Ok()))
)
.service(
web::resource("/v1/request-reset")
.route(web::post().to(password_reset_request_handler))
.route(web::head().to(|| HttpResponse::Ok()))
)
}
@@ -7,9 +7,13 @@
- provide new password
*/
use actix_web::{HttpRequest, HttpResponse, web};
use std::fmt::Display;
use actix_web::http::StatusCode;
use actix_web::{HttpRequest, HttpResponse, web, ResponseError};
use byteorder::ByteOrder;
use http_server_common::request::get_request_ip::get_request_ip;
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
use log::warn;
use mysql_queries::queries::users::user::create_password_reset_request::create_password_reset;
use mysql_queries::queries::users::user::lookup_user_for_login_by_email::lookup_user_for_login_by_email;
@@ -17,6 +21,7 @@ use mysql_queries::queries::users::user::lookup_user_for_login_by_username::look
use rand::RngCore;
use serde::Deserialize;
use sqlx::MySqlPool;
use strum_macros::Display;
/// This can be reused in login requests in the future!
#[derive(Deserialize)]
@@ -33,16 +38,22 @@ pub struct PasswordResetRequest {
#[derive(Serialize)]
pub struct PasswordResetResponse {}
#[derive(Serialize)]
#[derive(Serialize, Debug, Display)]
pub enum PasswordResetRequestError {
NoSuchUser,
Internal,
}
#[derive(Serialize)]
#[derive(Serialize, Debug)]
pub struct PasswordResetErrorResponse {
kind: PasswordResetRequestError,
}
impl Display for PasswordResetErrorResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl From<PasswordResetRequestError> for PasswordResetErrorResponse {
fn from(value: PasswordResetRequestError) -> Self {
Self { kind: value }
@@ -55,6 +66,17 @@ impl From<errors::AnyhowError> for PasswordResetErrorResponse {
}
}
impl ResponseError for PasswordResetErrorResponse {
//TODO: Yknow, clean this up and stuff
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse {
serialize_as_json_error(self)
}
}
///
/// Non-authenticated!
///
@@ -91,10 +113,16 @@ pub async fn password_reset_request_handler(
.map(|chunk| byteorder::LittleEndian::read_u64(chunk))
.for_each(|chunk| crockford::encode_into::<String>(chunk, &mut secret_key));
secret_key.truncate(32);
//TODO: Handle banned users, they shouldn't be able to do this
create_password_reset(&mysql_pool, &user, ip_address, secret_key.clone()).await?;
create_password_reset(&mysql_pool, &user, ip_address, secret_key.clone()).await
.map_err(|err| {
log::error!("AAAAAHHH! {:?}", err);
err
})?;
email::send_password_reset(&user.email_address, secret_key).await?;
Ok(HttpResponse::Ok().finish())