From f48a5e65d74ad0f277e96bb8a39cfc1277584d11 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Sun, 7 Jan 2018 20:00:56 +0330 Subject: [PATCH 01/14] Adds digest authentication example. --- .../DigestAuthorization.ino | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino new file mode 100644 index 0000000000..7147738fd3 --- /dev/null +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -0,0 +1,117 @@ +#include +#include + +#include + +#define USE_SERIAL Serial + +ESP8266WiFiMulti WiFiMulti; + +String _exractParam(String& authReq, const String& param, const char delimit){ + int _begin = authReq.indexOf(param); + if (_begin==-1) return ""; + return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); +} + +void setup() { + + USE_SERIAL.begin(9600); + // USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("Niligo-Prism-yyr9uma", ""); + +} + +void loop() { + // wait for WiFi connection + if((WiFiMulti.run() == WL_CONNECTED)) { + + HTTPClient http; + + USE_SERIAL.print("[HTTP] begin...\n"); + // configure traged server and url + + + // http.begin("http://admin:admin@192.168.100.1/api/state?power=1"); + http.begin("http://192.168.100.1/api/state?power=0"); + /* + // or + // or + http.begin("http://192.168.1.12/test.html"); + http.setAuthorization("dXNlcjpwYXN3b3Jk"); + */ + + + const char *keys[] = {"WWW-Authenticate", "Server", "Connection"}; + http.collectHeaders(keys, 3); + + USE_SERIAL.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); + + if (httpCode > 0) { + String authReq = http.header("WWW-Authenticate"); + USE_SERIAL.println(authReq); + + // file found at server + String payload = http.getString(); + USE_SERIAL.println(payload); + + String username = "admin"; + String password = "admin"; + + // extracting required parameters for RFC 2069 simpler Digest + // String _realm = _exractParam(authReq, "realm=\"", '"'); + String _realm = "Protected"; + String _nonce = _exractParam(authReq, "nonce=\"", '"'); + // String _nonce = "2cadff7e2f160969"; + String _opaque = _exractParam(authReq, "opaque=\"", '"'); + + // parameters for the RFC 2617 newer Digest + MD5Builder md5; + md5.begin(); + md5.add(username + ":" + _realm + ":" + password); // md5 of the user:realm:user + md5.calculate(); + String _H1 = md5.toString(); + + md5.begin(); + md5.add(String("GET:") + String("/api/state?power=0")); + md5.calculate(); + String _H2 = md5.toString(); + + md5.begin(); + md5.add(_H1 + ":" + _nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + _H2); + md5.calculate(); + String _response = md5.toString(); + + http.begin("http://192.168.100.1/api/state?power=0"); + USE_SERIAL.println(_nonce); + String Author = "Digest username=\"admin\", realm=\"Protected\", nonce=\"" + _nonce + "\", uri=\"/api/state?power=0\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + _response + "\"\r\n"; + USE_SERIAL.println(Author); + http.addHeader("Authorization", Author); + + http.GET(); + + payload = http.getString(); + USE_SERIAL.println(payload); + } else { + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); + } + + delay(10000); +} + From 4b69085aab3c2e89fea20fd1ea136c678fb58ca6 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 10:24:21 +0330 Subject: [PATCH 02/14] Reviews digest authentication example. --- .../DigestAuthorization.ino | 150 ++++++++---------- 1 file changed, 67 insertions(+), 83 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 7147738fd3..96bce6bf63 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -1,117 +1,101 @@ +/* + + This example is released into public domain, + or, at your option, CC0 licensed. + */ + #include -#include #include -#define USE_SERIAL Serial +const char* ssid = "........"; +const char* password = "........"; -ESP8266WiFiMulti WiFiMulti; +const char *username = "admin"; +const char *password = "admin"; -String _exractParam(String& authReq, const String& param, const char delimit){ +const char *server = "http://192.168.100.1"; +const char *uri = "/api/state?power=1"; + +String exractParam(String& authReq, const String& param, const char delimit){ int _begin = authReq.indexOf(param); if (_begin==-1) return ""; return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); } void setup() { + Serial.begin(9600); - USE_SERIAL.begin(9600); - // USE_SERIAL.setDebugOutput(true); - - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); - for(uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); } - WiFi.mode(WIFI_STA); - WiFiMulti.addAP("Niligo-Prism-yyr9uma", ""); - + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); } void loop() { - // wait for WiFi connection - if((WiFiMulti.run() == WL_CONNECTED)) { + HTTPClient http; - HTTPClient http; + Serial.print("[HTTP] begin...\n"); - USE_SERIAL.print("[HTTP] begin...\n"); - // configure traged server and url + // configure traged server and url + http.begin(String(server) + String(uri)); - // http.begin("http://admin:admin@192.168.100.1/api/state?power=1"); - http.begin("http://192.168.100.1/api/state?power=0"); - /* - // or - // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("dXNlcjpwYXN3b3Jk"); - */ + const char *keys[] = {"WWW-Authenticate"}; + http.collectHeaders(keys, 1); + Serial.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); - const char *keys[] = {"WWW-Authenticate", "Server", "Connection"}; - http.collectHeaders(keys, 3); + if (httpCode > 0) { + String authReq = http.header("WWW-Authenticate"); + Serial.println(authReq); - USE_SERIAL.print("[HTTP] GET...\n"); - // start connection and send HTTP header - int httpCode = http.GET(); + // extracting required parameters for RFC 2069 simpler Digest + String _realm = _exractParam(authReq, "realm=\"", '"'); + String _nonce = _exractParam(authReq, "nonce=\"", '"'); + String _opaque = _exractParam(authReq, "opaque=\"", '"'); - if (httpCode > 0) { - String authReq = http.header("WWW-Authenticate"); - USE_SERIAL.println(authReq); - - // file found at server - String payload = http.getString(); - USE_SERIAL.println(payload); - - String username = "admin"; - String password = "admin"; - - // extracting required parameters for RFC 2069 simpler Digest - // String _realm = _exractParam(authReq, "realm=\"", '"'); - String _realm = "Protected"; - String _nonce = _exractParam(authReq, "nonce=\"", '"'); - // String _nonce = "2cadff7e2f160969"; - String _opaque = _exractParam(authReq, "opaque=\"", '"'); - - // parameters for the RFC 2617 newer Digest - MD5Builder md5; - md5.begin(); - md5.add(username + ":" + _realm + ":" + password); // md5 of the user:realm:user - md5.calculate(); - String _H1 = md5.toString(); - - md5.begin(); - md5.add(String("GET:") + String("/api/state?power=0")); - md5.calculate(); - String _H2 = md5.toString(); - - md5.begin(); - md5.add(_H1 + ":" + _nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + _H2); - md5.calculate(); - String _response = md5.toString(); - - http.begin("http://192.168.100.1/api/state?power=0"); - USE_SERIAL.println(_nonce); - String Author = "Digest username=\"admin\", realm=\"Protected\", nonce=\"" + _nonce + "\", uri=\"/api/state?power=0\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + _response + "\"\r\n"; - USE_SERIAL.println(Author); - http.addHeader("Authorization", Author); - - http.GET(); + // parameters for the RFC 2617 newer Digest + MD5Builder md5; + md5.begin(); + md5.add(String(username) + ":" + _realm + ":" + String(password)); // md5 of the user:realm:user + md5.calculate(); + String _H1 = md5.toString(); + + md5.begin(); + md5.add(String("GET:") + String(uri)); + md5.calculate(); + String _H2 = md5.toString(); + + md5.begin(); + md5.add(_H1 + ":" + _nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + _H2); + md5.calculate(); + String _response = md5.toString(); + + http.begin(String(server) + String(uri)); + String authorization = "Digest username=\"admin\", realm=\"" + _realm + "\", nonce=\"" + _nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + _response + "\""; + Serial.println(authorization); + http.addHeader("Authorization", authorization); + int httpCode = http.GET(); + if (httpCode > 0) { payload = http.getString(); - USE_SERIAL.println(payload); - } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.println(payload); } - - http.end(); + } else { + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } + http.end(); delay(10000); } - From 8a35dcea9f870ea3cc6e4cdd390761dda78b1f89 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 10:34:07 +0330 Subject: [PATCH 03/14] Corrects compilation errors. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 96bce6bf63..b897d475b8 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -82,7 +82,9 @@ void loop() { md5.calculate(); String _response = md5.toString(); + http.end(); http.begin(String(server) + String(uri)); + String authorization = "Digest username=\"admin\", realm=\"" + _realm + "\", nonce=\"" + _nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + _response + "\""; Serial.println(authorization); http.addHeader("Authorization", authorization); @@ -91,9 +93,11 @@ void loop() { if (httpCode > 0) { payload = http.getString(); Serial.println(payload); + } else { + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); From f00654dc3aed0c33969be418f005fd0f793ffcab Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 10:57:06 +0330 Subject: [PATCH 04/14] Corrects compilation errors. --- .../DigestAuthorization/DigestAuthorization.ino | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index b897d475b8..01aae0cb1f 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -9,7 +9,7 @@ #include const char* ssid = "........"; -const char* password = "........"; +const char* ssidPassword = "........"; const char *username = "admin"; const char *password = "admin"; @@ -27,7 +27,7 @@ void setup() { Serial.begin(9600); WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); + WiFi.begin(ssid, ssidPassword); while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -61,9 +61,9 @@ void loop() { Serial.println(authReq); // extracting required parameters for RFC 2069 simpler Digest - String _realm = _exractParam(authReq, "realm=\"", '"'); - String _nonce = _exractParam(authReq, "nonce=\"", '"'); - String _opaque = _exractParam(authReq, "opaque=\"", '"'); + String _realm = exractParam(authReq, "realm=\"", '"'); + String _nonce = exractParam(authReq, "nonce=\"", '"'); + String _opaque = exractParam(authReq, "opaque=\"", '"'); // parameters for the RFC 2617 newer Digest MD5Builder md5; @@ -91,7 +91,7 @@ void loop() { int httpCode = http.GET(); if (httpCode > 0) { - payload = http.getString(); + String payload = http.getString(); Serial.println(payload); } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); From f6ae2b61ca1ea942b40e0e49d8021d46280a4086 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 11:08:18 +0330 Subject: [PATCH 05/14] Uses httpbin service. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 01aae0cb1f..453d6ccbce 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -14,8 +14,8 @@ const char* ssidPassword = "........"; const char *username = "admin"; const char *password = "admin"; -const char *server = "http://192.168.100.1"; -const char *uri = "/api/state?power=1"; +const char *server = "http://httpbin.org"; +const char *uri = "/digest-auth/auth/admin/admin/MD5"; String exractParam(String& authReq, const String& param, const char delimit){ int _begin = authReq.indexOf(param); From 13ea6e1338e0b3e29ed9d569c372b84727b7209a Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 19:18:15 +0330 Subject: [PATCH 06/14] Corrects variable names in digest authorization. --- .../DigestAuthorization/DigestAuthorization.ino | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 453d6ccbce..74dbd091d8 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -61,31 +61,30 @@ void loop() { Serial.println(authReq); // extracting required parameters for RFC 2069 simpler Digest - String _realm = exractParam(authReq, "realm=\"", '"'); - String _nonce = exractParam(authReq, "nonce=\"", '"'); - String _opaque = exractParam(authReq, "opaque=\"", '"'); + String realm = exractParam(authReq, "realm=\"", '"'); + String nonce = exractParam(authReq, "nonce=\"", '"'); // parameters for the RFC 2617 newer Digest MD5Builder md5; md5.begin(); - md5.add(String(username) + ":" + _realm + ":" + String(password)); // md5 of the user:realm:user + md5.add(String(username) + ":" + realm + ":" + String(password)); // md5 of the user:realm:user md5.calculate(); - String _H1 = md5.toString(); + String h1 = md5.toString(); md5.begin(); md5.add(String("GET:") + String(uri)); md5.calculate(); - String _H2 = md5.toString(); + String h2 = md5.toString(); md5.begin(); - md5.add(_H1 + ":" + _nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + _H2); + md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); md5.calculate(); - String _response = md5.toString(); + String response = md5.toString(); http.end(); http.begin(String(server) + String(uri)); - String authorization = "Digest username=\"admin\", realm=\"" + _realm + "\", nonce=\"" + _nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + _response + "\""; + String authorization = "Digest username=\"admin\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; Serial.println(authorization); http.addHeader("Authorization", authorization); From 2d0b6eb6f1f1230b39a36ff5060ece47dd4e964a Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 19:39:39 +0330 Subject: [PATCH 07/14] Create getDigestAuth function. --- .../DigestAuthorization.ino | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 74dbd091d8..4cfe026974 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -23,6 +23,34 @@ String exractParam(String& authReq, const String& param, const char delimit){ return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); } +String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri) { + // extracting required parameters for RFC 2069 simpler Digest + String realm = exractParam(authReq, "realm=\"", '"'); + String nonce = exractParam(authReq, "nonce=\"", '"'); + + // parameters for the RFC 2617 newer Digest + MD5Builder md5; + md5.begin(); + md5.add(username + ":" + realm + ":" + password); // md5 of the user:realm:user + md5.calculate(); + String h1 = md5.toString(); + + md5.begin(); + md5.add(String("GET:") + uri); + md5.calculate(); + String h2 = md5.toString(); + + md5.begin(); + md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); + md5.calculate(); + String response = md5.toString(); + + String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; + Serial.println(authorization); + + return authorization; +} + void setup() { Serial.begin(9600); @@ -60,32 +88,11 @@ void loop() { String authReq = http.header("WWW-Authenticate"); Serial.println(authReq); - // extracting required parameters for RFC 2069 simpler Digest - String realm = exractParam(authReq, "realm=\"", '"'); - String nonce = exractParam(authReq, "nonce=\"", '"'); - - // parameters for the RFC 2617 newer Digest - MD5Builder md5; - md5.begin(); - md5.add(String(username) + ":" + realm + ":" + String(password)); // md5 of the user:realm:user - md5.calculate(); - String h1 = md5.toString(); - - md5.begin(); - md5.add(String("GET:") + String(uri)); - md5.calculate(); - String h2 = md5.toString(); - - md5.begin(); - md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); - md5.calculate(); - String response = md5.toString(); + String authorization = getDigestAuth(authReq, String(username), String(password), String(uri)); http.end(); http.begin(String(server) + String(uri)); - String authorization = "Digest username=\"admin\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; - Serial.println(authorization); http.addHeader("Authorization", authorization); int httpCode = http.GET(); From b1549853bce8593fb8a15836cbcf50f90f594428 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 20:00:31 +0330 Subject: [PATCH 08/14] Uses 2 space instead of tab. --- .../DigestAuthorization.ino | 135 +++++++++--------- 1 file changed, 67 insertions(+), 68 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 4cfe026974..1afdd1f8cd 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -1,5 +1,4 @@ /* - This example is released into public domain, or, at your option, CC0 licensed. */ @@ -18,94 +17,94 @@ const char *server = "http://httpbin.org"; const char *uri = "/digest-auth/auth/admin/admin/MD5"; String exractParam(String& authReq, const String& param, const char delimit){ - int _begin = authReq.indexOf(param); - if (_begin==-1) return ""; - return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); + int _begin = authReq.indexOf(param); + if (_begin==-1) return ""; + return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); } String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri) { - // extracting required parameters for RFC 2069 simpler Digest - String realm = exractParam(authReq, "realm=\"", '"'); - String nonce = exractParam(authReq, "nonce=\"", '"'); - - // parameters for the RFC 2617 newer Digest - MD5Builder md5; - md5.begin(); - md5.add(username + ":" + realm + ":" + password); // md5 of the user:realm:user - md5.calculate(); - String h1 = md5.toString(); - - md5.begin(); - md5.add(String("GET:") + uri); - md5.calculate(); - String h2 = md5.toString(); - - md5.begin(); - md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); - md5.calculate(); - String response = md5.toString(); - - String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; - Serial.println(authorization); - - return authorization; + // extracting required parameters for RFC 2069 simpler Digest + String realm = exractParam(authReq, "realm=\"", '"'); + String nonce = exractParam(authReq, "nonce=\"", '"'); + + // parameters for the RFC 2617 newer Digest + MD5Builder md5; + md5.begin(); + md5.add(username + ":" + realm + ":" + password); // md5 of the user:realm:user + md5.calculate(); + String h1 = md5.toString(); + + md5.begin(); + md5.add(String("GET:") + uri); + md5.calculate(); + String h2 = md5.toString(); + + md5.begin(); + md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); + md5.calculate(); + String response = md5.toString(); + + String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; + Serial.println(authorization); + + return authorization; } void setup() { - Serial.begin(9600); + Serial.begin(9600); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, ssidPassword); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, ssidPassword); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); } void loop() { - HTTPClient http; + HTTPClient http; - Serial.print("[HTTP] begin...\n"); + Serial.print("[HTTP] begin...\n"); - // configure traged server and url - http.begin(String(server) + String(uri)); + // configure traged server and url + http.begin(String(server) + String(uri)); - const char *keys[] = {"WWW-Authenticate"}; - http.collectHeaders(keys, 1); + const char *keys[] = {"WWW-Authenticate"}; + http.collectHeaders(keys, 1); - Serial.print("[HTTP] GET...\n"); - // start connection and send HTTP header - int httpCode = http.GET(); + Serial.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); - if (httpCode > 0) { - String authReq = http.header("WWW-Authenticate"); - Serial.println(authReq); + if (httpCode > 0) { + String authReq = http.header("WWW-Authenticate"); + Serial.println(authReq); - String authorization = getDigestAuth(authReq, String(username), String(password), String(uri)); + String authorization = getDigestAuth(authReq, String(username), String(password), String(uri)); - http.end(); - http.begin(String(server) + String(uri)); + http.end(); + http.begin(String(server) + String(uri)); - http.addHeader("Authorization", authorization); + http.addHeader("Authorization", authorization); - int httpCode = http.GET(); - if (httpCode > 0) { - String payload = http.getString(); - Serial.println(payload); - } else { - Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); - } - } else { - Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); - } + int httpCode = http.GET(); + if (httpCode > 0) { + String payload = http.getString(); + Serial.println(payload); + } else { + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + } else { + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } - http.end(); - delay(10000); + http.end(); + delay(10000); } From 34a3e86bd5dd95f0cd03902a10ef430beb9cfdf8 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 20:01:57 +0330 Subject: [PATCH 09/14] Breaks long lines. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 1afdd1f8cd..214e183b09 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -44,7 +44,8 @@ String getDigestAuth(String& authReq, const String& username, const String& pass md5.calculate(); String response = md5.toString(); - String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; + String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; Serial.println(authorization); return authorization; From f6e8cf775806828cb40cbbdfcc1f89ac107f59ab Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 20:06:57 +0330 Subject: [PATCH 10/14] Adds cNonce generator. --- .../DigestAuthorization.ino | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 214e183b09..72a8316b5c 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -22,10 +22,25 @@ String exractParam(String& authReq, const String& param, const char delimit){ return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); } +String getCNonce(const int len) { + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + String s = ""; + + for (int i = 0; i < len; ++i) { + s += alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + return s; +} + String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri) { // extracting required parameters for RFC 2069 simpler Digest String realm = exractParam(authReq, "realm=\"", '"'); String nonce = exractParam(authReq, "nonce=\"", '"'); + String cNonce = getCNonce(8); // parameters for the RFC 2617 newer Digest MD5Builder md5; @@ -40,12 +55,12 @@ String getDigestAuth(String& authReq, const String& username, const String& pass String h2 = md5.toString(); md5.begin(); - md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + "gDBuFY4s" + ":" + "auth" + ":" + h2); + md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + cNonce + ":" + "auth" + ":" + h2); md5.calculate(); String response = md5.toString(); String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + - "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"gDBuFY4s\", response=\"" + response + "\""; + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"" + cNonce + "\", response=\"" + response + "\""; Serial.println(authorization); return authorization; From 1d0eadffa86492373bb190ca9fb728b113ea895e Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Mon, 8 Jan 2018 23:57:43 +0330 Subject: [PATCH 11/14] Adds some description into header. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 72a8316b5c..6221aa7b37 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -1,4 +1,8 @@ /* + This sketch shows how to handle HTTP Digest Authorization. + + Written by Parham Alvani and Sajjad Rahnama, 2018-01-07. + This example is released into public domain, or, at your option, CC0 licensed. */ From 857f9920c0dcb31a961c84ab22492613668be74f Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Tue, 9 Jan 2018 10:56:27 +0330 Subject: [PATCH 12/14] Adds counter into getDigestAuth. --- .../DigestAuthorization/DigestAuthorization.ino | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 6221aa7b37..0409db8e66 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -40,12 +40,15 @@ String getCNonce(const int len) { return s; } -String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri) { +String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri, int counter) { // extracting required parameters for RFC 2069 simpler Digest String realm = exractParam(authReq, "realm=\"", '"'); String nonce = exractParam(authReq, "nonce=\"", '"'); String cNonce = getCNonce(8); + char nc[8]; + sprintf(nc, "%08d", counter); + // parameters for the RFC 2617 newer Digest MD5Builder md5; md5.begin(); @@ -59,12 +62,12 @@ String getDigestAuth(String& authReq, const String& username, const String& pass String h2 = md5.toString(); md5.begin(); - md5.add(h1 + ":" + nonce + ":" + "00000001" + ":" + cNonce + ":" + "auth" + ":" + h2); + md5.add(h1 + ":" + nonce + ":" + String(nc) + ":" + cNonce + ":" + "auth" + ":" + h2); md5.calculate(); String response = md5.toString(); String authorization = "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + - "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=00000001, cnonce=\"" + cNonce + "\", response=\"" + response + "\""; + "\", uri=\"" + uri + "\", algorithm=\"MD5\", qop=auth, nc=" + String(nc) + ", cnonce=\"" + cNonce + "\", response=\"" + response + "\""; Serial.println(authorization); return authorization; @@ -107,7 +110,7 @@ void loop() { String authReq = http.header("WWW-Authenticate"); Serial.println(authReq); - String authorization = getDigestAuth(authReq, String(username), String(password), String(uri)); + String authorization = getDigestAuth(authReq, String(username), String(password), String(uri), 1); http.end(); http.begin(String(server) + String(uri)); From ec398bf6e8998258d1728ce475f92c99931fb3b6 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Wed, 10 Jan 2018 07:49:07 +0330 Subject: [PATCH 13/14] Using hex notation for nonce counter. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 0409db8e66..b07dd2e11f 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -47,7 +47,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass String cNonce = getCNonce(8); char nc[8]; - sprintf(nc, "%08d", counter); + sprintf(nc, "%08x", counter); // parameters for the RFC 2617 newer Digest MD5Builder md5; From 40904f44b90b60550ff8f494c7665eb5b4f406c5 Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Wed, 10 Jan 2018 10:35:47 +0330 Subject: [PATCH 14/14] Corrects sprintf issues. --- .../examples/DigestAuthorization/DigestAuthorization.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index b07dd2e11f..338a206c48 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -40,14 +40,14 @@ String getCNonce(const int len) { return s; } -String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri, int counter) { +String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri, unsigned int counter) { // extracting required parameters for RFC 2069 simpler Digest String realm = exractParam(authReq, "realm=\"", '"'); String nonce = exractParam(authReq, "nonce=\"", '"'); String cNonce = getCNonce(8); - char nc[8]; - sprintf(nc, "%08x", counter); + char nc[9]; + snprintf(nc, sizeof(nc), "%08x", counter); // parameters for the RFC 2617 newer Digest MD5Builder md5;