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: ⁨25c3410

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