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