-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.js
More file actions
196 lines (165 loc) · 7.13 KB
/
Copy pathviews.js
File metadata and controls
196 lines (165 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import express from 'express';
import { requireAuth, authEnabled, legacyAuthEnabled, superTokensEnabled, APP_LOGIN_USER, APP_LOGIN_PASSWORD } from '../lib/middleware.js';
import { logger } from '../lib/Delivera-Server-Logging-Utility.js';
import { buildReportUrlFromContext, readReportContextFromSession } from '../lib/Delivera-User-Context-SSOT.js';
import { PUBLIC_DIR } from '../lib/Delivera-Config-Env-Services-Core-SSOT.js';
const PUBLIC_ROOT = PUBLIC_DIR;
const router = express.Router();
const LOGIN_RATE_LIMIT_WINDOW_MS = 15 * 60 * 1000; // 15 min
const LOGIN_RATE_LIMIT_MAX_ATTEMPTS = 5;
const loginFailuresByIp = new Map(); // ip -> { count, resetAt }
const DEFAULT_APP_LANDING = '/governance';
function sendAppHtml(res, fileName) {
res.setHeader('Cache-Control', 'no-store, max-age=0');
return res.sendFile(fileName, { root: PUBLIC_ROOT });
}
function resolveExplicitRedirect(explicitRedirect = '') {
const raw = String(explicitRedirect || '').trim();
if (!raw || !raw.startsWith('/')) return '';
if (raw.startsWith('/report')) return raw;
if (raw.startsWith('/governance') || raw.startsWith('/brief')) return raw.startsWith('/brief') ? '/governance' : raw;
if (raw.startsWith('/current-sprint') || raw.startsWith('/sprints')) return raw;
if (raw.startsWith('/settings')) return raw;
if (raw.startsWith('/dashboard') || raw.startsWith('/home')) return raw;
return '';
}
function getPreferredAppRedirect(req, explicitRedirect = '') {
const safeRedirect = resolveExplicitRedirect(explicitRedirect);
if (safeRedirect) return safeRedirect;
return buildReportUrlFromContext(readReportContextFromSession(req) || {}, DEFAULT_APP_LANDING);
}
/** @deprecated use getPreferredAppRedirect */
function getPreferredReportRedirect(req, explicitRedirect = '') {
return getPreferredAppRedirect(req, explicitRedirect);
}
// Login: first screen for unauthenticated users
router.get('/', (req, res) => {
if (superTokensEnabled && !legacyAuthEnabled) return res.redirect('/auth');
if (!authEnabled) return res.redirect(getPreferredReportRedirect(req));
if (req.session && req.session.user) return res.redirect(getPreferredReportRedirect(req, req.query.redirect));
res.sendFile('login.html', { root: PUBLIC_ROOT });
});
router.get('/login', (req, res) => {
if (superTokensEnabled && !legacyAuthEnabled) return res.redirect('/auth');
if (!authEnabled) return res.redirect(getPreferredReportRedirect(req));
if (req.session && req.session.user) return res.redirect(getPreferredReportRedirect(req, req.query.redirect));
res.sendFile('login.html', { root: PUBLIC_ROOT });
});
router.post('/login', (req, res) => {
if (superTokensEnabled && !legacyAuthEnabled) return res.redirect('/auth');
if (!authEnabled) return res.redirect(DEFAULT_APP_LANDING);
const redirect = getPreferredReportRedirect(req, req.body.redirect);
const ip = req.ip || req.connection?.remoteAddress || 'unknown';
const now = Date.now();
let record = loginFailuresByIp.get(ip);
if (record && now > record.resetAt) {
loginFailuresByIp.delete(ip);
record = null;
}
if (record && record.count >= LOGIN_RATE_LIMIT_MAX_ATTEMPTS) {
logger.warn('Login rate limit exceeded', { ip });
return res.redirect(`/login?redirect=${encodeURIComponent(redirect)}&error=invalid`);
}
const honeypot = (req.body.website || '').trim();
if (honeypot) {
logger.warn('Login honeypot filled, rejecting', { ip });
return res.redirect(`/login?redirect=${encodeURIComponent(redirect)}&error=bot`);
}
const username = (req.body.username || '').trim();
const password = req.body.password || '';
if (username !== APP_LOGIN_USER || password !== APP_LOGIN_PASSWORD) {
if (!record) loginFailuresByIp.set(ip, { count: 1, resetAt: now + LOGIN_RATE_LIMIT_WINDOW_MS });
else record.count += 1;
return res.redirect(`/login?redirect=${encodeURIComponent(redirect)}&error=invalid`);
}
loginFailuresByIp.delete(ip);
req.session.user = username;
req.session.lastActivity = Date.now();
return res.redirect(redirect);
});
router.post('/logout', (req, res) => {
if (req.session && typeof req.session.destroy === 'function') {
req.session.destroy(() => {
res.redirect('/login');
});
return;
}
res.redirect(superTokensEnabled && !legacyAuthEnabled ? '/auth' : '/login');
});
/**
* GET /report - Serve the main report page (protected when auth enabled)
*/
router.get('/report', requireAuth, (req, res) => {
sendAppHtml(res, 'report.html');
});
// Legacy alias — /home → /dashboard 301 (keep for bookmarks and nav history).
router.get('/home', requireAuth, (req, res) => {
res.redirect(301, '/dashboard');
});
router.get('/dashboard', requireAuth, (req, res) => {
sendAppHtml(res, 'home.html');
});
// Legacy alias — /backlog-intake merged into /value-delivery (2025-05). Keep for bookmarks.
router.get('/backlog-intake', requireAuth, (req, res) => {
res.redirect('/value-delivery');
});
// Legacy alias — /roadmap → /program-increment (2025-03). Keep for bookmarks.
router.get('/roadmap', requireAuth, (req, res) => {
res.redirect('/program-increment');
});
router.get('/program-increment', requireAuth, (req, res) => {
res.redirect(302, '/leadership');
});
router.get('/value-delivery', requireAuth, (req, res) => {
res.redirect(302, '/report');
});
router.get('/risks-blockers', requireAuth, (req, res) => {
res.redirect(302, '/current-sprint#stuck-card');
});
router.get('/teams', requireAuth, (req, res) => {
res.redirect(302, '/current-sprint');
});
router.get('/settings', requireAuth, (req, res) => {
sendAppHtml(res, 'settings.html');
});
router.get('/actions', requireAuth, (req, res) => {
sendAppHtml(res, 'actions.html');
});
/**
* GET /reports - backward-compatible alias for report page
*/
router.get('/reports', requireAuth, (req, res) => {
const suffix = req.url.includes('?') ? req.url.slice(req.url.indexOf('?')) : '';
res.redirect(301, `/report${suffix}`);
});
/**
* GET /current-sprint - Current sprint transparency page (squad view)
*/
router.get('/current-sprint', requireAuth, (req, res) => {
sendAppHtml(res, 'current-sprint.html');
});
// Legacy alias — /sprints → /current-sprint (2025-01). Keep for bookmarks.
router.get('/sprints', requireAuth, (req, res) => {
res.redirect('/current-sprint');
});
/**
* GET /leadership - merged into Brief decision snapshot (bookmark-safe redirect)
*/
router.get('/leadership', requireAuth, (req, res) => {
res.redirect(302, '/governance#decision-snapshot');
});
/**
* GET /governance - Weekly Delivery Intelligence Brief (governance layer)
*/
router.get('/governance', requireAuth, (req, res) => {
sendAppHtml(res, 'governance.html');
});
// Alias — /brief → /governance for memorable links.
router.get('/brief', requireAuth, (req, res) => {
res.redirect('/governance');
});
// Legacy alias — /sprint-leadership → /leadership (2024-12). Keep for bookmarks.
router.get('/sprint-leadership', requireAuth, (req, res) => {
res.redirect(302, '/governance#decision-snapshot');
});
export default router;