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

ambee/giterated-api

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

Add authentication back into the API

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c3639d1

Showing ⁨⁨2⁩ changed files⁩ with ⁨⁨29⁩ insertions⁩ and ⁨⁨9⁩ deletions⁩

src/daemon_backend.rs

View file
@@ -1,13 +1,13 @@
1 use std::fmt::Debug;
1 use std::{fmt::Debug, sync::Arc};
2 2
3 3 use futures_util::{SinkExt, StreamExt};
4 4 use giterated_models::{
5 authenticated::Authenticated,
5 authenticated::{Authenticated, AuthenticationSourceProviders},
6 6 error::OperationError,
7 7 message::GiteratedMessage,
8 8 object::{GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse},
9 9 object_backend::ObjectBackend,
10 operation::{GiteratedOperation, GiteratedOperationState},
10 operation::GiteratedOperation,
11 11 };
12 12 use serde::de::DeserializeOwned;
13 13 use tokio_tungstenite::tungstenite::Message;
@@ -15,11 +15,22 @@ use tokio_tungstenite::tungstenite::Message;
15 15 use crate::{DaemonConnectionPool, Socket};
16 16
17 17 #[derive(Clone)]
18 pub struct NetworkOperationState {}
18 pub struct NetworkOperationState {
19 authentication: Vec<Arc<dyn AuthenticationSourceProviders + Send + Sync>>,
20 }
19 21
20 22 impl NetworkOperationState {
21 23 pub fn new() -> Self {
22 Self {}
24 Self {
25 authentication: vec![],
26 }
27 }
28
29 pub fn authenticate<S: AuthenticationSourceProviders + Send + Sync + 'static>(
30 &mut self,
31 source: S,
32 ) {
33 self.authentication.push(Arc::new(source))
23 34 }
24 35 }
25 36
@@ -44,7 +55,10 @@ impl ObjectBackend<NetworkOperationState> for DaemonConnectionPool {
44 55 .await
45 56 .map_err(|e| OperationError::Internal(e.to_string()))?;
46 57
47 let authenticated = Authenticated::new(message);
58 let mut authenticated = Authenticated::new(message);
59 for authentication in &operation_state.authentication {
60 authenticated.append_authentication(authentication.clone());
61 }
48 62
49 63 send_expect(&mut connection, authenticated).await
50 64 }
@@ -68,7 +82,10 @@ impl ObjectBackend<NetworkOperationState> for DaemonConnectionPool {
68 82 .await
69 83 .map_err(|e| OperationError::Internal(e.to_string()))?;
70 84
71 let authenticated = Authenticated::new(message);
85 let mut authenticated = Authenticated::new(message);
86 for authentication in &operation_state.authentication {
87 authenticated.append_authentication(authentication.clone());
88 }
72 89
73 90 let object_raw: ObjectResponse = send_expect(&mut connection, authenticated).await?;
74 91 Ok(unsafe {

src/main.rs

View file
@@ -2,13 +2,16 @@ use std::str::FromStr;
2 2
3 3 use giterated_api::{daemon_backend::NetworkOperationState, DaemonConnectionPool};
4 4 use giterated_models::{
5 authenticated::{InstanceAuthenticator, UserAuthenticator},
5 6 instance::Instance,
6 7 object_backend::ObjectBackend,
7 repository::Repository,
8 repository::{AccessList, Repository},
9 settings::AnySetting,
8 10 user::{DisplayName, User},
9 11 };
10 12
11 13 use color_eyre::eyre::Result;
14 use tokio::{fs::File, io::AsyncReadExt};
12 15 use tracing::info;
13 16
14 17 #[tokio::main]
@@ -22,7 +25,7 @@ async fn main() -> Result<()> {
22 25 )
23 26 .unwrap();
24 27
25 let operation_state = NetworkOperationState::new();
28 let mut operation_state = NetworkOperationState::new();
26 29
27 30 let mut user = pool
28 31 .get_object::<User>("ambee:giterated.dev", &operation_state)