bump package version to 1.5.0
[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 * @typedef {object} Context
8 * @property {string[]=} otpBlurb content accompanying otp entry
9 * @property {string} otpState packed otp state
10 */
11
12 /**
13 * @alias {object} HtmlOptions
14 */
15
16 /**
17 * @typedef {import('../session-manager').AppTemplateCallback} AppTemplateCallback
18 */
19
20
21 /**
22 * Login form, continued.
23 */
24
25 /**
26 *
27 * @param {Context} ctx context
28 * @param {HtmlOptions} options htmlOptions
29 * @returns {string} section
30 */
31 function otpSection(ctx, options) {
32 const otpBlurb = (options.otpBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
33 return `\t\t\t<section class="otp">
34 \t\t\t\t<form method="POST">
35 \t\t\t\t\t<fieldset>
36 \t\t\t\t\t\t<legend>Two-Factor Authentication</legend>
37 \t\t\t\t\t\t<label for="otp">OTP Code</label>
38 \t\t\t\t\t\t<input type="tel" id="otp" name="otp" value="">
39 \t\t\t\t\t\t<br>
40 \t\t\t\t\t\t<button type="submit">Confirm</button>
41 ${otpBlurb}
42 \t\t\t\t\t</fieldset>
43 \t\t\t\t\t<input type="hidden" name="state" value="${ctx.otpState}">
44 \t\t\t\t</form>
45 \t\t\t</section>`;
46 }
47
48
49 /**
50 * Render 2fs form.
51 * @param {Context} ctx context
52 * @param {object} options options
53 * @param {string[]=} options.authenticator.otpBlurb content accompanying otp entry
54 * @param {object} options.manager manager options
55 * @param {string} options.manager.pageTitle page title
56 * @param {string=} options.manager.logoUrl logo url
57 * @param {string=} options.manager.logoAlt logo alt text
58 * @param {object} options.dingus dingus options
59 * @param {string} options.dingus.selfBaseUrl root url
60 * @param {AppTemplateCallback} appCb function to mogrify htmlOptions
61 * @returns {string} page
62 */
63 module.exports = (ctx, options, appCb = () => {}) => {
64 const pagePathLevel = 1;
65 const htmlOptions = {
66 pageIdentifier: 'otp',
67 pageTitle: options.manager.pageTitle,
68 logoUrl: options.manager.logoUrl,
69 footerEntries: options.manager.footerEntries,
70 otpBlurb: options.authenticator?.otpBlurb,
71 };
72 appCb(pagePathLevel, ctx, htmlOptions);
73 sessionNavLinks(pagePathLevel, ctx, htmlOptions);
74 const mainContent = [
75 ...(options.authenticator?.loginBlurb || []),
76 otpSection(ctx, htmlOptions),
77 ];
78 return th.htmlPage(pagePathLevel, ctx, htmlOptions, mainContent);
79 };