1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Basic authentication with ESP8266httpUpdate (#7190)

Add ability to use basic access authentication with ESP8266httpUpdate
This commit is contained in:
drhideg 2020-07-30 17:55:29 +02:00 committed by GitHub
parent 85ea47e9bc
commit d92e1edac8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -24,6 +24,7 @@ update KEYWORD2
updateSpiffs KEYWORD2 updateSpiffs KEYWORD2
getLastError KEYWORD2 getLastError KEYWORD2
getLastErrorString KEYWORD2 getLastErrorString KEYWORD2
setAuthorization KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)
@ -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_SERVER_FAULTY_MD5 LITERAL1 RESERVED_WORD_2
HTTP_UE_BIN_VERIFY_HEADER_FAILED 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_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_FAILED LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2 HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2 HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2

View File

@ -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 #if HTTPUPDATE_1_2_COMPATIBLE
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion, HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
const String& httpsFingerprint, bool reboot) const String& httpsFingerprint, bool reboot)
@ -241,6 +261,8 @@ String ESP8266HTTPUpdate::getLastErrorString(void)
return F("Verify Bin Header Failed"); return F("Verify Bin Header Failed");
case HTTP_UE_BIN_FOR_WRONG_FLASH: case HTTP_UE_BIN_FOR_WRONG_FLASH:
return F("New Binary Does Not Fit Flash Size"); return F("New Binary Does Not Fit Flash Size");
case HTTP_UE_SERVER_UNAUTHORIZED:
return F("Unauthorized (401)");
} }
return String(); return String();
@ -282,6 +304,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
http.addHeader(F("x-ESP8266-version"), currentVersion); 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" }; const char * headerkeys[] = { "x-MD5" };
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
@ -432,6 +464,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
_setLastError(HTTP_UE_SERVER_FORBIDDEN); _setLastError(HTTP_UE_SERVER_FORBIDDEN);
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
break; break;
case HTTP_CODE_UNAUTHORIZED:
_setLastError(HTTP_UE_SERVER_UNAUTHORIZED);
ret = HTTP_UPDATE_FAILED;
break;
default: default:
_setLastError(HTTP_UE_SERVER_WRONG_HTTP_CODE); _setLastError(HTTP_UE_SERVER_WRONG_HTTP_CODE);
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;

View File

@ -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_SERVER_FAULTY_MD5 = (-105);
constexpr int HTTP_UE_BIN_VERIFY_HEADER_FAILED = (-106); constexpr int HTTP_UE_BIN_VERIFY_HEADER_FAILED = (-106);
constexpr int HTTP_UE_BIN_FOR_WRONG_FLASH = (-107); constexpr int HTTP_UE_BIN_FOR_WRONG_FLASH = (-107);
constexpr int HTTP_UE_SERVER_UNAUTHORIZED = (-108);
enum HTTPUpdateResult { enum HTTPUpdateResult {
HTTP_UPDATE_FAILED, HTTP_UPDATE_FAILED,
@ -111,6 +112,9 @@ public:
_ledOn = ledOn; _ledOn = ledOn;
} }
void setAuthorization(const String& user, const String& password);
void setAuthorization(const String& auth);
#if HTTPUPDATE_1_2_COMPATIBLE #if HTTPUPDATE_1_2_COMPATIBLE
// This function is deprecated, use rebootOnUpdate and the next one instead // This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return update(const String& url, const String& currentVersion, t_httpUpdate_return update(const String& url, const String& currentVersion,
@ -174,6 +178,10 @@ protected:
int _lastError; int _lastError;
bool _rebootOnUpdate = true; bool _rebootOnUpdate = true;
bool _closeConnectionsOnUpdate = true; bool _closeConnectionsOnUpdate = true;
String _user;
String _password;
String _auth;
private: private:
int _httpClientTimeout; int _httpClientTimeout;
followRedirects_t _followRedirects; followRedirects_t _followRedirects;