add login cookie verify.
All checks were successful
Deploy / Build (push) Successful in 7m46s

This commit is contained in:
amass 2024-12-26 23:10:41 +08:00
parent fed59acef3
commit 4b90a75a51
4 changed files with 36 additions and 5 deletions

View File

@ -16,6 +16,7 @@
#include <Wt/Dbo/FixedSqlConnectionPool.h> #include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/SqlConnectionPool.h> #include <Wt/Dbo/SqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h> #include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/Http/Cookie.h>
#include <Wt/WContainerWidget.h> #include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h> #include <Wt/WEnvironment.h>
#include <Wt/WServer.h> #include <Wt/WServer.h>
@ -90,6 +91,10 @@ void Application::authEvent() {
m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage)); m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage));
} }
setInternalPath("/", true); setInternalPath("/", true);
auto app = Amass::Singleton<WebToolkit::Server>::instance();
auto &service = app->authService();
Wt::Http::Cookie cookie(service.authTokenCookieName(), service.createAuthToken(u));
setCookie(cookie);
} else { } else {
m_loginPage = m_navigationBar->removeLoginItem(); m_loginPage = m_navigationBar->removeLoginItem();
LOG(info) << "User logged out."; LOG(info) << "User logged out.";
@ -167,7 +172,7 @@ void Server::initializeAuthenticationService() {
m_passwordService->setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>()); m_passwordService->setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
} }
const Wt::Auth::AuthService &Server::authService() { Wt::Auth::AuthService &Server::authService() {
return *m_authService; return *m_authService;
} }

View File

@ -49,7 +49,7 @@ public:
~Server(); ~Server();
void initializeAuthenticationService(); void initializeAuthenticationService();
const Wt::Auth::AuthService &authService(); Wt::Auth::AuthService &authService();
const Wt::Auth::PasswordService &passwordService(); const Wt::Auth::PasswordService &passwordService();
protected: protected:

View File

@ -1,22 +1,46 @@
#include "Restful.h" #include "Restful.h"
#include "Application.h"
#include "Database/Session.h" #include "Database/Session.h"
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/Identity.h>
#include <Wt/Dbo/Impl.h> #include <Wt/Dbo/Impl.h>
#include <Wt/Dbo/Json.h> #include <Wt/Dbo/Json.h>
#include <Wt/Dbo/backend/Sqlite3.h> #include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/Http/Response.h> #include <Wt/Http/Response.h>
#include <boost/scope/scope_exit.hpp>
DBO_INSTANTIATE_TEMPLATES(MyMessage) DBO_INSTANTIATE_TEMPLATES(MyMessage)
DbStruct *m_dbStruct; DbStruct *m_dbStruct;
void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
auto app = Amass::Singleton<WebToolkit::Server>::instance();
auto session = Database::session(); 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.setMimeType("application/json");
response.addHeader("Server", "Wt"); response.addHeader("Server", "Wt");
MyMessage message;
message.message = "Hello, World!"; message.message = "Hello, World!";
Wt::Dbo::JsonSerializer writer(response.out()); Wt::Dbo::JsonSerializer writer(response.out());
writer.serialize(message); writer.serialize(message);
} }

View File

@ -9,10 +9,12 @@
class MyMessage { class MyMessage {
public: public:
std::string message; std::string message;
std::string user;
template <class Action> template <class Action>
void persist(Action &a) { void persist(Action &a) {
Wt::Dbo::field(a, message, "message"); Wt::Dbo::field(a, message, "message");
Wt::Dbo::field(a, user, "user");
} }
}; };