JavaScript is disabled, refresh for a better experience. ambee/giterated-api

ambee/giterated-api

Git repository hosting, collaboration, and discovery for the Fediverse.

Many things

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨d782588

⁨src/main.rs⁩ - ⁨3852⁩ bytes
Raw
1 use std::{collections::BTreeMap, str::FromStr, time::SystemTime};
2
3 use chrono::{DateTime, NaiveDateTime, Utc};
4 use giterated_api::{GiteratedApi, GiteratedApiBuilder};
5 use giterated_daemon::{
6 messages::repository::CreateRepositoryRequest,
7 model::{
8 instance::Instance,
9 repository::{Repository, RepositoryVisibility},
10 user::User,
11 },
12 };
13 use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, TokenData, Validation};
14 use serde::{Deserialize, Serialize};
15 // use jwt::SignWithKey;
16
17 #[macro_use]
18 extern crate tracing;
19
20 #[tokio::main]
21 async fn main() {
22 tracing_subscriber::fmt::init();
23 // info!(
24 // "Response from Daemon: {:?}",
25 // GiteratedApi::repository_info(Repository {
26 // name: String::from("foo"),
27 // instance: Instance {
28 // url: String::from("127.0.0.1:8080")
29 // }
30 // })
31 // .await
32 // );
33
34 // let encoding_key =
35 // EncodingKey::from_rsa_pem(include_bytes!("example_keys/giterated.key")).unwrap();
36
37 // let claims = UserTokenMetadata {
38 // user: User {
39 // username: String::from("ambee"),
40 // instance: Instance {
41 // url: String::from("giterated.dev"),
42 // },
43 // },
44 // generated_for: Instance {
45 // url: String::from("giterated.dev"),
46 // },
47 // exp: SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs(),
48 // };
49
50 // let token = encode(
51 // &jsonwebtoken::Header::new(Algorithm::RS256),
52 // &claims,
53 // &encoding_key,
54 // )
55 // .unwrap();
56
57 let mut api = GiteratedApiBuilder::from_local("giterated.dev")
58 .unwrap()
59 .private_key(include_str!("example_keys/giterated.key"))
60 .public_key(include_str!("example_keys/giterated.key"))
61 .build()
62 .await
63 .unwrap();
64
65 info!("Lets try to make an account!");
66
67 let response = api
68 .register(
69 String::from("ambee"),
70 None,
71 String::from("lolthisisinthecommithistory"),
72 )
73 .await;
74
75 info!("Registration response: {:?}", response);
76
77 let token = api
78 .authentication_token(
79 String::from("foobar"),
80 String::from("ambee"),
81 String::from("password"),
82 )
83 .await
84 .unwrap();
85
86 println!("Token: {}", token);
87
88 let public_key = api.public_key().await;
89
90 println!("Server public key:\n{}", public_key);
91 let verification_key = DecodingKey::from_rsa_pem(public_key.as_bytes()).unwrap();
92 let data: TokenData<UserTokenMetadata> = decode(
93 &token,
94 &verification_key,
95 &Validation::new(Algorithm::RS256),
96 )
97 .unwrap();
98
99 println!("The token was valid! Data:\n{:#?}", data.claims);
100
101 info!("Lets extend that token!");
102
103 let new_token = api
104 .extend_token(String::from("foobar"), token.clone())
105 .await
106 .unwrap();
107 info!("New Token Returned:\n{:?}", new_token);
108
109 info!("Try to create a repository? uwu");
110
111 let repository = api
112 .create_repository(
113 new_token.unwrap(),
114 String::from("super-repository"),
115 None,
116 RepositoryVisibility::Public,
117 String::from("master"),
118 User::from_str("ambee:giterated.dev").unwrap(),
119 )
120 .await
121 .unwrap();
122
123 assert!(repository);
124
125 info!("Lets view our repository!");
126
127 let view = api
128 .repository_info(
129 &token,
130 Repository::from_str("ambee:giterated.dev/[email protected]").unwrap(),
131 )
132 .await
133 .unwrap();
134
135 info!("Repository Info:\n{:#?}", view);
136 }
137
138 #[derive(Debug, Serialize, Deserialize)]
139 struct UserTokenMetadata {
140 user: User,
141 generated_for: Instance,
142 exp: u64,
143 }
144