added password hash

This commit is contained in:
2026-01-23 22:08:28 +01:00
parent 16a8b446ed
commit 8097895361
6 changed files with 79 additions and 48 deletions

View File

@@ -1,38 +1,14 @@
#include <sodium.h>
#include "login.hpp"
#include "crow/http_response.h"
#include "databasepool.h"
#include "utils.hpp"
#include "SessionHandler.hpp"
namespace login
{
SessionHandler sessionHandler;
std::string hashPassword(const std::string& password)
{
// Allocate storage for the hash
char hash[crypto_pwhash_STRBYTES];
// Hash the password using Argon2id
if (crypto_pwhash_str(
hash,
password.c_str(),
password.size(),
crypto_pwhash_OPSLIMIT_INTERACTIVE,
crypto_pwhash_MEMLIMIT_INTERACTIVE
) != 0) {
CROW_LOG_ERROR << "Out of memory while hashing password!";
return "";
}
return hash;
}
bool verifyHashWithPassword(const std::string& hash, std::string const& password)
{
return crypto_pwhash_str_verify(hash.c_str(), password.c_str(), password.size()) == 0;
}
std::string getSessionId(const crow::request& req) {
auto cookie_header = req.get_header_value("Cookie");
std::string prefix = "session_id=";
@@ -51,23 +27,15 @@ bool isLoggedIn(const crow::request& req) {
std::optional<std::string> loginUser(const std::string& username, const std::string& password)
{
auto user = getUser(username);
auto user = getVerifiedUser(username, password);
if (user.has_value()) {
if (verifyHashWithPassword(user.value().password_hash, password))
{
return sessionHandler.createSession(user.value().id);
}
return sessionHandler.createSession(user.value().id);
}
return {};
}
bool initLogin(crow::SimpleApp& app)
{
if (sodium_init() < 0) {
CROW_LOG_ERROR << "Failed to Init Sodium";
return false;
}
// createUser("lukas", "Trollar4928");
CROW_ROUTE(app, "/login").methods("POST"_method)