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 { kTokenNotFound, kInternalCheckFailure, kInvalidToken, kForbidden, kOk };
18
19 Status status{Status::kOk};
20 std::optional<std::string> reason{};
21 std::optional<std::string> ext_reason{};
22 std::optional<HandlerErrorCode> code{};
23};
24
25const std::string& GetDefaultReasonForStatus(AuthCheckResult::Status status);
26void RaiseForStatus(const AuthCheckResult& auth_check);
27
28class AuthCheckerBase {
29public:
30 virtual ~AuthCheckerBase();
31
32 [[nodiscard]] virtual AuthCheckResult CheckAuth(const http::HttpRequest& request, request::RequestContext& context)
33 const = 0;
34
35 [[nodiscard]] virtual bool SupportsUserAuth() const noexcept = 0;
36
37protected:
38 void SetUserAuthInfo(server::request::RequestContext& request_context, server::auth::UserAuthInfo&& info) const;
39};
40
41using AuthCheckerBasePtr = std::shared_ptr<AuthCheckerBase>;
42
43} // namespace server::handlers::auth
44
45USERVER_NAMESPACE_END