blob: 7a45e937f7d502742bd450a920c4f84d0d463e45 [file] [log] [blame]
tylerliu01d63ca2020-10-06 16:29:23 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang74c61142020-10-07 21:00:49 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
tylerliu01d63ca2020-10-06 16:29:23 -07004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#ifndef NDNCERT_ASSIGNMENT_FUNCS_HPP
22#define NDNCERT_ASSIGNMENT_FUNCS_HPP
23
Zhiyi Zhang34f03f02020-10-29 18:34:42 -070024#include "detail/ca-request-state.hpp"
tylerliu01d63ca2020-10-06 16:29:23 -070025
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070029class NameAssignmentFunc : noncopyable
30{
tylerliu01d63ca2020-10-06 16:29:23 -070031public:
Zhiyi Zhang74c61142020-10-07 21:00:49 -070032 explicit NameAssignmentFunc(const std::string& factoryType, const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070033
Zhiyi Zhang74c61142020-10-07 21:00:49 -070034 virtual ~NameAssignmentFunc() = default;
tylerliu01d63ca2020-10-06 16:29:23 -070035
Zhiyi Zhang74c61142020-10-07 21:00:49 -070036 /**
37 * @brief The name assignment function provided by the CA operator to generate available
38 * namecomponents.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070039 *
Zhiyi Zhang74c61142020-10-07 21:00:49 -070040 * The function does not guarantee that all the returned names are available. Therefore the
41 * CA should further check the availability of each returned name and remove unavailable results.
42 *
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070043 * @param vector A list of parameter key-value pair used for name assignment.
Zhiyi Zhang74c61142020-10-07 21:00:49 -070044 * @return a vector containing the possible namespaces derived from the parameters.
45 */
46 virtual std::vector<PartialName>
47 assignName(const std::vector<std::tuple<std::string, std::string>>& params) = 0;
48
49 const std::string FACTORY_TYPE;
50 std::vector<std::string> m_nameFormat;
51
52public:
tylerliu01d63ca2020-10-06 16:29:23 -070053 template <class ChallengeType>
54 static void
Zhiyi Zhang74c61142020-10-07 21:00:49 -070055 registerNameAssignmentFunc(const std::string& typeName)
tylerliu01d63ca2020-10-06 16:29:23 -070056 {
57 FuncFactoryFactory& factory = getFactory();
58 BOOST_ASSERT(factory.count(typeName) == 0);
Zhiyi Zhang32437282020-10-10 16:15:37 -070059 factory[typeName] = [](const std::string& format) { return std::make_unique<ChallengeType>(format); };
tylerliu01d63ca2020-10-06 16:29:23 -070060 }
61
Zhiyi Zhang74c61142020-10-07 21:00:49 -070062 static unique_ptr<NameAssignmentFunc>
63 createNameAssignmentFunc(const std::string& challengeType, const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070064
65private:
Zhiyi Zhang74c61142020-10-07 21:00:49 -070066 typedef function<unique_ptr<NameAssignmentFunc>(const std::string&)> FactoryCreateFunc;
tylerliu01d63ca2020-10-06 16:29:23 -070067 typedef std::map<std::string, FactoryCreateFunc> FuncFactoryFactory;
68
69 static FuncFactoryFactory&
70 getFactory();
71};
72
Zhiyi Zhang74c61142020-10-07 21:00:49 -070073#define NDNCERT_REGISTER_FUNCFACTORY(C, T) \
74 static class NdnCert##C##FuncFactoryRegistrationClass { \
75 public: \
76 NdnCert##C##FuncFactoryRegistrationClass() \
77 { \
78 ::ndn::ndncert::NameAssignmentFunc::registerNameAssignmentFunc<C>(T); \
79 } \
tylerliu01d63ca2020-10-06 16:29:23 -070080 } g_NdnCert##C##ChallengeRegistrationVariable
81
Zhiyi Zhange4891b72020-10-10 15:11:57 -070082} // namespace ndncert
83} // namespace ndn
tylerliu01d63ca2020-10-06 16:29:23 -070084
Zhiyi Zhange4891b72020-10-10 15:11:57 -070085#endif // NDNCERT_ASSIGNMENT_FUNCS_HPP