Older/WebApplication/model/BlogSession.cpp
luocai 4021ffa190
Some checks failed
Deploy / PullDocker (push) Successful in 4s
Deploy / Build (push) Failing after 34s
Deploy Docker Images / Docusaurus build and Server deploy (push) Successful in 13s
add docker images.
2024-11-06 15:51:01 +08:00

71 lines
2.4 KiB
C++

#include "BlogSession.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>
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) {
}
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;
}