75 lines
3.2 KiB
C++
75 lines
3.2 KiB
C++
#include "Restful.h"
|
|
#include "Application.h"
|
|
#include "Database/Session.h"
|
|
#include "Database/User.h"
|
|
#include "model/AuthModel.h"
|
|
#include <Wt/Auth/AuthService.h>
|
|
#include <Wt/Auth/Identity.h>
|
|
#include <Wt/Dbo/Impl.h>
|
|
#include <Wt/Dbo/Json.h>
|
|
#include <Wt/Dbo/backend/Sqlite3.h>
|
|
#include <Wt/Http/Response.h>
|
|
#include <boost/beast/http/status.hpp>
|
|
#include <boost/scope/scope_exit.hpp>
|
|
#include <format>
|
|
|
|
DBO_INSTANTIATE_TEMPLATES(MyMessage)
|
|
|
|
void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
|
|
using namespace Wt;
|
|
auto tag = request.urlParam("tag");
|
|
// LOG(info) << "path: " << request.path() << ", tag: " << tag << ", server: " << request.hostName();
|
|
response.setMimeType("application/json");
|
|
MyMessage message;
|
|
auto app = Amass::Singleton<WebToolkit::Server>::instance();
|
|
auto &service = app->authService();
|
|
if (tag == "verify") {
|
|
const std::string *token = request.getCookieValue(service.authTokenCookieName());
|
|
Auth::AuthTokenState state = Auth::AuthTokenState::Invalid;
|
|
if (token != nullptr) {
|
|
if (auto u = app->user(*token); u) {
|
|
state = Auth::AuthTokenState::Valid;
|
|
message.user = u->identity;
|
|
} else {
|
|
Wt::Auth::User user;
|
|
auto session = Database::session();
|
|
auto enabled = service.authTokenUpdateEnabled();
|
|
boost::scope::scope_exit raii([&enabled, &service] { service.setAuthTokenUpdateEnabled(enabled); });
|
|
service.setAuthTokenUpdateEnabled(false);
|
|
if (service.authTokensEnabled()) {
|
|
Auth::AuthTokenResult result = service.processAuthToken(*token, session->users());
|
|
state = result.state();
|
|
if (state == Auth::AuthTokenState::Valid) {
|
|
user = result.user();
|
|
message.user = user.identity(Auth::Identity::LoginName).toUTF8();
|
|
}
|
|
}
|
|
}
|
|
// LOG(info) << "state: " << (int)state << " " << message.user;
|
|
} else {
|
|
LOG(warning) << "cannot access cookie.";
|
|
}
|
|
message.message = "Hello, World!";
|
|
message.status = state == Auth::AuthTokenState::Valid ? 0 : 404;
|
|
using namespace boost::beast::http;
|
|
response.setStatus(static_cast<int>(state == Auth::AuthTokenState::Valid ? status::ok : status::unauthorized));
|
|
} else { // logout
|
|
auto domain = request.hostName();
|
|
if (domain.find("amass.fun") != std::string::npos) {
|
|
domain = AuthModel::CookieDomain;
|
|
}
|
|
response.addHeader("Set-Cookie", std::format("{}=; path={}; Domain={}; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
|
service.authTokenCookieName(), AuthModel::CookiePath, domain));
|
|
}
|
|
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!";
|
|
}
|
|
|
|
AuthenticationResource::AuthenticationResource() {
|
|
} |