userver: userver/server/handlers/auth/auth_checker_factory.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
auth_checker_factory.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/auth/auth_checker_factory.hpp
4/// @brief Authorization factory registration and base classes.
5
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include <userver/components/component_context.hpp>
11#include <userver/server/handlers/auth/auth_checker_base.hpp>
12#include <userver/server/handlers/handler_config.hpp>
13#include <userver/utils/not_null.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace server::handlers::auth {
18
19/// Base class for all the authorization factory checkers.
21public:
22 virtual ~AuthCheckerFactoryBase() = default;
23
24 virtual AuthCheckerBasePtr MakeAuthChecker(const HandlerAuthConfig&) const = 0;
25};
26
27namespace impl {
28
29using AuthCheckerFactoryFactory = utils::UniqueRef<AuthCheckerFactoryBase> (*)(const components::ComponentContext&);
30
31void DoRegisterAuthCheckerFactory(std::string_view auth_type, AuthCheckerFactoryFactory factory);
32
34MakeAuthCheckerFactory(std::string_view auth_type, const components::ComponentContext& context);
35
36std::vector<std::string> GetAllAuthTypes();
37
38template <typename AuthCheckerFactory>
39utils::UniqueRef<AuthCheckerFactoryBase> MakeAuthCheckerFactory(const components::ComponentContext& context) {
40 return utils::MakeUniqueRef<AuthCheckerFactory>(context);
41}
42
43} // namespace impl
44
45/// @brief Function to call from main() to register an authorization checker.
46///
47/// @tparam AuthCheckerFactory must:
48/// 1) inherit from @ref server::handlers::auth::AuthCheckerFactoryBase;
49/// 2) have a constructor from `const components::ComponentContext&`;
50/// 3) have `static constexpr std::string_view kAuthType = "..."` member.
51template <typename AuthCheckerFactory>
53 const auto auth_type = std::string_view{AuthCheckerFactory::kAuthType};
54 impl::DoRegisterAuthCheckerFactory(auth_type, &impl::MakeAuthCheckerFactory<AuthCheckerFactory>);
55}
56
57} // namespace server::handlers::auth
58
59USERVER_NAMESPACE_END