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 
30 public:
40  virtual bool addAdditionalUUIDKey(const char* connectionName, const char* uuid)=0;
41 
48  virtual bool isAuthenticated(const char* connectionName, const char* authResponse)=0;
49 
56  virtual bool doesPinMatch(const char* pinAttempt)=0;
57 };
58 
65 private:
66  EepromAbstraction *eeprom;
67  EepromPosition romStart;
68  uint16_t magicKey;
69  uint8_t numberOfEntries;
70 public:
71  EepromAuthenticatorManager(uint8_t numOfEntries = 6) {
72  eeprom = NULL;
73  romStart = 0;
74  this->magicKey = 0;
75  this->numberOfEntries = numOfEntries;
76  }
77 
86  void initialise(EepromAbstraction* eeprom, EepromPosition start, uint16_t magicKey = 0x9B32);
87 
92  void resetAllKeys();
93 
98  void changePin(const char* newPin);
99 
105  void copyPinToBuffer(char* buffer, int size);
106 
113  void copyKeyNameToBuffer(int idx, char* buffer, int bufSize);
114 
121  bool addAdditionalUUIDKey(const char* connectionName, const char* uuid) override;
122 
128  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
129 
130  bool doesPinMatch(const char* pinAttempt) override;
131 
136  return numberOfEntries;
137  }
138 private:
139  // finds the slot (or an empty slot) or if neither are found returns -1
140  int findSlotFor(const char* name);
141 
142  // helper to calculate the eeprom position from an index.
143  EepromPosition eepromOffset(int i) {
144  return romStart + 2 + (i * TOTAL_KEY_SIZE);
145  }
146 };
147 
153 public:
155 
157  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return true; }
158 
160  bool isAuthenticated(const char* /*connectionName*/, const char* /*authResponse*/) override { return true; }
161 
163  bool doesPinMatch(const char*) override { return true; }
164 };
165 
170 struct AuthBlock {
171  char name[CLIENT_DESC_SIZE];
172  char uuid[UUID_KEY_SIZE];
173 };
174 
180 private:
181  const AuthBlock* authBlocksPgm;
182  int numberOfEntries;
183  const char* pgmActualPin;
184 public:
190  ReadOnlyAuthenticationManager(const AuthBlock* authBlocksPgm, int numberOfEntries, const char* pgmActualPin) {
191  this->authBlocksPgm = authBlocksPgm;
192  this->numberOfEntries = numberOfEntries;
193  this->pgmActualPin = pgmActualPin;
194  }
195 
196  ReadOnlyAuthenticationManager(const char* pgmActualPin) {
197  this->authBlocksPgm = NULL;
198  this->numberOfEntries = 0;
199  this->pgmActualPin = pgmActualPin;
200  }
201 
203  bool addAdditionalUUIDKey(const char* /*connectionName*/, const char* /*uuid*/) override { return false; }
204 
210  bool isAuthenticated(const char* connectionName, const char* authResponse) override;
211 
212  bool doesPinMatch(const char* pinAttempt) override {
213  return strcmp_P(pgmActualPin, pinAttempt) == 0;
214  }
215 };
216 
217 #endif //_REMOTE_AUTHENTICATION_H_
virtual bool addAdditionalUUIDKey(const char *connectionName, const char *uuid)=0
virtual bool isAuthenticated(const char *connectionName, const char *authResponse)=0
Definition: RemoteAuthentication.h:170
int getNumberOfEntries()
Definition: RemoteAuthentication.h:135
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:157
bool isAuthenticated(const char *, const char *) override
Definition: RemoteAuthentication.h:160
bool addAdditionalUUIDKey(const char *, const char *) override
Definition: RemoteAuthentication.h:203
ReadOnlyAuthenticationManager(const AuthBlock *authBlocksPgm, int numberOfEntries, const char *pgmActualPin)
Definition: RemoteAuthentication.h:190
bool doesPinMatch(const char *) override
Definition: RemoteAuthentication.h:163
Definition: RemoteAuthentication.h:29
Definition: RemoteAuthentication.h:152
bool doesPinMatch(const char *pinAttempt) override
Definition: RemoteAuthentication.h:212
Definition: RemoteAuthentication.h:179
virtual bool doesPinMatch(const char *pinAttempt)=0
Definition: RemoteAuthentication.h:64