2019-04-29 23:30:43 +00:00
|
|
|
#include <SecurityManager.h>
|
|
|
|
|
2019-05-18 18:35:27 +00:00
|
|
|
Authentication SecurityManager::authenticateRequest(AsyncWebServerRequest *request) {
|
2019-12-03 23:16:06 +00:00
|
|
|
AsyncWebHeader *authorizationHeader = request->getHeader(AUTHORIZATION_HEADER);
|
2019-05-18 18:35:27 +00:00
|
|
|
if (authorizationHeader) {
|
|
|
|
String value = authorizationHeader->value();
|
2019-12-03 23:16:06 +00:00
|
|
|
if (value.startsWith(AUTHORIZATION_HEADER_PREFIX)) {
|
2019-05-25 16:41:27 +00:00
|
|
|
value = value.substring(AUTHORIZATION_HEADER_PREFIX_LEN);
|
|
|
|
return authenticateJWT(value);
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 18:35:27 +00:00
|
|
|
return Authentication();
|
|
|
|
}
|
|
|
|
|
|
|
|
Authentication SecurityManager::authenticateJWT(String jwt) {
|
|
|
|
DynamicJsonDocument payloadDocument(MAX_JWT_SIZE);
|
2019-05-26 18:09:34 +00:00
|
|
|
_jwtHandler.parseJWT(jwt, payloadDocument);
|
2019-05-18 18:35:27 +00:00
|
|
|
if (payloadDocument.is<JsonObject>()) {
|
|
|
|
JsonObject parsedPayload = payloadDocument.as<JsonObject>();
|
|
|
|
String username = parsedPayload["username"];
|
2019-05-15 23:19:41 +00:00
|
|
|
for (User _user : _users) {
|
2019-12-03 23:16:06 +00:00
|
|
|
if (_user.getUsername() == username && validatePayload(parsedPayload, &_user)) {
|
2019-05-18 18:35:27 +00:00
|
|
|
return Authentication(_user);
|
2019-05-15 23:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 18:35:27 +00:00
|
|
|
return Authentication();
|
2019-04-29 23:30:43 +00:00
|
|
|
}
|
2019-04-30 22:49:28 +00:00
|
|
|
|
2019-05-15 23:19:41 +00:00
|
|
|
Authentication SecurityManager::authenticate(String username, String password) {
|
2019-04-30 22:49:28 +00:00
|
|
|
for (User _user : _users) {
|
2019-12-03 23:16:06 +00:00
|
|
|
if (_user.getUsername() == username && _user.getPassword() == password) {
|
2019-05-18 18:35:27 +00:00
|
|
|
return Authentication(_user);
|
2019-04-30 22:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 18:35:27 +00:00
|
|
|
return Authentication();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void populateJWTPayload(JsonObject &payload, User *user) {
|
|
|
|
payload["username"] = user->getUsername();
|
2019-12-03 23:16:06 +00:00
|
|
|
payload["admin"] = user->isAdmin();
|
2019-05-18 18:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean SecurityManager::validatePayload(JsonObject &parsedPayload, User *user) {
|
2019-12-03 23:16:06 +00:00
|
|
|
DynamicJsonDocument _jsonDocument(MAX_JWT_SIZE);
|
2019-05-18 18:35:27 +00:00
|
|
|
JsonObject payload = _jsonDocument.to<JsonObject>();
|
|
|
|
populateJWTPayload(payload, user);
|
|
|
|
return payload == parsedPayload;
|
2019-04-29 23:30:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 18:35:27 +00:00
|
|
|
String SecurityManager::generateJWT(User *user) {
|
2019-12-03 23:16:06 +00:00
|
|
|
DynamicJsonDocument _jsonDocument(MAX_JWT_SIZE);
|
2019-05-18 18:35:27 +00:00
|
|
|
JsonObject payload = _jsonDocument.to<JsonObject>();
|
|
|
|
populateJWTPayload(payload, user);
|
2019-05-26 18:09:34 +00:00
|
|
|
return _jwtHandler.buildJWT(payload);
|
2019-04-29 23:30:43 +00:00
|
|
|
}
|
2019-05-29 22:48:16 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
ArRequestHandlerFunction SecurityManager::wrapRequest(ArRequestHandlerFunction onRequest,
|
|
|
|
AuthenticationPredicate predicate) {
|
|
|
|
return [this, onRequest, predicate](AsyncWebServerRequest *request) {
|
2019-05-29 22:48:16 +00:00
|
|
|
Authentication authentication = authenticateRequest(request);
|
|
|
|
if (!predicate(authentication)) {
|
|
|
|
request->send(401);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onRequest(request);
|
|
|
|
};
|
2019-12-03 23:16:06 +00:00
|
|
|
}
|