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

jupp0r / prometheus-cpp / 11650949085

03 Nov 2024 11:28AM CUT coverage: 98.248%. Remained the same
11650949085

Pull #724

github

gjasny
ci: switch to supported macos-13
Pull Request #724: ci: switch to supported macos-13

841 of 856 relevant lines covered (98.25%)

102379.54 hits per line

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

96.97
/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)
12✔
11
    : callback_(std::move(callback)), realm_(std::move(realm)) {}
12✔
12

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

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

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

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

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

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

41
  std::string decoded;
20✔
42
  try {
43
    decoded = detail::base64_decode(b64Auth);
10✔
44
  } catch (...) {
2✔
45
    return false;
2✔
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(':');
8✔
52
  if (splitPos == std::string::npos) {
8✔
53
    return false;
2✔
54
  }
55

56
  auto username = decoded.substr(0, splitPos);
12✔
57
  auto password = decoded.substr(splitPos + 1);
12✔
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);
6✔
63
}
64

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