#include "BlogSession.h" #include "BoostLog.h" #include "Post.h" #include "Token.h" #include "User.h" #include "asciidoc.h" #include #include #include #include #include #include #include const std::string ADMIN_USERNAME = "admin"; const std::string ADMIN_PASSWORD = "admin"; class BlogOAuth : public std::vector { 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"); mapClass("post"); mapClass("tag"); mapClass("token"); mapClass("user"); try { Wt::Dbo::Transaction t(*this); createTables(); Wt::Dbo::ptr admin = add(std::make_unique()); 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 = add(std::make_unique()); 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 rootComment = add(std::make_unique()); 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 verifier = std::make_unique(); verifier->addHashFunction(std::make_unique(7)); #ifdef WT_WITH_SSL verifier->addHashFunction(std::make_unique()); #endif #ifdef HAVE_CRYPT verifier->addHashFunction(std::make_unique()); #endif blogPasswords.setVerifier(std::move(verifier)); blogPasswords.setPasswordThrottle(std::make_unique()); blogPasswords.setStrengthValidator(std::make_unique()); if (Wt::Auth::GoogleService::configured()) blogOAuth.push_back(new Wt::Auth::GoogleService(blogAuth)); } std::unique_ptr BlogSession::createConnectionPool(const std::string &sqlite3) { auto connection = std::make_unique(sqlite3); connection->setProperty("show-queries", "true"); connection->setDateTimeStorage(Wt::Dbo::SqlDateTimeType::DateTime, Wt::Dbo::backend::DateTimeStorage::PseudoISO8601AsText); return std::make_unique(std::move(connection), 10); } Wt::Dbo::ptr BlogSession::user() const { if (m_login.loggedIn()) return m_users.find(m_login.user()); else return Wt::Dbo::ptr(); } Wt::Signal> &BlogSession::commentsChanged() { return m_commentsChanged; } Wt::Auth::PasswordService *BlogSession::passwordAuth() const { return &blogPasswords; } const std::vector &BlogSession::oAuth() const { return blogOAuth; }