rename identity challenge dir to challenge

Change-Id: Ieb89704404dae2086bd83987624670432bb69663
diff --git a/src/challenge/challenge-module.cpp b/src/challenge/challenge-module.cpp
new file mode 100644
index 0000000..8de88b6
--- /dev/null
+++ b/src/challenge/challenge-module.cpp
@@ -0,0 +1,105 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017-2020, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "challenge/challenge-module.hpp"
+#include <ndn-cxx/util/random.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+ChallengeModule::ChallengeModule(const std::string& challengeType,
+                                 size_t maxAttemptTimes,
+                                 time::seconds secretLifetime)
+  : CHALLENGE_TYPE(challengeType)
+  , m_maxAttemptTimes(maxAttemptTimes)
+  , m_secretLifetime(secretLifetime)
+{
+}
+
+bool
+ChallengeModule::isChallengeSupported(const std::string& challengeType)
+{
+  ChallengeFactory& factory = getFactory();
+  auto i = factory.find(challengeType);
+  return i == factory.end() ? false : true;
+}
+
+unique_ptr<ChallengeModule>
+ChallengeModule::createChallengeModule(const std::string& challengeType)
+{
+  ChallengeFactory& factory = getFactory();
+  auto i = factory.find(challengeType);
+  return i == factory.end() ? nullptr : i->second();
+}
+
+ChallengeModule::ChallengeFactory&
+ChallengeModule::getFactory()
+{
+  static ChallengeModule::ChallengeFactory factory;
+  return factory;
+}
+
+std::string
+ChallengeModule::generateSecretCode()
+{
+  uint32_t securityCode = 0;
+  do {
+    securityCode = random::generateSecureWord32();
+  }
+  while (securityCode >= 4294000000);
+  securityCode /= 4294;
+  std::string result = std::to_string(securityCode);
+  while (result.length() < 6) {
+    result = "0" + result;
+  }
+  return result;
+}
+
+std::tuple<ErrorCode, std::string>
+ChallengeModule::returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string&& errorInfo)
+{
+  request.m_status = Status::FAILURE;
+  request.m_challengeType = "";
+  request.m_challengeState = nullopt;
+  return std::make_tuple(errorCode, std::move(errorInfo));
+}
+
+std::tuple<ErrorCode, std::string>
+ChallengeModule::returnWithNewChallengeStatus(ca::RequestState& request, const std::string& challengeStatus,
+                                              JsonSection&& challengeSecret, size_t remainingTries, time::seconds remainingTime)
+{
+  request.m_status = Status::CHALLENGE;
+  request.m_challengeType = CHALLENGE_TYPE;
+  request.m_challengeState = ca::ChallengeState(challengeStatus, time::system_clock::now(), remainingTries, remainingTime,
+                                                std::move(challengeSecret));
+  return std::make_tuple(ErrorCode::NO_ERROR, "");
+}
+
+std::tuple<ErrorCode, std::string>
+ChallengeModule::returnWithSuccess(ca::RequestState& request)
+{
+  request.m_status = Status::PENDING;
+  request.m_challengeType = CHALLENGE_TYPE;
+  request.m_challengeState = nullopt;
+  return std::make_tuple(ErrorCode::NO_ERROR, "");
+}
+
+} // namespace ndncert
+} // namespace ndn