Move oidc views to ejs templating

This commit is contained in:
Johannes Zellner
2025-07-11 14:26:57 +02:00
parent fb424d28b9
commit 93e48fabdf
5 changed files with 53 additions and 60 deletions

View File

@@ -20,6 +20,7 @@ const assert = require('assert'),
dashboard = require('./dashboard.js'),
debug = require('debug')('box:oidcserver'),
dns = require('./dns.js'),
ejs = require('ejs'),
express = require('express'),
eventlog = require('./eventlog.js'),
fs = require('fs'),
@@ -271,20 +272,22 @@ async function cleanupExpired() {
}
}
const TEMPLATE_LOGIN = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'oidc_login.html'), 'utf-8');
const TEMPLATE_INTERACTION_CONFIRM = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'oidc_interaction_confirm.html'), 'utf8');
const TEMPLATE_INTERACTION_ABORT = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'oidc_interaction_abort.html'), 'utf8');
const TEMPLATE_ERROR = fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'oidc_error.html'), 'utf8');
async function renderError(error) {
const data = {
ICON_URL: '/api/v1/cloudron/avatar',
NAME: 'Cloudron',
ERROR_MESSAGE: error.error_description || error.error_detail || error.message || 'Internal error',
FOOTER: marked.parse(await branding.renderFooter())
iconUrl: '/api/v1/cloudron/avatar',
name: 'Cloudron',
errorMessage: error.error_description || error.error_detail || error.message || 'Internal error',
footer: marked.parse(await branding.renderFooter())
};
debug('renderError: %o', error);
let html = fs.readFileSync(path.join(__dirname, '/../dashboard/dist/oidc_error.html'), 'utf8');
for (const key in data) {
html = html.replaceAll(`##${key}##`, data[key]);
}
return html;
return ejs.render(TEMPLATE_ERROR, data);
}
async function renderInteractionPage(req, res, next) {
@@ -302,53 +305,43 @@ async function renderInteractionPage(req, res, next) {
res.set('Content-Type', 'text/html');
if (prompt.name === 'login') {
const options = {
SUBMIT_URL: `${ROUTE_PREFIX}/interaction/${uid}/login`,
ICON_URL: '/api/v1/cloudron/avatar',
NAME: client.name || await branding.getCloudronName(),
FOOTER: marked.parse(await branding.renderFooter()),
NOTE: constants.DEMO ? `This is a demo. Username and password is "${constants.DEMO_USERNAME}"` : '',
const data = {
submitUrl: `${ROUTE_PREFIX}/interaction/${uid}/login`,
iconUrl: '/api/v1/cloudron/avatar',
name: client.name || await branding.getCloudronName(),
footer: marked.parse(await branding.renderFooter()),
note: constants.DEMO ? `This is a demo. Username and password is "${constants.DEMO_USERNAME}"` : '',
};
if (app) {
options.NAME = app.label || app.fqdn;
options.ICON_URL = app.iconUrl;
data.name = app.label || app.fqdn;
data.iconUrl = app.iconUrl;
}
let html = fs.readFileSync(__dirname + '/../dashboard/dist/oidc_login.html', 'utf-8');
for (const key in options) {
html = html.replaceAll(`##${key}##`, options[key]);
}
return res.send(html);
return res.send(ejs.render(TEMPLATE_LOGIN, data));
} else if (prompt.name === 'consent') {
let hasAccess = false;
const options = {
ICON_URL: '/api/v1/cloudron/avatar',
NAME: client.name || '',
FOOTER: marked.parse(await branding.renderFooter())
const data = {
iconUrl: '/api/v1/cloudron/avatar',
name: client.name || '',
footer: marked.parse(await branding.renderFooter())
};
// check if user has access to the app if client refers to an app
if (app) {
const user = await users.getByUsername(session.accountId);
options.NAME = app.label || app.fqdn;
options.ICON_URL = app.iconUrl;
data.name = app.label || app.fqdn;
data.iconUrl = app.iconUrl;
hasAccess = apps.canAccess(app, user);
} else {
hasAccess = true;
}
options.SUBMIT_URL = `${ROUTE_PREFIX}/interaction/${uid}/${hasAccess ? 'confirm' : 'abort'}`;
data.submitUrl = `${ROUTE_PREFIX}/interaction/${uid}/${hasAccess ? 'confirm' : 'abort'}`;
let html = fs.readFileSync(path.join(__dirname, hasAccess ? '/../dashboard/dist/oidc_interaction_confirm.html' : '/../dashboard/dist/oidc_interaction_abort.html'), 'utf8');
for (const key in options) {
html = html.replaceAll(`##${key}##`, options[key]);
}
return res.send(html);
return res.send(ejs.render(hasAccess ? TEMPLATE_INTERACTION_CONFIRM : TEMPLATE_INTERACTION_ABORT, data));
}
}