2019-04-30 22:49:28 +00:00
|
|
|
#ifndef SecurityManager_h
|
|
|
|
#define SecurityManager_h
|
|
|
|
|
2019-05-06 14:50:19 +00:00
|
|
|
#include <ArduinoJsonJWT.h>
|
2019-05-29 22:48:16 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2019-12-03 23:16:06 +00:00
|
|
|
#include <list>
|
2019-04-29 23:30:43 +00:00
|
|
|
|
|
|
|
#define DEFAULT_JWT_SECRET "esp8266-react"
|
|
|
|
|
2019-05-18 18:35:27 +00:00
|
|
|
#define AUTHORIZATION_HEADER "Authorization"
|
|
|
|
#define AUTHORIZATION_HEADER_PREFIX "Bearer "
|
|
|
|
#define AUTHORIZATION_HEADER_PREFIX_LEN 7
|
2019-04-29 23:30:43 +00:00
|
|
|
|
2019-05-02 23:31:20 +00:00
|
|
|
#define MAX_JWT_SIZE 128
|
2019-04-29 23:30:43 +00:00
|
|
|
|
|
|
|
class User {
|
2019-12-03 23:16:06 +00:00
|
|
|
private:
|
|
|
|
String _username;
|
|
|
|
String _password;
|
|
|
|
bool _admin;
|
|
|
|
|
|
|
|
public:
|
|
|
|
User(String username, String password, bool admin) : _username(username), _password(password), _admin(admin) {
|
|
|
|
}
|
|
|
|
String getUsername() {
|
|
|
|
return _username;
|
|
|
|
}
|
|
|
|
String getPassword() {
|
|
|
|
return _password;
|
|
|
|
}
|
|
|
|
bool isAdmin() {
|
|
|
|
return _admin;
|
|
|
|
}
|
2019-04-29 23:30:43 +00:00
|
|
|
};
|
|
|
|
|
2019-05-15 23:19:41 +00:00
|
|
|
class Authentication {
|
2019-12-03 23:16:06 +00:00
|
|
|
private:
|
|
|
|
User* _user;
|
|
|
|
boolean _authenticated;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Authentication(User& user) : _user(new User(user)), _authenticated(true) {
|
|
|
|
}
|
|
|
|
Authentication() : _user(nullptr), _authenticated(false) {
|
|
|
|
}
|
|
|
|
~Authentication() {
|
|
|
|
delete (_user);
|
|
|
|
}
|
|
|
|
User* getUser() {
|
|
|
|
return _user;
|
|
|
|
}
|
|
|
|
bool isAuthenticated() {
|
|
|
|
return _authenticated;
|
|
|
|
}
|
2019-05-15 23:19:41 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
typedef std::function<boolean(Authentication& authentication)> AuthenticationPredicate;
|
2019-04-29 23:30:43 +00:00
|
|
|
|
2019-05-29 22:48:16 +00:00
|
|
|
class AuthenticationPredicates {
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
static bool NONE_REQUIRED(Authentication& authentication) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
static bool IS_AUTHENTICATED(Authentication& authentication) {
|
|
|
|
return authentication.isAuthenticated();
|
|
|
|
};
|
|
|
|
static bool IS_ADMIN(Authentication& authentication) {
|
|
|
|
return authentication.isAuthenticated() && authentication.getUser()->isAdmin();
|
|
|
|
};
|
2019-05-29 22:48:16 +00:00
|
|
|
};
|
2019-04-29 23:30:43 +00:00
|
|
|
|
2019-05-29 22:48:16 +00:00
|
|
|
class SecurityManager {
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* Authenticate, returning the user if found
|
|
|
|
*/
|
|
|
|
Authentication authenticate(String username, String password);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the request header for the Authorization token
|
|
|
|
*/
|
|
|
|
Authentication authenticateRequest(AsyncWebServerRequest* request);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a JWT for the user provided
|
|
|
|
*/
|
|
|
|
String generateJWT(User* user);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the provided request to provide validation against an AuthenticationPredicate.
|
|
|
|
*/
|
|
|
|
ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, AuthenticationPredicate predicate);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ArduinoJsonJWT _jwtHandler = ArduinoJsonJWT(DEFAULT_JWT_SECRET);
|
|
|
|
std::list<User> _users;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*
|
|
|
|
* Lookup the user by JWT
|
|
|
|
*/
|
|
|
|
Authentication authenticateJWT(String jwt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the payload is correct
|
|
|
|
*/
|
|
|
|
boolean validatePayload(JsonObject& parsedPayload, User* user);
|
2019-04-29 23:30:43 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
#endif // end SecurityManager_h
|