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

ambee/giterated

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

Fixes for authentication!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨969964d

⁨src/connection/authentication.rs⁩ - ⁨3718⁩ bytes
Raw
1 use anyhow::Error;
2 use thiserror::Error;
3
4 use crate::messages::authentication::{
5 AuthenticationTokenRequest, RegisterAccountRequest, TokenExtensionRequest,
6 };
7 use crate::model::authenticated::{AuthenticatedInstance, NetworkMessage, State};
8 use crate::model::authenticated::{Message, MessageHandler};
9
10 use super::wrapper::ConnectionState;
11
12 pub async fn authentication_handle(
13 message_type: &str,
14 message: &NetworkMessage,
15 state: &ConnectionState,
16 ) -> Result<bool, Error> {
17 match message_type {
18 "&giterated_daemon::messages::authentication::RegisterAccountRequest" => {
19 register_account_request
20 .handle_message(&message, state)
21 .await?;
22
23 Ok(true)
24 }
25 "&giterated_daemon::messages::authentication::AuthenticationTokenRequest" => {
26 authentication_token_request
27 .handle_message(&message, state)
28 .await?;
29
30 Ok(true)
31 }
32 "&giterated_daemon::messages::authentication::TokenExtensionRequest" => {
33 token_extension_request
34 .handle_message(&message, state)
35 .await?;
36
37 Ok(true)
38 }
39 _ => Ok(false),
40 }
41 }
42
43 async fn register_account_request(
44 State(connection_state): State<ConnectionState>,
45 Message(request): Message<RegisterAccountRequest>,
46 instance: AuthenticatedInstance,
47 ) -> Result<(), AuthenticationConnectionError> {
48 if *instance.inner() != connection_state.instance {
49 return Err(AuthenticationConnectionError::SameInstance);
50 }
51
52 let mut user_backend = connection_state.user_backend.lock().await;
53
54 let response = user_backend
55 .register(request.clone())
56 .await
57 .map_err(|e| AuthenticationConnectionError::Registration(e))?;
58 drop(user_backend);
59
60 connection_state
61 .send(response)
62 .await
63 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
64
65 Ok(())
66 }
67
68 async fn authentication_token_request(
69 State(connection_state): State<ConnectionState>,
70 Message(request): Message<AuthenticationTokenRequest>,
71 instance: AuthenticatedInstance,
72 ) -> Result<(), AuthenticationConnectionError> {
73 let issued_for = instance.inner().clone();
74
75 let mut token_granter = connection_state.auth_granter.lock().await;
76
77 let response = token_granter
78 .token_request(issued_for, request.username, request.password)
79 .await
80 .map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?;
81
82 connection_state
83 .send(response)
84 .await
85 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
86
87 Ok(())
88 }
89
90 async fn token_extension_request(
91 State(connection_state): State<ConnectionState>,
92 Message(request): Message<TokenExtensionRequest>,
93 instance: AuthenticatedInstance,
94 ) -> Result<(), AuthenticationConnectionError> {
95 let issued_for = instance.inner().clone();
96
97 let mut token_granter = connection_state.auth_granter.lock().await;
98
99 let response = token_granter
100 .extension_request(&issued_for, request.token)
101 .await
102 .map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?;
103
104 connection_state
105 .send(response)
106 .await
107 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
108
109 Ok(())
110 }
111
112 #[derive(Debug, Error)]
113 pub enum AuthenticationConnectionError {
114 #[error("the request was invalid")]
115 InvalidRequest,
116 #[error("request must be from the same instance")]
117 SameInstance,
118 #[error("issue during registration {0}")]
119 Registration(Error),
120 #[error("sending error")]
121 Sending(Error),
122 #[error("error issuing token")]
123 TokenIssuance(Error),
124 }
125