Skip to content

Commit

Permalink
v.2.3.3 - secure password field, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arkhipenko committed Sep 24, 2021
1 parent 021e9b0 commit 17a7678
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 43 deletions.
8 changes: 5 additions & 3 deletions src/EspBootstrapDict.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EspBootstrapDict : public EspBootstrapBase {
EspBootstrapDict();
virtual ~EspBootstrapDict();

int8_t run(Dictionary &aDict, uint8_t aNum = 0, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE);
int8_t run(Dictionary &aDict, uint8_t aNum = 0, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE, bool aSecPass = true);
void handleRoot ();
void handleSubmit ();
inline void cancel() { iCancelAP = true; } ;
Expand All @@ -51,6 +51,7 @@ class EspBootstrapDict : public EspBootstrapBase {
int8_t doRun();

bool iCancelAP;
bool iSecurePassword;
Dictionary* iDict;

};
Expand All @@ -75,7 +76,7 @@ void __espbootstrap_handlesubmit() {
}


int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout) {
int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout, bool aSecPass) {
if (aNum == 0) {
iNum = aDict.count() - 1;
}
Expand All @@ -87,6 +88,7 @@ int8_t EspBootstrapDict::run(Dictionary &aDict, uint8_t aNum, uint32_t aTimeout)

iDict = &aDict;
iTimeout = aTimeout;
iSecurePassword = aSecPass;

iCancelAP = false;
return doRun();
Expand Down Expand Up @@ -165,7 +167,7 @@ void EspBootstrapDict::handleRoot() {
for (int i = 1; i <= iNum; i++) {
String s = d(i);
s.toUpperCase();
if ( s.indexOf("PASSWORD") >= 0 || s.indexOf("PWD") >= 0 ) {
if ( iSecurePassword && (s.indexOf("PASSWORD") >= 0 || s.indexOf("PWD") >= 0) ) {
snprintf(buf, BUFLEN, "<label for=\"par%02d\"><b>%s:</b></label><br><input type=\"password\" id=\"par%02d\" name=\"par%02d\" value=\"%s\"><br>", i, d(i).c_str(), i, i, d[i].c_str() );
}
else {
Expand Down
15 changes: 11 additions & 4 deletions src/EspBootstrapMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class EspBootstrapMap : public EspBootstrapBase {
EspBootstrapMap();
virtual ~EspBootstrapMap();

int8_t run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE);
int8_t run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout = 10 * BOOTSTRAP_MINUTE, bool aSecPass = true);
void handleRoot ();
void handleSubmit ();
inline void cancel() { iCancelAP = true; } ;
Expand All @@ -51,6 +51,7 @@ class EspBootstrapMap : public EspBootstrapBase {
int8_t doRun();

bool iCancelAP;
bool iSecurePassword;
const char** iTitles;
char** iMap;
};
Expand All @@ -76,14 +77,15 @@ void __espbootstrap_handlesubmit() {
}


int8_t EspBootstrapMap::run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout) {
int8_t EspBootstrapMap::run(const char** aTitles, char** aMap, uint8_t aNum, uint32_t aTimeout, bool aSecPass) {

iNum = aNum;
iTitles = aTitles;
iMap = aMap;
iTimeout = aTimeout;
iCancelAP = false;

iSecurePassword = aSecPass;

return doRun();
}

Expand Down Expand Up @@ -157,7 +159,12 @@ void EspBootstrapMap::handleRoot() {
iServer->sendContent(buf);

for (int i = 1; i <= iNum; i++) {
snprintf(buf, BUFLEN, "<label for=\"par%02d\"><b>%s:</b></label><br><input type=\"text\" id=\"par%02d\" name=\"par%02d\" value=\"%s\"><br>", i, iTitles[i], i, i, iMap[i - 1] );
if ( iSecurePassword && false ) { // fr future use
snprintf(buf, BUFLEN, "<label for=\"par%02d\"><b>%s:</b></label><br><input type=\"password\" id=\"par%02d\" name=\"par%02d\" value=\"%s\"><br>", i, iTitles[i], i, i, iMap[i - 1] );
}
else {
snprintf(buf, BUFLEN, "<label for=\"par%02d\"><b>%s:</b></label><br><input type=\"text\" id=\"par%02d\" name=\"par%02d\" value=\"%s\"><br>", i, iTitles[i], i, i, iMap[i - 1] );
}
iServer->sendContent(buf);
}
iServer->sendContent("<br><input type=\"submit\" value=\"Submit\"></form></body></html>");
Expand Down
127 changes: 121 additions & 6 deletions src/JsonConfigBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE.

#include <Arduino.h>


#define JSON_OK 0
#define JSON_ERR (-1)
#define JSON_COMMA (-20)
Expand All @@ -51,15 +50,127 @@ class JsonConfigBase {
virtual ~JsonConfigBase();

protected:
virtual int8_t _doParse(size_t aLen, uint16_t aNum);
virtual int16_t _nextChar() { return JSON_EOF; };
virtual int8_t _doParse(Stream& aJson, uint16_t aNum);
virtual int8_t _storeKeyValue(const char* aKey, const char* aValue) { return JSON_MEM; };
};

JsonConfigBase::JsonConfigBase() {}
JsonConfigBase::~JsonConfigBase() {}

int8_t JsonConfigBase::_doParse(size_t aLen, uint16_t aNum) {
int8_t JsonConfigBase::_doParse(Stream& aJson, uint16_t aNum) {
bool insideQoute = false;
bool nextVerbatim = false;
bool isValue = false;
bool isComment = false;
int p = 0;
int8_t rc;
String currentKey;
String currentValue;

while ( aJson.peek() >= 0 ) {
char c = aJson.read();

//#ifdef _LIBDEBUG_
//Serial.print((uint8_t)c);
//Serial.print(" (");
//Serial.print(c);
//Serial.println(")");
//#endif

if ( isComment ) {
if ( c == '\n' ) {
isComment = false;
isValue = false;
}
continue;
}
if (nextVerbatim) {
nextVerbatim = false;
}

// not a comment and not a verbatim char
else {
// process all special cases: '\', '"', ':', and ','
if (c == '\\' ) {
nextVerbatim = true;
continue;
}

if ( c == '\"' ) {
if (!insideQoute) {
if ( isValue ) {
if ( currentValue.length() > 0 ) return JSON_FMT;
}
else {
if ( currentKey.length() > 0 ) return JSON_FMT;
}
insideQoute = true;
continue;
}
else {
insideQoute = false;
continue;
}
}

if (c == '\n') {
if ( insideQoute ) {
return JSON_QUOTE;
}
if ( nextVerbatim ) {
return JSON_BCKSL;
}
}

#ifdef _JSON_ASCII_ONLY
if ( c > 127 ) continue; // ignore non-ascii characters
#endif

if (!insideQoute) {
if ( c == '#' ) {
isComment = true;
continue;
}

if (c == ':') {
if ( isValue ) {
return JSON_COMMA; //missing comma probably
}
isValue = true;
continue;
}

if ( c == '{' || c == ' ' || c == '\t' || c == '\r' ) continue;

if ( c == ',' || c == '\n' || c == '}') {
if ( isValue ) {
if ( currentValue.length() == 0 ) return JSON_FMT;
isValue = false;
rc = _storeKeyValue( currentKey.c_str(), currentValue.c_str() );
if (rc) return JSON_MEM; // if error - exit with an error code
currentValue = String();
currentKey = String();
p++;
if (aNum > 0 && p >= aNum) break;
}
else {
if ( c == ',' ) return JSON_FMT;
}
continue;
}
}
}
if (isValue) currentValue.concat(c);
else currentKey.concat(c);
}
if (insideQoute || nextVerbatim || (aNum > 0 && p < aNum )) return JSON_EOF;
#ifdef _LIBDEBUG_
Serial.printf("Dictionary::jload: DICTIONARY_OK\n");
#endif
return JSON_OK;
}

/* int8_t JsonConfigBase::_doParse(size_t aLen, uint16_t aNum) {
bool insideQoute = false;
bool nextVerbatim = false;
Expand Down Expand Up @@ -143,7 +254,11 @@ Serial.print(c); Serial.print("("); Serial.print((int)c); Serial.print(")");
isValue = false;
continue;
}
if ( c == '{' || c == '}' || c == ' ' || c == '\t' || c == '\r' ) continue;
if ( c == '{' || c == ' ' || c == '\t' || c == '\n' || c == '\r' ) continue;
#ifdef _JSON_ASCII_ONLY
if ( c > 127 ) continue; // ignore non-ascii characters
#endif
if ( c == '}' ) break;
return JSON_FMT;
}
}
Expand All @@ -157,7 +272,7 @@ Serial.print(c); Serial.print("("); Serial.print((int)c); Serial.print(")");
Serial.printf("JsonConfigBase::_doParse: JSON_OK\n");
#endif
return JSON_OK;
}
} */



Expand Down
17 changes: 8 additions & 9 deletions src/JsonConfigHttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ class JsonConfigHttp : public JsonConfigBase {
int8_t parse(const String aHost, uint16_t aPort, String aUrl, Dictionary& aDict, int aNum = 0);

protected:
virtual int16_t _nextChar();
virtual int8_t _storeKeyValue(const char* aKey, const char* aValue);
virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); };
virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); };

private:
int8_t parseCommon(int aHttpResult, int aNum);

Dictionary* iDict;
WiFiClient iClient;
HTTPClient iHttp;
String iPayload;
size_t iIndex;
// String iPayload;
// size_t iIndex;
};

#ifndef _JSONCONFIG_NOSTATIC
Expand Down Expand Up @@ -122,9 +121,9 @@ int8_t JsonConfigHttp::parseCommon(int aHttpResult, int aNum) {
#endif
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
iPayload = iHttp.getString();
iIndex = 0;
rc = _doParse(iPayload.length(), aNum);
// iPayload = iHttp.getString();
// iIndex = 0;
rc = _doParse(iHttp.getStream(), aNum);
return rc;
}
}
Expand All @@ -140,14 +139,14 @@ int8_t JsonConfigHttp::parseCommon(int aHttpResult, int aNum) {
}


int16_t JsonConfigHttp::_nextChar() {
/* int16_t JsonConfigHttp::_nextChar() {
if (iIndex < iPayload.length() ) {
return (int16_t) iPayload[iIndex++];
}
else {
return JSON_EOF;
}
}
} */


int8_t JsonConfigHttp::_storeKeyValue(const char* aKey, const char* aValue){
Expand Down
17 changes: 8 additions & 9 deletions src/JsonConfigHttpMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ class JsonConfigHttpMap : public JsonConfigBase {
int8_t parse(const String aHost, uint16_t aPort, String aUrl, char** aMap, int aNum);

protected:
virtual int16_t _nextChar();
virtual int8_t _storeKeyValue(const char* aKey, const char* aValue);
virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); };
virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); };

