2020-11-16 06:13:16 +00:00
|
|
|
use anyhow::*;
|
|
|
|
use log::LevelFilter;
|
2020-11-25 05:42:09 +00:00
|
|
|
use kochab::{
|
2020-11-22 06:29:24 +00:00
|
|
|
Document,
|
2020-11-16 06:13:16 +00:00
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Server,
|
2020-11-22 06:29:24 +00:00
|
|
|
user_management::{
|
2020-11-22 07:56:41 +00:00
|
|
|
user::RegisteredUser,
|
2020-11-22 06:29:24 +00:00
|
|
|
UserManagementRoutes,
|
|
|
|
},
|
2020-11-16 06:13:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::main]
|
2020-11-22 06:29:24 +00:00
|
|
|
/// An ultra-simple demonstration of authentication.
|
|
|
|
///
|
|
|
|
/// The user should be able to set a secret string that only they can see. They should be
|
|
|
|
/// able to change this at any time to any thing. Both the string and the user account
|
|
|
|
/// will persist across restarts.
|
|
|
|
///
|
|
|
|
/// This method sets up and starts the server
|
2020-11-16 06:13:16 +00:00
|
|
|
async fn main() -> Result<()> {
|
2020-11-22 06:29:24 +00:00
|
|
|
// Turn on logging
|
2020-11-16 06:13:16 +00:00
|
|
|
env_logger::builder()
|
2020-11-25 05:42:09 +00:00
|
|
|
.filter_module("kochab", LevelFilter::Debug)
|
2020-11-16 06:13:16 +00:00
|
|
|
.init();
|
|
|
|
|
2020-12-01 07:31:08 +00:00
|
|
|
Server::new()
|
2020-11-22 06:29:24 +00:00
|
|
|
|
|
|
|
// Add our main routes
|
2020-11-22 07:56:41 +00:00
|
|
|
.add_authenticated_route("/", handle_main)
|
2020-11-23 01:56:15 +00:00
|
|
|
.add_authenticated_input_route("/update", "Enter your new string:", handle_update)
|
2020-11-22 06:29:24 +00:00
|
|
|
|
|
|
|
// Add routes for handling user authentication
|
2020-11-26 18:50:46 +00:00
|
|
|
.add_um_routes::<String>()
|
2020-11-22 06:29:24 +00:00
|
|
|
|
|
|
|
// Start the server
|
2020-12-16 00:16:00 +00:00
|
|
|
.serve_ip("localhost:1965")
|
2020-11-16 06:13:16 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2020-11-22 06:29:24 +00:00
|
|
|
/// The landing page
|
|
|
|
///
|
|
|
|
/// Displays the user's current secret string, or prompts the user to sign in if they
|
|
|
|
/// haven't. Includes links to update your string (`/update`) or your account
|
|
|
|
/// (`/account`). Even though we haven't added an explicit handler for `/account`, this
|
2020-11-25 05:42:09 +00:00
|
|
|
/// route is managed by kochab.
|
2020-11-22 07:56:41 +00:00
|
|
|
///
|
|
|
|
/// Because this route is registered as an authenticated route, any connections without a
|
|
|
|
/// certificate will be prompted to add a certificate and register.
|
|
|
|
async fn handle_main(_req: Request, user: RegisteredUser<String>) -> Result<Response> {
|
|
|
|
// If the user is signed in, render and return their page
|
|
|
|
let response = Document::new()
|
|
|
|
.add_text("Your personal secret string:")
|
|
|
|
.add_text(user.as_ref())
|
|
|
|
.add_blank_line()
|
|
|
|
.add_link("/update", "Change your string")
|
|
|
|
.add_link("/account", "Update your account")
|
|
|
|
.into();
|
|
|
|
Ok(response)
|
2020-11-22 06:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The update endpoint
|
2020-11-16 06:13:16 +00:00
|
|
|
///
|
2020-11-22 07:56:41 +00:00
|
|
|
/// Users can update their secret string here.
|
2020-11-23 01:56:15 +00:00
|
|
|
async fn handle_update(_request: Request, mut user: RegisteredUser<String>, input: String) -> Result<Response> {
|
2020-11-22 06:29:24 +00:00
|
|
|
|
2020-11-23 01:56:15 +00:00
|
|
|
// The user has already been prompted to log in if they weren't and asked to give an
|
|
|
|
// input string, so all we need to do is...
|
2020-11-22 06:29:24 +00:00
|
|
|
|
2020-11-23 01:56:15 +00:00
|
|
|
// Update the users data
|
|
|
|
*user.as_mut() = input;
|
2020-11-22 06:29:24 +00:00
|
|
|
|
2020-11-23 01:56:15 +00:00
|
|
|
// Render a response
|
|
|
|
let response = Document::new()
|
|
|
|
.add_text("String updated!")
|
|
|
|
.add_blank_line()
|
|
|
|
.add_link("/", "Back")
|
|
|
|
.into();
|
|
|
|
Ok(response)
|
2020-11-22 07:56:41 +00:00
|
|
|
|
2020-11-16 06:13:16 +00:00
|
|
|
}
|