basic_auth.h 1.0 KB

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