2 use diesel::{dsl::*, result::Error, PgConnection, *};
4 schema::password_reset_request::dsl::*,
5 source::password_reset_request::*,
8 use sha2::{Digest, Sha256};
10 impl Crud<PasswordResetRequestForm, i32> for PasswordResetRequest {
11 fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
12 password_reset_request
13 .find(password_reset_request_id)
16 fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
17 insert_into(password_reset_request)
19 .get_result::<Self>(conn)
23 password_reset_request_id: i32,
24 form: &PasswordResetRequestForm,
25 ) -> Result<Self, Error> {
26 diesel::update(password_reset_request.find(password_reset_request_id))
28 .get_result::<Self>(conn)
32 pub trait PasswordResetRequest_ {
35 from_local_user_id: LocalUserId,
37 ) -> Result<PasswordResetRequest, Error>;
38 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error>;
41 impl PasswordResetRequest_ for PasswordResetRequest {
44 from_local_user_id: LocalUserId,
46 ) -> Result<PasswordResetRequest, Error> {
47 let mut hasher = Sha256::new();
49 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
51 let form = PasswordResetRequestForm {
52 local_user_id: from_local_user_id,
53 token_encrypted: token_hash,
56 Self::create(conn, &form)
58 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error> {
59 let mut hasher = Sha256::new();
61 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
62 password_reset_request
63 .filter(token_encrypted.eq(token_hash))
64 .filter(published.gt(now - 1.days()))
69 fn bytes_to_hex(bytes: Vec<u8>) -> String {
70 let mut str = String::new();
72 str = format!("{}{:02x}", str, byte);
80 establish_unpooled_connection,
81 source::password_reset_request::PasswordResetRequest_,
84 use lemmy_db_schema::source::{
85 local_user::{LocalUser, LocalUserForm},
86 password_reset_request::PasswordResetRequest,
89 use serial_test::serial;
94 let conn = establish_unpooled_connection();
96 let new_person = PersonForm {
97 name: "thommy prw".into(),
98 ..PersonForm::default()
101 let inserted_person = Person::create(&conn, &new_person).unwrap();
103 let new_local_user = LocalUserForm {
104 person_id: inserted_person.id,
105 password_encrypted: "pass".to_string(),
106 ..LocalUserForm::default()
109 let inserted_local_user = LocalUser::create(&conn, &new_local_user).unwrap();
112 let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
114 let inserted_password_reset_request =
115 PasswordResetRequest::create_token(&conn, inserted_local_user.id, token).unwrap();
117 let expected_password_reset_request = PasswordResetRequest {
118 id: inserted_password_reset_request.id,
119 local_user_id: inserted_local_user.id,
120 token_encrypted: token_encrypted_.to_string(),
121 published: inserted_password_reset_request.published,
124 let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
125 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
127 assert_eq!(expected_password_reset_request, read_password_reset_request);
129 expected_password_reset_request,
130 inserted_password_reset_request
132 assert_eq!(1, num_deleted);