Add token extension
parent: tbd commit: 86d028f
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!; |
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 | Request
188 | => | CreateRepository
189 | let mut backend = backend.lock .await; |
190 | let response = backend.create_repository .await; |
191 | |
192 | let response = match response |
193 | Ok => response, |
194 | Err => |
195 | error!; |
196 | continue; |
197 | |
198 | ; |
199 | drop; |
200 | |
201 | socket |
202 | .send |
203 | to_vec |
204 | RepositoryMessage |
205 | target: repository.target.clone, |
206 | command: Response |
207 | , | CreateRepository
208 | , |
209 | , |
210 | |
211 | .unwrap, |
212 | |
213 | .await |
214 | .unwrap; |
215 | |
216 | continue; |
217 | |
218 | => | RepositoryFileInspect
219 | let mut backend = backend.lock .await; |
220 | let response = backend.repository_file_inspect; |
221 | |
222 | let response = match response |
223 | Ok => response, |
224 | Err => |
225 | error!; |
226 | continue; |
227 | |
228 | ; |
229 | drop; |
230 | |
231 | socket |
232 | .send |
233 | to_vec |
234 | RepositoryMessage |
235 | target: repository.target.clone, |
236 | command: Response |
237 | RepositoryFileInspection |
238 | response, |
239 | , |
240 | , |
241 | , |
242 | |
243 | .unwrap, |
244 | |
245 | .await |
246 | .unwrap; |
247 | continue; |
248 | |
249 | => | RepositoryInfo
250 | let mut backend = backend.lock .await; |
251 | let response = backend.repository_info .await; |
252 | |
253 | let response = match response |
254 | Ok => response, |
255 | Err => |
256 | error!; |
257 | continue; |
258 | |
259 | ; |
260 | drop; |
261 | |
262 | socket |
263 | .send |
264 | to_vec |
265 | RepositoryMessage |
266 | target: repository.target.clone, |
267 | command: Response |
268 | , | RepositoryInfo
269 | , |
270 | , |
271 | |
272 | .unwrap, |
273 | |
274 | .await |
275 | .unwrap; |
276 | continue; |
277 | |
278 | => | IssuesCount
279 | let mut backend = backend.lock .await; |
280 | let response = backend.issues_count; |
281 | |
282 | let response = match response |
283 | Ok => response, |
284 | Err => |
285 | error!; |
286 | continue; |
287 | |
288 | ; |
289 | drop; |
290 | |
291 | socket |
292 | .send |
293 | to_vec |
294 | RepositoryMessage |
295 | target: repository.target.clone, |
296 | command: Response |
297 | , | IssuesCount
298 | , |
299 | , |
300 | |
301 | .unwrap, |
302 | |
303 | .await |
304 | .unwrap; |
305 | continue; |
306 | |
307 | => | IssueLabels
308 | let mut backend = backend.lock .await; |
309 | let response = backend.issue_labels; |
310 | |
311 | let response = match response |
312 | Ok => response, |
313 | Err => |
314 | error!; |
315 | continue; |
316 | |
317 | ; |
318 | drop; |
319 | socket |
320 | .send |
321 | to_vec |
322 | RepositoryMessage |
323 | target: repository.target.clone, |
324 | command: Response |
325 | , | IssueLabels
326 | , |
327 | , |
328 | |
329 | .unwrap, |
330 | |
331 | .await |
332 | .unwrap; |
333 | continue; |
334 | |
335 | => | Issues
336 | let mut backend = backend.lock .await; |
337 | let response = backend.issues; |
338 | |
339 | let response = match response |
340 | Ok => response, |
341 | Err => |
342 | error!; |
343 | continue; |
344 | |
345 | ; |
346 | drop; |
347 | |
348 | socket |
349 | .send |
350 | to_vec |
351 | RepositoryMessage |
352 | target: repository.target.clone, |
353 | command: Response |
354 | , | Issues
355 | , |
356 | , |
357 | |
358 | .unwrap, |
359 | |
360 | .await |
361 | .unwrap; |
362 | continue; |
363 | |
364 | , |
365 | => | Response
366 | unreachable! |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | if let Authentication = &message |
373 | match authentication |
374 | => match request | Request
375 | => | AuthenticationToken
376 | let mut granter = auth_granter.lock .await; |
377 | |
378 | let response = granter.token_request .await.unwrap; |
379 | drop; |
380 | |
381 | socket |
382 | .send |
383 | to_vec |
384 | Response |
385 | |
386 | .unwrap, |
387 | |
388 | .await |
389 | .unwrap; |
390 | continue; |
391 | |
392 | => | TokenExtension
393 | let mut granter = auth_granter.lock .await; |
394 | |
395 | let response = granter |
396 | .extension_request |
397 | .await |
398 | .unwrap_or_else; |
399 | drop; |
400 | |
401 | socket |
402 | .send |
403 | to_vec |
404 | Response |
405 | |
406 | .unwrap, |
407 | |
408 | .await |
409 | .unwrap; |
410 | continue; |
411 | |
412 | , |
413 | => unreachable!, | Response
414 | |
415 | |
416 | |
417 | |
418 | info!; |
419 | |
420 | |
421 | async |
422 | message: MessageKind, |
423 | listeners: & , |
424 | connections: & , |
425 | |
426 | let : = |
427 | match &message |
428 | => | Handshake
429 | todo! |
430 | |
431 | => , | Repository
432 | => todo!, | Authentication
433 | ; |
434 | |
435 | let target = match |
436 | => instance.clone, |
437 | => user.instance.clone, |
438 | => repository.instance.clone, |
439 | _ => unreachable!, |
440 | ; |
441 | |
442 | let mut listeners = listeners.lock .await; |
443 | let listener = listeners.add; |
444 | drop; |
445 | |
446 | let connections = connections.lock .await; |
447 | |
448 | if let Some = connections.instance_connections.get |
449 | connection.sender.send; |
450 | else |
451 | error!; |
452 | |
453 | panic!; |
454 | |
455 | |
456 | drop; |
457 | |
458 | listener |
459 | |
460 |