rudimentary support for totp 2fa
[squeep-authentication-module] / lib / template / otp-html.js
diff --git a/lib/template/otp-html.js b/lib/template/otp-html.js
new file mode 100644 (file)
index 0000000..cf7cef6
--- /dev/null
@@ -0,0 +1,54 @@
+'use strict';
+
+const { TemplateHelper: th } = require('@squeep/html-template-helper');
+
+/**
+ * Login form, continued.
+ */
+
+function otpSection(ctx, options) {
+  const otpBlurb = (options.otpBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
+  return `\t\t\t<section class="otp">
+\t\t\t\t<form action="" method="POST">
+\t\t\t\t\t<fieldset>
+\t\t\t\t\t\t<legend>Two-Factor Authentication</legend>
+\t\t\t\t\t\t<label for="otp">OTP Code</label>
+\t\t\t\t\t\t<input id="otp" name="otp" value="">
+\t\t\t\t\t\t<br>
+\t\t\t\t\t\t<button>Confirm</button>
+${otpBlurb}
+\t\t\t\t\t</fieldset>
+\t\t\t\t\t<input type="hidden" name="state" value="${ctx.otpState}">
+\t\t\t\t</form>
+\t\t\t</section`;
+}
+
+
+/**
+ * Render 2fs form.
+ * @param {Object} ctx
+ * @param {String[]=} ctx.errors
+ * @param {String} ctx.otpState
+ * @param {Object} options
+ * @param {String[]=} options.authenticator.otpBlurb
+ * @param {String[]=} options.authenticator.loginBlurb
+ * @param {Object} options.manager
+ * @param {String} options.manager.pageTitle
+ * @param {String=} options.manager.logoUrl
+ * @param {Object} options.dingus
+ * @param {String} options.dingus.selfBaseUrl
+ * @returns {String}
+ */
+module.exports = (ctx, options) => {
+  const htmlOptions = {
+    pageTitle: options.manager.pageTitle,
+    logoUrl: options.manager.logoUrl,
+    footerEntries: options.manager.footerEntries,
+    otpBlurb: options.authenticator?.otpBlurb,
+  };
+  const mainContent = [
+    ...(options.authenticator?.loginBlurb || []),
+    otpSection(ctx, htmlOptions),
+  ];
+  return th.htmlPage(2, ctx, htmlOptions, mainContent);
+};
\ No newline at end of file