userver: userver/server/handlers/auth/auth_checker_base.hpp Source File
Loading...
Searching...
No Matches
auth_checker_base.hpp
1#pragma once
2
3#include <memory>
4#include <optional>
5#include <string>
6
7#include <userver/server/auth/user_auth_info.hpp>
8#include <userver/server/handlers/exceptions.hpp>
9#include <userver/server/http/http_request.hpp>
10#include <userver/server/request/request_context.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::handlers::auth {
15
16struct AuthCheckResult {
17 enum class Status {
18 kTokenNotFound,
19 kInternalCheckFailure,
20 kInvalidToken,
21 kForbidden,
22 kOk
23 };
24
25 Status status{Status::kOk};
26 std::optional<std::string> reason{};
27 std::optional<std::string> ext_reason{};
28 std::optional<HandlerErrorCode> code{};
29};
30
31const std::string& GetDefaultReasonForStatus(AuthCheckResult::Status status);
32void RaiseForStatus(const AuthCheckResult& auth_check);
33
34class AuthCheckerBase {
35 public:
36 virtual ~AuthCheckerBase();
37
38 [[nodiscard]] virtual AuthCheckResult CheckAuth(
39 const http::HttpRequest& request,
40 request::RequestContext& context) const = 0;
41
42 [[nodiscard]] virtual bool SupportsUserAuth() const noexcept = 0;
43
44 protected:
45 void SetUserAuthInfo(server::request::RequestContext& request_context,
46 server::auth::UserAuthInfo&& info) const;
47};
48
49using AuthCheckerBasePtr = std::shared_ptr<AuthCheckerBase>;
50
51} // namespace server::handlers::auth
52
53USERVER_NAMESPACE_END