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