7d059e0d505caf4d4fb503f554dcd91d9d05ccc3
[squeep-authentication-module] / lib / template / login-html.js
1 'use strict';
2
3 const { TemplateHelper: th } = require('@squeep/html-template-helper');
4
5 /**
6 * Login form.
7 */
8 function indieAuthSection(ctx, options) {
9 const indieAuthBlurb = (options.indieAuthBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
10 const showIndieAuthForm = options.authnEnabled.includes('indieAuth');
11 return showIndieAuthForm ? `\t\t\t<section class="indieauth">
12 \t\t\t\t<h2>Login</h2>
13 \t\t\t\t<form action="" method="POST">
14 \t\t\t\t\t<fieldset>
15 \t\t\t\t\t\t<legend>IndieAuth</legend>
16 \t\t\t\t\t\t<label for="me">Profile URL:</label>
17 \t\t\t\t\t\t<input id="me" name="me" type="url" size="40" placeholder="https://example.com/my_profile_url" value="" autofocus>
18 \t\t\t\t\t\t<button>Login</button>
19 ${indieAuthBlurb}
20 \t\t\t\t\t</fieldset>
21 \t\t\t\t</form>
22 \t\t\t</section>`
23 : '';
24 }
25
26
27 const userAuthn = ['argon2', 'pam', 'DEBUG_ANY'];
28 function userSection(ctx, options) {
29 const userBlurb = (options.userBlurb || []).map((x) => '\t'.repeat(6) + x).join('\n');
30 const secure = (ctx.clientProtocol || '').toLowerCase() === 'https';
31 const showUserForm = options.authnEnabled.filter((x) => userAuthn.includes(x)).length
32 && (secure || !options.secureAuthOnly);
33 return showUserForm ? `\t\t\t<section class="user">
34 \t\t\t\t<form action="" method="POST">
35 \t\t\t\t\t<fieldset>
36 \t\t\t\t\t\t<legend>User Account</legend>
37 \t\t\t\t\t\t<label for="identifier">Username:</label>
38 \t\t\t\t\t\t<input id="identifier" name="identifier" value="">
39 \t\t\t\t\t\t<br>
40 \t\t\t\t\t\t<label for="credential">Password:</label>
41 \t\t\t\t\t\t<input id="credential" name="credential" type="password" value="">
42 \t\t\t\t\t\t<br>
43 \t\t\t\t\t\t<button>Login</button>
44 ${userBlurb}
45 \t\t\t\t\t</fieldset>
46 \t\t\t\t</form>
47 \t\t\t</section>`
48 : '';
49 }
50
51
52 /**
53 * Render login form for both local and profile authentication.
54 * @param {Object} ctx
55 * @param {String[]=} ctx.errors
56 * @param {String} ctx.clientProtocol
57 * @param {Object} options
58 * @param {Boolean} options.authenticator.secureAuthOnly
59 * @param {String[]=} options.authenticator.loginBlurb
60 * @param {String[]=} options.authenticator.indieAuthBlurb
61 * @param {String[]=} options.authenticator.userBlurb
62 * @param {Object} options.manager
63 * @param {String} options.manager.pageTitle
64 * @param {String=} options.manager.logoUrl
65 * @param {Object} options.dingus
66 * @param {String} options.dingus.selfBaseUrl
67 * @returns {String}
68 */
69 module.exports = (ctx, options) => {
70 const htmlOptions = {
71 pageTitle: options.manager.pageTitle,
72 logoUrl: options.manager.logoUrl,
73 footerEntries: options.manager.footerEntries,
74 secureAuthOnly: options.authenticator.secureAuthOnly,
75 authnEnabled: options.authenticator.authnEnabled,
76 indieAuthBlurb: options.authenticator.indieAuthBlurb,
77 userBlurb: options.authenticator.userBlurb,
78 };
79 const mainContent = [
80 ...(options.authenticator.loginBlurb || []),
81 indieAuthSection(ctx, htmlOptions),
82 userSection(ctx, htmlOptions),
83 ];
84 return th.htmlPage(2, ctx, htmlOptions, mainContent);
85 };