Git repository hosting, collaboration, and discovery for the Fediverse.
Many things
parent: tbd commit: d782588
1 | use Infallible; |
2 | use FromStr; |
3 | use ; |
4 | |
5 | use ; |
6 | use ; |
7 | use UnvalidatedUserAuthenticated; |
8 | use RepositoryVisibility; |
9 | use User; |
10 | use ; |
11 | use |
12 | , |
13 | |
14 | |
15 | AuthenticationMessage, AuthenticationRequest, AuthenticationResponse, |
16 | AuthenticationTokenRequest, TokenExtensionRequest, |
17 | , |
18 | |
19 | CreateRepositoryRequest, RepositoryInfoRequest, RepositoryMessage, |
20 | RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
21 | , |
22 | InstanceAuthenticated, MessageKind, |
23 | , |
24 | |
25 | , | Instance
26 | , |
27 | , |
28 | ; |
29 | use Serialize; |
30 | use TcpStream; |
31 | use ; |
32 | |
33 | type Socket = ; |
34 | |
35 | |
36 | extern crate tracing; |
37 | |
38 | |
39 | our_instance: Instance, |
40 | our_private_key: , |
41 | our_public_key: , |
42 | target_instance: , |
43 | |
44 | |
45 | |
46 | type Error: Error + Send + Sync + 'static; |
47 | |
48 | ; |
49 | |
50 | |
51 | |
52 | type Error = Err; |
53 | |
54 | |
55 | from_str |
56 | |
57 | |
58 | |
59 | |
60 | type Error = Infallible; |
61 | |
62 | |
63 | Ok |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | Ok |
70 | our_instance: instance.into_instance?, |
71 | our_private_key: None, |
72 | our_public_key: None, |
73 | target_instance: None, |
74 | |
75 | |
76 | |
77 | |
78 | instance: impl AsInstance, |
79 | other: impl AsInstance, |
80 | |
81 | Ok |
82 | our_instance: instance.into_instance?, |
83 | our_private_key: None, |
84 | our_public_key: None, |
85 | target_instance: Some, |
86 | |
87 | |
88 | |
89 | |
90 | self.our_private_key = Some; |
91 | |
92 | self |
93 | |
94 | |
95 | |
96 | self.our_public_key = Some; |
97 | |
98 | self |
99 | |
100 | |
101 | pub async |
102 | Ok |
103 | self.our_instance.clone, |
104 | self.our_private_key.clone .unwrap, |
105 | self.our_public_key.clone .unwrap, |
106 | self.target_instance.clone, |
107 | |
108 | .await? |
109 | |
110 | |
111 | |
112 | |
113 | pub connection: Socket, |
114 | pub our_instance: Instance, |
115 | pub our_private_key: String, |
116 | pub our_public_key: String, |
117 | pub target_instance: , |
118 | pub target_public_key: , |
119 | |
120 | |
121 | |
122 | pub async |
123 | local_instance: Instance, |
124 | private_key: String, |
125 | public_key: String, |
126 | target_instance: , |
127 | |
128 | let connection = Self connect_to |
129 | target_instance |
130 | .clone |
131 | .unwrap_or_else, |
132 | |
133 | .await?; |
134 | |
135 | let mut api = GiteratedApi |
136 | connection, |
137 | our_instance: local_instance, |
138 | our_private_key: private_key, |
139 | our_public_key: public_key, |
140 | target_instance, |
141 | target_public_key: None, |
142 | ; |
143 | |
144 | // Handle handshake |
145 | api.handle_handshake .await?; |
146 | |
147 | Ok |
148 | |
149 | |
150 | pub async |
151 | if let Some = &self.target_public_key |
152 | public_key.clone |
153 | else |
154 | let key = get |
155 | "https://{}/.giterated/pubkey.pem" |
156 | self.target_instance |
157 | .as_ref |
158 | .unwrap_or_else |
159 | .url |
160 | ) |
161 | .await |
162 | .unwrap |
163 | .text |
164 | .await |
165 | .unwrap; |
166 | |
167 | self.target_public_key = Some; |
168 | |
169 | key |
170 | |
171 | |
172 | |
173 | /// Register on an [`Instance`]. |
174 | /// |
175 | /// # Authorization |
176 | /// - Must be made by the same instance its being sent to |
177 | pub async |
178 | &mut self, |
179 | username: String, |
180 | email: , |
181 | password: String, |
182 | |
183 | let message = new |
184 | RegisterAccountRequest |
185 | username, |
186 | email, |
187 | password, |
188 | , |
189 | self.our_instance.clone, |
190 | self.our_private_key.clone, |
191 | |
192 | .unwrap; |
193 | |
194 | self.send_message |
195 | , | Request
196 | |
197 | .await?; |
198 | |
199 | while let Ok = self.next_payload .await |
200 | if let Ok |
201 | , | RegisterAccount
202 | = from_slice |
203 | |
204 | return Ok; |
205 | |
206 | |
207 | |
208 | unreachable! |
209 | |
210 | |
211 | /// Create repository on the target instance. |
212 | pub async |
213 | &mut self, |
214 | user_token: String, |
215 | name: String, |
216 | description: , |
217 | visibility: RepositoryVisibility, |
218 | default_branch: String, |
219 | owner: User, |
220 | |
221 | let target_respository = Repository |
222 | owner: owner.clone, |
223 | name: name.clone, |
224 | instance: self |
225 | .target_instance |
226 | .as_ref |
227 | .unwrap_or |
228 | .clone, |
229 | ; |
230 | |
231 | let request = CreateRepositoryRequest |
232 | name, |
233 | description, |
234 | visibility, |
235 | default_branch, |
236 | owner, |
237 | ; |
238 | |
239 | let message = |
240 | new |
241 | .unwrap; |
242 | |
243 | self.send_message |
244 | target: target_respository, |
245 | command: Request, |
246 | |
247 | .await?; |
248 | |
249 | while let Ok = self.next_payload .await |
250 | if let Ok |
251 | command: |
252 | , | Response
253 | .. |
254 | = from_slice |
255 | |
256 | return Ok; |
257 | |
258 | |
259 | |
260 | unreachable! |
261 | |
262 | |
263 | pub async |
264 | &mut self, |
265 | token: &str, |
266 | repository: Repository, |
267 | |
268 | let message = new |
269 | RepositoryInfoRequest |
270 | repository: repository.clone, |
271 | extra_metadata: true, |
272 | rev: None, |
273 | path: None, |
274 | , |
275 | token.to_string, |
276 | self.our_private_key.clone, |
277 | |
278 | .unwrap; |
279 | |
280 | self.send_message |
281 | target: repository.clone, |
282 | command: Request, |
283 | |
284 | .await?; |
285 | |
286 | loop |
287 | // while let Ok(payload) = Self::next_payload(&mut socket).await { |
288 | let payload = match self.next_payload .await |
289 | Ok => payload, |
290 | Err => |
291 | error!; |
292 | continue; |
293 | |
294 | ; |
295 | |
296 | if let Ok |
297 | command: |
298 | , | Response
299 | .. |
300 | = from_slice |
301 | |
302 | return Ok; |
303 | |
304 | |
305 | |
306 | |
307 | /// Requests an authentication token for the given login. |
308 | /// |
309 | /// # Authorization |
310 | /// This request can only be sent to the same instance from which |
311 | /// it is issued. |
312 | pub async |
313 | &mut self, |
314 | secret_key: String, |
315 | username: String, |
316 | password: String, |
317 | |
318 | let request = new |
319 | AuthenticationTokenRequest |
320 | secret_key, |
321 | username, |
322 | password, |
323 | , |
324 | self.our_instance.clone, |
325 | include_str! .to_string, |
326 | |
327 | .unwrap; |
328 | |
329 | self.send_message |
330 | , | Request
331 | |
332 | .await?; |
333 | |
334 | loop |
335 | // while let Ok(payload) = Self::next_payload(&mut socket).await { |
336 | let payload = match self.next_payload .await |
337 | Ok => payload, |
338 | Err => |
339 | error!; |
340 | continue; |
341 | |
342 | ; |
343 | |
344 | if let Ok |
345 | , | AuthenticationToken
346 | = from_slice |
347 | |
348 | return Ok; |
349 | |
350 | |
351 | |
352 | |
353 | /// Requests a new token for the given login. |
354 | /// |
355 | /// # Authorization |
356 | /// This request can only be sent to the same instance from which |
357 | /// it is issued. |
358 | pub async |
359 | &mut self, |
360 | secret_key: String, |
361 | token: String, |
362 | |
363 | let request = new |
364 | TokenExtensionRequest , |
365 | self.our_instance.clone, |
366 | self.our_private_key.clone, |
367 | |
368 | .unwrap; |
369 | |
370 | self.send_message |
371 | , | Request
372 | |
373 | .await?; |
374 | |
375 | while let Ok = self.next_payload .await |
376 | if let Ok |
377 | , | TokenExtension
378 | = from_slice |
379 | |
380 | return Ok; |
381 | |
382 | |
383 | |
384 | todo! |
385 | |
386 | |
387 | async |
388 | let url = &instance.url; |
389 | info! |
390 | "Connecting to {}", |
391 | format! |
392 | ; |
393 | let = |
394 | connect_async .await?; |
395 | info!; |
396 | |
397 | Ok |
398 | |
399 | |
400 | async |
401 | // Send handshake initiation |
402 | |
403 | self.send_message |
404 | InitiateHandshake |
405 | identity: self.our_instance.clone, |
406 | version: version, |
407 | , |
408 | |
409 | .await?; |
410 | |
411 | while let Some = self.connection.next .await |
412 | let message = match message |
413 | Ok => message, |
414 | Err => |
415 | error!; |
416 | continue; |
417 | |
418 | ; |
419 | |
420 | let payload = match message |
421 | => text.into_bytes, | Text
422 | => bytes, | Binary
423 | => continue, | Ping
424 | => continue, | Pong
425 | => | Close
426 | panic! |
427 | |
428 | _ => unreachable!, |
429 | ; |
430 | |
431 | info!; |
432 | |
433 | let message = match |
434 | Ok => message, |
435 | Err => |
436 | error!; |
437 | continue; |
438 | |
439 | ; |
440 | |
441 | if let Handshake = message |
442 | match handshake |
443 | => unimplemented!, | Initiate
444 | => | Response
445 | |
446 | let message = if !validate_version |
447 | error! |
448 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
449 | version, |
450 | response.version |
451 | ; |
452 | |
453 | HandshakeFinalize |
454 | else |
455 | info!; |
456 | |
457 | HandshakeFinalize |
458 | ; |
459 | // Send HandshakeMessage::Finalize |
460 | self.send_message |
461 | message |
462 | |
463 | .await?; |
464 | |
465 | => | Finalize
466 | if finalize.success |
467 | return Ok; |
468 | else |
469 | panic! |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | Ok |
477 | |
478 | |
479 | async |
480 | self.connection |
481 | .send |
482 | .await?; |
483 | Ok |
484 | |
485 | |
486 | async |
487 | while let Some = self.connection.next .await |
488 | let message = message?; |
489 | |
490 | match message |
491 | => return Ok, | Text
492 | => return Ok, | Binary
493 | => continue, | Ping
494 | => continue, | Pong
495 | => | Close
496 | panic! |
497 | |
498 | _ => unreachable!, |
499 | |
500 | |
501 | |
502 | unreachable! |
503 | |
504 | |
505 |