2 use crate::schema::password_reset_request;
3 use crate::schema::password_reset_request::dsl::*;
4 use sha2::{Digest, Sha256};
6 #[derive(Queryable, Identifiable, PartialEq, Debug)]
7 #[table_name = "password_reset_request"]
8 pub struct PasswordResetRequest {
11 pub token_encrypted: String,
12 pub published: chrono::NaiveDateTime,
15 #[derive(Insertable, AsChangeset, Clone)]
16 #[table_name = "password_reset_request"]
17 pub struct PasswordResetRequestForm {
19 pub token_encrypted: String,
22 impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
23 fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
24 use crate::schema::password_reset_request::dsl::*;
25 password_reset_request
26 .find(password_reset_request_id)
29 fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result<usize, Error> {
30 diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn)
32 fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
33 insert_into(password_reset_request)
35 .get_result::<Self>(conn)
39 password_reset_request_id: i32,
40 form: &PasswordResetRequestForm,
41 ) -> Result<Self, Error> {
42 diesel::update(password_reset_request.find(password_reset_request_id))
44 .get_result::<Self>(conn)
48 impl PasswordResetRequest {
49 pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> {
50 let mut hasher = Sha256::new();
52 let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.result().to_vec());
54 let form = PasswordResetRequestForm {
55 user_id: from_user_id,
56 token_encrypted: token_hash,
59 Self::create(&conn, &form)
61 pub fn read_from_token(conn: &PgConnection, token: &str) -> Result<Self, Error> {
62 let mut hasher = Sha256::new();
64 let token_hash: String = PasswordResetRequest::bytes_to_hex(hasher.result().to_vec());
65 password_reset_request
66 .filter(token_encrypted.eq(token_hash))
67 .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 use super::super::user::*;
87 let conn = establish_unpooled_connection();
89 let new_user = UserForm {
90 name: "thommy prw".into(),
91 preferred_username: None,
92 password_encrypted: "nope".into(),
100 theme: "darkly".into(),
101 default_sort_type: SortType::Hot as i16,
102 default_listing_type: ListingType::Subscribed as i16,
103 lang: "browser".into(),
105 send_notifications_to_email: false,
106 actor_id: "changeme".into(),
111 last_refreshed_at: None,
114 let inserted_user = User_::create(&conn, &new_user).unwrap();
117 let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
119 let inserted_password_reset_request =
120 PasswordResetRequest::create_token(&conn, inserted_user.id, token).unwrap();
122 let expected_password_reset_request = PasswordResetRequest {
123 id: inserted_password_reset_request.id,
124 user_id: inserted_user.id,
125 token_encrypted: token_encrypted_.to_string(),
126 published: inserted_password_reset_request.published,
129 let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
130 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
132 assert_eq!(expected_password_reset_request, read_password_reset_request);
134 expected_password_reset_request,
135 inserted_password_reset_request
137 assert_eq!(1, num_deleted);