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

ambee/giterated

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

Fix authentication

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨cef865c

⁨src/main.rs⁩ - ⁨4523⁩ bytes
Raw
1 use anyhow::Error;
2 use connection::{connection_worker, Connections, RawConnection};
3 use giterated_daemon::{
4 authentication::AuthenticationTokenGranter,
5 backend::{
6 discovery::GiteratedDiscoveryProtocol, git::GitBackend, user::UserAuth, DiscoveryBackend,
7 RepositoryBackend, UserBackend,
8 },
9 connection, listener,
10 model::instance::Instance,
11 };
12 use listener::Listeners;
13 use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool};
14 use std::{net::SocketAddr, str::FromStr, sync::Arc};
15 use tokio::{
16 fs::File,
17 io::{AsyncRead, AsyncReadExt, AsyncWrite},
18 net::{TcpListener, TcpStream},
19 sync::Mutex,
20 };
21 use tokio_tungstenite::{accept_async, WebSocketStream};
22 use toml::Table;
23
24 #[macro_use]
25 extern crate tracing;
26
27 #[tokio::main]
28 async fn main() -> Result<(), Error> {
29 tracing_subscriber::fmt::init();
30 let mut listener = TcpListener::bind("0.0.0.0:7270").await?;
31 let connections: Arc<Mutex<Connections>> = Arc::default();
32 let listeners: Arc<Mutex<Listeners>> = Arc::default();
33 let config: Table = {
34 let mut file = File::open("Giterated.toml").await?;
35 let mut text = String::new();
36 file.read_to_string(&mut text).await?;
37 text.parse()?
38 };
39 let db_conn_options = PgConnectOptions::new()
40 .host(config["postgres"]["host"].as_str().unwrap())
41 .port(config["postgres"]["port"].as_integer().unwrap() as u16)
42 .database(config["postgres"]["database"].as_str().unwrap())
43 .username(config["postgres"]["user"].as_str().unwrap())
44 .password(config["postgres"]["password"].as_str().unwrap())
45 .log_statements(log::LevelFilter::Off);
46 let db_pool = PgPool::connect_with(db_conn_options).await?;
47
48 debug!("Running database migrations...");
49 sqlx::migrate!().run(&db_pool).await?;
50 info!("Connected");
51
52 let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> =
53 Arc::new(Mutex::new(GitBackend {
54 pg_pool: db_pool.clone(),
55 repository_folder: String::from(
56 config["giterated"]["backend"]["git"]["root"]
57 .as_str()
58 .unwrap(),
59 ),
60 instance: Instance::from_str("giterated.dev").unwrap(),
61 }));
62
63 let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
64 config: config.clone(),
65 instance: Instance::from_str("giterated.dev").unwrap(),
66 }));
67
68 let user_backend: Arc<Mutex<dyn UserBackend + Send>> = Arc::new(Mutex::new(UserAuth::new(
69 db_pool.clone(),
70 &Instance::from_str("giterated.dev").unwrap(),
71 token_granter.clone(),
72 )));
73
74 let discovery_backend: Arc<Mutex<dyn DiscoveryBackend + Send>> =
75 Arc::new(Mutex::new(GiteratedDiscoveryProtocol {
76 pool: db_pool.clone(),
77 }));
78
79 info!("Connected");
80
81 loop {
82 let stream = accept_stream(&mut listener).await;
83 info!("Connected");
84
85 let (stream, address) = match stream {
86 Ok(stream) => stream,
87 Err(err) => {
88 error!("Failed to accept connection. {:?}", err);
89 continue;
90 }
91 };
92
93 info!("Accepted connection from {}", address);
94
95 let connection = accept_websocket_connection(stream).await;
96
97 let connection = match connection {
98 Ok(connection) => connection,
99 Err(err) => {
100 error!(
101 "Failed to initiate Websocket connection from {}. {:?}",
102 address, err
103 );
104 continue;
105 }
106 };
107
108 info!("Websocket connection established with {}", address);
109
110 let connection = RawConnection {
111 task: tokio::spawn(connection_worker(
112 connection,
113 listeners.clone(),
114 connections.clone(),
115 repository_backend.clone(),
116 user_backend.clone(),
117 token_granter.clone(),
118 discovery_backend.clone(),
119 address,
120 )),
121 };
122
123 connections.lock().await.connections.push(connection);
124 }
125 }
126
127 async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> {
128 let stream = listener.accept().await?;
129
130 Ok(stream)
131 }
132
133 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
134 stream: S,
135 ) -> Result<WebSocketStream<S>, Error> {
136 let connection = accept_async(stream).await?;
137
138 Ok(connection)
139 }
140