blob: 4421bd3aa17b819b23686b61c78ae5fcbaba247d [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, Regents of the University of California.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08004 *
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>
Suyong Won19fba4d2020-05-09 13:39:46 -070026#include <ndn-cxx/encoding/block.hpp>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080027
28namespace ndn {
29namespace ndncert {
30
31/**
32 * @brief Provide Email based challenge
33 *
Zhiyi Zhang576aad12017-10-03 15:41:53 -070034 * For challenge design
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080035 * @sa https://github.com/named-data/ndncert/wiki/NDN-Certificate-Management-Protocol
Zhiyi Zhang576aad12017-10-03 15:41:53 -070036 * For deployment instructions:
37 * @sa https://github.com/named-data/ndncert/wiki/Deploy-Email-Challenge
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080038 *
39 * The main process of this challenge module is:
40 * 1. Requester provides its email address.
41 * 2. The challenge module will send a verification code to this email address.
42 * 3. Requester provides the verification code to challenge module.
43 *
44 * There are several challenge status in EMAIL challenge:
45 * NEED_CODE: When email address is provided and the verification code has been sent out.
46 * WRONG_CODE: Wrong code but still within secret lifetime and within max try times.
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070047 *
48 * Failure info when application fails:
49 * FAILURE_MAXRETRY: When run out retry times.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080050 * FAILURE_INVALID_EMAIL: When the email is invalid.
51 * FAILURE_TIMEOUT: When the secret lifetime expires.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080052 */
53class ChallengeEmail : public ChallengeModule
54{
55public:
Zhiyi Zhang576aad12017-10-03 15:41:53 -070056 ChallengeEmail(const std::string& scriptPath = "ndncert-send-email-challenge",
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080057 const size_t& maxAttemptTimes = 3,
58 const time::seconds secretLifetime = time::minutes(20));
59
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 // For CA
61 void
Suyong Won19fba4d2020-05-09 13:39:46 -070062 handleChallengeRequest(const Block& params, CertificateRequest& request) override;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070063
64 // For Client
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080065 JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 getRequirementForChallenge(int status, const std::string& challengeStatus) override;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080067
68 JsonSection
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069 genChallengeRequestJson(int status, const std::string& challengeStatus, const JsonSection& params) override;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080070
Suyong Won19fba4d2020-05-09 13:39:46 -070071 Block
72 genChallengeRequestTLV(int status, const std::string& challengeStatus, const JsonSection& params) override;
73
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080074PUBLIC_WITH_TESTS_ELSE_PRIVATE:
75 static bool
76 isValidEmailAddress(const std::string& emailAddress);
77
78 void
Zhiyi Zhang576aad12017-10-03 15:41:53 -070079 sendEmail(const std::string& emailAddress, const std::string& secret,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 const CertificateRequest& request) const;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080081
82PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083 // challenge status
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080084 static const std::string NEED_CODE;
85 static const std::string WRONG_CODE;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070086 static const std::string FAILURE_INVALID_EMAIL;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087 // JSON attribute
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080088 static const std::string JSON_EMAIL;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080089 static const std::string JSON_CODE;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080090
91private:
92 std::string m_sendEmailScript;
93 int m_maxAttemptTimes;
94 time::seconds m_secretLifetime;
95};
96
97} // namespace ndncert
98} // namespace ndn
99
100#endif // NDNCERT_CHALLENGE_EMAIL_HPP