From 12edd7f69c55b01fa2dfe64686db1f3e52560ff2 Mon Sep 17 00:00:00 2001 From: mtrx Date: Thu, 16 Jan 2025 22:08:51 +0100 Subject: [PATCH] added login route and renamed submit token to login token --- site/src/api.rs | 55 ++++++++++++++++++++++++++++++------------- site/src/config.rs | 4 ++-- site/src/form_data.rs | 15 ++++++++++++ site/src/main.rs | 2 ++ 4 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 site/src/form_data.rs diff --git a/site/src/api.rs b/site/src/api.rs index 7e7eab0..a0dcdee 100644 --- a/site/src/api.rs +++ b/site/src/api.rs @@ -1,19 +1,39 @@ use crate::config::CONFIG; use crate::db::*; use crate::routes::{id_valid, replace_newlines}; +use actix_identity::Identity; use actix_web::{get, http::StatusCode, post, web, web::Form, HttpResponse, Responder}; -use serde::Deserialize; +use actix_web::{HttpMessage, HttpRequest}; -#[derive(Deserialize)] -struct NewPostForm { - title: String, - body: String, - token: String, +use crate::form_data::NewPostForm; +use crate::form_data::{BlogActionForm, LoginForm}; + +#[get("/")] +async fn index(user: Option) -> impl Responder { + if let Some(user) = user { + format!("Welcome! {}", user.id().unwrap()) + } else { + "Welcome Anonymous!".to_owned() + } } -#[derive(Deserialize)] -struct BlogActionForm { - token: String, +#[post("/login")] +async fn blog_login(form: Form, req: HttpRequest) -> impl Responder { + let submitted_login_token = form.login_token.clone(); + if submitted_login_token == CONFIG.login_token { + // attach a verified user identity to the active session + Identity::login(&req.extensions(), "default_user".into()).unwrap(); + + HttpResponse::Ok() + } else { + HttpResponse::Unauthorized() + } +} + +#[post("/logout")] +async fn blog_logout(user: Identity) -> impl Responder { + user.logout(); + HttpResponse::Ok() } #[post("/api/blog/create")] @@ -51,12 +71,10 @@ async fn blog_edit_post(post_id: web::Path, form: Form) -> } #[post("/api/blog/posts/delete/{post_id}")] -async fn blog_delete_post( - post_id: web::Path, - form: Form, -) -> impl Responder { +async fn blog_delete_post(post_id: web::Path) -> impl Responder { let (valid, id) = id_valid(post_id.into_inner()); - if valid && CONFIG.submit_token == form.token { + // TODO + if valid && AUTHENTICATED { println!("Deleted post: {}", id); delete_post_by_id(id as i32); } else { @@ -86,7 +104,10 @@ async fn blog_hide_post(post_id: web::Path, form: Form) } #[get("/api/blog/posts")] -async fn blog_get_posts_json() -> impl Responder { - let posts = get_all_posts(); - HttpResponse::Ok().json(posts) +async fn get_posts_json(user: Option) -> impl Responder { + if let Some(user) = user { + let posts = get_all_posts(); + HttpResponse::Ok().json(posts) + } + return HttpResponse::new(StatusCode::UNAUTHORIZED); } diff --git a/site/src/config.rs b/site/src/config.rs index 2defd0c..ac7bb1b 100644 --- a/site/src/config.rs +++ b/site/src/config.rs @@ -4,7 +4,7 @@ use once_cell::sync::Lazy; pub const ENV_PREFIX: &str = "CL_"; pub struct Config { - pub submit_token: String, + pub login_token: String, pub session_secret: String, pub root_path: String, pub username: String, @@ -66,7 +66,7 @@ fn load_config() -> Config { } Config { - submit_token: eval_conf_var("SUBMIT_TOKEN", true, None).unwrap(), + login_token: eval_conf_var("SUBMIT_TOKEN", true, None).unwrap(), session_secret: eval_conf_var("SESSION_SECRET", true, None).unwrap(), root_path: eval_conf_var("ROOT_PATH", false, Some("./content")).unwrap(), username: eval_conf_var("USERNAME", true, None).unwrap(), diff --git a/site/src/form_data.rs b/site/src/form_data.rs new file mode 100644 index 0000000..f142207 --- /dev/null +++ b/site/src/form_data.rs @@ -0,0 +1,15 @@ +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct LoginForm { + pub login_token: String, +} + +#[derive(Deserialize)] +pub struct NewPostForm { + title: String, + body: String, +} + +#[derive(Deserialize)] +pub struct BlogActionForm {} diff --git a/site/src/main.rs b/site/src/main.rs index 1e04360..1c0d65c 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -1,6 +1,7 @@ mod api; mod config; mod db; +mod form_data; mod routes; #[macro_use] @@ -44,6 +45,7 @@ async fn main() -> std::io::Result<()> { .service(routes::blog_submit) .service(routes::blog_edit) .service(routes::blog_edit_by_id) + .service(api::blog_login) .service(api::blog_get_posts_json) .service(api::blog_create_post) .service(api::blog_edit_post)