blob: a014d004068440d9e94ea8cd71e5caff1489d695 [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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_CHALLENGE_EMAIL_HPP
22#define NDNCERT_CHALLENGE_EMAIL_HPP
23
24#include "../challenge-module.hpp"
25#include <ndn-cxx/util/time.hpp>
26
27namespace ndn {
28namespace ndncert {
29
30/**
31 * @brief Provide Email based challenge
32 *
Zhiyi Zhang576aad12017-10-03 15:41:53 -070033 * For challenge design
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080034 * @sa https://github.com/named-data/ndncert/wiki/NDN-Certificate-Management-Protocol
Zhiyi Zhang576aad12017-10-03 15:41:53 -070035 * For deployment instructions:
36 * @sa https://github.com/named-data/ndncert/wiki/Deploy-Email-Challenge
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080037 *
38 * The main process of this challenge module is:
39 * 1. Requester provides its email address.
40 * 2. The challenge module will send a verification code to this email address.
41 * 3. Requester provides the verification code to challenge module.
42 *
43 * There are several challenge status in EMAIL challenge:
44 * NEED_CODE: When email address is provided and the verification code has been sent out.
45 * WRONG_CODE: Wrong code but still within secret lifetime and within max try times.
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070046 *
47 * Failure info when application fails:
48 * FAILURE_MAXRETRY: When run out retry times.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080049 * FAILURE_INVALID_EMAIL: When the email is invalid.
50 * FAILURE_TIMEOUT: When the secret lifetime expires.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080051 */
52class ChallengeEmail : public ChallengeModule
53{
54public:
Zhiyi Zhang576aad12017-10-03 15:41:53 -070055 ChallengeEmail(const std::string& scriptPath = "ndncert-send-email-challenge",
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080056 const size_t& maxAttemptTimes = 3,
57 const time::seconds secretLifetime = time::minutes(20));
58
59PUBLIC_WITH_TESTS_ELSE_PROTECTED:
60 JsonSection
61 processSelectInterest(const Interest& interest, CertificateRequest& request) override;
62
63 JsonSection
64 processValidateInterest(const Interest& interest, CertificateRequest& request) override;
65
66 std::list<std::string>
67 getSelectRequirements() override;
68
69 std::list<std::string>
70 getValidateRequirements(const std::string& status) override;
71
72 JsonSection
73 doGenSelectParamsJson(const std::string& status,
74 const std::list<std::string>& paramList) override;
75
76 JsonSection
77 doGenValidateParamsJson(const std::string& status,
78 const std::list<std::string>& paramList) override;
79
80PUBLIC_WITH_TESTS_ELSE_PRIVATE:
81 static bool
82 isValidEmailAddress(const std::string& emailAddress);
83
84 void
Zhiyi Zhang576aad12017-10-03 15:41:53 -070085 sendEmail(const std::string& emailAddress, const std::string& secret,
86 const std::string& caName) const;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080087
88PUBLIC_WITH_TESTS_ELSE_PRIVATE:
89 static std::tuple<time::system_clock::TimePoint, std::string, int>
90 parseStoredSecrets(const JsonSection& storedSecret);
91
92 static JsonSection
93 generateStoredSecrets(const time::system_clock::TimePoint& tp, const std::string& secretCode,
94 int attempTimes);
95
96PUBLIC_WITH_TESTS_ELSE_PRIVATE:
97 static const std::string NEED_CODE;
98 static const std::string WRONG_CODE;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070099
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800100 static const std::string FAILURE_TIMEOUT;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -0700101 static const std::string FAILURE_INVALID_EMAIL;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800102 static const std::string FAILURE_MAXRETRY;
103
104 static const std::string JSON_EMAIL;
105 static const std::string JSON_CODE_TP;
106 static const std::string JSON_CODE;
107 static const std::string JSON_ATTEMPT_TIMES;
108
109private:
110 std::string m_sendEmailScript;
111 int m_maxAttemptTimes;
112 time::seconds m_secretLifetime;
113};
114
115} // namespace ndncert
116} // namespace ndn
117
118#endif // NDNCERT_CHALLENGE_EMAIL_HPP