Create `NetworkedSubstack`.
# giterated-protocol - Create `NetworkedSubstack` which will handle all networked operations giterated needs - Add support for `NetworkedSubstack` for both the daemon and client - Pipe everything through but leave APIs temp # `giterated-daemon` - Remove a bunch of random old code, dead code, and files that aren't needed. - Moved all connection handling to `client.rs`, simplified connection logic with new types
parent: tbd commit: 202bb12
1 | use Error; |
2 | use ; |
3 | |
4 | use Instance; |
5 | |
6 | use User; |
7 | |
8 | use ; |
9 | use HashMap; |
10 | use ; |
11 | use ; |
12 | use Table; |
13 | |
14 | |
15 | pub config: Table, |
16 | pub instance: Instance, |
17 | |
18 | |
19 | |
20 | async |
21 | let mut file = open |
22 | self.config |
23 | .as_str |
24 | .unwrap, |
25 | |
26 | .await |
27 | .unwrap; |
28 | |
29 | let mut key = vec!; |
30 | file.read_to_end .await.unwrap; |
31 | |
32 | key |
33 | |
34 | |
35 | pub async |
36 | &mut self, |
37 | user: &User, |
38 | generated_for: &Instance, |
39 | |
40 | let private_key = self.private_key .await; |
41 | |
42 | let encoding_key = from_rsa_pem .unwrap; |
43 | |
44 | let claims = UserTokenMetadata |
45 | user: user.clone, |
46 | generated_for: generated_for.clone, |
47 | exp: |
48 | + from_secs |
49 | .as_secs, |
50 | ; |
51 | |
52 | encode |
53 | & new, |
54 | &claims, |
55 | &encoding_key, |
56 | |
57 | .unwrap |
58 | |
59 | |
60 | pub async |
61 | &mut self, |
62 | issued_for: impl , |
63 | username: String, |
64 | _password: String, |
65 | |
66 | let private_key = |
67 | let mut file = open |
68 | self.config |
69 | .as_str |
70 | .unwrap, |
71 | |
72 | .await |
73 | .unwrap; |
74 | |
75 | let mut key = vec!; |
76 | file.read_to_end .await.unwrap; |
77 | |
78 | key |
79 | ; |
80 | |
81 | let encoding_key = from_rsa_pem .unwrap; |
82 | |
83 | let claims = UserTokenMetadata |
84 | user: User |
85 | username, |
86 | instance: self.instance.clone, |
87 | , |
88 | generated_for: issued_for.to_owned, |
89 | exp: |
90 | + from_secs |
91 | .as_secs, |
92 | ; |
93 | |
94 | let token = encode |
95 | & new, |
96 | &claims, |
97 | &encoding_key, |
98 | |
99 | .unwrap; |
100 | |
101 | Ok |
102 | |
103 | |
104 | pub async |
105 | &mut self, |
106 | issued_for: &Instance, |
107 | key_cache: & , |
108 | token: UserAuthenticationToken, |
109 | |
110 | let mut key_cache = key_cache.lock .await; |
111 | let server_public_key = key_cache.get .await?; |
112 | drop; |
113 | |
114 | let verification_key = from_rsa_pem .unwrap; |
115 | |
116 | let data: = decode |
117 | token.as_ref, |
118 | &verification_key, |
119 | & new, |
120 | |
121 | .unwrap; |
122 | |
123 | if data.claims.generated_for != *issued_for |
124 | panic! |
125 | |
126 | |
127 | let private_key = |
128 | let mut file = open |
129 | self.config |
130 | .as_str |
131 | .unwrap, |
132 | |
133 | .await |
134 | .unwrap; |
135 | |
136 | let mut key = vec!; |
137 | file.read_to_end .await.unwrap; |
138 | |
139 | key |
140 | ; |
141 | |
142 | let encoding_key = from_rsa_pem .unwrap; |
143 | |
144 | let claims = UserTokenMetadata |
145 | // TODO: Probably exploitable |
146 | user: data.claims.user, |
147 | generated_for: issued_for.clone, |
148 | exp: |
149 | + from_secs |
150 | .as_secs, |
151 | ; |
152 | |
153 | let token = encode |
154 | & new, |
155 | &claims, |
156 | &encoding_key, |
157 | |
158 | .unwrap; |
159 | |
160 | Ok |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | pub keys: , |
167 | |
168 | |
169 | |
170 | pub async |
171 | if let Some = self.keys.get |
172 | Ok |
173 | else |
174 | let key = get |
175 | .await? |
176 | .text |
177 | .await?; |
178 | |
179 | self.keys.insert; |
180 | |
181 | Ok |
182 | |
183 | |
184 | |
185 |