diff --git a/src/proxyauth.js b/src/proxyauth.js index 08aa24414..3c72e6ab3 100644 --- a/src/proxyauth.js +++ b/src/proxyauth.js @@ -12,6 +12,7 @@ const apps = require('./apps.js'), basicAuth = require('basic-auth'), constants = require('./constants.js'), debug = require('debug')('box:proxyAuth'), + ejs = require('ejs'), express = require('express'), fs = require('fs'), hat = require('./hat.js'), @@ -23,6 +24,7 @@ const apps = require('./apps.js'), path = require('path'), paths = require('./paths.js'), safe = require('safetydance'), + translation = require('./translation.js'), users = require('./users.js'); let gHttpServer = null; @@ -65,15 +67,33 @@ function loginPage(req, res, next) { const appId = req.headers['x-app-id'] || ''; if (!appId) return next(new HttpError(503, 'Nginx misconfiguration')); - apps.get(appId, function (error, app) { - if (error) return next(new HttpError(503, error.message)); + translation.getTranslations(function (error, translationAssets) { + if (error) return next(new HttpError(500, 'No translation found')); - const title = app.label || app.manifest.title; + const raw = safe.fs.readFileSync(path.join(paths.DASHBOARD_DIR, 'templates/proxyauth-login.ejs'), 'utf8'); + if (raw === null) return next(new HttpError(500, 'Login template not found')); - apps.getIconPath(app, {}, function (error, iconPath) { - const icon = 'data:image/png;base64,' + safe.fs.readFileSync(iconPath || '', 'base64'); + const translatedContent = translation.translate(raw, translationAssets.translations || {}, translationAssets.fallback || {}); + var finalContent = ''; - return res.render(path.join(paths.DASHBOARD_DIR, 'templates/proxyauth-login.ejs'), { title, icon }); + apps.get(appId, function (error, app) { + if (error) return next(new HttpError(503, error.message)); + + const title = app.label || app.manifest.title; + + apps.getIconPath(app, {}, function (error, iconPath) { + const icon = 'data:image/png;base64,' + safe.fs.readFileSync(iconPath || '', 'base64'); + + try { + finalContent = ejs.render(translatedContent, { title, icon }); + } catch (e) { + debug('Error rendering proxyauth-login.ejs', e); + return next(new HttpError(500, 'Login template error')); + } + + res.set('Content-Type', 'text/html'); + return res.send(finalContent); + }); }); }); } @@ -165,8 +185,6 @@ function initializeAuthwallExpressSync() { var router = new express.Router(); router.del = router.delete; // amend router.del for readability further on - app.set('view engine', 'ejs'); - app .use(middleware.timeout(REQUEST_TIMEOUT)) .use(middleware.cookieParser())