45 lines
1.8 KiB
C++
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);
|
|
}
|