From 01d6c3d3cbd82a3333ef12d36559b63749eca226 Mon Sep 17 00:00:00 2001 From: Imk Date: Tue, 8 Oct 2024 19:13:04 +0200 Subject: [PATCH] stuff --- .gitignore | 1 + Cargo.lock | 223 ++++++++++++++++++++++++++++++++++- Cargo.toml | 3 + src/handlers/user_handler.rs | 24 ++++ src/lib.rs | 23 +++- templates/login.html | 9 ++ 6 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 src/handlers/user_handler.rs create mode 100644 templates/login.html diff --git a/.gitignore b/.gitignore index ea8c4bf..fedaa2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.env diff --git a/Cargo.lock b/Cargo.lock index 5d30ee4..26441fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,7 +31,7 @@ dependencies = [ "actix-web", "bitflags", "bytes", - "derive_more", + "derive_more 0.99.18", "futures-core", "http-range", "log", @@ -53,12 +53,12 @@ dependencies = [ "actix-service", "actix-utils", "ahash", - "base64", + "base64 0.22.1", "bitflags", "brotli", "bytes", "bytestring", - "derive_more", + "derive_more 0.99.18", "encoding_rs", "flate2", "futures-core", @@ -81,6 +81,22 @@ dependencies = [ "zstd", ] +[[package]] +name = "actix-identity" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b8ddc6f6a8b19c4016aaa13519968da9969bc3bc1c1c883cdb0f25dd6c8cf7" +dependencies = [ + "actix-service", + "actix-session", + "actix-utils", + "actix-web", + "derive_more 1.0.0", + "futures-core", + "serde", + "tracing", +] + [[package]] name = "actix-macros" version = "0.2.4" @@ -144,6 +160,23 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "actix-session" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efe6976a74f34f1b6d07a6c05aadc0ed0359304a7781c367fa5b4029418db08f" +dependencies = [ + "actix-service", + "actix-utils", + "actix-web", + "anyhow", + "derive_more 1.0.0", + "rand", + "serde", + "serde_json", + "tracing", +] + [[package]] name = "actix-utils" version = "3.0.1" @@ -174,7 +207,7 @@ dependencies = [ "bytestring", "cfg-if", "cookie", - "derive_more", + "derive_more 0.99.18", "encoding_rs", "futures-core", "futures-util", @@ -223,6 +256,41 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "ahash" version = "0.8.11" @@ -324,6 +392,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + [[package]] name = "autocfg" version = "1.4.0" @@ -345,6 +419,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5" + [[package]] name = "base64" version = "0.22.1" @@ -371,7 +451,10 @@ name = "blog" version = "0.1.0" dependencies = [ "actix-files", + "actix-identity", "actix-web", + "base64 0.22.1", + "dotenv", "env_logger", "ignore", "lazy_static", @@ -491,6 +574,16 @@ dependencies = [ "phf_codegen", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "colorchoice" version = "1.0.2" @@ -509,7 +602,14 @@ version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e859cd57d0710d9e06c381b550c06e76992472a8c6d527aecd2fc673dcc231fb" dependencies = [ + "aes-gcm", + "base64 0.20.0", + "hkdf", + "hmac", "percent-encoding", + "rand", + "sha2", + "subtle", "time", "version_check", ] @@ -570,9 +670,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core", "typenum", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "deranged" version = "0.3.11" @@ -595,6 +705,27 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "deunicode" version = "1.6.0" @@ -609,8 +740,15 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.34" @@ -734,6 +872,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gimli" version = "0.31.0" @@ -795,6 +943,24 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.12" @@ -904,6 +1070,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1068,6 +1243,12 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "parking_lot" version = "0.12.3" @@ -1213,6 +1394,18 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + [[package]] name = "portable-atomic" version = "1.9.0" @@ -1518,6 +1711,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.79" @@ -1799,6 +1998,22 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "url" version = "2.5.2" diff --git a/Cargo.toml b/Cargo.toml index a90213c..cd1c0f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,10 @@ edition = "2021" [dependencies] actix-files = "0.6.6" +actix-identity = "0.8.0" actix-web = "4.9.0" +base64 = "0.22.1" +dotenv = "0.15.0" env_logger = "0.11.5" ignore = "0.4.23" lazy_static = "1.5.0" diff --git a/src/handlers/user_handler.rs b/src/handlers/user_handler.rs new file mode 100644 index 0000000..c4bf859 --- /dev/null +++ b/src/handlers/user_handler.rs @@ -0,0 +1,24 @@ +use std::{fs, io::Error}; +use actix_web::{get, web, HttpResponse, Responder, HttpMessage}; +use actix_identity::{Identity, IdentityMiddleware}; +use actix_session::{config::PresistentSession, storage::CookieSessionStore, SessionMiddleware}; + +async fn login(req: HttpRequest) -> impl Responder{ + Identity::login(&req.extension(),"user1".to_owned()).unwrap(); + + web::Redirect::to("/").using_status_code(StatusCode::FOUND) +} + +#[get("/entry")] +pub async fn index(templates: web::Data) -> impl Responder { + let mut context = tera::Context::new(); + match templates.render("login.html", &context) { + Ok(s) => HttpResponse::Ok().content_type("text/html").body(s), + Err(e) => { + println!("{:?}",e); + HttpResponse::InternalServerError() + .content_type("text/html") + .body("

Something is fucked up :33333 tell IMK(jet) to fix this if refreshing doesn't works or to shoot himself :3c

") + } + } +} diff --git a/src/lib.rs b/src/lib.rs index be7afe8..592b585 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,13 @@ - +use dotenv::dotenv; use actix_files::Files; use std::net::TcpListener; -use actix_web::{dev::Server, web, App, HttpResponse, HttpServer, middleware}; +use actix_identity::{Identity, IdentityMiddleware}; +use actix_session::{config::PresistentSession, storage::CookieSessionStore, SessionMiddleware} +use actix_web::{dev::Server, web, http::StatusCode, HttpRequest, App, HttpResponse, HttpServer, middleware, cookie::{time::Duration, Key}}; use tera::Tera; +use base64::prelude::*; pub mod handlers; + #[macro_use] extern crate lazy_static; @@ -21,13 +25,28 @@ lazy_static! { }; } + + +const SES_TIME: Duration = Duration::minutes(5); + pub fn start_blog(listener: TcpListener) -> Result{ + dotenv().ok(); + let secret_key = Key::from(&BASE64_STANDARD.decode(&std::env::var("KEY").expect("KEY UNSET")).unwrap()); let srv = HttpServer::new(move || { App::new() .app_data(web::Data::new(TEMPLATES.clone())) .wrap(middleware::Logger::default()) + .wrap(IdentityMiddleware::default()) + .wrap( + SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone) + .cookie_name("auth".to_owned()) + .cookie_secure(false) + .session_lifecycle(PresistentSession::default().session_ttl(SES_TIME)) + .build() + ) .service(Files::new("/static","static/").use_last_modified(true)) .route("/health", web::get().to(HttpResponse::Ok)) + .route("/login", web::post().to(login)) .service(handlers::index) .service(handlers::post) }) diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..acb4464 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} +{% block title %}ALEPH 1 | IMK'S BLOG; ENTRY {% endblock title %} +{% block content %} +
+

ℵ₁ | IMK'S BLOG

+
+ +
+{% endblock content %}