Skip to content

Commit

Permalink
send-email-challenge: improve default email templates
Browse files Browse the repository at this point in the history
And use built-in interpolation of ConfigParser

Change-Id: Ifb534d1a49a6ab4fcbd08b49082e8676646797c6
  • Loading branch information
Pesa committed Jan 5, 2025
1 parent cf216ea commit 397faa9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
21 changes: 17 additions & 4 deletions ndncert-mail.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ SMTP_USER = leave it empty if you do not have one
SMTP_PASSWORD = leave it empty if you do not have one

[ndncert.email]
MAIL_FROM = NDN Testbed Certificate Robot <[email protected]>
SUBJECT = Email Challenge Triggered by NDNCERT
TEXT_TEMPLATE = Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}. Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type it in your application to complete the certificate issuance process. If you do not know what is going on, please ignore this message.
HTML_TEMPLATE = <html><head></head><body><p><b>Your PIN code: {0} from NDNCERT CA {1}. Certificate Name: {2}.</b></p><p>Your email has been used to apply for a digital certificate from NDNCERT. Please keep it secret and type it in your application to complete the certificate issuance process. If you do not know what is going on, please ignore this message.</p><p>Sincerely,<br/>NDN Testbed NDNCERT robot</p>
from = NDN Certificate Robot <[email protected]>
subject = Your NDNCERT verification code
text_template = Your email was recently used to apply for a digital certificate from NDNCERT.
Here is the verification code to enter into your application and complete the certificate issuance process. Do not share this code with anyone else.

Your verification code is: ${secret}
CA name: ${ca_name}
Certificate name: ${cert_name}

If you do not know what is going on, please ignore this message.
html_template = <!DOCTYPE html>
<p>Your email was recently used to apply for a digital certificate from NDNCERT.<br>
Here is the verification code to enter into your application and complete the certificate issuance process. Do not share this code with anyone else.</p>
<p><strong>Your verification code is: <code>${secret}</code></strong><br>
CA name: <code>${ca_name}</code><br>
Certificate name: <code>${cert_name}</code></p>
<p>If you do not know what is going on, please ignore this message.</p>
22 changes: 15 additions & 7 deletions ndncert-send-email-challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

# init arg parser and parse
parser = argparse.ArgumentParser(description='Email challenge sender for NDNCERT CA')
parser.add_argument('email', help='email address of the recipient')
parser.add_argument('recipient', help='email address of the recipient')
parser.add_argument('secret', help='secret code for the challenge')
parser.add_argument('ca_name', help='name of the certificate authority')
parser.add_argument('cert_name', help='name of the certificate being requested')
args = parser.parse_args()

vars = {
'ca_name': args.ca_name,
'cert_name': args.cert_name,
'recipient': args.recipient,
'secret': args.secret,
}

# open config file
confParser = configparser.ConfigParser()
confParser = configparser.ConfigParser(empty_lines_in_values=True,
interpolation=configparser.ExtendedInterpolation())
confParser.read('@SYSCONFDIR@/ndncert/ndncert-mail.conf')

# read smtp settings
Expand All @@ -24,15 +32,15 @@
password = confParser.get('ndncert.smtp', 'smtp_password')

# read email settings
from_addr = confParser.get('ndncert.email', 'mail_from')
subject = confParser.get('ndncert.email', 'subject')
text = confParser.get('ndncert.email', 'text_template').format(args.secret, args.ca_name, args.cert_name)
html = confParser.get('ndncert.email', 'html_template').format(args.secret, args.ca_name, args.cert_name)
from_addr = confParser.get('ndncert.email', 'from', vars=vars)
subject = confParser.get('ndncert.email', 'subject', vars=vars)
text = confParser.get('ndncert.email', 'text_template', vars=vars)
html = confParser.get('ndncert.email', 'html_template', vars=vars)

# create email message
msg = EmailMessage()
msg['From'] = from_addr
msg['To'] = args.email
msg['To'] = args.recipient
msg['Subject'] = subject
msg.set_content(text)
msg.add_alternative(html, subtype='html')
Expand Down
2 changes: 1 addition & 1 deletion src/challenge/challenge-email.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ChallengeEmail::handleChallengeRequest(const Block& params, ca::RequestState& re
if (request.challengeState->challengeStatus == NEED_CODE ||
request.challengeState->challengeStatus == WRONG_CODE) {
NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus);
// the incoming interest should bring the pin code
// the incoming interest should bring the verification code
std::string givenCode = readString(params.get(tlv::ParameterValue));
auto secret = request.challengeState->secrets;
// check if run out of time
Expand Down

0 comments on commit 397faa9

Please sign in to comment.