-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Adds digest authentication example. #4112
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f48a5e6
Adds digest authentication example.
1995parham 4b69085
Reviews digest authentication example.
1995parham 8a35dce
Corrects compilation errors.
1995parham f00654d
Corrects compilation errors.
1995parham f6ae2b6
Uses httpbin service.
1995parham 13ea6e1
Corrects variable names in digest authorization.
1995parham 2d0b6eb
Create getDigestAuth function.
1995parham b154985
Uses 2 space instead of tab.
1995parham 34a3e86
Breaks long lines.
1995parham f6e8cf7
Adds cNonce generator.
1995parham 1d0eadf
Adds some description into header.
1995parham 857f992
Adds counter into getDigestAuth.
1995parham ec398bf
Using hex notation for nonce counter.
1995parham 40904f4
Corrects sprintf issues.
1995parham cffefde
Merge branch 'master' into master
igrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
#include <ESP8266WiFi.h> | ||
|
||
#include <ESP8266HTTPClient.h> | ||
|
||
const char* ssid = "........"; | ||
const char* ssidPassword = "........"; | ||
|
||
const char *username = "admin"; | ||
const char *password = "admin"; | ||
|
||
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())); | ||
} | ||
|
||
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, 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); | ||
|
||
// 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 + ":" + 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=" + String(nc) + ", cnonce=\"" + cNonce + "\", response=\"" + response + "\""; | ||
Serial.println(authorization); | ||
|
||
return authorization; | ||
} | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
|
||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(ssid, ssidPassword); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
|
||
Serial.println(""); | ||
Serial.println("WiFi connected"); | ||
Serial.println("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
} | ||
|
||
void loop() { | ||
HTTPClient http; | ||
|
||
Serial.print("[HTTP] begin...\n"); | ||
|
||
// configure traged server and url | ||
http.begin(String(server) + String(uri)); | ||
|
||
|
||
const char *keys[] = {"WWW-Authenticate"}; | ||
http.collectHeaders(keys, 1); | ||
|
||
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); | ||
|
||
String authorization = getDigestAuth(authReq, String(username), String(password), String(uri), 1); | ||
|
||
http.end(); | ||
http.begin(String(server) + String(uri)); | ||
|
||
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()); | ||
} | ||
|
||
http.end(); | ||
delay(10000); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, nc is still an 8 byte buffer, so the zero terminator will be written out of array bounds. Could you please change the array size to 9, and use
snprintf(nc, sizeof(nc), "%08x", counter);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sorry for these mistakes.