Older/WebApplication/model/AuthModel.cpp

45 lines
1.8 KiB
C++
Raw Permalink Normal View History

2025-01-03 14:12:02 +08:00
#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() {
2025-01-09 22:23:03 +08:00
using namespace Wt;
2025-01-03 14:12:02 +08:00
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) {
2025-01-09 22:23:03 +08:00
Auth::AuthTokenResult result = baseAuth()->processAuthToken(*token, users());
2025-01-03 14:12:02 +08:00
auto server = Amass::Singleton<WebToolkit::Server>::instance();
auto cookie = server->updateCookie(*token, result, app->environment().urlScheme() == "https");
2025-01-09 22:23:03 +08:00
if ((result.state() == Auth::AuthTokenState::Invalid) || !cookie.value().empty()) {
2025-01-03 14:12:02 +08:00
app->setCookie(cookie);
}
2025-01-09 22:23:03 +08:00
return result.state() == Auth::AuthTokenState::Valid ? result.user() : Auth::User();
2025-01-03 14:12:02 +08:00
}
}
2025-01-09 22:23:03 +08:00
return Auth::User();
2025-01-03 14:12:02 +08:00
}
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();
2025-01-09 22:23:03 +08:00
server->insertCookie(cookie.value(), user);
2025-01-03 14:12:02 +08:00
app->setCookie(cookie);
}