4 * This renders the administrative view for an account,
5 * allowing for adding profile URIs, custom scope bundles,
6 * and management of issued tokens.
9 const th
= require('./template-helper');
10 const { sessionNavLinks
} = require('@squeep/authentication-module');
13 function renderProfileLI(profile
) {
14 return `\t<li><a class="uri" id="${profile}">${profile}</a></li>`;
18 function renderProfileScopeIndicator(profile
, scope
, selected
) {
19 const checked
= selected
? ' checked' : '';
21 \t\t\t<input type="checkbox" id="${profile}-${scope}" name="scopes-${profile}[]" value="${scope}"${checked}>
25 function renderScopeRow(scope
, details
, profiles
) {
26 return `\t<tr class="scope">
27 ${(profiles || []).map((profile) => renderProfileScopeIndicator(profile, scope, details.profiles.includes(profile))).join('\n')}
28 \t\t<th scope="row"><label>${scope}</label></th>
29 \t\t<td class="description">${details.description}</td>
30 \t\t<td>${details.application}</td>
31 \t\t<td class="scope-actions">` +
32 (details
.isManuallyAdded
? `
33 \t\t\t<button type="submit" name="action" value="delete-scope-${encodeURIComponent(scope)}">Delete</button>
40 function renderProfileHeader(profile
) {
41 return `<th scope="col" class="vertical uri">
47 function scopeIndexTable(scopeIndex
, profiles
) {
51 ${(profiles || []).map((profile) => renderProfileHeader(profile)).join('\n')}
52 \t\t<th scope="col">Scope</th>
53 \t\t<th scope="col">Description</th>
54 \t\t<th scope="col">Application</th>
55 \t\t<th scope="col" class="scope-actions"></th>
59 ${Object.entries(scopeIndex).sort(th.scopeCompare).map(([scope, details]) => renderScopeRow(scope, details, profiles)).join('\n')}
64 function _tokenType(token
) {
66 return 'ticket-token';
74 function renderTokenRow(token
) {
75 const createdTitle
= token
.refreshed
? 'Refreshed At' : 'Created At';
76 const createdDate
= token
.refreshed
? token
.refreshed : token
.created
;
78 <td>${_tokenType(token)}</td>
79 \t\t\t<td class="uri">${token.clientId}</td>
80 \t\t\t<td class="uri">${token.profile}</td>
81 <td class="scope">${(token.scopes || []).join(', ')}</td>
82 \t\t\t<td class="code">${token.codeId}</td>
83 \t\t\t<td>${th.timeElement(createdDate, { title: createdTitle })}</td>
84 \t\t\t<td>${th.timeElement(token.expires, { title: 'Expires At' })}</td>
85 \t\t\t<td>${token.isRevoked}</td>
86 <td>${token.resource ? token.resource : ''}</td>
88 token
.isRevoked
? '' : `
89 \t\t\t\t<button type="submit" name="action" value="revoke-${token.codeId}">Revoke</button>`) + `
94 function noTokensRows() {
96 \t\t\t<td colspan="10" class="centered">(No active or recent tokens.)</td>
100 function tokenTable(tokens
) {
101 const tokenRows
= tokens
?.length
? tokens
.map((token
) => renderTokenRow(token
)) : noTokensRows();
102 const formOpen
= tokens
?.length
? '<form method="POST">\n' : '';
103 const formClose
= tokens
?.length
? '\n</form>' : '';
104 return `${formOpen}<table>
107 \t\t\t<th scope="col">Type</th>
108 \t\t\t<th scope="col">Client Identifier / Ticket Subject</th>
109 \t\t\t<th scope="col">Profile</th>
110 \t\t\t<th scope="col">Scopes</th>
111 \t\t\t<th scope="col">Code</th>
112 \t\t\t<th scope="col">Created or Refreshed</th>
113 \t\t\t<th scope="col">Expires</th>
114 \t\t\t<th scope="col">Revoked</th>
115 \t\t\t<th scope="col">Resource</th>
116 \t\t\t<th scope="col"></th>
120 ${tokenRows.join('\n')}
122 </table>${formClose}`;
125 function mainContent(ctx
) {
126 const profileList
= (ctx
.profilesScopes
?.profiles
|| []).map((p
) => renderProfileLI(p
)).join('\n');
132 \t<form method="POST">
134 \t\t\t<legend>Add New Profile</legend>
136 \t\t\t\tThe profile identity URIs associated with this account.
137 \t\t\t\tEach must indicate this service as the authorization endpoint.
140 \t\t\t<label for="profile">Profile URL:</label>
141 \t\t\t<input type="url" id="profile" name="profile" size="96">
142 \t\t\t<button type="submit" name="action" value="new-profile">Add Profile</button>
150 \t\t\t\tScopes Associated with Profiles for Convenience
152 \t\t<form method="POST">
154 \t\t\t\t<legend>Manage Additional Profile Scope Availability</legend>
156 \t\t\t\t\tThis table lists pre-defined scopes which you can choose to add to any authorization request, whether the client requested them or not.
157 \t\t\t\t\tSelecting one for a profile makes it conveniently available for quick inclusion when authorizing a client request.
158 \t\t\t\t\tAny scope not in this table or not selected for a profile can always be added in the ad hoc field on the authorization request.
161 ${scopeIndexTable(ctx.profilesScopes.scopeIndex, ctx.profilesScopes.profiles)}
162 \t\t\t\t<button type="submit" name="action" value="save-scopes">Save</button>
166 \t\t<form method="POST">
168 \t\t\t\t<legend>Add New Scope</legend>
169 \t\t\t\t<label for="scope">Scope:</label>
170 \t\t\t\t<input type="text" id="scope" name="scope">
171 \t\t\t\t<label for="description">Description:</label>
172 \t\t\t\t<input type="text" id="description" name="description">
173 \t\t\t\t<label for="application">Application:</label>
174 \t\t\t\t<input type="text" id="application" name="application">
175 \t\t\t\t<button type="submit" name="action" value="new-scope">Add Scope</button>
182 ${tokenTable(ctx.tokens)}
189 * @param {Object} ctx
190 * @param {Object} ctx.profilesScopes.scopeIndex
191 * @param {String[]} ctx.profilesScopes.profiles
192 * @param {Object[]} ctx.tokens
193 * @param {Object} options
194 * @param {Object} options.manager
195 * @param {String} options.manager.pageTitle
196 * @param {String} options.manager.logoUrl
197 * @param {String[]} options.manager.footerEntries
200 module
.exports
= (ctx
, options
) => {
201 const pagePathLevel
= 1;
202 const htmlOptions
= {
203 pageIdentifier: 'admin',
204 pageTitle: options
.manager
.pageTitle
+ ' - Admin',
205 logoUrl: options
.manager
.logoUrl
,
206 footerEntries: options
.manager
.footerEntries
,
208 th
.navLinks(pagePathLevel
, ctx
, htmlOptions
);
209 sessionNavLinks(1, ctx
, htmlOptions
);
213 return th
.htmlPage(1, ctx
, htmlOptions
, content
);