Older/WebApplication/model/AuthModel.cpp
amass c3f535e82e
All checks were successful
Deploy / Build (push) Successful in 7m23s
update cookie lifetime.
2025-01-03 14:12:02 +08:00

45 lines
1.8 KiB
C++

#include "AuthModel.h"
#include "../Application.h"
#include <Wt/Http/Cookie.h>
#include <Wt/WApplication.h>
#include <Wt/WEnvironment.h>
AuthModel::AuthModel(const Wt::Auth::AuthService &baseAuth, Wt::Auth::AbstractUserDatabase &users)
: Wt::Auth::AuthModel(baseAuth, users) {
}
Wt::Auth::User AuthModel::processAuthToken() {
using namespace Wt::Auth;
if (baseAuth()->authTokensEnabled()) {
Wt::WApplication *app = Wt::WApplication::instance();
const Wt::WEnvironment &env = app->environment();
const std::string *token = env.getCookie(baseAuth()->authTokenCookieName());
if (token) {
AuthTokenResult result = baseAuth()->processAuthToken(*token, users());
auto server = Amass::Singleton<WebToolkit::Server>::instance();
auto cookie = server->updateCookie(*token, result, app->environment().urlScheme() == "https");
if ((result.state() == AuthTokenState::Invalid) || !cookie.value().empty()) {
app->setCookie(cookie);
}
return result.state() == AuthTokenState::Valid ? result.user() : User();
}
}
return User();
}
void AuthModel::setRememberMeCookie(const Wt::Auth::User &user) {
using namespace Wt::Auth;
Wt::WApplication *app = Wt::WApplication::instance();
const AuthService *s = baseAuth();
Wt::Http::Cookie cookie(s->authTokenCookieName(), s->createAuthToken(user),
std::chrono::seconds(s->authTokenValidity() * 60));
cookie.setDomain(s->authTokenCookieDomain());
cookie.setPath(CookiePath);
cookie.setSecure(app->environment().urlScheme() == "https");
auto server = Amass::Singleton<WebToolkit::Server>::instance();
server->insertCookie(cookie.value());
app->setCookie(cookie);
}