Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic authentication with ESP8266httpUpdate #7190

Merged
merged 7 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/ESP8266httpUpdate/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ update KEYWORD2
updateSpiffs KEYWORD2
getLastError KEYWORD2
getLastErrorString KEYWORD2
setAuthorization KEYWORD2

#######################################
# Constants (LITERAL1)
Expand All @@ -37,6 +38,7 @@ HTTP_UE_SERVER_WRONG_HTTP_CODE LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_FAULTY_MD5 LITERAL1 RESERVED_WORD_2
HTTP_UE_BIN_VERIFY_HEADER_FAILED LITERAL1 RESERVED_WORD_2
HTTP_UE_BIN_FOR_WRONG_FLASH LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_UNAUTHORIZED LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_FAILED LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2
36 changes: 36 additions & 0 deletions libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void)
{
}

/**
* set the Authorization for the http request
* @param user const String&
* @param password const String&
*/
void ESP8266HTTPUpdate::setAuthorization(const String &user, const String &password)
{
_user = user;
_password = password;
}

/**
* set the Authorization for the http request
* @param auth const String& base64
*/
void ESP8266HTTPUpdate::setAuthorization(const String &auth)
{
_auth = auth;
}

#if HTTPUPDATE_1_2_COMPATIBLE
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
const String& httpsFingerprint, bool reboot)
Expand Down Expand Up @@ -241,6 +261,8 @@ String ESP8266HTTPUpdate::getLastErrorString(void)
return F("Verify Bin Header Failed");
case HTTP_UE_BIN_FOR_WRONG_FLASH:
return F("New Binary Does Not Fit Flash Size");
case HTTP_UE_SERVER_UNAUTHORIZED:
return F("Unauthorized (401)");
}

return String();
Expand Down Expand Up @@ -282,6 +304,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
http.addHeader(F("x-ESP8266-version"), currentVersion);
}

if (!_user.isEmpty() && !_password.isEmpty())
{
http.setAuthorization(_user.c_str(), _password.c_str());
}

if (!_auth.isEmpty())
{
http.setAuthorization(_auth.c_str());
}

const char * headerkeys[] = { "x-MD5" };
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);

Expand Down Expand Up @@ -432,6 +464,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
_setLastError(HTTP_UE_SERVER_FORBIDDEN);
ret = HTTP_UPDATE_FAILED;
break;
case HTTP_CODE_UNAUTHORIZED:
_setLastError(HTTP_UE_SERVER_UNAUTHORIZED);
ret = HTTP_UPDATE_FAILED;
break;
default:
_setLastError(HTTP_UE_SERVER_WRONG_HTTP_CODE);
ret = HTTP_UPDATE_FAILED;
Expand Down
8 changes: 8 additions & 0 deletions libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ constexpr int HTTP_UE_SERVER_WRONG_HTTP_CODE = (-104);
constexpr int HTTP_UE_SERVER_FAULTY_MD5 = (-105);
constexpr int HTTP_UE_BIN_VERIFY_HEADER_FAILED = (-106);
constexpr int HTTP_UE_BIN_FOR_WRONG_FLASH = (-107);
constexpr int HTTP_UE_SERVER_UNAUTHORIZED = (-108);

enum HTTPUpdateResult {
HTTP_UPDATE_FAILED,
Expand Down Expand Up @@ -111,6 +112,9 @@ class ESP8266HTTPUpdate
_ledOn = ledOn;
}

void setAuthorization(const String& user, const String& password);
void setAuthorization(const String& auth);

#if HTTPUPDATE_1_2_COMPATIBLE
// This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return update(const String& url, const String& currentVersion,
Expand Down Expand Up @@ -174,6 +178,10 @@ class ESP8266HTTPUpdate
int _lastError;
bool _rebootOnUpdate = true;
bool _closeConnectionsOnUpdate = true;
String _user;
String _password;
String _auth;

private:
int _httpClientTimeout;
followRedirects_t _followRedirects;
Expand Down