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

ambee/giterated

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

Major post-refactor cleanup

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f90d7fb

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