Files
2026-11-30 16:32:37 +00:00

595 lines
22 KiB
Plaintext

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Opening <%= serviceLabel %>...</title>
<link id="favicon" rel="icon" type="<%= (typeof branding !== 'undefined' && branding && branding.faviconMimeType) ? branding.faviconMimeType : 'image/webp' %>" sizes="<%= (typeof branding !== 'undefined' && branding && branding.faviconMimeType === 'image/svg+xml') ? 'any' : '16x16' %>" href="<%= (typeof branding !== 'undefined' && branding && branding.faviconUrl) ? branding.faviconUrl : ((typeof branding !== 'undefined' && branding && branding.logoUrl) ? branding.logoUrl : '/images/adpanel-dark.svg') %>" />
<link rel="alternate icon" type="<%= (typeof branding !== 'undefined' && branding && branding.faviconMimeType) ? branding.faviconMimeType : 'image/webp' %>" href="<%= (typeof branding !== 'undefined' && branding && branding.faviconUrl) ? branding.faviconUrl : ((typeof branding !== 'undefined' && branding && branding.logoUrl) ? branding.logoUrl : '/images/adpanel-dark.svg') %>" />
<style nonce="<%= cspNonce %>">
:root {
color-scheme: light;
}
html, body {
margin: 0;
padding: 0;
font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
background: #0b1220;
color: #e7eefc;
height: 100%;
}
.wrap {
min-height: 100%;
display: grid;
place-items: center;
padding: 24px;
box-sizing: border-box;
}
.card {
width: min(560px, 100%);
background: linear-gradient(160deg, rgba(18, 28, 48, 0.96), rgba(13, 20, 34, 0.98));
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 16px;
padding: 24px;
box-sizing: border-box;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.35);
}
.title {
font-size: 20px;
font-weight: 700;
margin: 0 0 8px;
letter-spacing: 0.2px;
}
.subtitle {
margin: 0;
color: #b6c5e5;
font-size: 14px;
line-height: 1.55;
}
.status {
margin-top: 18px;
display: flex;
align-items: center;
gap: 10px;
color: #d5e2ff;
font-size: 14px;
}
.spinner {
width: 16px;
height: 16px;
border-radius: 999px;
border: 2px solid rgba(255, 255, 255, 0.26);
border-top-color: #8fc8ff;
animation: spin 0.9s linear infinite;
}
.error {
margin-top: 16px;
display: none;
color: #ffd4d4;
background: rgba(255, 80, 80, 0.14);
border: 1px solid rgba(255, 80, 80, 0.28);
border-radius: 10px;
padding: 10px 12px;
font-size: 13px;
line-height: 1.5;
white-space: pre-wrap;
word-break: break-word;
}
.actions {
margin-top: 16px;
display: none;
gap: 10px;
flex-wrap: wrap;
}
.btn {
border: 0;
border-radius: 10px;
padding: 10px 14px;
color: #eaf1ff;
background: #2a3c5f;
text-decoration: none;
font-size: 13px;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 8px;
}
.btn.secondary {
background: #192742;
border: 1px solid rgba(255, 255, 255, 0.14);
}
/* Inline identity verification for the pgAdmin step-up challenge. The
credential is sent only in the JSON POST body and never rendered. */
.stepup {
margin-top: 16px;
display: none;
}
.stepup.show {
display: block;
}
.stepup-label {
display: block;
margin-bottom: 6px;
color: #b6c5e5;
font-size: 13px;
}
.stepup-input {
width: 100%;
box-sizing: border-box;
color: #e7eefc;
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 10px;
padding: 10px 12px;
font-size: 13px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
/* One-time display of credentials reset by backend recovery. Values are
injected via textContent only (never markup, URLs, storage, or logs). */
.recover-overlay {
position: fixed;
inset: 0;
background: rgba(4, 8, 16, 0.78);
display: none;
place-items: center;
padding: 24px;
box-sizing: border-box;
z-index: 50;
}
.recover-overlay.show {
display: grid;
}
.recover-warning {
margin-top: 12px;
color: #ffe1a8;
background: rgba(255, 176, 32, 0.12);
border: 1px solid rgba(255, 176, 32, 0.35);
border-radius: 10px;
padding: 10px 12px;
font-size: 13px;
line-height: 1.5;
}
.recover-note {
margin-top: 12px;
color: #b6c5e5;
font-size: 13px;
line-height: 1.5;
white-space: pre-wrap;
word-break: break-word;
}
.cred-rows {
margin-top: 14px;
display: grid;
gap: 10px;
}
.cred-row {
display: flex;
gap: 8px;
align-items: stretch;
}
.cred-value {
flex: 1;
min-width: 0;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 10px;
padding: 10px 12px;
font-size: 13px;
word-break: break-all;
align-self: center;
}
.cred-actions {
margin-top: 18px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="wrap">
<div class="card" data-service="<%= service %>">
<h1 class="title">Opening <%= serviceLabel %></h1>
<p class="subtitle">Issuing a short-lived, cookie-scoped session for this browser and preparing a safe session for this tab.</p>
<div class="status"><span class="spinner"></span><span id="statusText">Requesting secure token...</span></div>
<div id="errorBox" class="error"></div>
<div id="actionBox" class="actions">
<button id="retryBtn" class="btn" type="button">Retry</button>
<a class="btn secondary" href="/settings">Back to Settings</a>
</div>
</div>
</div>
<div class="recover-overlay" id="recoverOverlay" role="dialog" aria-modal="true" aria-labelledby="recoverTitle">
<div class="card">
<h2 class="title" id="recoverTitle">pgAdmin credentials were reset during recovery</h2>
<p class="subtitle">Backend recovery reset the pgAdmin admin account. The new login is below — it is sealed on the server and can be re-displayed later via the "Show saved pgAdmin login" button in the panel settings.</p>
<div class="recover-warning">Save it now — the previous password no longer works.</div>
<div class="recover-note" id="recoverNote" style="display: none;"></div>
<div class="cred-rows">
<div class="cred-row">
<div class="cred-value" id="recoverEmail"></div>
<button class="btn secondary" type="button" id="recoverCopyEmail">Copy email</button>
</div>
<div class="cred-row">
<div class="cred-value" id="recoverPassword"></div>
<button class="btn secondary" type="button" id="recoverCopyPassword">Copy password</button>
</div>
</div>
<div class="cred-actions">
<button class="btn" type="button" id="recoverOk">I've saved the new password</button>
</div>
</div>
</div>
<script nonce="<%= cspNonce %>">
(function () {
'use strict';
const card = document.querySelector('.card');
const service = String(card?.dataset?.service || '').trim();
// Session-bound CSRF token for the access-token POST below, issued at
// render time; refreshed once from the database settings API if it has
// expired by the time an attempt runs.
let actionToken = '<%= locals.actionToken || "" %>';
const statusText = document.getElementById('statusText');
const errorBox = document.getElementById('errorBox');
const actionBox = document.getElementById('actionBox');
const retryBtn = document.getElementById('retryBtn');
const recoverOverlay = document.getElementById('recoverOverlay');
const recoverEmailEl = document.getElementById('recoverEmail');
const recoverPasswordEl = document.getElementById('recoverPassword');
const recoverNoteEl = document.getElementById('recoverNote');
const recoverCopyEmail = document.getElementById('recoverCopyEmail');
const recoverCopyPassword = document.getElementById('recoverCopyPassword');
const recoverOk = document.getElementById('recoverOk');
// One-time display of credentials reset by a backend recovery. The
// values arrive only in this page's no-store JSON response from the
// access-token POST; they are injected via textContent — never into
// markup, URLs, storage, or logs — and scrubbed once acknowledged.
function showRecoveredCredentialsModal(creds, note) {
return new Promise(function (resolve) {
if (!creds || !recoverOverlay || !recoverOk || !recoverEmailEl || !recoverPasswordEl) {
resolve();
return;
}
recoverEmailEl.textContent = String(creds.email || '');
recoverPasswordEl.textContent = String(creds.password || '');
if (note) {
recoverNoteEl.textContent = String(note);
recoverNoteEl.style.display = 'block';
} else {
recoverNoteEl.textContent = '';
recoverNoteEl.style.display = 'none';
}
recoverOverlay.classList.add('show');
let acknowledged = false;
const acknowledge = function () {
if (acknowledged) return;
acknowledged = true;
recoverEmailEl.textContent = '';
recoverPasswordEl.textContent = '';
recoverOverlay.classList.remove('show');
recoverOk.removeEventListener('click', acknowledge);
resolve();
};
recoverOk.addEventListener('click', acknowledge);
});
}
async function copySecretValue(value, btn) {
if (!btn || !value) return;
const original = btn.textContent;
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(value);
} else {
// The async clipboard API needs a secure context; fall back to a
// transient textarea that is removed immediately after the copy.
const ta = document.createElement('textarea');
ta.value = value;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
ta.remove();
}
btn.textContent = 'Copied';
setTimeout(function () { btn.textContent = original; }, 1500);
} catch (err) {
btn.textContent = original;
}
}
if (recoverCopyEmail) {
recoverCopyEmail.addEventListener('click', function () {
copySecretValue(recoverEmailEl.textContent, recoverCopyEmail);
});
}
if (recoverCopyPassword) {
recoverCopyPassword.addEventListener('click', function () {
copySecretValue(recoverPasswordEl.textContent, recoverCopyPassword);
});
}
async function fetchFreshActionToken() {
try {
const res = await fetch('/api/settings/database', {
headers: { 'Accept': 'application/json' }
});
if (!res.ok) return null;
const data = await res.json().catch(function () { return null; });
return (data && data.actionTokens && data.actionTokens.accessToken) || null;
} catch (err) {
return null;
}
}
// credential: optional pgAdmin step-up payload ({ currentPassword } or
// { twoFactorCode }) — the server reads exactly these body fields.
// phpMyAdmin and grant-backed pgAdmin opens post { service } only.
function postAccessToken(credential) {
const body = { service };
if (credential && typeof credential === 'object') {
if (typeof credential.currentPassword === 'string') body.currentPassword = credential.currentPassword;
if (typeof credential.twoFactorCode === 'string') body.twoFactorCode = credential.twoFactorCode;
}
return fetch('/api/settings/database/access-token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-action-token': actionToken
},
body: JSON.stringify(body)
});
}
// Identity verification for the pgAdmin credential step-up (403
// password-required / 2fa-required / invalid-password / invalid-2fa).
// The entered credential lives only in this closure, is re-attached to
// every retry (including a mid-entry action-token refresh), and is
// never written to markup, storage, or logs. phpmyadmin never gets
// this challenge from the server, and the service guard below keeps
// the form off the phpmyadmin page regardless.
let stepUpCredential = null;
let stepUpBox = null;
let stepUpLabel = null;
let stepUpInput = null;
let stepUpMsg = null;
let stepUpVerifyBtn = null;
let stepUpMode = '';
function stepUpModeFor(payload) {
const security = payload && payload.revokeSecurity;
if (security && (security.authMethod === '2fa' || security.requiresTwoFactor)) return '2fa';
return 'password';
}
function hideStepUpForm() {
if (stepUpBox) stepUpBox.classList.remove('show');
}
function showStepUpFormError(msg) {
if (!stepUpMsg) return;
if (msg) {
stepUpMsg.textContent = msg;
stepUpMsg.style.display = 'block';
} else {
stepUpMsg.textContent = '';
stepUpMsg.style.display = 'none';
}
}
function submitStepUp() {
if (!stepUpInput) return;
const value = stepUpInput.value;
// The server requires exactly six digits in 2FA mode and bounces any
// other payload back as a bare challenge with no message, so the
// malformed case must be caught here to keep inline feedback.
if (!value || (stepUpMode === '2fa' && !/^\d{6}$/.test(value))) {
showStepUpFormError(stepUpMode === '2fa'
? 'Enter the 6-digit code from your authenticator app.'
: 'Enter your current password.');
return;
}
stepUpCredential = stepUpMode === '2fa'
? { twoFactorCode: value }
: { currentPassword: value };
start();
}
function showStepUpForm(payload) {
if (service !== 'pgadmin') return;
const mode = stepUpModeFor(payload);
if (!stepUpBox) {
stepUpBox = document.createElement('div');
stepUpBox.className = 'stepup';
stepUpLabel = document.createElement('label');
stepUpLabel.className = 'stepup-label';
stepUpLabel.htmlFor = 'dbOpenStepUpInput';
stepUpInput = document.createElement('input');
stepUpInput.id = 'dbOpenStepUpInput';
stepUpInput.className = 'stepup-input';
stepUpInput.autocomplete = 'off';
stepUpInput.addEventListener('keydown', function (ev) {
if (ev.key === 'Enter') {
ev.preventDefault();
submitStepUp();
}
});
stepUpMsg = document.createElement('div');
stepUpMsg.className = 'error';
stepUpVerifyBtn = document.createElement('button');
stepUpVerifyBtn.type = 'button';
stepUpVerifyBtn.className = 'btn';
stepUpVerifyBtn.textContent = 'Verify & continue';
stepUpVerifyBtn.addEventListener('click', submitStepUp);
stepUpBox.appendChild(stepUpLabel);
stepUpBox.appendChild(stepUpInput);
stepUpBox.appendChild(stepUpMsg);
stepUpBox.appendChild(stepUpVerifyBtn);
if (actionBox && actionBox.parentNode) {
actionBox.parentNode.insertBefore(stepUpBox, actionBox);
} else if (card) {
card.appendChild(stepUpBox);
}
}
if (mode !== stepUpMode) {
stepUpMode = mode;
stepUpInput.value = '';
}
if (mode === '2fa') {
stepUpLabel.textContent = 'Authenticator code (6 digits)';
stepUpInput.type = 'text';
stepUpInput.inputMode = 'numeric';
stepUpInput.autocomplete = 'one-time-code';
stepUpInput.setAttribute('maxlength', '6');
stepUpInput.placeholder = '123456';
} else {
stepUpLabel.textContent = 'Current password';
stepUpInput.type = 'password';
stepUpInput.removeAttribute('inputmode');
stepUpInput.autocomplete = 'current-password';
stepUpInput.removeAttribute('maxlength');
stepUpInput.placeholder = '';
}
// The server's message is shown inline ONLY for invalid-password /
// invalid-2fa retries; the initial challenge shows just the form.
const retryError = payload && (payload.code === 'invalid-password' || payload.code === 'invalid-2fa') &&
typeof payload.error === 'string' ? payload.error : '';
showStepUpFormError(retryError);
hideError();
stepUpBox.classList.add('show');
setStatus('Identity verification required.');
}
// At most one attempt in flight: the Retry button, the Verify button
// and the input's Enter key all funnel through start(), and stacked
// attempts would double-POST — superseding each other's proxy token
// and racing a second readiness run's 503 against the recovery modal.
let attemptInFlight = false;
async function start() {
if (attemptInFlight) return;
attemptInFlight = true;
try {
await runAttempt();
} finally {
attemptInFlight = false;
}
}
async function runAttempt() {
if (!service) {
showError('Missing service identifier.');
return;
}
setStatus('Requesting secure token...');
hideError();
// At most one token refresh per attempt, and ONLY for action-token
// rejections: the pgAdmin step-up challenge is also a 403 and must
// surface as the verification form, never be swallowed by a silent
// refresh. The action token is repeatable (TTL 300s), so it stays
// valid for verification retries unless it expires mid-entry.
let refreshed = false;
try {
let res = await postAccessToken(stepUpCredential);
let payload = await res.json().catch(function () { return {}; });
if (res.status === 403 && !refreshed && payload &&
(payload.code === 'ACTION_TOKEN_INVALID' ||
(typeof payload.error === 'string' && /action token/i.test(payload.error)))) {
refreshed = true;
const fresh = await fetchFreshActionToken();
if (fresh) {
actionToken = fresh;
setStatus('Refreshing security token...');
res = await postAccessToken(stepUpCredential);
payload = await res.json().catch(function () { return {}; });
}
}
// pgAdmin credential step-up: no live grant and no credential yet,
// or the previous one was rejected. Show the inline verification
// form instead of the generic error box and wait for resubmission;
// recovery must not run and no credential is received until the
// step-up passes.
const code = payload && typeof payload.code === 'string' ? payload.code : '';
if (res.status === 403 &&
(code === 'password-required' || code === '2fa-required' ||
code === 'invalid-password' || code === 'invalid-2fa')) {
showStepUpForm(payload);
return;
}
if (!res.ok || !payload || !payload.url) {
// A recovery may have reset the pgAdmin admin password even though
// the backend is still unhealthy (503): the one-time credential
// handoff rides this response, so surface it instead of leaving
// only a generic failure message behind.
if (payload && payload.recoveredCredentials) {
await showRecoveredCredentialsModal(payload.recoveredCredentials, (payload && payload.error) || '');
}
throw new Error(payload?.error || 'Failed to generate secure link.');
}
// Success path: the readiness probe may have reset the pgAdmin admin
// password. Show the one-time handoff BEFORE redirecting (the same
// sealed pair is also retrievable via "Show saved pgAdmin login" in
// the panel settings).
if (payload.recoveredCredentials) {
await showRecoveredCredentialsModal(payload.recoveredCredentials, '');
}
setStatus('Redirecting to secure session...');
window.location.replace(payload.url);
} catch (err) {
showError(err && err.message ? err.message : 'Unexpected error while opening database tool.');
}
}
function setStatus(msg) {
if (statusText) statusText.textContent = msg;
}
function showError(msg) {
// The verification form and the generic failure box are mutually
// exclusive; the Retry button must remain the way back into start().
hideStepUpForm();
setStatus('Unable to open secure session.');
if (errorBox) {
errorBox.style.display = 'block';
errorBox.textContent = msg;
}
if (actionBox) actionBox.style.display = 'flex';
}
function hideError() {
if (errorBox) {
errorBox.style.display = 'none';
errorBox.textContent = '';
}
if (actionBox) actionBox.style.display = 'none';
}
if (retryBtn) {
retryBtn.addEventListener('click', function () {
start();
});
}
start();
})();
</script>
</body>
</html>