119 lines
4.1 KiB
C++
119 lines
4.1 KiB
C++
#include "BlogSession.h"
|
|
#include "BoostLog.h"
|
|
#include "Post.h"
|
|
#include "Token.h"
|
|
#include "User.h"
|
|
#include "asciidoc.h"
|
|
#include <Wt/Auth/AuthService.h>
|
|
#include <Wt/Auth/GoogleService.h>
|
|
#include <Wt/Auth/HashFunction.h>
|
|
#include <Wt/Auth/PasswordService.h>
|
|
#include <Wt/Auth/PasswordStrengthValidator.h>
|
|
#include <Wt/Auth/PasswordVerifier.h>
|
|
#include <Wt/Dbo/FixedSqlConnectionPool.h>
|
|
|
|
const std::string ADMIN_USERNAME = "admin";
|
|
const std::string ADMIN_PASSWORD = "admin";
|
|
|
|
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;
|
|
|
|
BlogSession::BlogSession(Wt::Dbo::SqlConnectionPool &connectionPool)
|
|
: m_connectionPool(connectionPool), m_users(*this) {
|
|
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";
|
|
}
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
Wt::Dbo::ptr<User> BlogSession::user() const {
|
|
if (m_login.loggedIn())
|
|
return m_users.find(m_login.user());
|
|
else
|
|
return Wt::Dbo::ptr<User>();
|
|
}
|
|
|
|
Wt::Signal<Wt::Dbo::ptr<Comment>> &BlogSession::commentsChanged() {
|
|
return m_commentsChanged;
|
|
}
|
|
|
|
Wt::Auth::PasswordService *BlogSession::passwordAuth() const {
|
|
return &blogPasswords;
|
|
}
|
|
|
|
const std::vector<const Wt::Auth::OAuthService *> &BlogSession::oAuth() const {
|
|
return blogOAuth;
|
|
}
|