]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | #pragma once |
2 | ||
3 | #include <functional> | |
4 | #include <string> | |
5 | ||
6 | #include "CivetServer.h" | |
7 | #include "civetweb.h" | |
8 | ||
9 | namespace prometheus { | |
10 | ||
11 | /** | |
12 | * Handler for HTTP Basic authentication for Endpoints. | |
13 | */ | |
14 | class BasicAuthHandler : public CivetAuthHandler { | |
15 | public: | |
16 | using AuthFunc = std::function<bool(const std::string&, const std::string&)>; | |
17 | explicit BasicAuthHandler(AuthFunc callback, std::string realm); | |
18 | ||
19 | /** | |
20 | * Implements civetweb authorization interface. | |
21 | * | |
22 | * Attempts to extract a username and password from the Authorization header | |
23 | * to pass to the owning AuthHandler, `this->handler`. | |
24 | * If handler returns true, permits the request to proceed. | |
25 | * If handler returns false, or the Auth header is absent, | |
26 | * rejects the request with 401 Unauthorized. | |
27 | */ | |
28 | bool authorize(CivetServer* server, mg_connection* conn) override; | |
29 | ||
30 | private: | |
31 | bool AuthorizeInner(CivetServer* server, mg_connection* conn); | |
32 | void WriteUnauthorizedResponse(mg_connection* conn); | |
33 | ||
34 | AuthFunc callback_; | |
35 | std::string realm_; | |
36 | }; | |
37 | ||
38 | } // namespace prometheus |