aba75f921126dbb42682bd77adf5f43df2b976ed
3 /* eslint-disable no-unused-vars */
5 const { TemplateHelper: th
} = require('@squeep/html-template-helper');
6 const { sessionNavLinks
} = require('./helpers');
7 const { TOTP
} = require('@squeep/totp');
10 * @typedef {object} Context
11 * @property {string=} otpConfirmBox encrypted otp state
12 * @property {string=} otpConfirmKey otp key to confirm adding to identifier
13 * @property {string=} authenticationId identifier
17 * @alias {object} HtmlOptions
21 * @typedef {import('../session-manager').AppTemplateCallback} AppTemplateCallback
25 * Render password update section.
26 * @param {Context} ctx context
27 * @param {HtmlOptions} htmlOptions options
28 * @returns {string} section
30 function updatePasswordSection(ctx
, htmlOptions
) {
31 return `\t\t\t<section class="settings-update-password">
32 \t\t\t\t<h2>Password</h2>
33 \t\t\t\t<form method="POST">
35 \t\t\t\t\t\t<legend>Update Password</legend>
36 \t\t\t\t\t\t<label for="credential-current">Current Password:</label>
37 \t\t\t\t\t\t<input type="password" id="credential-current" name="credential-current" value="">
39 \t\t\t\t\t\t<label for="credential-new">New Password:</label>
40 \t\t\t\t\t\t<input type="password" id="credential-new" name="credential-new" value="">
42 \t\t\t\t\t\t<label for="credential-new-2">Confirm New Password:</label>
43 \t\t\t\t\t\t<input type="password" id="credential-new-2" name="credential-new-2" value="">
45 \t\t\t\t\t\t<button type="submit" name="credential" value="update">Update</button>
54 * @param {Context} ctx context
55 * @param {HtmlOptions} htmlOptions options
56 * @returns {string} otp enable section
58 function enableOTPSection(ctx
, htmlOptions
) {
59 return `\t\t\t<section class="settings-otp">
60 \t\t\t\t<h2>OTP 2FA</h2>
61 \t\t\t\t<form method="POST">
63 \t\t\t\t\t\t<legend>Enable OTP</legend>
64 \t\t\t\t\t\t<button type="submit" name="otp" value="enable">Enable OTP</button>
73 * @param {Context} ctx context
74 * @param {htmlOptions} htmlOptions options
75 * @returns {string} otp confirm section
77 function confirmOTPSection(ctx
, htmlOptions
) {
78 const { secret
, svg
, uri
} = TOTP
.createKeySVG({
79 accountname: ctx
.authenticationId
,
80 }, ctx
.otpConfirmKey
, 'base32');
81 return `\t\t\t<section class="settings-otp">
82 \t\t\t\t<h2>OTP 2FA</h2>
83 \t\t\t\t<form method="POST">
85 \t\t\t\t\t<legend>Confirm OTP Key</legend>
87 \t\t\t\t\t\t\t<details>
88 \t\t\t\t\t\t\t\t<summary>Show Key</summary>
89 \t\t\t\t\t\t\t\tOTP Key (base32): <code>${secret}</code>
91 \t\t\t\t\t\t\t\t\tURI: <code>${uri}</code>
92 \t\t\t\t\t\t\t\t</div>
93 \t\t\t\t\t\t\t</details>
95 \t\t\t\t\t\t<div class="otp-key-qr">
99 \t\t\t\t\t\t<label for="otp-token">Enter OTP token to enable:</label>
100 \t\t\t\t\t\t<input id="otp-token" name="otp-token" type="text" value="">
102 \t\t\t\t\t\t<input type="hidden" name="otp-box" value="${ctx.otpConfirmBox}">
103 \t\t\t\t\t\t<button type="submit" name="otp" value="confirm">Confirm OTP</button>
104 \t\t\t\t\t</fieldset>
112 * @param {Context} ctx context
113 * @param {HtmlOptions} htmlOptions options
114 * @returns {string} disable otp section
116 function disableOTPSection(ctx
, htmlOptions
) {
117 return `\t\t\t<section class="settings-otp">
118 \t\t\t\t<h2>OTP 2FA</h2>
119 \t\t\t\t<p>OTP is currrently enabled. It may be removed here.</p>
120 \t\t\t\t<form method="POST">
121 \t\t\t\t\t<button type="submit" name="otp" value="disable">Disable OTP</button>
129 * @param {Context} ctx context
130 * @param {HtmlOptions} htmlOptions options
131 * @returns {string} otp section
133 function OTPSection(ctx
, htmlOptions
) {
134 const OTPToggle
= ctx
.otpKey
? disableOTPSection : enableOTPSection
;
135 const OTPContent
= ctx
.otpConfirmBox
? confirmOTPSection : OTPToggle
;
136 return '\t\t\t<section class="settings-otp">' +
137 OTPContent(ctx
, htmlOptions
) +
142 * Render settings form for managing credential and otp key.
143 * @param {Context} ctx context
144 * @param {object} options options
145 * @param {AppTemplateCallback} appCb function to mogrify htmlOptions
146 * @returns {string} page
148 module
.exports
= (ctx
, options
, appCb
= () => {}) => {
149 const pagePathLevel
= 1;
150 const htmlOptions
= {
151 pageIdentifier: 'account',
152 pageTitle: options
.manager
.pageTitle
,
153 logoUrl: options
.manager
.logoUrl
,
154 footerEntries: options
.manager
.footerEntries
,
156 appCb(pagePathLevel
, ctx
, htmlOptions
);
157 sessionNavLinks(pagePathLevel
, ctx
, htmlOptions
);
158 const mainContent
= [
159 OTPSection(ctx
, htmlOptions
),
160 updatePasswordSection(ctx
, htmlOptions
),
163 return th
.htmlPage(pagePathLevel
, ctx
, htmlOptions
, mainContent
);