User Auth Early
parent: tbd commit: 8069fba
1 | use ; |
2 | |
3 | use ; |
4 | use |
5 | , | TcpStream
6 | |
7 | , |
8 | Mutex, |
9 | , |
10 | , | JoinHandle
11 | ; |
12 | use ; |
13 | |
14 | use crate:: |
15 | , | AuthenticationTokenGranter
16 | , |
17 | , |
18 | , | Listeners
19 | |
20 | , |
21 | |
22 | RepositoryMessage, RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
23 | , |
24 | MessageKind, |
25 | , |
26 | |
27 | , |
28 | , | Repository
29 | , | User
30 | , |
31 | ; |
32 | |
33 | |
34 | pub task: , |
35 | |
36 | |
37 | |
38 | pub instance: InstanceMeta, |
39 | pub sender: , |
40 | pub task: , |
41 | |
42 | |
43 | /// Represents a connection which hasn't finished the handshake. |
44 | |
45 | pub socket: , |
46 | |
47 | |
48 | |
49 | |
50 | pub connections: , |
51 | pub instance_connections: , |
52 | |
53 | |
54 | pub async |
55 | mut socket: , |
56 | listeners: , |
57 | connections: , |
58 | backend: , |
59 | auth_granter: , |
60 | addr: SocketAddr, |
61 | |
62 | let mut handshaked = false; |
63 | let this_instance = Instance |
64 | url: String from, |
65 | ; |
66 | |
67 | while let Some = socket.next .await |
68 | let message = match message |
69 | Ok => message, |
70 | Err => |
71 | error!; |
72 | continue; |
73 | |
74 | ; |
75 | |
76 | let payload = match message |
77 | => text.into_bytes, | Text
78 | => bytes, | Binary
79 | => continue, | Ping
80 | => continue, | Pong
81 | => | Close
82 | info!; |
83 | |
84 | return; |
85 | |
86 | _ => unreachable!, |
87 | ; |
88 | |
89 | let message = match |
90 | Ok => message, |
91 | Err => |
92 | error!; |
93 | continue; |
94 | |
95 | ; |
96 | |
97 | // info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); |
98 | |
99 | if let Handshake = message |
100 | match handshake |
101 | => | Initiate
102 | // Send HandshakeMessage::Response |
103 | let message = HandshakeResponse |
104 | identity: Instance |
105 | url: String from, |
106 | , |
107 | version: String from, |
108 | ; |
109 | |
110 | socket |
111 | .send |
112 | to_vec |
113 | , | Response
114 | |
115 | .unwrap, |
116 | |
117 | .await |
118 | .unwrap; |
119 | |
120 | continue; |
121 | |
122 | => | Response
123 | // Send HandshakeMessage::Finalize |
124 | let message = HandshakeFinalize ; |
125 | |
126 | socket |
127 | .send |
128 | to_vec |
129 | , | Finalize
130 | |
131 | .unwrap, |
132 | |
133 | .await |
134 | .unwrap; |
135 | |
136 | continue; |
137 | |
138 | => | Finalize
139 | handshaked = true; |
140 | |
141 | // Send HandshakeMessage::Finalize |
142 | let message = HandshakeFinalize ; |
143 | |
144 | socket |
145 | .send |
146 | to_vec |
147 | , | Finalize
148 | |
149 | .unwrap, |
150 | |
151 | .await |
152 | .unwrap; |
153 | |
154 | continue; |
155 | |
156 | |
157 | |
158 | |
159 | if !handshaked |
160 | continue; |
161 | |
162 | |
163 | if let Repository = &message |
164 | if repository.target.instance != this_instance |
165 | info!; |
166 | // We need to send this command to a different instance |
167 | |
168 | let mut listener = send_and_get_listener .await; |
169 | |
170 | // Wait for response |
171 | while let Ok = listener.recv .await |
172 | if let Repository |
173 | command: Response, |
174 | .. |
175 | = message |
176 | |
177 | socket |
178 | .send |
179 | .await |
180 | .unwrap; |
181 | |
182 | |
183 | continue; |
184 | else |
185 | // This message is targeting this instance |
186 | match &repository.command |
187 | => match request.clone | Request
188 | => | CreateRepository
189 | let mut backend = backend.lock .await; |
190 | let request = request.validate .await.unwrap; |
191 | let response = backend.create_repository .await; |
192 | |
193 | let response = match response |
194 | Ok => response, |
195 | Err => |
196 | error!; |
197 | continue; |
198 | |
199 | ; |
200 | drop; |
201 | |
202 | socket |
203 | .send |
204 | to_vec |
205 | RepositoryMessage |
206 | target: repository.target.clone, |
207 | command: Response |
208 | , | CreateRepository
209 | , |
210 | , |
211 | |
212 | .unwrap, |
213 | |
214 | .await |
215 | .unwrap; |
216 | |
217 | continue; |
218 | |
219 | => | RepositoryFileInspect
220 | let mut backend = backend.lock .await; |
221 | let request = request.validate .await.unwrap; |
222 | let response = backend.repository_file_inspect; |
223 | |
224 | let response = match response |
225 | Ok => response, |
226 | Err => |
227 | error!; |
228 | continue; |
229 | |
230 | ; |
231 | drop; |
232 | |
233 | socket |
234 | .send |
235 | to_vec |
236 | RepositoryMessage |
237 | target: repository.target.clone, |
238 | command: Response |
239 | RepositoryFileInspection |
240 | response, |
241 | , |
242 | , |
243 | , |
244 | |
245 | .unwrap, |
246 | |
247 | .await |
248 | .unwrap; |
249 | continue; |
250 | |
251 | => | RepositoryInfo
252 | let mut backend = backend.lock .await; |
253 | let request = request.validate .await.unwrap; |
254 | let response = backend.repository_info .await; |
255 | |
256 | let response = match response |
257 | Ok => response, |
258 | Err => |
259 | error!; |
260 | continue; |
261 | |
262 | ; |
263 | drop; |
264 | |
265 | socket |
266 | .send |
267 | to_vec |
268 | RepositoryMessage |
269 | target: repository.target.clone, |
270 | command: Response |
271 | , | RepositoryInfo
272 | , |
273 | , |
274 | |
275 | .unwrap, |
276 | |
277 | .await |
278 | .unwrap; |
279 | continue; |
280 | |
281 | => | IssuesCount
282 | let request = &request.validate .await.unwrap; |
283 | |
284 | let mut backend = backend.lock .await; |
285 | let response = backend.issues_count; |
286 | |
287 | let response = match response |
288 | Ok => response, |
289 | Err => |
290 | error!; |
291 | continue; |
292 | |
293 | ; |
294 | drop; |
295 | |
296 | socket |
297 | .send |
298 | to_vec |
299 | RepositoryMessage |
300 | target: repository.target.clone, |
301 | command: Response |
302 | , | IssuesCount
303 | , |
304 | , |
305 | |
306 | .unwrap, |
307 | |
308 | .await |
309 | .unwrap; |
310 | continue; |
311 | |
312 | => | IssueLabels
313 | let request = &request.validate .await.unwrap; |
314 | |
315 | let mut backend = backend.lock .await; |
316 | let response = backend.issue_labels; |
317 | |
318 | let response = match response |
319 | Ok => response, |
320 | Err => |
321 | error!; |
322 | continue; |
323 | |
324 | ; |
325 | drop; |
326 | socket |
327 | .send |
328 | to_vec |
329 | RepositoryMessage |
330 | target: repository.target.clone, |
331 | command: Response |
332 | , | IssueLabels
333 | , |
334 | , |
335 | |
336 | .unwrap, |
337 | |
338 | .await |
339 | .unwrap; |
340 | continue; |
341 | |
342 | => | Issues
343 | let request = request.validate .await.unwrap; |
344 | |
345 | let mut backend = backend.lock .await; |
346 | let response = backend.issues; |
347 | |
348 | let response = match response |
349 | Ok => response, |
350 | Err => |
351 | error!; |
352 | continue; |
353 | |
354 | ; |
355 | drop; |
356 | |
357 | socket |
358 | .send |
359 | to_vec |
360 | RepositoryMessage |
361 | target: repository.target.clone, |
362 | command: Response |
363 | , | Issues
364 | , |
365 | , |
366 | |
367 | .unwrap, |
368 | |
369 | .await |
370 | .unwrap; |
371 | continue; |
372 | |
373 | , |
374 | => | Response
375 | unreachable! |
376 | |
377 | |
378 | |
379 | |
380 | |
381 | if let Authentication = &message |
382 | match authentication |
383 | => match request | Request
384 | => | AuthenticationToken
385 | let mut granter = auth_granter.lock .await; |
386 | |
387 | let response = granter.token_request .await.unwrap; |
388 | drop; |
389 | |
390 | socket |
391 | .send |
392 | to_vec |
393 | Response |
394 | |
395 | .unwrap, |
396 | |
397 | .await |
398 | .unwrap; |
399 | continue; |
400 | |
401 | => | TokenExtension
402 | let mut granter = auth_granter.lock .await; |
403 | |
404 | let response = granter |
405 | .extension_request |
406 | .await |
407 | .unwrap_or; |
408 | drop; |
409 | |
410 | socket |
411 | .send |
412 | to_vec |
413 | Response |
414 | |
415 | .unwrap, |
416 | |
417 | .await |
418 | .unwrap; |
419 | continue; |
420 | |
421 | , |
422 | => unreachable!, | Response
423 | |
424 | |
425 | |
426 | |
427 | info!; |
428 | |
429 | |
430 | async |
431 | message: MessageKind, |
432 | listeners: & , |
433 | connections: & , |
434 | |
435 | let : = |
436 | match &message |
437 | => | Handshake
438 | todo! |
439 | |
440 | => , | Repository
441 | => todo!, | Authentication
442 | ; |
443 | |
444 | let target = match |
445 | => instance.clone, |
446 | => user.instance.clone, |
447 | => repository.instance.clone, |
448 | _ => unreachable!, |
449 | ; |
450 | |
451 | let mut listeners = listeners.lock .await; |
452 | let listener = listeners.add; |
453 | drop; |
454 | |
455 | let connections = connections.lock .await; |
456 | |
457 | if let Some = connections.instance_connections.get |
458 | connection.sender.send; |
459 | else |
460 | error!; |
461 | |
462 | panic!; |
463 | |
464 | |
465 | drop; |
466 | |
467 | listener |
468 | |
469 |