added login suppport

This commit is contained in:
2025-10-20 23:06:33 +02:00
parent 4916b4f1c1
commit 5a1297dc80
10 changed files with 227 additions and 118 deletions

View File

@@ -1,25 +1,19 @@
#include "login.hpp"
#include "string.h"
#include "sodium.h"
#include "unordered_map"
#include "database.hpp"
#include "utils.hpp"
#include <iostream>
namespace login
{
struct Session {
std::string user_id;
std::string csrf_token;
};
std::unordered_map<std::string, Session> sessions;
bool verifyHashWithPassword(const std::string& hash, std::string const& password)
{
if (crypto_pwhash_str_verify(hash.c_str(), password.c_str(), password.size()) == 0) {
return true;
} else {
return false;
}
bool is_logged_in(const crow::request& req) {
std::string session_id = get_session_id(req);
if (session_id.empty()) return false;
auto it = sessions.find(session_id);
if (it == sessions.end()) return false;
return !it->second.user_id.empty();
}
std::string hashPassword(const std::string& password)
@@ -41,6 +35,31 @@ std::string hashPassword(const std::string& password)
return hash;
}
std::optional<int> createUser(const std::string& username, const std::string& password){
auto db = Database();
if (!db.open())
return false;
std::string password_hash = hashPassword(password);
if(password_hash.empty())
return false;
std::string insert_sql =
"INSERT INTO users (username, password_hash) VALUES ('"
+ username + "', '" + password_hash + "');";
return db.insert(insert_sql);
}
bool verifyHashWithPassword(const std::string& hash, std::string const& password)
{
if (crypto_pwhash_str_verify(hash.c_str(), password.c_str(), password.size()) == 0) {
return true;
} else {
return false;
}
}
std::string get_session_id(const crow::request& req) {
auto cookie_header = req.get_header_value("Cookie");
std::string prefix = "session_id=";
@@ -61,45 +80,21 @@ std::string random_string(size_t length) {
return result;
}
bool is_logged_in(const crow::request& req) {
std::string session_id = get_session_id(req);
if (session_id.empty()) return false;
auto it = sessions.find(session_id);
if (it == sessions.end()) return false;
return !it->second.user_id.empty();
}
// lambda to be used by endpoint that requiere login
auto login_required = [](auto handler){
return [handler](const crow::request& req){
if (!is_logged_in(req)) {
crow::response res;
if (req.get_header_value("HX-Request") == "true")
res = crow::response(401, "Login required");
else {
res = crow::response(302);
res.add_header("Location", "/login");
}
return res;
}
return handler(req);
};
};
bool loginUser(const std::string& username, const std::string& password)
std::optional<int> loginUser(const std::string& username, const std::string& password)
{
auto sql = "SELECT id password_hash FROM users WHERE username = '?' LIMIT 1;";
auto db = Database();
if (!db.open())
return false;
return {};
auto opt_pair = db.get<int, std::string>(sql, {username});
if (opt_pair.has_value()) {
return verifyHashWithPassword(opt_pair.value().second, password);
} else {
return false;
}
if (verifyHashWithPassword(opt_pair.value().second, password))
{
return opt_pair.value().first;
}
}
return {};
}
bool initDB()
@@ -135,6 +130,8 @@ bool initLogin(crow::SimpleApp& app)
{
return false;
}
// createUser("lukas", "Trollar%4928");
CROW_ROUTE(app, "/login").methods("GET"_method)
([](const crow::request& req){
@@ -167,19 +164,22 @@ bool initLogin(crow::SimpleApp& app)
auto session = it->second;
// parse form
auto body = crow::query_string(req.body);
std::string csrf_token = body.get("csrf_token");
std::string username = body.get("username");
std::string password = body.get("password");
auto body = utils::parseBody(req.body);
if (body.empty())
return crow::response(400);
std::string csrf_token = body.at("csrf_token");
std::string username = body.at("username");
std::string password = body.at("password");
if (csrf_token != session.csrf_token) return crow::response(403, "CSRF failed");
bool ok = loginUser(username, password);
std::optional<int> userId = loginUser(username, password);
if (!ok) return crow::response(401, "Invalid credentials");
if (!userId.has_value()) return crow::response(401, "Invalid credentials");
// regenerate session, mark as logged in
sessions[session_id].user_id = "123"; // user ID
// set user id
sessions[session_id].user_id = std::to_string(userId.value());
crow::response res;
res.add_header("HX-Redirect", "/dashboard"); // htmx redirect