mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-24 19:42:27 +03:00
Basic authentication with ESP8266httpUpdate (#7190)
Add ability to use basic access authentication with ESP8266httpUpdate
This commit is contained in:
@ -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)
|
||||
@ -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();
|
||||
@ -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*);
|
||||
|
||||
@ -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;
|
||||
|
Reference in New Issue
Block a user