#include "Restful.h" #include "Application.h" #include "Database/Session.h" #include #include #include #include #include #include #include DBO_INSTANTIATE_TEMPLATES(MyMessage) DbStruct *m_dbStruct; void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { auto app = Amass::Singleton::instance(); auto session = Database::session(); auto &service = app->authService(); auto enabled = service.authTokenUpdateEnabled(); boost::scope::scope_exit raii([&enabled, &service] { service.setAuthTokenUpdateEnabled(enabled); }); service.setAuthTokenUpdateEnabled(false); Wt::Auth::AuthTokenState state; Wt::Auth::User user; if (service.authTokensEnabled()) { const std::string *token = request.getCookieValue(service.authTokenCookieName()); if (token != nullptr) { Wt::Auth::AuthTokenResult result = service.processAuthToken(*token, session->users()); state = result.state(); if (state == Wt::Auth::AuthTokenState::Valid) { user = result.user(); } } } MyMessage message; if (user.isValid()) { message.user = user.identity(Wt::Auth::Identity::LoginName).toUTF8(); } LOG(info) << "state: " << (int)state << " " << message.user; response.setMimeType("application/json"); response.addHeader("Server", "Wt"); message.message = "Hello, World!"; Wt::Dbo::JsonSerializer writer(response.out()); writer.serialize(message); } void PlaintextResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { response.setMimeType("text/plain"); response.addHeader("Server", "Wt"); response.out() << "Hello, World!"; } int DbStruct::rand() { return distribution(rng); } AuthenticationResource::AuthenticationResource() { } DbStruct::DbStruct(const std::string &db) : rng(clock()), distribution(1, 10000) { session.setConnection(std::make_unique(db)); session.mapClass("world"); session.mapClass("fortune"); }