initial commit
[squeep-authentication-module] / lib / template / login-html.js
diff --git a/lib/template/login-html.js b/lib/template/login-html.js
new file mode 100644 (file)
index 0000000..7d059e0
--- /dev/null
@@ -0,0 +1,85 @@
+'use strict';
+
+const { TemplateHelper: th } = require('@squeep/html-template-helper');
+
+/**
+ * Login form.
+ */
+function indieAuthSection(ctx, options) {
+  const indieAuthBlurb = (options.indieAuthBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
+  const showIndieAuthForm = options.authnEnabled.includes('indieAuth');
+  return showIndieAuthForm ? `\t\t\t<section class="indieauth">
+\t\t\t\t<h2>Login</h2>
+\t\t\t\t<form action="" method="POST">
+\t\t\t\t\t<fieldset>
+\t\t\t\t\t\t<legend>IndieAuth</legend>
+\t\t\t\t\t\t<label for="me">Profile URL:</label>
+\t\t\t\t\t\t<input id="me" name="me" type="url" size="40" placeholder="https://example.com/my_profile_url" value="" autofocus>
+\t\t\t\t\t\t<button>Login</button>
+${indieAuthBlurb}
+\t\t\t\t\t</fieldset>
+\t\t\t\t</form>
+\t\t\t</section>`
+    : '';
+}
+
+
+const userAuthn = ['argon2', 'pam', 'DEBUG_ANY'];
+function userSection(ctx, options) {
+  const userBlurb = (options.userBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
+  const secure = (ctx.clientProtocol || '').toLowerCase() === 'https';
+  const showUserForm = options.authnEnabled.filter((x) => userAuthn.includes(x)).length
+    && (secure || !options.secureAuthOnly);
+  return showUserForm ? `\t\t\t<section class="user">
+\t\t\t\t<form action="" method="POST">
+\t\t\t\t\t<fieldset>
+\t\t\t\t\t\t<legend>User Account</legend>
+\t\t\t\t\t\t<label for="identifier">Username:</label>
+\t\t\t\t\t\t<input id="identifier" name="identifier" value="">
+\t\t\t\t\t\t<br>
+\t\t\t\t\t\t<label for="credential">Password:</label>
+\t\t\t\t\t\t<input id="credential" name="credential" type="password" value="">
+\t\t\t\t\t\t<br>
+\t\t\t\t\t\t<button>Login</button>
+${userBlurb}
+\t\t\t\t\t</fieldset>
+\t\t\t\t</form>
+\t\t\t</section>`
+    : '';
+}
+
+
+/**
+ * Render login form for both local and profile authentication.
+ * @param {Object} ctx
+ * @param {String[]=} ctx.errors
+ * @param {String} ctx.clientProtocol
+ * @param {Object} options
+ * @param {Boolean} options.authenticator.secureAuthOnly
+ * @param {String[]=} options.authenticator.loginBlurb
+ * @param {String[]=} options.authenticator.indieAuthBlurb
+ * @param {String[]=} options.authenticator.userBlurb
+ * @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,
+    secureAuthOnly: options.authenticator.secureAuthOnly,
+    authnEnabled: options.authenticator.authnEnabled,
+    indieAuthBlurb: options.authenticator.indieAuthBlurb,
+    userBlurb: options.authenticator.userBlurb,
+  };
+  const mainContent = [
+    ...(options.authenticator.loginBlurb || []),
+    indieAuthSection(ctx, htmlOptions),
+    userSection(ctx, htmlOptions),
+  ];
+  return th.htmlPage(2, ctx, htmlOptions, mainContent);
+};
\ No newline at end of file