-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.rs
113 lines (99 loc) · 2.77 KB
/
models.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// models.rs
// Copyright (C) 2021-2025 Óscar García Amor <[email protected]>
// Distributed under terms of the GNU GPLv3 license.
//
use rocket::serde::{Serialize, Deserialize, Deserializer};
use chrono::NaiveDateTime;
use crate::schema::passwords;
pub struct AuthorizedUser {
pub id: i32,
pub email: String,
pub password: String,
pub token_id: i32
}
#[derive(Serialize, Deserialize, Queryable)]
#[serde(crate = "rocket::serde")]
pub struct User {
pub id: i32,
pub email: String,
pub password: String
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct NewUser {
pub email: String,
pub password: String
}
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct UserPassword {
pub current_password: String
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct NewUserPassword {
pub current_password: String,
pub new_password: String
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct JWTRefreshToken {
pub refresh: String
}
#[derive(Serialize, Deserialize, Queryable)]
#[serde(crate = "rocket::serde")]
pub struct DBToken {
pub id: i32,
pub user_id: i32,
pub access_token: String,
pub refresh_token: String,
pub created: NaiveDateTime,
pub modified: NaiveDateTime
}
#[derive(Serialize, Deserialize, Queryable, Selectable)]
#[serde(crate = "rocket::serde")]
pub struct Password {
pub id: i32,
pub user_id: i32,
pub login: String,
pub site: String,
pub uppercase: bool,
pub symbols: bool,
pub lowercase: bool,
pub digits: bool,
pub counter: i32,
pub version: i32,
pub length: i32,
pub created: NaiveDateTime,
pub modified: NaiveDateTime
}
// Older versions of LessPass send `numbers` instead of `digits`, other versions can even send both
// values, this structure takes care of deserializing this.
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct DigitsOrNumbers {
digits: Option<bool>,
numbers: Option<bool>
}
fn digits_or_numbers<'d, D: Deserializer<'d>>(d: D) -> Result<bool, D::Error> {
let DigitsOrNumbers { digits, numbers } = DigitsOrNumbers::deserialize(d)?;
digits.or(numbers).ok_or(rocket::serde::de::Error::missing_field("digits or numbers"))
}
#[derive(Clone, Deserialize, Insertable, AsChangeset)]
#[serde(crate = "rocket::serde")]
#[diesel(table_name = passwords)]
pub struct NewPassword {
pub login: String,
pub site: String,
pub uppercase: bool,
pub symbols: bool,
pub lowercase: bool,
#[serde(deserialize_with = "digits_or_numbers", flatten)]
pub digits: bool,
pub counter: i32,
#[serde(default = "default_version")]
pub version: i32,
pub length: i32,
}
const fn default_version() -> i32 { 2 }