2020-11-13 19:20:59 +00:00
|
|
|
use anyhow::*;
|
2020-11-14 02:56:50 +00:00
|
|
|
use log::LevelFilter;
|
2020-11-13 19:20:59 +00:00
|
|
|
use tokio::sync::RwLock;
|
2020-12-01 07:31:08 +00:00
|
|
|
use kochab::{Certificate, Request, Response, Server};
|
2020-11-13 19:20:59 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
// Workaround for Certificates not being hashable
|
|
|
|
type CertBytes = Vec<u8>;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2020-11-14 02:56:50 +00:00
|
|
|
env_logger::builder()
|
2020-11-25 05:42:09 +00:00
|
|
|
.filter_module("kochab", LevelFilter::Debug)
|
2020-11-14 02:56:50 +00:00
|
|
|
.init();
|
|
|
|
|
2020-11-13 19:20:59 +00:00
|
|
|
let users = Arc::<RwLock::<HashMap<CertBytes, String>>>::default();
|
|
|
|
|
2020-12-01 07:31:08 +00:00
|
|
|
Server::new()
|
2020-11-20 03:33:44 +00:00
|
|
|
.add_route("/", move|req| handle_request(users.clone(), req))
|
2020-12-01 07:31:08 +00:00
|
|
|
.serve_unix("kochab.sock")
|
2020-11-13 19:20:59 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An ultra-simple demonstration of simple authentication.
|
|
|
|
///
|
|
|
|
/// If the user attempts to connect, they will be prompted to create a client certificate.
|
|
|
|
/// Once they've made one, they'll be given the opportunity to create an account by
|
|
|
|
/// selecting a username. They'll then get a message confirming their account creation.
|
|
|
|
/// Any time this user visits the site in the future, they'll get a personalized welcome
|
|
|
|
/// message.
|
2020-11-20 04:39:07 +00:00
|
|
|
async fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Request) -> Result<Response> {
|
|
|
|
if let Some(Certificate(cert_bytes)) = request.certificate() {
|
|
|
|
// The user provided a certificate
|
|
|
|
let users_read = users.read().await;
|
|
|
|
if let Some(user) = users_read.get(cert_bytes) {
|
|
|
|
// The user has already registered
|
|
|
|
Ok(
|
2020-11-25 04:04:26 +00:00
|
|
|
Response::success_gemini(format!("Welcome {}!", user))
|
2020-11-20 04:39:07 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// The user still needs to register
|
|
|
|
drop(users_read);
|
|
|
|
if let Some(query_part) = request.uri().query() {
|
|
|
|
// The user provided some input (a username request)
|
|
|
|
let username = query_part.as_str();
|
|
|
|
let mut users_write = users.write().await;
|
|
|
|
users_write.insert(cert_bytes.clone(), username.to_owned());
|
2020-11-13 19:20:59 +00:00
|
|
|
Ok(
|
2020-11-25 04:04:26 +00:00
|
|
|
Response::success_gemini(
|
2020-11-20 04:39:07 +00:00
|
|
|
format!(
|
|
|
|
"Your account has been created {}! Welcome!",
|
|
|
|
username
|
|
|
|
)
|
2020-11-18 02:41:18 +00:00
|
|
|
)
|
2020-11-13 19:20:59 +00:00
|
|
|
)
|
|
|
|
} else {
|
2020-11-20 04:39:07 +00:00
|
|
|
// The user didn't provide input, and should be prompted
|
|
|
|
Response::input("What username would you like?")
|
2020-11-13 19:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 04:39:07 +00:00
|
|
|
} else {
|
|
|
|
// The user didn't provide a certificate
|
|
|
|
Ok(Response::client_certificate_required())
|
|
|
|
}
|
2020-11-13 19:20:59 +00:00
|
|
|
}
|