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

ambee/giterated

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

Major connection refactor base

Type: Refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨8dcc111

⁨src/connection/authentication.rs⁩ - ⁨5073⁩ bytes
Raw
1 use anyhow::Error;
2 use thiserror::Error;
3
4 use crate::messages::authentication::{AuthenticationMessage, AuthenticationResponse};
5 use crate::model::authenticated::MessageHandler;
6 use crate::{
7 messages::{authentication::AuthenticationRequest, MessageKind},
8 model::authenticated::{AuthenticatedInstance, NetworkMessage, State},
9 };
10
11 use super::wrapper::ConnectionState;
12 use super::HandlerUnhandled;
13
14 pub async fn authentication_handle(
15 message: &NetworkMessage,
16 state: &ConnectionState,
17 ) -> Result<(), Error> {
18 let message_kind: MessageKind = serde_json::from_slice(&message.0).unwrap();
19
20 match message_kind {
21 MessageKind::Authentication(AuthenticationMessage::Request(request)) => match request {
22 AuthenticationRequest::RegisterAccount(_) => {
23 register_account_request
24 .handle_message(message, state)
25 .await
26 }
27 AuthenticationRequest::AuthenticationToken(_) => {
28 authentication_token_request
29 .handle_message(message, state)
30 .await
31 }
32 AuthenticationRequest::TokenExtension(_) => {
33 token_extension_request.handle_message(message, state).await
34 }
35 },
36 _ => Err(Error::from(HandlerUnhandled)),
37 }
38 }
39
40 async fn register_account_request(
41 State(connection_state): State<ConnectionState>,
42 request: MessageKind,
43 instance: AuthenticatedInstance,
44 ) -> Result<(), AuthenticationConnectionError> {
45 let request = if let MessageKind::Authentication(AuthenticationMessage::Request(
46 AuthenticationRequest::RegisterAccount(request),
47 )) = request
48 {
49 request
50 } else {
51 return Err(AuthenticationConnectionError::InvalidRequest);
52 };
53
54 if *instance.inner() != connection_state.instance {
55 return Err(AuthenticationConnectionError::SameInstance);
56 }
57
58 let mut user_backend = connection_state.user_backend.lock().await;
59
60 let response = user_backend
61 .register(request.clone())
62 .await
63 .map_err(|e| AuthenticationConnectionError::Registration(e))?;
64 drop(user_backend);
65
66 connection_state
67 .send(MessageKind::Authentication(
68 AuthenticationMessage::Response(AuthenticationResponse::RegisterAccount(response)),
69 ))
70 .await
71 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
72
73 Ok(())
74 }
75
76 async fn authentication_token_request(
77 State(connection_state): State<ConnectionState>,
78 request: MessageKind,
79 instance: AuthenticatedInstance,
80 ) -> Result<(), AuthenticationConnectionError> {
81 let request = if let MessageKind::Authentication(AuthenticationMessage::Request(
82 AuthenticationRequest::AuthenticationToken(request),
83 )) = request
84 {
85 request
86 } else {
87 return Err(AuthenticationConnectionError::InvalidRequest);
88 };
89
90 let issued_for = instance.inner().clone();
91
92 let mut token_granter = connection_state.auth_granter.lock().await;
93
94 let response = token_granter
95 .token_request(issued_for, request.username, request.password)
96 .await
97 .map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?;
98
99 connection_state
100 .send(MessageKind::Authentication(
101 AuthenticationMessage::Response(AuthenticationResponse::AuthenticationToken(response)),
102 ))
103 .await
104 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
105
106 Ok(())
107 }
108
109 async fn token_extension_request(
110 State(connection_state): State<ConnectionState>,
111 request: MessageKind,
112 instance: AuthenticatedInstance,
113 ) -> Result<(), AuthenticationConnectionError> {
114 let request = if let MessageKind::Authentication(AuthenticationMessage::Request(
115 AuthenticationRequest::TokenExtension(request),
116 )) = request
117 {
118 request
119 } else {
120 return Err(AuthenticationConnectionError::InvalidRequest);
121 };
122
123 let issued_for = instance.inner().clone();
124
125 let mut token_granter = connection_state.auth_granter.lock().await;
126
127 let response = token_granter
128 .extension_request(&issued_for, request.token)
129 .await
130 .map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?;
131
132 connection_state
133 .send(MessageKind::Authentication(
134 AuthenticationMessage::Response(AuthenticationResponse::TokenExtension(response)),
135 ))
136 .await
137 .map_err(|e| AuthenticationConnectionError::Sending(e))?;
138
139 Ok(())
140 }
141
142 async fn verify(state: ConnectionState) {
143 register_account_request
144 .handle_message(&NetworkMessage(vec![]), &state)
145 .await;
146 }
147
148 #[derive(Debug, Error)]
149 pub enum AuthenticationConnectionError {
150 #[error("the request was invalid")]
151 InvalidRequest,
152 #[error("request must be from the same instance")]
153 SameInstance,
154 #[error("issue during registration {0}")]
155 Registration(Error),
156 #[error("sending error")]
157 Sending(Error),
158 #[error("error issuing token")]
159 TokenIssuance(Error),
160 }
161