2 schema::{password_reset_request, password_reset_request::dsl::*},
5 use diesel::{dsl::*, result::Error, *};
6 use sha2::{Digest, Sha256};
8 #[derive(Queryable, Identifiable, PartialEq, Debug)]
9 #[table_name = "password_reset_request"]
10 pub struct PasswordResetRequest {
13 pub token_encrypted: String,
14 pub published: chrono::NaiveDateTime,
17 #[derive(Insertable, AsChangeset, Clone)]
18 #[table_name = "password_reset_request"]
19 pub struct PasswordResetRequestForm {
21 pub token_encrypted: String,
24 impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
25 fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
26 use crate::schema::password_reset_request::dsl::*;
27 password_reset_request
28 .find(password_reset_request_id)
31 fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result<usize, Error> {
32 diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn)
34 fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
35 insert_into(password_reset_request)
37 .get_result::<Self>(conn)
41 password_reset_request_id: i32,
42 form: &PasswordResetRequestForm,
43 ) -> Result<Self, Error> {
44 diesel::update(password_reset_request.find(password_reset_request_id))
46 .get_result::<Self>(conn)
50 impl PasswordResetRequest {
51 pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> {
52 let mut hasher = Sha256::new();
54 let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.finalize().to_vec());
56 let form = PasswordResetRequestForm {
57 user_id: from_user_id,
58 token_encrypted: token_hash,
61 Self::create(&conn, &form)
63 pub fn read_from_token(conn: &PgConnection, token: &str) -> Result<Self, Error> {
64 let mut hasher = Sha256::new();
66 let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.finalize().to_vec());
67 password_reset_request
68 .filter(token_encrypted.eq(token_hash))
69 .filter(published.gt(now - 1.days()))
73 fn bytes_to_hex(bytes: Vec<u8>) -> String {
74 let mut str = String::new();
76 str = format!("{}{:02x}", str, byte);
84 use super::{super::user::*, *};
85 use crate::{tests::establish_unpooled_connection, ListingType, SortType};
89 let conn = establish_unpooled_connection();
91 let new_user = UserForm {
92 name: "thommy prw".into(),
93 preferred_username: None,
94 password_encrypted: "nope".into(),
102 theme: "darkly".into(),
103 default_sort_type: SortType::Hot as i16,
104 default_listing_type: ListingType::Subscribed as i16,
105 lang: "browser".into(),
107 send_notifications_to_email: false,
108 actor_id: "changeme_8292378".into(),
113 last_refreshed_at: None,
116 let inserted_user = User_::create(&conn, &new_user).unwrap();
119 let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
121 let inserted_password_reset_request =
122 PasswordResetRequest::create_token(&conn, inserted_user.id, token).unwrap();
124 let expected_password_reset_request = PasswordResetRequest {
125 id: inserted_password_reset_request.id,
126 user_id: inserted_user.id,
127 token_encrypted: token_encrypted_.to_string(),
128 published: inserted_password_reset_request.published,
131 let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
132 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
134 assert_eq!(expected_password_reset_request, read_password_reset_request);
136 expected_password_reset_request,
137 inserted_password_reset_request
139 assert_eq!(1, num_deleted);