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