| 
									
										
										
										
											2019-04-30 23:49:28 +01:00
										 |  |  | #ifndef SecurityManager_h
 | 
					
						
							|  |  |  | #define SecurityManager_h
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:50:19 +01:00
										 |  |  | #include <ArduinoJsonJWT.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-29 23:48:16 +01:00
										 |  |  | #include <ESPAsyncWebServer.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-20 00:32:49 +01:00
										 |  |  | #include <ESPUtils.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | #include <AsyncJson.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 00:32:49 +01:00
										 |  |  | #ifndef FACTORY_JWT_SECRET
 | 
					
						
							|  |  |  | #define FACTORY_JWT_SECRET ESPUtils::defaultDeviceValue()
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | #define ACCESS_TOKEN_PARAMATER "access_token"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 19:35:27 +01:00
										 |  |  | #define AUTHORIZATION_HEADER "Authorization"
 | 
					
						
							|  |  |  | #define AUTHORIZATION_HEADER_PREFIX "Bearer "
 | 
					
						
							|  |  |  | #define AUTHORIZATION_HEADER_PREFIX_LEN 7
 | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 00:31:20 +01:00
										 |  |  | #define MAX_JWT_SIZE 128
 | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class User { | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |  public: | 
					
						
							|  |  |  |   String username; | 
					
						
							|  |  |  |   String password; | 
					
						
							|  |  |  |   bool admin; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   User(String username, String password, bool admin) : username(username), password(password), admin(admin) { | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 00:19:41 +01:00
										 |  |  | class Authentication { | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |  public: | 
					
						
							|  |  |  |   User* user; | 
					
						
							|  |  |  |   boolean authenticated; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |  public: | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   Authentication(User& user) : user(new User(user)), authenticated(true) { | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   Authentication() : user(nullptr), authenticated(false) { | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   ~Authentication() { | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |     delete (user); | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-05-16 00:19:41 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | typedef std::function<boolean(Authentication& authentication)> AuthenticationPredicate; | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 23:48:16 +01: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) { | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |     return authentication.authenticated; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   }; | 
					
						
							|  |  |  |   static bool IS_ADMIN(Authentication& authentication) { | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |     return authentication.authenticated && authentication.user->admin; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-05-29 23:48:16 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 23:48:16 +01:00
										 |  |  | class SecurityManager { | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |  public: | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * Authenticate, returning the user if found | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-05-21 08:42:21 +01:00
										 |  |  |   virtual Authentication authenticate(const String& username, const String& password) = 0; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * Check the request header for the Authorization token | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   virtual Authentication authenticateRequest(AsyncWebServerRequest* request) = 0; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * Generate a JWT for the user provided | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   virtual String generateJWT(User* user) = 0; | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Filter a request with the provided predicate, only returning true if the predicate matches. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   virtual ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate) = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Wrap the provided request to provide validation against an AuthenticationPredicate. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-01 08:44:26 +00:00
										 |  |  |   virtual ArRequestHandlerFunction wrapRequest(ArRequestHandlerFunction onRequest, | 
					
						
							|  |  |  |                                                AuthenticationPredicate predicate) = 0; | 
					
						
							| 
									
										
										
										
											2020-05-14 23:23:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Wrap the provided json request callback to provide validation against an AuthenticationPredicate. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   virtual ArJsonRequestHandlerFunction wrapCallback(ArJsonRequestHandlerFunction callback, | 
					
						
							|  |  |  |                                                     AuthenticationPredicate predicate) = 0; | 
					
						
							| 
									
										
										
										
											2019-04-30 00:30:43 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 23:16:06 +00:00
										 |  |  | #endif  // end SecurityManager_h
 |