Older/WebApplication/Restful.cpp

75 lines
3.2 KiB
C++
Raw Permalink Normal View History

2024-11-01 19:05:20 +08:00
#include "Restful.h"
2024-12-26 23:10:41 +08:00
#include "Application.h"
2024-11-26 22:58:54 +08:00
#include "Database/Session.h"
2025-01-09 22:23:03 +08:00
#include "Database/User.h"
2025-01-03 22:17:45 +08:00
#include "model/AuthModel.h"
2024-12-26 23:10:41 +08:00
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/Identity.h>
2024-11-01 19:05:20 +08:00
#include <Wt/Dbo/Impl.h>
#include <Wt/Dbo/Json.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/Http/Response.h>
2025-01-09 19:16:00 +08:00
#include <boost/beast/http/status.hpp>
2024-12-26 23:10:41 +08:00
#include <boost/scope/scope_exit.hpp>
2025-01-03 22:17:45 +08:00
#include <format>
2024-11-01 19:05:20 +08:00
DBO_INSTANTIATE_TEMPLATES(MyMessage)
2024-11-14 21:53:18 +08:00
void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
2025-01-09 22:23:03 +08:00
using namespace Wt;
2025-01-03 22:17:45 +08:00
auto tag = request.urlParam("tag");
2025-01-09 19:38:36 +08:00
// LOG(info) << "path: " << request.path() << ", tag: " << tag << ", server: " << request.hostName();
2025-01-03 22:17:45 +08:00
response.setMimeType("application/json");
MyMessage message;
2024-12-26 23:10:41 +08:00
auto app = Amass::Singleton<WebToolkit::Server>::instance();
auto &service = app->authService();
2025-01-03 22:17:45 +08:00
if (tag == "verify") {
2025-01-09 22:23:03 +08:00
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();
}
2025-01-03 22:17:45 +08:00
}
2024-12-26 23:10:41 +08:00
}
2025-01-09 22:23:03 +08:00
// LOG(info) << "state: " << (int)state << " " << message.user;
} else {
LOG(warning) << "cannot access cookie.";
2024-12-26 23:10:41 +08:00
}
2025-01-03 22:17:45 +08:00
message.message = "Hello, World!";
2025-01-09 22:23:03 +08:00
message.status = state == Auth::AuthTokenState::Valid ? 0 : 404;
2025-01-09 19:16:00 +08:00
using namespace boost::beast::http;
2025-01-09 22:23:03 +08:00
response.setStatus(static_cast<int>(state == Auth::AuthTokenState::Valid ? status::ok : status::unauthorized));
2025-01-03 22:17:45 +08:00
} else { // logout
2025-01-09 19:38:36 +08:00
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));
2024-12-26 23:10:41 +08:00
}
2025-01-09 22:23:03 +08:00
Dbo::JsonSerializer writer(response.out());
2024-11-01 19:05:20 +08:00
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!";
}
2024-11-26 22:58:54 +08:00
AuthenticationResource::AuthenticationResource() {
2025-01-03 22:17:45 +08:00
}