Add authentication back into the operation states
parent: tbd commit: 97a26fd
1 | use |
2 | , | SocketAddr
3 | , | Deref
4 | , |
5 | ; |
6 | |
7 | use Error; |
8 | use ; |
9 | |
10 | use |
11 | , |
12 | , | OperationError
13 | , | Instance
14 | ; |
15 | |
16 | use ObjectBackend; |
17 | |
18 | use |
19 | , GiteratedMessage, AnyObject, | AuthenticatedPayload
20 | , | AnyOperation
21 | ; |
22 | use |
23 | , AuthenticatedInstance, AuthenticatedUser, StackOperationState, | GiteratedBackend
24 | ; |
25 | use ; |
26 | use |
27 | , | DecodeRsaPublicKey
28 | , |
29 | , | Sha256
30 | , | Verifier
31 | RsaPublicKey, |
32 | ; |
33 | use Serialize; |
34 | |
35 | use ; |
36 | use ; |
37 | use Table; |
38 | |
39 | use crate:: |
40 | , | AuthenticationTokenGranter
41 | , |
42 | , | DatabaseBackend
43 | , | InstanceConnections
44 | , | PublicKeyCache
45 | ; |
46 | |
47 | use Connections; |
48 | |
49 | pub async |
50 | socket: , |
51 | connections: , |
52 | repository_backend: , |
53 | user_backend: , |
54 | auth_granter: , |
55 | settings_backend: , |
56 | addr: SocketAddr, |
57 | instance: impl , |
58 | instance_connections: , |
59 | config: Table, |
60 | backend: , |
61 | mut operation_state: StackOperationState, |
62 | |
63 | let connection_state = ConnectionState |
64 | socket: new, |
65 | connections, |
66 | repository_backend, |
67 | user_backend, |
68 | auth_granter, |
69 | settings_backend, |
70 | addr, |
71 | instance: instance.to_owned, |
72 | handshaked: new, |
73 | key_cache: default, |
74 | instance_connections: instance_connections.clone, |
75 | config, |
76 | ; |
77 | |
78 | let _handshaked = false; |
79 | let mut key_cache = default; |
80 | |
81 | loop |
82 | let mut socket = connection_state.socket.lock .await; |
83 | let message = socket.next .await; |
84 | drop; |
85 | |
86 | match message |
87 | Some => |
88 | let payload = match message |
89 | => payload, | Binary
90 | => | Ping
91 | let mut socket = connection_state.socket.lock .await; |
92 | let _ = socket.send .await; |
93 | drop; |
94 | continue; |
95 | |
96 | => return, | Close
97 | _ => continue, |
98 | ; |
99 | |
100 | let message: AuthenticatedPayload = deserialize .unwrap; |
101 | |
102 | // Get authentication |
103 | let instance = |
104 | let mut verified_instance: = None; |
105 | for source in &message.source |
106 | if let Instance |
107 | instance, |
108 | signature, |
109 | = source |
110 | |
111 | let public_key = key_cache.get .await.unwrap; |
112 | let public_key = from_pkcs1_pem .unwrap; |
113 | let verifying_key = new; |
114 | |
115 | if verifying_key |
116 | .verify |
117 | &message.payload, |
118 | & try_from .unwrap, |
119 | |
120 | .is_ok |
121 | |
122 | verified_instance = |
123 | Some; |
124 | |
125 | break; |
126 | |
127 | |
128 | |
129 | |
130 | verified_instance |
131 | ; |
132 | |
133 | let user = |
134 | let mut verified_user = None; |
135 | if let Some = &instance |
136 | for source in &message.source |
137 | if let User = source |
138 | // Get token |
139 | let public_key = key_cache.get .await.unwrap; |
140 | |
141 | let token: = decode |
142 | token.as_ref, |
143 | & from_rsa_pem .unwrap, |
144 | & new, |
145 | |
146 | .unwrap; |
147 | |
148 | if token.claims.generated_for != *verified_instance.deref |
149 | // Nope! |
150 | break; |
151 | |
152 | |
153 | if token.claims.user != *user |
154 | // Nope! |
155 | break; |
156 | |
157 | |
158 | verified_user = Some; |
159 | break; |
160 | |
161 | |
162 | |
163 | |
164 | verified_user |
165 | ; |
166 | |
167 | let message: = message.into_message; |
168 | |
169 | operation_state.user = user; |
170 | operation_state.instance = instance; |
171 | |
172 | let result = backend |
173 | .object_operation |
174 | message.object, |
175 | &message.operation, |
176 | message.payload, |
177 | &operation_state, |
178 | |
179 | .await; |
180 | |
181 | // Asking for exploits here |
182 | operation_state.user = None; |
183 | operation_state.instance = None; |
184 | |
185 | // Map result to Vec<u8> on both |
186 | let result = match result |
187 | Ok => Ok, |
188 | Err => Err |
189 | => | Operation
190 | Operation |
191 | |
192 | => Internal, | Internal
193 | => Unhandled, | Unhandled
194 | , |
195 | ; |
196 | |
197 | let mut socket = connection_state.socket.lock .await; |
198 | let _ = socket |
199 | .send |
200 | .await; |
201 | |
202 | drop; |
203 | |
204 | _ => |
205 | return; |
206 | |
207 | |
208 | |
209 | |
210 | // loop { |
211 | // let mut socket = connection_state.socket.lock().await; |
212 | // let message = socket.next().await; |
213 | // drop(socket); |
214 | |
215 | // match message { |
216 | // Some(Ok(message)) => { |
217 | // let payload = match message { |
218 | // Message::Binary(payload) => payload, |
219 | // Message::Ping(_) => { |
220 | // let mut socket = connection_state.socket.lock().await; |
221 | // let _ = socket.send(Message::Pong(vec![])).await; |
222 | // drop(socket); |
223 | // continue; |
224 | // } |
225 | // Message::Close(_) => return, |
226 | // _ => continue, |
227 | // }; |
228 | |
229 | // let message = NetworkMessage(payload.clone()); |
230 | |
231 | // if !handshaked { |
232 | // if handshake_handle(&message, &connection_state).await.is_ok() { |
233 | // if connection_state.handshaked.load(Ordering::SeqCst) { |
234 | // handshaked = true; |
235 | // } |
236 | // } |
237 | // } else { |
238 | // let raw = serde_json::from_slice::<AuthenticatedPayload>(&payload).unwrap(); |
239 | |
240 | // if let Some(target_instance) = &raw.target_instance { |
241 | // if connection_state.instance != *target_instance { |
242 | // // Forward request |
243 | // info!("Forwarding message to {}", target_instance.url); |
244 | // let mut instance_connections = instance_connections.lock().await; |
245 | // let pool = instance_connections.get_or_open(&target_instance).unwrap(); |
246 | // let pool_clone = pool.clone(); |
247 | // drop(pool); |
248 | |
249 | // let result = wrap_forwarded(&pool_clone, raw).await; |
250 | |
251 | // let mut socket = connection_state.socket.lock().await; |
252 | // let _ = socket.send(result).await; |
253 | |
254 | // continue; |
255 | // } |
256 | // } |
257 | |
258 | // let message_type = &raw.message_type; |
259 | |
260 | // match authentication_handle(message_type, &message, &connection_state).await { |
261 | // Err(e) => { |
262 | // let _ = connection_state |
263 | // .send_raw(ConnectionError(e.to_string())) |
264 | // .await; |
265 | // } |
266 | // Ok(true) => continue, |
267 | // Ok(false) => {} |
268 | // } |
269 | |
270 | // match repository_handle(message_type, &message, &connection_state).await { |
271 | // Err(e) => { |
272 | // let _ = connection_state |
273 | // .send_raw(ConnectionError(e.to_string())) |
274 | // .await; |
275 | // } |
276 | // Ok(true) => continue, |
277 | // Ok(false) => {} |
278 | // } |
279 | |
280 | // match user_handle(message_type, &message, &connection_state).await { |
281 | // Err(e) => { |
282 | // let _ = connection_state |
283 | // .send_raw(ConnectionError(e.to_string())) |
284 | // .await; |
285 | // } |
286 | // Ok(true) => continue, |
287 | // Ok(false) => {} |
288 | // } |
289 | |
290 | // match authentication_handle(message_type, &message, &connection_state).await { |
291 | // Err(e) => { |
292 | // let _ = connection_state |
293 | // .send_raw(ConnectionError(e.to_string())) |
294 | // .await; |
295 | // } |
296 | // Ok(true) => continue, |
297 | // Ok(false) => {} |
298 | // } |
299 | |
300 | // error!( |
301 | // "Message completely unhandled: {}", |
302 | // std::str::from_utf8(&payload).unwrap() |
303 | // ); |
304 | // } |
305 | // } |
306 | // Some(Err(e)) => { |
307 | // error!("Closing connection for {:?} for {}", e, addr); |
308 | // return; |
309 | // } |
310 | // _ => { |
311 | // continue; |
312 | // } |
313 | // } |
314 | // } |
315 | |
316 | |
317 | |
318 | |
319 | socket: , |
320 | pub connections: , |
321 | pub repository_backend: , |
322 | pub user_backend: , |
323 | pub auth_granter: , |
324 | pub settings_backend: , |
325 | pub addr: SocketAddr, |
326 | pub instance: Instance, |
327 | pub handshaked: , |
328 | pub key_cache: , |
329 | pub instance_connections: , |
330 | pub config: Table, |
331 | |
332 | |
333 | |
334 | pub async |
335 | let payload = to_string?; |
336 | self.socket |
337 | .lock |
338 | .await |
339 | .send |
340 | .await?; |
341 | |
342 | Ok |
343 | |
344 | |
345 | pub async |
346 | let payload = to_string?; |
347 | self.socket |
348 | .lock |
349 | .await |
350 | .send |
351 | .await?; |
352 | |
353 | Ok |
354 | |
355 | |
356 | pub async |
357 | let mut keys = self.key_cache.lock .await; |
358 | keys.get .await |
359 | |
360 | |
361 |