a0475adc09122d0f9f29dd2b5629b5cc6bc612d5
[squeep-indie-auther] / src / template / admin-ticket-html.js
1 'use strict';
2
3 /**
4 * This renders the interface for submitting a ticket proffer to a third-party.
5 */
6
7 const th = require('./template-helper');
8 const { sessionNavLinks } = require('@squeep/authentication-module');
9
10
11 function renderProfileOption(profile) {
12 return `<option value="${profile}">${profile}</option>`;
13 }
14
15 function renderScopeCheckboxTR(scope) {
16 const defaultChecked = ['read'];
17 const checked = defaultChecked.includes(scope) ? ' checked' : '';
18 return `<tr class="scope">
19 \t<td><input type="checkbox" id="scopes-${scope}" name="scopes[]" value="${scope}"${checked}></td>
20 \t<td>${scope}</td>
21 </tr>`;
22 }
23
24 function mainContent(ctx) {
25 const profileOptions = th.indented(4, (ctx?.profilesScopes?.profiles || []).map((profile) => renderProfileOption(profile)))
26 .join('\n');
27 const elideScopes = ['profile', 'email'];
28 const allScopes = Object.keys(ctx?.profilesScopes?.scopeIndex || {});
29 const displayScopes = (allScopes).filter((scope) => !elideScopes.includes(scope));
30 const scopesCheckboxRows = th.indented(5, displayScopes.map((scope) => renderScopeCheckboxTR(scope)))
31 .join('\n');
32 return `<section>
33 \t<form method="POST">
34 \t\t<div>
35 \t\t\tYou may proactively send a ticket to a third-party site,
36 \t\t\twhich they may redeem for an access token which grants additional
37 \t\t\taccess to the specified resource.
38 \t\t</div>
39 \t\t<br>
40 \t\t<fieldset>
41 \t\t\t<legend>Proffer A Ticket</legend>
42 \t\t\t<label for="profile-select">Profile Granting this Ticket</label>
43 \t\t\t<select id="profile-select" name="profile">
44 ${profileOptions}
45 \t\t\t</select>
46 \t\t\t<br>
47 \t\t\t<label for="resource-url">Resource URL:</label>
48 \t\t\t<input type="url" id="resource-url" name="resource" size="96">
49 \t\t\t<br>
50 \t\t\t<label for="recipient-url">Recipient URL:</label>
51 \t\t\t<input type="url" id="recipient-url" name="subject" size="96">
52 \t\t\t<br>
53 <fieldset>
54 <legend>Scopes</legend>
55 \t\t\t<table>
56 \t\t\t\t<tbody>
57 ${scopesCheckboxRows}
58 \t\t\t\t</tbody>
59 \t\t\t</table>
60 </fieldset>
61 \t\t\t<br>
62 \t\t\t<label for="scopes-adhoc">Additional Scopes (space separated):</label>
63 \t\t\t<input type="text" id="scopes-adhoc" name="adhoc" size="96">
64 \t\t\t<br>
65 \t\t\t<button type="submit" name="action" value="proffer-ticket">Send Ticket</button>
66 \t\t</fieldset>
67 \t</form>
68 </section>`;
69 }
70
71
72 /**
73 *
74 * @param {Object} ctx
75 * @param {Object} ctx.profilesScopes.scopeIndex
76 * @param {String[]} ctx.profileScopes.profiles
77 * @param {Object} options
78 * @param {Object} options.manager
79 * @param {String} options.manager.pageTitle
80 * @param {String} options.manager.logoUrl
81 * @param {String[]} options.manager.footerEntries
82 * @returns {String}
83 */
84 module.exports = (ctx, options) => {
85 const pagePathLevel = 1;
86 const htmlOptions = {
87 padeIdentifier: 'ticketProffer',
88 pageTitle: options.manager.pageTitle + ' - Ticket Proffer',
89 logoUrl: options.manager.logoUrl,
90 footerEntries: options.manager.footerEntries,
91 errorContent: ['Unable to send ticket.'],
92 };
93 th.navLinks(pagePathLevel, ctx, htmlOptions);
94 sessionNavLinks(pagePathLevel, ctx, htmlOptions);
95 const content = [
96 mainContent(ctx),
97 ];
98 return th.htmlPage(pagePathLevel, ctx, htmlOptions, content);
99 };