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

ambee/giterated

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

Expose errors

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨6b2125c

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