Older/WebApplication/Session.cpp
luocai 1eaa4a6787
All checks were successful
Deploy / PullDocker (push) Successful in 8s
Deploy / Build (push) Successful in 2m46s
add code.
2024-10-31 19:40:29 +08:00

81 lines
2.5 KiB
C++

#include "Session.h"
#include "BoostLog.h"
#include <Wt/Auth/Dbo/AuthInfo.h>
#include <Wt/Auth/Dbo/UserDatabase.h>
#include <Wt/Auth/FacebookService.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/backend/Sqlite3.h>
Wt::Auth::AuthService myAuthService;
Wt::Auth::PasswordService myPasswordService(myAuthService);
std::vector<std::unique_ptr<Wt::Auth::OAuthService>> myOAuthServices;
Session::Session(const std::string &sqliteDb) {
auto connection = std::make_unique<Wt::Dbo::backend::Sqlite3>(sqliteDb);
connection->setProperty("show-queries", "true");
setConnection(std::move(connection));
mapClass<User>("user");
mapClass<AuthInfo>("auth_info");
mapClass<AuthInfo::AuthIdentityType>("auth_identity");
mapClass<AuthInfo::AuthTokenType>("auth_token");
try {
createTables();
LOG(info) << "Created database.";
} catch (Wt::Dbo::Exception &e) {
LOG(error) << e.what() << ", using existing database";
}
m_users = std::make_unique<UserDatabase>(*this);
}
Session::~Session() {
}
Wt::Auth::AbstractUserDatabase &Session::users() {
return *m_users;
}
Wt::Auth::Login &Session::login() {
return m_login;
}
void Session::configureAuth() {
myAuthService.setAuthTokensEnabled(true, "logincookie");
// myAuthService.setEmailVerificationEnabled(true);
// myAuthService.setEmailVerificationRequired(true);
auto verifier = std::make_unique<Wt::Auth::PasswordVerifier>();
verifier->addHashFunction(std::make_unique<Wt::Auth::BCryptHashFunction>(7));
myPasswordService.setVerifier(std::move(verifier));
myPasswordService.setPasswordThrottle(std::make_unique<Wt::Auth::AuthThrottle>());
myPasswordService.setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
if (Wt::Auth::GoogleService::configured()) {
myOAuthServices.push_back(std::make_unique<Wt::Auth::GoogleService>(myAuthService));
}
if (Wt::Auth::FacebookService::configured()) {
myOAuthServices.push_back(std::make_unique<Wt::Auth::FacebookService>(myAuthService));
}
for (const auto &oAuthService : myOAuthServices) {
oAuthService->generateRedirectEndpoint();
}
}
const Wt::Auth::AuthService &Session::auth() {
return myAuthService;
}
const Wt::Auth::PasswordService &Session::passwordAuth() {
return myPasswordService;
}