blob: b00a3dede1bb60a10a399148fe563aa8419daae4 [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 *
33 * @sa https://github.com/named-data/ndncert/wiki/NDN-Certificate-Management-Protocol
34 *
35 * The main process of this challenge module is:
36 * 1. Requester provides its email address.
37 * 2. The challenge module will send a verification code to this email address.
38 * 3. Requester provides the verification code to challenge module.
39 *
40 * There are several challenge status in EMAIL challenge:
41 * NEED_CODE: When email address is provided and the verification code has been sent out.
42 * WRONG_CODE: Wrong code but still within secret lifetime and within max try times.
43 * FAILURE_INVALID_EMAIL: When the email is invalid.
44 * FAILURE_TIMEOUT: When the secret lifetime expires.
45 * FAILURE_MAXRETRY: When run out retry times.
46 */
47class ChallengeEmail : public ChallengeModule
48{
49public:
50 ChallengeEmail(const std::string& scriptPath = "send-mail.sh",
51 const size_t& maxAttemptTimes = 3,
52 const time::seconds secretLifetime = time::minutes(20));
53
54PUBLIC_WITH_TESTS_ELSE_PROTECTED:
55 JsonSection
56 processSelectInterest(const Interest& interest, CertificateRequest& request) override;
57
58 JsonSection
59 processValidateInterest(const Interest& interest, CertificateRequest& request) override;
60
61 std::list<std::string>
62 getSelectRequirements() override;
63
64 std::list<std::string>
65 getValidateRequirements(const std::string& status) override;
66
67 JsonSection
68 doGenSelectParamsJson(const std::string& status,
69 const std::list<std::string>& paramList) override;
70
71 JsonSection
72 doGenValidateParamsJson(const std::string& status,
73 const std::list<std::string>& paramList) override;
74
75PUBLIC_WITH_TESTS_ELSE_PRIVATE:
76 static bool
77 isValidEmailAddress(const std::string& emailAddress);
78
79 void
80 sendEmail(const std::string& emailAddress, const std::string& secret) const;
81
82PUBLIC_WITH_TESTS_ELSE_PRIVATE:
83 static std::tuple<time::system_clock::TimePoint, std::string, int>
84 parseStoredSecrets(const JsonSection& storedSecret);
85
86 static JsonSection
87 generateStoredSecrets(const time::system_clock::TimePoint& tp, const std::string& secretCode,
88 int attempTimes);
89
90PUBLIC_WITH_TESTS_ELSE_PRIVATE:
91 static const std::string NEED_CODE;
92 static const std::string WRONG_CODE;
93 static const std::string FAILURE_INVALID_EMAIL;
94 static const std::string FAILURE_TIMEOUT;
95 static const std::string FAILURE_MAXRETRY;
96
97 static const std::string JSON_EMAIL;
98 static const std::string JSON_CODE_TP;
99 static const std::string JSON_CODE;
100 static const std::string JSON_ATTEMPT_TIMES;
101
102private:
103 std::string m_sendEmailScript;
104 int m_maxAttemptTimes;
105 time::seconds m_secretLifetime;
106};
107
108} // namespace ndncert
109} // namespace ndn
110
111#endif // NDNCERT_CHALLENGE_EMAIL_HPP