29 char auth_header[1024];
30 if (httpd_req_get_hdr_value_str(req,
"Authorization", auth_header, 1024) !=
35 String auth_header_str(auth_header);
36 int space_pos = auth_header_str.indexOf(
' ');
37 if (space_pos == -1) {
41 String auth_type = auth_header_str.substring(0, space_pos);
42 if (auth_type !=
"Digest") {
46 String auth_str = auth_header_str.substring(space_pos + 1);
51 if (userhash ==
"true") {
53 if (username != ref_username) {
65 String algorithm =
extract_param(
"algorithm", auth_str,
false);
73 if (realm ==
"" || nonce ==
"" || uri ==
"" || response ==
"" ||
74 opaque ==
"" || nc ==
"" || cnonce ==
"") {
79 int count = strtol(nc.c_str(), NULL, 16);
83 if (nonce_status == 0) {
85 }
else if (nonce_status == -1) {
94 int method = req->method;
107 method_str =
"DELETE";
114 String a2 = method_str +
":" + uri;
115 String expected_response =
MD5(
ha1_ +
":" + nonce +
":" + nc +
":" +
116 cnonce +
":" + qop +
":" +
MD5(a2));
118 return response == expected_response ? 1 : 0;
147 String timestamp_str = String(millis());
148 String to_hash = timestamp_str +
":" +
secret_;
149 String hash =
MD5(to_hash);
150 String nonce = timestamp_str +
" " + hash;
154 size_t output_length;
155 mbedtls_base64_encode((
unsigned char*)encoded,
sizeof(encoded),
156 &output_length, (uint8_t*)nonce.c_str(),
158 encoded[output_length] =
'\0';
159 String nonce_str(encoded);
161 nonces_.push_front({nonce, 0});
162 constexpr size_t kMaxNonces = 50;
163 while (
nonces_.size() > kMaxNonces) {
170 std::list<NonceData>::iterator it;
171 String decoded_nonce;
173 size_t output_length;
175 mbedtls_base64_decode((
unsigned char*)decoded,
sizeof(decoded),
176 &output_length, (uint8_t*)nonce.c_str(),
178 decoded[output_length] =
'\0';
179 decoded_nonce = String(decoded);
181 if (it->nonce == decoded_nonce) {
185 constexpr int kNcTolerance = 5;
186 if (count <= it->count && (it->count - count) >= kNcTolerance) {
189 if (count > it->count) {
193 String timestamp_str = decoded_nonce.substring(0, nonce.indexOf(
' '));
194 unsigned long timestamp = timestamp_str.toInt();
202 String timestamp_str = it->nonce.substring(0, it->nonce.indexOf(
' '));
203 unsigned long timestamp = timestamp_str.toInt();
213 String quote = quoted ?
"\"" :
"";
214 int start = auth_str.indexOf(param +
"=" + quote);
218 start += param.length() + 1 + (quoted ? 1 : 0);
219 int end = auth_str.indexOf(quoted ? quote :
",", start);
221 end = auth_str.length();
223 return auth_str.substring(start, end);