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