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

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