Older/WebApplication/model/AuthModel.cpp
amass b17b50b751
All checks were successful
Deploy / Build (push) Successful in 5m33s
fix login rediret retry error.
2025-01-09 22:23:03 +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;
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) {
Auth::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() == Auth::AuthTokenState::Invalid) || !cookie.value().empty()) {
app->setCookie(cookie);
}
return result.state() == Auth::AuthTokenState::Valid ? result.user() : Auth::User();
}
}
return Auth::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(), user);
app->setCookie(cookie);
}