This commit is contained in:
2026-07-21 19:50:36 +10:00
parent 0965749fc8
commit dd7e1f8981
9 changed files with 372 additions and 35 deletions

View File

@@ -9,6 +9,7 @@ sources = [
'src/generated/header_bottom.cpp',
'src/generated/footer.cpp',
'src/generated/login.cpp',
'src/generated/settings.cpp',
'src/generated/style.cpp',
'src/generated/script.cpp',

View File

@@ -38,7 +38,8 @@ Database::Database(const std::string& path) {
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
password TEXT NOT NULL
password TEXT NOT NULL,
bio TEXT
);
CREATE TABLE IF NOT EXISTS posts (
@@ -283,10 +284,17 @@ std::optional<User> Database::getUser(uint64_t id) {
std::string name{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1))};
std::string password{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))};
const char* bioText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
std::string bio;
if (bioText == NULL) {
bio = "";
} else {
bio = std::string(bioText);
}
sqlite3_finalize(stmt);
return User(id, name, password);
return User(id, name, password, bio);
}
std::optional<User> Database::getUserByName(const std::string& name) {
@@ -307,9 +315,16 @@ std::optional<User> Database::getUserByName(const std::string& name) {
uint64_t id = sqlite3_column_int64(stmt, 0);
std::string password{reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2))};
const char* bioText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
std::string bio;
if (bioText == NULL) {
bio = "";
} else {
bio = std::string(bioText);
}
sqlite3_finalize(stmt);
return User(id, name, password);
return User(id, name, password, bio);
}
std::optional<std::string> Database::createNewToken(uint64_t id) {
@@ -388,3 +403,48 @@ void Database::addUser(User& user) {
sqlite3_finalize(stmt);
}
void Database::updateUser(const User& user) {
const char* sql = R"(
UPDATE users
SET
name = ?,
bio = ?,
password = ?
WHERE id = ?;
)";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
}
sqlite3_bind_text(stmt, 1, user.name.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, user.bio.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, user.passwordHash.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 4, user.id);
if (sqlite3_step(stmt) != SQLITE_DONE) {
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
}
sqlite3_finalize(stmt);
}
void Database::invalidateUserSessions(const User& user) {
const char* sql = R"(
DELETE FROM sessions WHERE userid = ?;
)";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
}
sqlite3_bind_int64(stmt, 1, user.id);
if (sqlite3_step(stmt) != SQLITE_DONE) {
throw std::runtime_error("sqlite3 error: " + std::string(sqlite3_errmsg(db)));
}
sqlite3_finalize(stmt);
}

View File

@@ -17,9 +17,10 @@ struct User {
uint64_t id = 0;
std::string name = "";
std::string passwordHash = "";
std::string bio = "";
User(uint64_t id, std::string namein, const std::string& passwordHash) :
id(id), passwordHash(passwordHash) {
User(uint64_t id, std::string namein, const std::string& passwordHash, const std::string& bio) :
id(id), passwordHash(passwordHash), bio(bio) {
sanitize(namein);
name = namein;
}
@@ -48,6 +49,8 @@ class Database {
// this will modify the user to have their user ID
void addUser(User& user);
void updateUser(const User& user);
void invalidateUserSessions(const User& user);
};

View File

@@ -16,6 +16,7 @@
#include "generated/header_bottom.h"
#include "generated/footer.h"
#include "generated/login.h"
#include "generated/settings.h"
#include "generated/style.h"
#include "generated/script.h"
@@ -42,16 +43,18 @@ std::optional<User> getLoggedInUser(const httplib::Request& request, Database& d
int main() {
const bin2cpp::File& headerTopFile = bin2cpp::getHeader_topHtmlFile();
const bin2cpp::File& headerBottomFile = bin2cpp::getHeader_bottomHtmlFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& loginfile = bin2cpp::getLoginHtmlFile();
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& loginfile = bin2cpp::getLoginHtmlFile();
const bin2cpp::File& settingsfile = bin2cpp::getSettingsHtmlFile();
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
std::string headerTop{headerTopFile.getBuffer(), headerTopFile.getSize()};
std::string headerBottom{headerBottomFile.getBuffer(), headerBottomFile.getSize()};
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
std::string login{loginfile.getBuffer(), loginfile.getSize()};
std::string settings{settingsfile.getBuffer(), settingsfile.getSize()};
std::string e404{e404file.getBuffer(), e404file.getSize()};
std::string style{stylefile.getBuffer(), stylefile.getSize()};
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
@@ -78,6 +81,14 @@ int main() {
response.set_content(login, "text/html");
});
svr.Get("/settings", [&settings](const httplib::Request& request, httplib::Response& response) {
response.set_content(settings, "text/html");
});
svr.Get("/settings.html", [&settings](const httplib::Request& request, httplib::Response& response) {
response.set_content(settings, "text/html");
});
svr.Post("/login", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string username = request.form.get_field("username");
std::string password = request.form.get_field("password");
@@ -139,7 +150,7 @@ int main() {
response.set_content("<p>hey you can't have an empty username!!!!1!!!1! >:(</p>", "text/html");
return;
}
User newUser{0, username, bcrypt::generateHash(password)};
User newUser{0, username, bcrypt::generateHash(password), ""};
database.addUser(newUser);
user = newUser;
}
@@ -187,6 +198,9 @@ int main() {
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
@@ -228,7 +242,8 @@ int main() {
std::stringstream ss;
ss << headerTop << "<meta content='See posts from " << username << " on Chookchat' property='og:title' />" << headerBottom;
ss << "<div id='posts-header'><h1>" << username << "'s posts</h1></div>";
ss << "<div id='posts-header'><h1>" << username << "</h1></div>";
ss << "<p>" << user->bio << "</p>";
for (const auto& post : posts) {
ss << post.genHtml();
}
@@ -270,7 +285,7 @@ int main() {
return;
}
// create user
User newUser{0, username, bcrypt::generateHash(password)};
User newUser{0, username, bcrypt::generateHash(password), ""};
database.addUser(newUser);
userId = newUser.id;
}
@@ -336,17 +351,134 @@ int main() {
svr.Get("/me", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::lock_guard<std::mutex> lock(data_mutex);
std::optional<User> user = getLoggedInUser(request, database);
response.set_header("Cache-Control", "no-store");
try {
std::optional<User> user = getLoggedInUser(request, database);
response.set_header("Cache-Control", "no-store");
if (!user.has_value()) {
response.set_header("X-Logged-In", "false");
return;
if (!user.has_value()) {
response.set_header("X-Logged-In", "false");
return;
}
response.set_header("X-Logged-In", "true");
response.set_header("X-Username", user->name);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
response.set_header("X-Logged-In", "true");
response.set_header("X-Username", user->name);
});
// settings endpoints
svr.Post("/settings/changePassword", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string oldpassword = request.form.get_field("oldpassword");
std::string newpassword = request.form.get_field("newpassword");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
if (!bcrypt::validatePassword(oldpassword, user->passwordHash)) {
response.status = 400;
response.set_content("invalid password", "text/plain");
return;
}
user->passwordHash = bcrypt::generateHash(newpassword);
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/changeUsername", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string password = request.form.get_field("password");
std::string newusername = request.form.get_field("newusername");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
if (!bcrypt::validatePassword(password, user->passwordHash)) {
response.status = 400;
response.set_content("invalid password", "text/plain");
return;
}
user->name = newusername;
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/changeBio", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string bio = request.form.get_field("bio");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
user->bio = bio;
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/invalidateAllSessions", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
database.invalidateUserSessions(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.listen("0.0.0.0", 8080);