Fix settings serialization
parent: tbd commit: a70c63a
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 settings = self.settings .await?; |
62 | |
63 | let name = settings.iter .find; |
64 | |
65 | if let Some = name |
66 | let name: UserDisplayName = from_value .unwrap; |
67 | |
68 | Ok |
69 | else |
70 | Ok |
71 | |
72 | |
73 | |
74 | async |
75 | &mut self, |
76 | request: UserDisplayImageRequest, |
77 | |
78 | let db_row = query_as! |
79 | UserRow, |
80 | r#"SELECT * FROM users WHERE username = $1"#, |
81 | request.user.username |
82 | |
83 | .fetch_one |
84 | .await |
85 | .unwrap; |
86 | |
87 | Ok |
88 | image_url: db_row.image_url, |
89 | |
90 | |
91 | |
92 | async |
93 | let settings = self.settings .await?; |
94 | |
95 | let bio = settings.iter .find; |
96 | |
97 | if let Some = bio |
98 | let bio: UserBio = from_value .unwrap; |
99 | |
100 | Ok |
101 | else |
102 | Ok |
103 | |
104 | |
105 | |
106 | async |
107 | Ok |
108 | UserRow, |
109 | r#"SELECT * FROM users WHERE username = $1"#, |
110 | user.username |
111 | |
112 | .fetch_one |
113 | .await |
114 | .is_err |
115 | |
116 | |
117 | async |
118 | let settings = query_as! |
119 | UserSettingRow, |
120 | r#"SELECT * FROM user_settings WHERE username = $1"#, |
121 | user.username |
122 | |
123 | .fetch_many |
124 | .filter_map |
125 | if let Ok = result |
126 | Some |
127 | else |
128 | None |
129 | |
130 | |
131 | .filter_map |
132 | if let Ok = from_str |
133 | Some |
134 | else |
135 | None |
136 | |
137 | |
138 | . |
139 | .await; |
140 | |
141 | Ok |
142 | |
143 | |
144 | async |
145 | &mut self, |
146 | user: &User, |
147 | settings: &[ ], |
148 | |
149 | for in settings |
150 | ! | query
151 | user.username, name, value |
152 | .execute .await?; |
153 | |
154 | |
155 | Ok |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | async |
162 | &mut self, |
163 | request: RegisterAccountRequest, |
164 | |
165 | const BITS: usize = 2048; |
166 | |
167 | let private_key = new .unwrap; |
168 | let public_key = from; |
169 | |
170 | let key = |
171 | let mut target: = ; |
172 | |
173 | let mut index = 0; |
174 | let mut iterator = request.password.as_bytes .iter; |
175 | while index < 32 |
176 | if let Some = iterator.next |
177 | target= *next; |
178 | index += 1; |
179 | else |
180 | iterator = request.password.as_bytes .iter; |
181 | |
182 | |
183 | |
184 | target |
185 | ; |
186 | |
187 | let key: & = &key.into; |
188 | let cipher = new; |
189 | let nonce = generate_nonce; |
190 | let ciphertext = cipher |
191 | .encrypt |
192 | .unwrap; |
193 | |
194 | let private_key_enc = format!; |
195 | |
196 | let salt = generate; |
197 | |
198 | let argon2 = default; |
199 | |
200 | let password_hash = argon2 |
201 | .hash_password |
202 | .unwrap |
203 | .to_string; |
204 | |
205 | let user = match query_as! |
206 | UserRow, |
207 | r#"INSERT INTO users VALUES ($1, null, $2, null, null, $3, $4, $5) returning *"#, |
208 | request.username, |
209 | "example.com", |
210 | password_hash, |
211 | public_key |
212 | .to_public_key_pem |
213 | .unwrap, |
214 | private_key_enc |
215 | |
216 | .fetch_one |
217 | .await |
218 | |
219 | Ok => user, |
220 | Err => |
221 | error!; |
222 | |
223 | return Err; |
224 | |
225 | ; |
226 | |
227 | let mut granter = self.auth_granter.lock .await; |
228 | let token = granter |
229 | .create_token_for |
230 | &User |
231 | username: user.username, |
232 | instance: self.this_instance.clone, |
233 | , |
234 | &self.this_instance, |
235 | |
236 | .await; |
237 | |
238 | Ok |
239 | |
240 | |
241 | async |
242 | &mut self, |
243 | _request: AuthenticationTokenRequest, |
244 | |
245 | todo! |
246 | |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | pub username: String, |
253 | pub image_url: , |
254 | pub display_name: , |
255 | pub bio: , |
256 | pub email: , |
257 | pub password: String, |
258 | pub public_key: String, |
259 | pub enc_private_key: , |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | pub username: String, |
266 | pub name: String, |
267 | pub value: String, |
268 | |
269 |