blob: 0f0d93599fd3f4511727391fc5c03053cec59a38 [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.
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070043 *
44 * Failure info when application fails:
45 * FAILURE_MAXRETRY: When run out retry times.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080046 * FAILURE_INVALID_EMAIL: When the email is invalid.
47 * FAILURE_TIMEOUT: When the secret lifetime expires.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080048 */
49class ChallengeEmail : public ChallengeModule
50{
51public:
52 ChallengeEmail(const std::string& scriptPath = "send-mail.sh",
53 const size_t& maxAttemptTimes = 3,
54 const time::seconds secretLifetime = time::minutes(20));
55
56PUBLIC_WITH_TESTS_ELSE_PROTECTED:
57 JsonSection
58 processSelectInterest(const Interest& interest, CertificateRequest& request) override;
59
60 JsonSection
61 processValidateInterest(const Interest& interest, CertificateRequest& request) override;
62
63 std::list<std::string>
64 getSelectRequirements() override;
65
66 std::list<std::string>
67 getValidateRequirements(const std::string& status) override;
68
69 JsonSection
70 doGenSelectParamsJson(const std::string& status,
71 const std::list<std::string>& paramList) override;
72
73 JsonSection
74 doGenValidateParamsJson(const std::string& status,
75 const std::list<std::string>& paramList) override;
76
77PUBLIC_WITH_TESTS_ELSE_PRIVATE:
78 static bool
79 isValidEmailAddress(const std::string& emailAddress);
80
81 void
82 sendEmail(const std::string& emailAddress, const std::string& secret) const;
83
84PUBLIC_WITH_TESTS_ELSE_PRIVATE:
85 static std::tuple<time::system_clock::TimePoint, std::string, int>
86 parseStoredSecrets(const JsonSection& storedSecret);
87
88 static JsonSection
89 generateStoredSecrets(const time::system_clock::TimePoint& tp, const std::string& secretCode,
90 int attempTimes);
91
92PUBLIC_WITH_TESTS_ELSE_PRIVATE:
93 static const std::string NEED_CODE;
94 static const std::string WRONG_CODE;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070095
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080096 static const std::string FAILURE_TIMEOUT;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070097 static const std::string FAILURE_INVALID_EMAIL;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080098 static const std::string FAILURE_MAXRETRY;
99
100 static const std::string JSON_EMAIL;
101 static const std::string JSON_CODE_TP;
102 static const std::string JSON_CODE;
103 static const std::string JSON_ATTEMPT_TIMES;
104
105private:
106 std::string m_sendEmailScript;
107 int m_maxAttemptTimes;
108 time::seconds m_secretLifetime;
109};
110
111} // namespace ndncert
112} // namespace ndn
113
114#endif // NDNCERT_CHALLENGE_EMAIL_HPP