support interaction between module and apps for updating templates before rendering
[squeep-authentication-module] / lib / template / settings-html.js
1 'use strict';
2
3 /* eslint-disable no-unused-vars */
4
5 const { TemplateHelper: th } = require('@squeep/html-template-helper');
6 const { sessionNavLinks } = require('./helpers');
7 const { TOTP } = require('@squeep/totp');
8
9 function updatePasswordSection(ctx, htmlOptions) {
10 return `\t\t\t<section class="settings-update-password">
11 \t\t\t\t<h2>Password</h2>
12 \t\t\t\t<form method="POST">
13 \t\t\t\t\t<fieldset>
14 \t\t\t\t\t\t<legend>Update Password</legend>
15 \t\t\t\t\t\t<label for="credential-current">Current Password:</label>
16 \t\t\t\t\t\t<input type="password" id="credential-current" name="credential-current" value="">
17 \t\t\t\t\t\t<br>
18 \t\t\t\t\t\t<label for="credential-new">New Password:</label>
19 \t\t\t\t\t\t<input type="password" id="credential-new" name="credential-new" value="">
20 \t\t\t\t\t\t<br>
21 \t\t\t\t\t\t<label for="credential-new-2">Confirm New Password:</label>
22 \t\t\t\t\t\t<input type="password" id="credential-new-2" name="credential-new-2" value="">
23 \t\t\t\t\t\t<br>
24 \t\t\t\t\t\t<button type="submit" name="credential" value="update">Update</button>
25 \t\t\t\t\t</fieldset>
26 \t\t\t\t</form>
27 \t\t\t</section>`;
28 }
29
30
31 function enableOTPSection(ctx, htmlOptions) {
32 return `\t\t\t<section class="settings-otp">
33 \t\t\t\t<h2>OTP 2FA</h2>
34 \t\t\t\t<form method="POST">
35 \t\t\t\t\t<fieldset>
36 \t\t\t\t\t\t<legend>Enable OTP</legend>
37 \t\t\t\t\t\t<button type="submit" name="otp" value="enable">Enable OTP</button>
38 \t\t\t\t\t</fieldset>
39 \t\t\t\t</form>
40 \t\t\t</section>`;
41 }
42
43
44 function confirmOTPSection(ctx, htmlOptions) {
45 const { secret, svg, uri } = TOTP.createKeySVG({
46 accountname: ctx.authenticationId,
47 }, ctx.otpConfirmKey, 'base32');
48 return `\t\t\t<section class="settings-otp">
49 \t\t\t\t<h2>OTP 2FA</h2>
50 \t\t\t\t<form method="POST">
51 \t\t\t\t\t<fieldset>
52 \t\t\t\t\t<legend>Confirm OTP Key</legend>
53 \t\t\t\t\t\t<div>
54 \t\t\t\t\t\t\t<details>
55 \t\t\t\t\t\t\t\t<summary>Show Key</summary>
56 \t\t\t\t\t\t\t\tOTP Key (base32): <code>${secret}</code>
57 \t\t\t\t\t\t\t\t<div>
58 \t\t\t\t\t\t\t\t\tURI: <code>${uri}</code>
59 \t\t\t\t\t\t\t\t</div>
60 \t\t\t\t\t\t\t</details>
61 \t\t\t\t\t\t</div>
62 \t\t\t\t\t\t<div class="otp-key-qr">
63 ${svg}
64 \t\t\t\t\t\t</div>
65 \t\t\t\t\t\t<br>
66 \t\t\t\t\t\t<label for="otp-token">Enter OTP token to enable:</label>
67 \t\t\t\t\t\t<input id="otp-token" name="otp-token" type="text" value="">
68 \t\t\t\t\t\t<br>
69 \t\t\t\t\t\t<input type="hidden" name="otp-box" value="${ctx.otpConfirmBox}">
70 \t\t\t\t\t\t<button type="submit" name="otp" value="confirm">Confirm OTP</button>
71 \t\t\t\t\t</fieldset>
72 \t\t\t\t</form>
73 \t\t\t</section>`;
74 }
75
76
77 function disableOTPSection(ctx, htmlOptions) {
78 return `\t\t\t<section class="settings-otp">
79 \t\t\t\t<h2>OTP 2FA</h2>
80 \t\t\t\t<p>OTP is currrently enabled. It may be removed here.</p>
81 \t\t\t\t<form method="POST">
82 \t\t\t\t\t<button type="submit" name="otp" value="disable">Disable OTP</button>
83 \t\t\t\t</form>
84 \t\t\t</section>`;
85 }
86
87
88 function OTPSection(ctx, htmlOptions) {
89 const OTPToggle = ctx.otpKey ? disableOTPSection : enableOTPSection;
90 const OTPContent = ctx.otpConfirmBox ? confirmOTPSection : OTPToggle;
91 return '\t\t\t<section class="settings-otp">' +
92 OTPContent(ctx, htmlOptions) +
93 '\t\t\t</section>';
94 }
95
96
97 module.exports = (ctx, options, appCb = () => {}) => {
98 const pagePathLevel = 1;
99 const htmlOptions = {
100 pageIdentifier: 'account',
101 pageTitle: options.manager.pageTitle,
102 logoUrl: options.manager.logoUrl,
103 footerEntries: options.manager.footerEntries,
104 };
105 appCb(pagePathLevel, ctx, htmlOptions);
106 sessionNavLinks(pagePathLevel, ctx, htmlOptions);
107 const mainContent = [
108 OTPSection(ctx, htmlOptions),
109 updatePasswordSection(ctx, htmlOptions),
110 ];
111
112 return th.htmlPage(pagePathLevel, ctx, htmlOptions, mainContent);
113 };