kochab/examples/certificates.rs

47 lines
1.8 KiB
Rust
Raw Permalink Normal View History

use anyhow::Result;
2020-11-14 02:56:50 +00:00
use log::LevelFilter;
2020-11-13 19:20:59 +00:00
use kochab::{Request, Response, Server};
2020-11-13 19:20:59 +00:00
#[tokio::main]
/// This is a super quick demonstration of how you can check user certificates with kochab
///
/// The goal of this example is just to read the user's certificate and tell them their
/// certificate fingerprint, or if they aren't using a certificate, just tell them that
/// they didn't provide one.
///
/// You can of course require a certificate by sending a [`Response::client_certificate_required()`].
/// But, if you're interested in more advanced user management features, like letting
/// users create an account and persist data, you might want to check out the
/// user_management feature.
2020-11-13 19:20:59 +00:00
async fn main() -> Result<()> {
// We set up logging so we can see what's happening. This isn't technically required,
// and you can use a simpler solution (like env_logger::init()) during production
2020-11-14 02:56:50 +00:00
env_logger::builder()
.filter_module("kochab", LevelFilter::Debug)
2020-11-14 02:56:50 +00:00
.init();
Server::new() // Create a new server
.add_route("/", handle_request) // Bind our handling function to the root path
.serve_ip("localhost:1965") // Start serving content on the default gemini port
2020-11-13 19:20:59 +00:00
.await
}
/// This is the actual handler that does most of the actual work.
/// It'll be called by the server when we receive a request
async fn handle_request(request: Request) -> Result<Response> {
if let Some(fingerprint) = request.fingerprint() {
let message = format!(
"You connected with a certificate with a fingerprint of:\n{}",
fingerprint,
);
Ok(Response::success_plain(message))
} else {
// The user didn't provide a certificate
Ok(Response::client_certificate_required("You didn't provide a client certificate"))
}
2020-11-13 19:20:59 +00:00
}