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 for PasswordResetRequest {
11 type Form = PasswordResetRequestForm;
13 fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
14 password_reset_request
15 .find(password_reset_request_id)
18 fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
19 insert_into(password_reset_request)
21 .get_result::<Self>(conn)
25 password_reset_request_id: i32,
26 form: &PasswordResetRequestForm,
27 ) -> Result<Self, Error> {
28 diesel::update(password_reset_request.find(password_reset_request_id))
30 .get_result::<Self>(conn)
34 pub trait PasswordResetRequest_ {
37 from_local_user_id: LocalUserId,
39 ) -> Result<PasswordResetRequest, Error>;
40 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error>;
43 impl PasswordResetRequest_ for PasswordResetRequest {
46 from_local_user_id: LocalUserId,
48 ) -> Result<PasswordResetRequest, Error> {
49 let mut hasher = Sha256::new();
51 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
53 let form = PasswordResetRequestForm {
54 local_user_id: from_local_user_id,
55 token_encrypted: token_hash,
58 Self::create(conn, &form)
60 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error> {
61 let mut hasher = Sha256::new();
63 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
64 password_reset_request
65 .filter(token_encrypted.eq(token_hash))
66 .filter(published.gt(now - 1.days()))
71 fn bytes_to_hex(bytes: Vec<u8>) -> String {
72 let mut str = String::new();
74 str = format!("{}{:02x}", str, byte);
82 establish_unpooled_connection,
83 source::password_reset_request::PasswordResetRequest_,
86 use lemmy_db_schema::source::{
87 local_user::{LocalUser, LocalUserForm},
88 password_reset_request::PasswordResetRequest,
91 use serial_test::serial;
96 let conn = establish_unpooled_connection();
98 let new_person = PersonForm {
99 name: "thommy prw".into(),
100 ..PersonForm::default()
103 let inserted_person = Person::create(&conn, &new_person).unwrap();
105 let new_local_user = LocalUserForm {
106 person_id: inserted_person.id,
107 password_encrypted: "pass".to_string(),
108 ..LocalUserForm::default()
111 let inserted_local_user = LocalUser::create(&conn, &new_local_user).unwrap();
114 let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
116 let inserted_password_reset_request =
117 PasswordResetRequest::create_token(&conn, inserted_local_user.id, token).unwrap();
119 let expected_password_reset_request = PasswordResetRequest {
120 id: inserted_password_reset_request.id,
121 local_user_id: inserted_local_user.id,
122 token_encrypted: token_encrypted_.to_string(),
123 published: inserted_password_reset_request.published,
126 let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
127 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
129 assert_eq!(expected_password_reset_request, read_password_reset_request);
131 expected_password_reset_request,
132 inserted_password_reset_request
134 assert_eq!(1, num_deleted);