Older/WebApplication/model/BlogSession.cpp

119 lines
4.1 KiB
C++
Raw Normal View History

2024-11-01 19:05:20 +08:00
#include "BlogSession.h"
2024-11-07 18:21:04 +08:00
#include "BoostLog.h"
#include "Post.h"
#include "Token.h"
#include "User.h"
#include "asciidoc.h"
2024-11-02 00:30:14 +08:00
#include <Wt/Auth/AuthService.h>
2024-11-06 15:51:01 +08:00
#include <Wt/Auth/GoogleService.h>
2024-11-02 00:30:14 +08:00
#include <Wt/Auth/HashFunction.h>
#include <Wt/Auth/PasswordService.h>
#include <Wt/Auth/PasswordStrengthValidator.h>
2024-11-06 15:51:01 +08:00
#include <Wt/Auth/PasswordVerifier.h>
#include <Wt/Dbo/FixedSqlConnectionPool.h>
2024-11-02 00:30:14 +08:00
2024-11-07 18:21:04 +08:00
const std::string ADMIN_USERNAME = "admin";
const std::string ADMIN_PASSWORD = "admin";
2024-11-02 00:30:14 +08:00
class BlogOAuth : public std::vector<const Wt::Auth::OAuthService *> {
public:
~BlogOAuth() {
for (unsigned i = 0; i < size(); ++i) delete (*this)[i];
}
};
Wt::Auth::AuthService blogAuth;
Wt::Auth::PasswordService blogPasswords(blogAuth);
BlogOAuth blogOAuth;
2024-11-01 19:05:20 +08:00
BlogSession::BlogSession(Wt::Dbo::SqlConnectionPool &connectionPool)
: m_connectionPool(connectionPool), m_users(*this) {
2024-11-07 18:21:04 +08:00
setConnectionPool(m_connectionPool);
mapClass<Comment>("comment");
mapClass<Post>("post");
mapClass<Tag>("tag");
mapClass<Token>("token");
mapClass<User>("user");
try {
Wt::Dbo::Transaction t(*this);
createTables();
Wt::Dbo::ptr<User> admin = add(std::make_unique<User>());
User *a = admin.modify();
a->name = ADMIN_USERNAME;
a->role = User::Admin;
Wt::Auth::User authAdmin = m_users.findWithIdentity(Wt::Auth::Identity::LoginName, a->name);
blogPasswords.updatePassword(authAdmin, ADMIN_PASSWORD);
Wt::Dbo::ptr<Post> post = add(std::make_unique<Post>());
Post *p = post.modify();
p->state = Post::Published;
p->author = admin;
p->title = "Welcome!";
p->briefSrc = "Welcome to your own blog.";
p->bodySrc = "We have created for you an " + ADMIN_USERNAME + " user with password " + ADMIN_PASSWORD;
p->briefHtml = asciidoc(p->briefSrc);
p->bodyHtml = asciidoc(p->bodySrc);
p->date = Wt::WDateTime::currentDateTime();
Wt::Dbo::ptr<Comment> rootComment = add(std::make_unique<Comment>());
rootComment.modify()->post = post;
t.commit();
LOG(info) << "Created database, and user " << ADMIN_USERNAME << " / " << ADMIN_PASSWORD << std::endl;
} catch (std::exception &e) {
LOG(error) << e.what() << std::endl;
LOG(info) << "Using existing database";
}
2024-11-01 19:05:20 +08:00
}
2024-11-02 00:30:14 +08:00
void BlogSession::configureAuth() {
blogAuth.setAuthTokensEnabled(true, "bloglogin");
std::unique_ptr<Wt::Auth::PasswordVerifier> verifier = std::make_unique<Wt::Auth::PasswordVerifier>();
verifier->addHashFunction(std::make_unique<Wt::Auth::BCryptHashFunction>(7));
#ifdef WT_WITH_SSL
verifier->addHashFunction(std::make_unique<Wt::Auth::SHA1HashFunction>());
#endif
#ifdef HAVE_CRYPT
verifier->addHashFunction(std::make_unique<UnixCryptHashFunction>());
#endif
blogPasswords.setVerifier(std::move(verifier));
blogPasswords.setPasswordThrottle(std::make_unique<Wt::Auth::AuthThrottle>());
blogPasswords.setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
if (Wt::Auth::GoogleService::configured()) blogOAuth.push_back(new Wt::Auth::GoogleService(blogAuth));
}
2024-11-01 19:05:20 +08:00
std::unique_ptr<Wt::Dbo::SqlConnectionPool> BlogSession::createConnectionPool(const std::string &sqlite3) {
auto connection = std::make_unique<Wt::Dbo::backend::Sqlite3>(sqlite3);
connection->setProperty("show-queries", "true");
connection->setDateTimeStorage(Wt::Dbo::SqlDateTimeType::DateTime,
Wt::Dbo::backend::DateTimeStorage::PseudoISO8601AsText);
return std::make_unique<Wt::Dbo::FixedSqlConnectionPool>(std::move(connection), 10);
}
2024-11-02 00:30:14 +08:00
Wt::Dbo::ptr<User> BlogSession::user() const {
if (m_login.loggedIn())
return m_users.find(m_login.user());
else
return Wt::Dbo::ptr<User>();
}
2024-11-06 15:51:01 +08:00
Wt::Signal<Wt::Dbo::ptr<Comment>> &BlogSession::commentsChanged() {
return m_commentsChanged;
}
2024-11-02 00:30:14 +08:00
Wt::Auth::PasswordService *BlogSession::passwordAuth() const {
return &blogPasswords;
}
const std::vector<const Wt::Auth::OAuthService *> &BlogSession::oAuth() const {
return blogOAuth;
}