tcMenu
RemoteAuthentication.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
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 #ifndef MAX_PIN_LENGTH
18 #define MAX_PIN_LENGTH 16
19 #endif
20 
21 #ifndef CLIENT_DESC_SIZE
22 #define CLIENT_DESC_SIZE 16
23 #endif
24 
25 #define UUID_KEY_SIZE 40
26 #define TOTAL_KEY_SIZE (UUID_KEY_SIZE + CLIENT_DESC_SIZE)
27 
28 enum AuthenticationManagerType: uint8_t {
29  AUTHENTICATION_NONE, AUTHENTICATION_IN_EEPROM, AUTHENTICATION_IN_FLASH
30 };
31 
39 private:
40  AuthenticationManagerType authenticationManagerType;
41 public:
42  explicit AuthenticationManager(AuthenticationManagerType authType) : authenticationManagerType(authType) {}
43 
48  AuthenticationManagerType getAuthenticationManagerType() { return authenticationManagerType; }
49 
59  virtual bool addAdditionalUUIDKey(const char* connectionName, const char* uuid)=0;
60 
67  virtual bool isAuthenticated(const char* connectionName, const char* authResponse)=0;
68 
75  virtual bool doesPinMatch(const char* pinAttempt)=0;
76 };
77 
84 private:
85  EepromAbstraction *eeprom;
86  EepromPosition romStart;
87  uint16_t magicKey;
88  uint8_t numberOfEntries;
89 public:
90  explicit EepromAuthenticatorManager(uint8_t numOfEntries = 6) : AuthenticationManager(AUTHENTICATION_IN_EEPROM) {
91  eeprom = nullptr;
92  romStart = 0;
93  this->magicKey = 0;
94  this->numberOfEntries = numOfEntries;
95  }
96 
105  void initialise(EepromAbstraction* eeprom, EepromPosition start, uint16_t magicKey = 0x9B32);
106 
111  void resetAllKeys();
112 
117  void changePin(const char* newPin);
118 
124  void copyPinToBuffer(char* buffer, int size);
125 
132  void copyKeyNameToBuffer(int idx, char* buffer, int bufSize);
133 
140  bool addAdditionalUUIDKey(const char* connectionName, const char* uuid) override;
141 
147  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
148 
149  bool doesPinMatch(const char* pinAttempt) override;
150 
154  int getNumberOfEntries() const {
155  return numberOfEntries;
156  }
157 private:
158  // finds the slot (or an empty slot) or if neither are found returns -1
159  int findSlotFor(const char* name);
160 
161  // helper to calculate the eeprom position from an index.
162  EepromPosition eepromOffset(int i) const {
163  return romStart + 2 + (i * TOTAL_KEY_SIZE);
164  }
165 };
166 
172 public:
173  NoAuthenticationManager() : AuthenticationManager(AUTHENTICATION_NONE) { }
174 
176  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return true; }
177 
179  bool isAuthenticated(const char* /*connectionName*/, const char* /*authResponse*/) override { return true; }
180 
182  bool doesPinMatch(const char*) override { return true; }
183 };
184 
189 struct AuthBlock {
190  char name[CLIENT_DESC_SIZE];
191  char uuid[UUID_KEY_SIZE];
192 };
193 
199 private:
200  const AuthBlock* authBlocksPgm;
201  int numberOfEntries;
202  const char* pgmActualPin;
203 public:
209  ReadOnlyAuthenticationManager(const AuthBlock* authBlocksPgm, int numberOfEntries, const char* pgmActualPin)
210  : AuthenticationManager(AUTHENTICATION_IN_FLASH) {
211  this->authBlocksPgm = authBlocksPgm;
212  this->numberOfEntries = numberOfEntries;
213  this->pgmActualPin = pgmActualPin;
214  }
215 
216  explicit ReadOnlyAuthenticationManager(const char* pgmActualPin) : AuthenticationManager(AUTHENTICATION_IN_FLASH) {
217  this->authBlocksPgm = nullptr;
218  this->numberOfEntries = 0;
219  this->pgmActualPin = pgmActualPin;
220  }
221 
223  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return false; }
224 
230  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
231 
232  bool doesPinMatch(const char* pinAttempt) override {
233  return strcmp_P(pgmActualPin, pinAttempt) == 0;
234  }
235 };
236 
237 #endif //_REMOTE_AUTHENTICATION_H_
Definition: RemoteAuthentication.h:189
Definition: RemoteAuthentication.h:38
virtual bool isAuthenticated(const char *connectionName, const char *authResponse)=0
virtual bool doesPinMatch(const char *pinAttempt)=0
AuthenticationManagerType getAuthenticationManagerType()
Definition: RemoteAuthentication.h:48
virtual bool addAdditionalUUIDKey(const char *connectionName, const char *uuid)=0
Definition: RemoteAuthentication.h:83
bool addAdditionalUUIDKey(const char *connectionName, const char *uuid) override
Definition: RemoteAuthentication.cpp:19
void copyKeyNameToBuffer(int idx, char *buffer, int bufSize)
Definition: RemoteAuthentication.cpp:72
void initialise(EepromAbstraction *eeprom, EepromPosition start, uint16_t magicKey=0x9B32)
Definition: RemoteAuthentication.cpp:9
void copyPinToBuffer(char *buffer, int size)
Definition: RemoteAuthentication.cpp:124
bool isAuthenticated(const char *connectionName, const char *authResponse) override
Definition: RemoteAuthentication.cpp:49
bool doesPinMatch(const char *pinAttempt) override
Definition: RemoteAuthentication.cpp:117
void resetAllKeys()
Definition: RemoteAuthentication.cpp:83
int getNumberOfEntries() const
Definition: RemoteAuthentication.h:154
void changePin(const char *newPin)
Definition: RemoteAuthentication.cpp:129
Definition: RemoteAuthentication.h:171
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:176
bool doesPinMatch(const char *) override
Definition: RemoteAuthentication.h:182
bool isAuthenticated(const char *, const char *) override
Definition: RemoteAuthentication.h:179
Definition: RemoteAuthentication.h:198
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:223
bool isAuthenticated(const char *connectionName, const char *authResponse) override
Definition: RemoteAuthentication.cpp:133
ReadOnlyAuthenticationManager(const AuthBlock *authBlocksPgm, int numberOfEntries, const char *pgmActualPin)
Definition: RemoteAuthentication.h:209
bool doesPinMatch(const char *pinAttempt) override
Definition: RemoteAuthentication.h:232
A series of utilities that used throughout tcMenu.