tcMenu
RemoteAuthentication.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
11 #ifndef _REMOTE_AUTHENTICATION_H_
12 #define _REMOTE_AUTHENTICATION_H_
13 
14 #include <EepromAbstraction.h>
15 #include "tcUtil.h"
16 
17 #define MAX_PIN_LENGTH 16
18 
19 #define UUID_KEY_SIZE 40
20 #define CLIENT_DESC_SIZE 16
21 #define TOTAL_KEY_SIZE (UUID_KEY_SIZE + CLIENT_DESC_SIZE)
22 
23 enum AuthenticationManagerType: uint8_t {
24  AUTHENTICATION_NONE, AUTHENTICATION_IN_EEPROM, AUTHENTICATION_IN_FLASH
25 };
26 
34 private:
35  AuthenticationManagerType authenticationManagerType;
36 public:
37  explicit AuthenticationManager(AuthenticationManagerType authType) : authenticationManagerType(authType) {}
38 
43  AuthenticationManagerType getAuthenticationManagerType() { return authenticationManagerType; }
44 
54  virtual bool addAdditionalUUIDKey(const char* connectionName, const char* uuid)=0;
55 
62  virtual bool isAuthenticated(const char* connectionName, const char* authResponse)=0;
63 
70  virtual bool doesPinMatch(const char* pinAttempt)=0;
71 };
72 
79 private:
80  EepromAbstraction *eeprom;
81  EepromPosition romStart;
82  uint16_t magicKey;
83  uint8_t numberOfEntries;
84 public:
85  explicit EepromAuthenticatorManager(uint8_t numOfEntries = 6) : AuthenticationManager(AUTHENTICATION_IN_EEPROM) {
86  eeprom = nullptr;
87  romStart = 0;
88  this->magicKey = 0;
89  this->numberOfEntries = numOfEntries;
90  }
91 
100  void initialise(EepromAbstraction* eeprom, EepromPosition start, uint16_t magicKey = 0x9B32);
101 
106  void resetAllKeys();
107 
112  void changePin(const char* newPin);
113 
119  void copyPinToBuffer(char* buffer, int size);
120 
127  void copyKeyNameToBuffer(int idx, char* buffer, int bufSize);
128 
135  bool addAdditionalUUIDKey(const char* connectionName, const char* uuid) override;
136 
142  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
143 
144  bool doesPinMatch(const char* pinAttempt) override;
145 
149  int getNumberOfEntries() const {
150  return numberOfEntries;
151  }
152 private:
153  // finds the slot (or an empty slot) or if neither are found returns -1
154  int findSlotFor(const char* name);
155 
156  // helper to calculate the eeprom position from an index.
157  EepromPosition eepromOffset(int i) const {
158  return romStart + 2 + (i * TOTAL_KEY_SIZE);
159  }
160 };
161 
167 public:
168  NoAuthenticationManager() : AuthenticationManager(AUTHENTICATION_NONE) { }
169 
171  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return true; }
172 
174  bool isAuthenticated(const char* /*connectionName*/, const char* /*authResponse*/) override { return true; }
175 
177  bool doesPinMatch(const char*) override { return true; }
178 };
179 
184 struct AuthBlock {
185  char name[CLIENT_DESC_SIZE];
186  char uuid[UUID_KEY_SIZE];
187 };
188 
194 private:
195  const AuthBlock* authBlocksPgm;
196  int numberOfEntries;
197  const char* pgmActualPin;
198 public:
204  ReadOnlyAuthenticationManager(const AuthBlock* authBlocksPgm, int numberOfEntries, const char* pgmActualPin)
205  : AuthenticationManager(AUTHENTICATION_IN_FLASH) {
206  this->authBlocksPgm = authBlocksPgm;
207  this->numberOfEntries = numberOfEntries;
208  this->pgmActualPin = pgmActualPin;
209  }
210 
211  explicit ReadOnlyAuthenticationManager(const char* pgmActualPin) : AuthenticationManager(AUTHENTICATION_IN_FLASH) {
212  this->authBlocksPgm = nullptr;
213  this->numberOfEntries = 0;
214  this->pgmActualPin = pgmActualPin;
215  }
216 
218  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return false; }
219 
225  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
226 
227  bool doesPinMatch(const char* pinAttempt) override {
228  return strcmp_P(pgmActualPin, pinAttempt) == 0;
229  }
230 };
231 
232 #endif //_REMOTE_AUTHENTICATION_H_
virtual bool addAdditionalUUIDKey(const char *connectionName, const char *uuid)=0
virtual bool isAuthenticated(const char *connectionName, const char *authResponse)=0
int getNumberOfEntries() const
Definition: RemoteAuthentication.h:149
Definition: RemoteAuthentication.h:184
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:171
bool isAuthenticated(const char *, const char *) override
Definition: RemoteAuthentication.h:174
AuthenticationManagerType getAuthenticationManagerType()
Definition: RemoteAuthentication.h:43
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:218
ReadOnlyAuthenticationManager(const AuthBlock *authBlocksPgm, int numberOfEntries, const char *pgmActualPin)
Definition: RemoteAuthentication.h:204
bool doesPinMatch(const char *) override
Definition: RemoteAuthentication.h:177
Definition: RemoteAuthentication.h:33
Definition: RemoteAuthentication.h:166
bool doesPinMatch(const char *pinAttempt) override
Definition: RemoteAuthentication.h:227
Definition: RemoteAuthentication.h:193
virtual bool doesPinMatch(const char *pinAttempt)=0
Definition: RemoteAuthentication.h:78