support interaction between module and apps for updating templates before rendering
[squeep-authentication-module] / lib / template / otp-html.js
1 'use strict';
2
3 const { TemplateHelper: th } = require('@squeep/html-template-helper');
4 const { sessionNavLinks } = require('./helpers');
5
6 /**
7 * Login form, continued.
8 */
9
10 function otpSection(ctx, options) {
11 const otpBlurb = (options.otpBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
12 return `\t\t\t<section class="otp">
13 \t\t\t\t<form method="POST">
14 \t\t\t\t\t<fieldset>
15 \t\t\t\t\t\t<legend>Two-Factor Authentication</legend>
16 \t\t\t\t\t\t<label for="otp">OTP Code</label>
17 \t\t\t\t\t\t<input type="tel" id="otp" name="otp" value="">
18 \t\t\t\t\t\t<br>
19 \t\t\t\t\t\t<button type="submit">Confirm</button>
20 ${otpBlurb}
21 \t\t\t\t\t</fieldset>
22 \t\t\t\t\t<input type="hidden" name="state" value="${ctx.otpState}">
23 \t\t\t\t</form>
24 \t\t\t</section>`;
25 }
26
27
28 /**
29 * Render 2fs form.
30 * @param {Object} ctx
31 * @param {String[]=} ctx.errors
32 * @param {String} ctx.otpState
33 * @param {Object} options
34 * @param {String[]=} options.authenticator.otpBlurb
35 * @param {String[]=} options.authenticator.loginBlurb
36 * @param {Object} options.manager
37 * @param {String} options.manager.pageTitle
38 * @param {String=} options.manager.logoUrl
39 * @param {Object} options.dingus
40 * @param {String} options.dingus.selfBaseUrl
41 * @param {() => {}} appCb
42 * @returns {String}
43 */
44 module.exports = (ctx, options, appCb = () => {}) => {
45 const pagePathLevel = 1;
46 const htmlOptions = {
47 pageIdentifier: 'otp',
48 pageTitle: options.manager.pageTitle,
49 logoUrl: options.manager.logoUrl,
50 footerEntries: options.manager.footerEntries,
51 otpBlurb: options.authenticator?.otpBlurb,
52 };
53 appCb(pagePathLevel, ctx, htmlOptions);
54 sessionNavLinks(pagePathLevel, ctx, htmlOptions);
55 const mainContent = [
56 ...(options.authenticator?.loginBlurb || []),
57 otpSection(ctx, htmlOptions),
58 ];
59 return th.htmlPage(pagePathLevel, ctx, htmlOptions, mainContent);
60 };