private:
int8_t parseCommon(int aHttpResult, int aNum);

char** iMap;
HTTPClient iHttp;
String iPayload;
size_t iIndex;
// String iPayload;
// size_t iIndex;
size_t iParamIndex;
};

Expand Down Expand Up @@ -124,10 +123,10 @@ int8_t JsonConfigHttpMap::parseCommon(int aHttpResult, int aNum) {
#endif
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
iPayload = iHttp.getString();
iIndex = 0;
// iPayload = iHttp.getString();
// iIndex = 0;
iParamIndex = 0;
rc = _doParse(iPayload.length(), aNum);
rc = _doParse(iHttp.getStream(), aNum);
#ifdef _LIBDEBUG_
Serial.printf("JsonConfigHttpMap::parseCommon rc %d\n", rc );
#endif
Expand All @@ -145,7 +144,7 @@ int8_t JsonConfigHttpMap::parseCommon(int aHttpResult, int aNum) {
return JSON_ERR; // should never get here anyway - but stupid compiler complains.
}


/*
int16_t JsonConfigHttpMap::_nextChar() {
if (iIndex < iPayload.length() ) {
return (int16_t) iPayload[iIndex++];
Expand All @@ -154,7 +153,7 @@ int16_t JsonConfigHttpMap::_nextChar() {
return JSON_EOF;
}
}

*/

int8_t JsonConfigHttpMap::_storeKeyValue(const char* aKey, const char* aValue){
#ifdef _LIBDEBUG_
Expand Down
11 changes: 5 additions & 6 deletions src/JsonConfigSPIFFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ class JsonConfigSPIFFS : public JsonConfigBase {
int8_t parse(const String aUrl, Dictionary& aDict, int aNum = 0);

protected:
virtual int16_t _nextChar();
virtual int8_t _storeKeyValue(const char* aKey, const char* aValue);
virtual int8_t _doParse(size_t aLen, uint16_t aNum) { return JsonConfigBase::_doParse(aLen, aNum); };
virtual int8_t _doParse(Stream& aJson, uint16_t aNum) { return JsonConfigBase::_doParse(aJson, aNum); };

private:
Dictionary* iDict;
Expand Down Expand Up @@ -89,16 +88,16 @@ int8_t JsonConfigSPIFFS::parse(const String aUrl, Dictionary& aDict, int aNum) {
}

iDict = &aDict;
rc = _doParse ( iF.size(), aNum );
rc = _doParse ( iF, aNum );

iF.close();
return rc;
}


int16_t JsonConfigSPIFFS::_nextChar() {
return (int16_t) iF.read();
}
// int16_t JsonConfigSPIFFS::_nextChar() {
// return (int16_t) iF.read();
// }


int8_t JsonConfigSPIFFS::_storeKeyValue(const char* aKey, const char* aValue){
Expand Down
Loading

0 comments on commit 17a7678

Please sign in to comment.