• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jupp0r / prometheus-cpp / 7267948222

19 Dec 2023 09:49PM CUT coverage: 96.499%. Remained the same
7267948222

Pull #676

github

web-flow
Merge 8832e95d6 into 540a5a522
Pull Request #676: feat(push): allow timeout for HTTP requests

827 of 857 relevant lines covered (96.5%)

102248.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.58
/pull/src/basic_auth.cc
1
#include "basic_auth.h"
2

3
#include <utility>
4

5
#include "CivetServer.h"
6
#include "prometheus/detail/base64.h"
7

8
namespace prometheus {
9

10
BasicAuthHandler::BasicAuthHandler(AuthFunc callback, std::string realm)
2✔
11
    : callback_(std::move(callback)), realm_(std::move(realm)) {}
2✔
12

13
bool BasicAuthHandler::authorize(CivetServer* server, mg_connection* conn) {
2✔
14
  if (!AuthorizeInner(server, conn)) {
2✔
15
    WriteUnauthorizedResponse(conn);
×
16
    return false;
×
17
  }
18
  return true;
2✔
19
}
20

21
bool BasicAuthHandler::AuthorizeInner(CivetServer*, mg_connection* conn) {
2✔
22
  const char* authHeader = mg_get_header(conn, "Authorization");
2✔
23

24
  if (authHeader == nullptr) {
2✔
25
    // No auth header was provided.
26
    return false;
×
27
  }
28
  std::string authHeaderStr = authHeader;
4✔
29

30
  // Basic auth header is expected to be of the form:
31
  // "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
32

33
  const std::string prefix = "Basic ";
4✔
34
  if (authHeaderStr.compare(0, prefix.size(), prefix) != 0) {
2✔
35
    return false;
×
36
  }
37

38
  // Strip the "Basic " prefix leaving the base64 encoded auth string
39
  auto b64Auth = authHeaderStr.substr(prefix.size());
4✔
40

41
  std::string decoded;
4✔
42
  try {
43
    decoded = detail::base64_decode(b64Auth);
2✔
44
  } catch (...) {
×
45
    return false;
×
46
  }
47

48
  // decoded auth string is expected to be of the form:
49
  // "username:password"
50
  // colons may not appear in the username.
51
  auto splitPos = decoded.find(':');
2✔
52
  if (splitPos == std::string::npos) {
2✔
53
    return false;
×
54
  }
55

56
  auto username = decoded.substr(0, splitPos);
4✔
57
  auto password = decoded.substr(splitPos + 1);
4✔
58

59
  // TODO: bool does not permit a distinction between 401 Unauthorized
60
  //  and 403 Forbidden. Authentication may succeed, but the user still
61
  //  not be authorized to perform the request.
62
  return callback_(username, password);
2✔
63
}
64

65
void BasicAuthHandler::WriteUnauthorizedResponse(mg_connection* conn) {
×
66
  mg_printf(conn, "HTTP/1.1 401 Unauthorized\r\n");
×
67
  mg_printf(conn, "WWW-Authenticate: Basic realm=\"%s\"\r\n", realm_.c_str());
×
68
  mg_printf(conn, "Connection: close\r\n");
×
69
  mg_printf(conn, "Content-Length: 0\r\n");
×
70
  // end headers
71
  mg_printf(conn, "\r\n");
×
72
}
×
73

74
}  // namespace prometheus
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc