Completely refactor project structure
parent: tbd commit: ae8ff44
1 | use Arc; |
2 | |
3 | use Error; |
4 | |
5 | use ; |
6 | use ; |
7 | use ; |
8 | use StreamExt; |
9 | use |
10 | |
11 | |
12 | AuthenticationTokenRequest, AuthenticationTokenResponse, RegisterAccountRequest, |
13 | RegisterAccountResponse, |
14 | , |
15 | |
16 | UserBioRequest, UserBioResponse, UserDisplayImageRequest, UserDisplayImageResponse, |
17 | UserDisplayNameRequest, UserDisplayNameResponse, |
18 | , |
19 | , |
20 | , |
21 | ; |
22 | use |
23 | , |
24 | , | OsRng
25 | RsaPrivateKey, RsaPublicKey, |
26 | ; |
27 | use Value; |
28 | use ; |
29 | use Mutex; |
30 | |
31 | use crate AuthenticationTokenGranter; |
32 | |
33 | use ; |
34 | |
35 | |
36 | pub pg_pool: PgPool, |
37 | pub this_instance: Instance, |
38 | pub auth_granter: , |
39 | |
40 | |
41 | |
42 | |
43 | pool: PgPool, |
44 | this_instance: &Instance, |
45 | granter: , |
46 | |
47 | Self |
48 | pg_pool: pool, |
49 | this_instance: this_instance.clone, |
50 | auth_granter: granter, |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | async |
58 | &mut self, |
59 | request: UserDisplayNameRequest, |
60 | |
61 | let db_row = query_as! |
62 | UserRow, |
63 | r#"SELECT * FROM users WHERE username = $1"#, |
64 | request.user.username |
65 | |
66 | .fetch_one |
67 | .await |
68 | .unwrap; |
69 | |
70 | Ok |
71 | display_name: db_row.display_name, |
72 | |
73 | |
74 | |
75 | async |
76 | &mut self, |
77 | request: UserDisplayImageRequest, |
78 | |
79 | let db_row = query_as! |
80 | UserRow, |
81 | r#"SELECT * FROM users WHERE username = $1"#, |
82 | request.user.username |
83 | |
84 | .fetch_one |
85 | .await |
86 | .unwrap; |
87 | |
88 | Ok |
89 | image_url: db_row.image_url, |
90 | |
91 | |
92 | |
93 | async |
94 | let db_row = query_as! |
95 | UserRow, |
96 | r#"SELECT * FROM users WHERE username = $1"#, |
97 | request.user.username |
98 | |
99 | .fetch_one |
100 | .await?; |
101 | |
102 | Ok |
103 | |
104 | |
105 | async |
106 | Ok |
107 | UserRow, |
108 | r#"SELECT * FROM users WHERE username = $1"#, |
109 | user.username |
110 | |
111 | .fetch_one |
112 | .await |
113 | .is_err |
114 | |
115 | |
116 | async |
117 | let settings = query_as! |
118 | UserSettingRow, |
119 | r#"SELECT * FROM user_settings WHERE username = $1"#, |
120 | user.username |
121 | |
122 | .fetch_many |
123 | .filter_map |
124 | if let Ok = result |
125 | Some |
126 | else |
127 | None |
128 | |
129 | |
130 | .filter_map |
131 | if let Ok = from_str |
132 | Some |
133 | else |
134 | None |
135 | |
136 | |
137 | . |
138 | .await; |
139 | |
140 | Ok |
141 | |
142 | |
143 | async |
144 | &mut self, |
145 | user: &User, |
146 | settings: &[ ], |
147 | |
148 | for in settings |
149 | let serialized = to_string?; |
150 | |
151 | ! | query
152 | user.username, name, serialized |
153 | .execute .await?; |
154 | |
155 | |
156 | Ok |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | async |
163 | &mut self, |
164 | request: RegisterAccountRequest, |
165 | |
166 | const BITS: usize = 2048; |
167 | |
168 | let private_key = new .unwrap; |
169 | let public_key = from; |
170 | |
171 | let key = |
172 | let mut target: = ; |
173 | |
174 | let mut index = 0; |
175 | let mut iterator = request.password.as_bytes .iter; |
176 | while index < 32 |
177 | if let Some = iterator.next |
178 | target= *next; |
179 | index += 1; |
180 | else |
181 | iterator = request.password.as_bytes .iter; |
182 | |
183 | |
184 | |
185 | target |
186 | ; |
187 | |
188 | let key: & = &key.into; |
189 | let cipher = new; |
190 | let nonce = generate_nonce; |
191 | let ciphertext = cipher |
192 | .encrypt |
193 | .unwrap; |
194 | |
195 | let private_key_enc = format!; |
196 | |
197 | let salt = generate; |
198 | |
199 | let argon2 = default; |
200 | |
201 | let password_hash = argon2 |
202 | .hash_password |
203 | .unwrap |
204 | .to_string; |
205 | |
206 | let user = match query_as! |
207 | UserRow, |
208 | r#"INSERT INTO users VALUES ($1, null, $2, null, null, $3, $4, $5) returning *"#, |
209 | request.username, |
210 | "example.com", |
211 | password_hash, |
212 | public_key |
213 | .to_public_key_pem |
214 | .unwrap, |
215 | private_key_enc |
216 | |
217 | .fetch_one |
218 | .await |
219 | |
220 | Ok => user, |
221 | Err => |
222 | error!; |
223 | |
224 | return Err; |
225 | |
226 | ; |
227 | |
228 | let mut granter = self.auth_granter.lock .await; |
229 | let token = granter |
230 | .create_token_for |
231 | &User |
232 | username: user.username, |
233 | instance: self.this_instance.clone, |
234 | , |
235 | &self.this_instance, |
236 | |
237 | .await; |
238 | |
239 | Ok |
240 | |
241 | |
242 | async |
243 | &mut self, |
244 | _request: AuthenticationTokenRequest, |
245 | |
246 | todo! |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | pub username: String, |
254 | pub image_url: , |
255 | pub display_name: , |
256 | pub bio: , |
257 | pub email: , |
258 | pub password: String, |
259 | pub public_key: String, |
260 | pub enc_private_key: , |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | pub username: String, |
267 | pub name: String, |
268 | pub value: String, |
269 | |
270 |