merge all URI handlers into a single dispatch function

This commit is contained in:
proddy
2026-05-19 21:28:30 +02:00
parent d670bc2b07
commit 3fd0573339
10 changed files with 225 additions and 124 deletions

View File

@@ -4,11 +4,13 @@
AuthenticationService::AuthenticationService(AsyncWebServer * server, SecurityManager * securityManager)
: _securityManager(securityManager) {
// none of these need authentication
server->on(VERIFY_AUTHORIZATION_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { verifyAuthorization(request); });
auto * handler = new AsyncCallbackJsonWebHandler(SIGN_IN_PATH);
handler->onRequest([this](AsyncWebServerRequest * request, JsonVariant json) { signIn(request, json); });
server->addHandler(handler);
// None of these need authentication: verifyAuthorization checks the JWT itself, and signIn IS the authentication flow.
securityManager->addEndpoint(server, VERIFY_AUTHORIZATION_PATH, AuthenticationPredicates::NONE_REQUIRED, [this](AsyncWebServerRequest * request) {
verifyAuthorization(request);
});
securityManager->addEndpoint(server, SIGN_IN_PATH, AuthenticationPredicates::NONE_REQUIRED, [this](AsyncWebServerRequest * request, JsonVariant json) {
signIn(request, json);
});
}
// Verifies that the request supplied a valid JWT.
@@ -24,10 +26,9 @@ void AuthenticationService::signIn(AsyncWebServerRequest * request, JsonVariant
String password = json["password"];
Authentication authentication = _securityManager->authenticate(username, password);
if (authentication.authenticated) {
User * user = authentication.user;
auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot();
jsonObject["access_token"] = _securityManager->generateJWT(user);
jsonObject["access_token"] = _securityManager->generateJWT(authentication.user.get());
response->setLength();
request->send(response);
return;