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

ambee/giterated

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

Fixes!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨398622e

Showing ⁨⁨9⁩ changed files⁩ with ⁨⁨104⁩ insertions⁩ and ⁨⁨76⁩ deletions⁩

src/authentication.rs

View file
@@ -107,7 +107,9 @@ impl AuthenticationTokenGranter {
107 107 )
108 108 .unwrap();
109 109
110 Ok(AuthenticationTokenResponse { token: UserAuthenticationToken::from(token) })
110 Ok(AuthenticationTokenResponse {
111 token: UserAuthenticationToken::from(token),
112 })
111 113 }
112 114
113 115 pub async fn extension_request(

src/backend/git.rs

View file
@@ -262,8 +262,7 @@ impl RepositoryBackend for GitBackend {
262 262 error!("Failed creating repository on disk!? {:?}", err);
263 263
264 264 // Delete repository from database
265 self
266 .delete_by_owner_user_name(&request.owner, request.name.as_str())
265 self.delete_by_owner_user_name(&request.owner, request.name.as_str())
267 266 .await?;
268 267
269 268 // ???

src/backend/mod.rs

View file
@@ -6,6 +6,7 @@ pub mod user;
6 6 use anyhow::Error;
7 7 use async_trait::async_trait;
8 8
9 use crate::backend::git::GitBackendError;
9 10 use crate::{
10 11 messages::{
11 12 authentication::{
@@ -28,7 +29,6 @@ use crate::{
28 29 user::User,
29 30 },
30 31 };
31 use crate::backend::git::GitBackendError;
32 32
33 33 #[async_trait]
34 34 pub trait RepositoryBackend: IssuesBackend {

src/connection/authentication.rs

View file
@@ -11,29 +11,27 @@ use super::wrapper::ConnectionState;
11 11 use super::HandlerUnhandled;
12 12
13 13 pub async fn authentication_handle(
14 message_type: &str,
14 15 message: &NetworkMessage,
15 16 state: &ConnectionState,
16 17 ) -> Result<(), Error> {
17 if register_account_request
18 .handle_message(&message, state)
19 .await
20 .is_ok()
21 {
22 Ok(())
23 } else if authentication_token_request
24 .handle_message(&message, state)
25 .await
26 .is_ok()
27 {
28 Ok(())
29 } else if token_extension_request
30 .handle_message(&message, state)
31 .await
32 .is_ok()
33 {
34 Ok(())
35 } else {
36 Err(Error::from(HandlerUnhandled))
18 match message_type {
19 "&giterated_daemon::messages::authentication::RegisterAccountRequest" => {
20 register_account_request
21 .handle_message(&message, state)
22 .await
23 }
24 "&giterated_daemon::messages::authentication::AuthenticationTokenRequest" => {
25 authentication_token_request
26 .handle_message(&message, state)
27 .await
28 }
29 "&giterated_daemon::messages::authentication::TokenExtensionRequest" => {
30 token_extension_request
31 .handle_message(&message, state)
32 .await
33 }
34 _ => Err(Error::from(HandlerUnhandled)),
37 35 }
38 36 }
39 37

src/connection/handshake.rs

View file
@@ -7,7 +7,7 @@ use crate::{
7 7 connection::ConnectionError,
8 8 messages::handshake::{HandshakeFinalize, HandshakeResponse, InitiateHandshake},
9 9 model::authenticated::{AuthenticatedInstance, Message, MessageHandler, NetworkMessage, State},
10 validate_version,
10 validate_version, version,
11 11 };
12 12
13 13 use super::{wrapper::ConnectionState, HandlerUnhandled};
@@ -42,7 +42,6 @@ pub async fn handshake_handle(
42 42 async fn initiate_handshake(
43 43 Message(initiation): Message<InitiateHandshake>,
44 44 State(connection_state): State<ConnectionState>,
45 _instance: AuthenticatedInstance,
46 45 ) -> Result<(), HandshakeError> {
47 46 if !validate_version(&initiation.version) {
48 47 error!(
@@ -59,7 +58,10 @@ async fn initiate_handshake(
59 58 Ok(())
60 59 } else {
61 60 connection_state
62 .send(HandshakeFinalize { success: true })
61 .send(HandshakeResponse {
62 identity: connection_state.instance.clone(),
63 version: version(),
64 })
63 65 .await
64 66 .map_err(|e| HandshakeError::SendError(e))?;
65 67
@@ -70,7 +72,6 @@ async fn initiate_handshake(
70 72 async fn handshake_response(
71 73 Message(response): Message<HandshakeResponse>,
72 74 State(connection_state): State<ConnectionState>,
73 _instance: AuthenticatedInstance,
74 75 ) -> Result<(), HandshakeError> {
75 76 if !validate_version(&response.version) {
76 77 error!(
@@ -98,7 +99,6 @@ async fn handshake_response(
98 99 async fn handshake_finalize(
99 100 Message(finalize): Message<HandshakeFinalize>,
100 101 State(connection_state): State<ConnectionState>,
101 _instance: AuthenticatedInstance,
102 102 ) -> Result<(), HandshakeError> {
103 103 if !finalize.success {
104 104 error!("Error during handshake, aborting connection");

src/connection/repository.rs

View file
@@ -1,5 +1,6 @@
1 1 use anyhow::Error;
2 2
3 use crate::backend::git::GitBackendError;
3 4 use crate::{
4 5 messages::repository::{
5 6 RepositoryCreateRequest, RepositoryFileInspectRequest, RepositoryInfoRequest,
@@ -7,40 +8,36 @@ use crate::{
7 8 },
8 9 model::authenticated::{AuthenticatedUser, Message, MessageHandler, NetworkMessage, State},
9 10 };
10 use crate::backend::git::GitBackendError;
11 11
12 12 use super::{wrapper::ConnectionState, HandlerUnhandled};
13 13
14 14 pub async fn repository_handle(
15 message_type: &str,
15 16 message: &NetworkMessage,
16 17 state: &ConnectionState,
17 18 ) -> Result<(), Error> {
18 if create_repository
19 .handle_message(&message, state)
20 .await
21 .is_ok()
22 {
23 Ok(())
24 } else if repository_file_inspect
25 .handle_message(&message, state)
26 .await
27 .is_ok()
28 {
29 Ok(())
30 } else if repository_info
31 .handle_message(&message, state)
32 .await
33 .is_ok()
34 {
35 Ok(())
36 } else if issues_count.handle_message(&message, state).await.is_ok() {
37 Ok(())
38 } else if issue_labels.handle_message(&message, state).await.is_ok() {
39 Ok(())
40 } else if issues.handle_message(&message, state).await.is_ok() {
41 Ok(())
42 } else {
43 Err(Error::from(HandlerUnhandled))
19 match message_type {
20 "&giterated_daemon::messages::repository::RepositoryCreateRequest" => {
21 create_repository.handle_message(&message, state).await
22 }
23 "&giterated_daemon::messages::repository::RepositoryFileInspectRequest" => {
24 repository_file_inspect
25 .handle_message(&message, state)
26 .await
27 }
28 "&giterated_daemon::messages::repository::RepositoryInfoRequest" => {
29 repository_info.handle_message(&message, state).await
30 }
31 "&giterated_daemon::messages::repository::RepositoryIssuesCountRequest" => {
32 issues_count.handle_message(&message, state).await
33 }
34 "&giterated_daemon::messages::repository::RepositoryIssueLabelsRequest" => {
35 issue_labels.handle_message(&message, state).await
36 }
37 "&giterated_daemon::messages::repository::RepositoryIssuesRequest" => {
38 issues.handle_message(&message, state).await
39 }
40 _ => Err(Error::from(HandlerUnhandled)),
44 41 }
45 42 }
46 43

src/connection/user.rs

View file
@@ -1,4 +1,5 @@
1 1 use anyhow::Error;
2 use serde_json::Value;
2 3
3 4 use crate::{
4 5 messages::user::{
@@ -10,17 +11,25 @@ use crate::{
10 11
11 12 use super::{wrapper::ConnectionState, HandlerUnhandled};
12 13
13 pub async fn user_handle(message: &NetworkMessage, state: &ConnectionState) -> Result<(), Error> {
14 if display_name.handle_message(&message, state).await.is_ok() {
15 Ok(())
16 } else if display_image.handle_message(&message, state).await.is_ok() {
17 Ok(())
18 } else if bio.handle_message(&message, state).await.is_ok() {
19 Ok(())
20 } else if repositories.handle_message(&message, state).await.is_ok() {
21 Ok(())
22 } else {
23 Err(Error::from(HandlerUnhandled))
14 pub async fn user_handle(
15 message_type: &str,
16 message: &NetworkMessage,
17 state: &ConnectionState,
18 ) -> Result<(), Error> {
19 match message_type {
20 "&giterated_daemon::messages::user::UserDisplayNameRequest" => {
21 display_name.handle_message(&message, state).await
22 }
23 "&giterated_daemon::messages::user::UserDisplayImageRequest" => {
24 display_image.handle_message(&message, state).await
25 }
26 "&giterated_daemon::messages::user::UserBioRequest" => {
27 bio.handle_message(&message, state).await
28 }
29 "&giterated_daemon::messages::user::UserRepositoriesRequest" => {
30 repositories.handle_message(&message, state).await
31 }
32 _ => Err(Error::from(HandlerUnhandled)),
24 33 }
25 34 }
26 35

src/connection/wrapper.rs

View file
@@ -9,6 +9,7 @@ use std::{
9 9 use anyhow::Error;
10 10 use futures_util::{SinkExt, StreamExt};
11 11 use serde::Serialize;
12 use serde_json::Value;
12 13 use tokio::{net::TcpStream, sync::Mutex};
13 14 use tokio_tungstenite::{tungstenite::Message, WebSocketStream};
14 15
@@ -64,7 +65,7 @@ pub async fn connection_wrapper(
64 65 _ => continue,
65 66 };
66 67
67 let message = NetworkMessage(payload);
68 let message = NetworkMessage(payload.clone());
68 69
69 70 if !handshaked {
70 71 if handshake_handle(&message, &connection_state).await.is_ok() {
@@ -73,25 +74,41 @@ pub async fn connection_wrapper(
73 74 }
74 75 }
75 76 } else {
76 if authentication_handle(&message, &connection_state)
77 let raw = serde_json::from_slice::<Value>(&payload).unwrap();
78 let message_type = raw.get("message_type").unwrap().as_str().unwrap();
79
80 if authentication_handle(message_type, &message, &connection_state)
77 81 .await
78 82 .is_ok()
79 83 {
80 84 continue;
81 } else if repository_handle(&message, &connection_state).await.is_ok() {
85 } else if repository_handle(message_type, &message, &connection_state)
86 .await
87 .is_ok()
88 {
82 89 continue;
83 } else if user_handle(&message, &connection_state).await.is_ok() {
90 } else if user_handle(message_type, &message, &connection_state)
91 .await
92 .is_ok()
93 {
84 94 continue;
85 95 } else {
86 error!("Message completely unhandled");
96 error!(
97 "Message completely unhandled: {}",
98 std::str::from_utf8(&payload).unwrap()
99 );
87 100 continue;
88 101 }
89 102 }
90 103 }
91 _ => {
92 error!("Closing connection for {}", addr);
104 Some(Err(e)) => {
105 error!("Closing connection for {:?} for {}", e, addr);
93 106 return;
94 107 }
108 _ => {
109 info!("Unhandled");
110 continue;
111 }
95 112 }
96 113 }
97 114 }
@@ -110,10 +127,12 @@ pub struct ConnectionState {
110 127
111 128 impl ConnectionState {
112 129 pub async fn send<T: Serialize>(&self, message: T) -> Result<(), Error> {
130 let payload = serde_json::to_string(&message)?;
131 info!("Sending payload: {}", &payload);
113 132 self.socket
114 133 .lock()
115 134 .await
116 .send(Message::Binary(serde_json::to_vec(&message)?))
135 .send(Message::Binary(payload.into_bytes()))
117 136 .await?;
118 137
119 138 Ok(())

src/model/authenticated.rs

View file
@@ -243,7 +243,11 @@ impl FromMessage<ConnectionState> for AuthenticatedInstance {
243 243 .source
244 244 .iter()
245 245 .filter_map(|auth| {
246 if let AuthenticationSource::Instance { instance, signature } = auth {
246 if let AuthenticationSource::Instance {
247 instance,
248 signature,
249 } = auth
250 {
247 251 Some((instance, signature))
248 252 } else {
249 253 None