Skip to content

Commit d92e1ed

Browse files
authored
Basic authentication with ESP8266httpUpdate (#7190)
Add ability to use basic access authentication with ESP8266httpUpdate
1 parent 85ea47e commit d92e1ed

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

libraries/ESP8266httpUpdate/keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ update KEYWORD2
2424
updateSpiffs KEYWORD2
2525
getLastError KEYWORD2
2626
getLastErrorString KEYWORD2
27+
setAuthorization KEYWORD2
2728

2829
#######################################
2930
# Constants (LITERAL1)
@@ -37,6 +38,7 @@ HTTP_UE_SERVER_WRONG_HTTP_CODE LITERAL1 RESERVED_WORD_2
3738
HTTP_UE_SERVER_FAULTY_MD5 LITERAL1 RESERVED_WORD_2
3839
HTTP_UE_BIN_VERIFY_HEADER_FAILED LITERAL1 RESERVED_WORD_2
3940
HTTP_UE_BIN_FOR_WRONG_FLASH LITERAL1 RESERVED_WORD_2
41+
HTTP_UE_SERVER_UNAUTHORIZED LITERAL1 RESERVED_WORD_2
4042
HTTP_UPDATE_FAILED LITERAL1 RESERVED_WORD_2
4143
HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2
4244
HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void)
4343
{
4444
}
4545

46+
/**
47+
* set the Authorization for the http request
48+
* @param user const String&
49+
* @param password const String&
50+
*/
51+
void ESP8266HTTPUpdate::setAuthorization(const String &user, const String &password)
52+
{
53+
_user = user;
54+
_password = password;
55+
}
56+
57+
/**
58+
* set the Authorization for the http request
59+
* @param auth const String& base64
60+
*/
61+
void ESP8266HTTPUpdate::setAuthorization(const String &auth)
62+
{
63+
_auth = auth;
64+
}
65+
4666
#if HTTPUPDATE_1_2_COMPATIBLE
4767
HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
4868
const String& httpsFingerprint, bool reboot)
@@ -241,6 +261,8 @@ String ESP8266HTTPUpdate::getLastErrorString(void)
241261
return F("Verify Bin Header Failed");
242262
case HTTP_UE_BIN_FOR_WRONG_FLASH:
243263
return F("New Binary Does Not Fit Flash Size");
264+
case HTTP_UE_SERVER_UNAUTHORIZED:
265+
return F("Unauthorized (401)");
244266
}
245267

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

307+
if (!_user.isEmpty() && !_password.isEmpty())
308+
{
309+
http.setAuthorization(_user.c_str(), _password.c_str());
310+
}
311+
312+
if (!_auth.isEmpty())
313+
{
314+
http.setAuthorization(_auth.c_str());
315+
}
316+
285317
const char * headerkeys[] = { "x-MD5" };
286318
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
287319

@@ -432,6 +464,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
432464
_setLastError(HTTP_UE_SERVER_FORBIDDEN);
433465
ret = HTTP_UPDATE_FAILED;
434466
break;
467+
case HTTP_CODE_UNAUTHORIZED:
468+
_setLastError(HTTP_UE_SERVER_UNAUTHORIZED);
469+
ret = HTTP_UPDATE_FAILED;
470+
break;
435471
default:
436472
_setLastError(HTTP_UE_SERVER_WRONG_HTTP_CODE);
437473
ret = HTTP_UPDATE_FAILED;

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ constexpr int HTTP_UE_SERVER_WRONG_HTTP_CODE = (-104);
5656
constexpr int HTTP_UE_SERVER_FAULTY_MD5 = (-105);
5757
constexpr int HTTP_UE_BIN_VERIFY_HEADER_FAILED = (-106);
5858
constexpr int HTTP_UE_BIN_FOR_WRONG_FLASH = (-107);
59+
constexpr int HTTP_UE_SERVER_UNAUTHORIZED = (-108);
5960

6061
enum HTTPUpdateResult {
6162
HTTP_UPDATE_FAILED,
@@ -111,6 +112,9 @@ class ESP8266HTTPUpdate
111112
_ledOn = ledOn;
112113
}
113114

115+
void setAuthorization(const String& user, const String& password);
116+
void setAuthorization(const String& auth);
117+
114118
#if HTTPUPDATE_1_2_COMPATIBLE
115119
// This function is deprecated, use rebootOnUpdate and the next one instead
116120
t_httpUpdate_return update(const String& url, const String& currentVersion,
@@ -174,6 +178,10 @@ class ESP8266HTTPUpdate
174178
int _lastError;
175179
bool _rebootOnUpdate = true;
176180
bool _closeConnectionsOnUpdate = true;
181+
String _user;
182+
String _password;
183+
String _auth;
184+
177185
private:
178186
int _httpClientTimeout;
179187
followRedirects_t _followRedirects;

0 commit comments

Comments
 (0)