-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
391 lines (337 loc) · 12.5 KB
/
Copy pathserver.js
File metadata and controls
391 lines (337 loc) · 12.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2025-Present Datadog, Inc.
*/
// Datadog APM initialisation (must be required before other imports)
require('dd-trace').init({
service: 'web-backend',
env: process.env.DD_ENV || 'local',
runtimeMetrics: true,
analytics: true
});
const express = require('express')
const session = require('express-session')
const crypto = require('crypto')
const helmet = require('helmet')
const { Issuer, generators } = require('openid-client')
const lusca = require('lusca')
const rateLimit = require('express-rate-limit')
const app = express()
const port = 3000
// Security headers with Helmet
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'"],
imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", "http://localhost:*", "http://user-management:*"], // Allow OAuth endpoints
fontSrc: ["'self'"],
objectSrc: ["'none'"],
mediaSrc: ["'self'"],
frameSrc: ["'none'"],
formAction: ["'self'", "http://localhost:*", "http://user-management:*"], // Allow OAuth form submissions
},
},
crossOriginEmbedderPolicy: false // Allow iframe embedding if needed
}))
// Session configuration - HttpOnly, SameSite cookies
const isProduction = process.env.NODE_ENV === 'production'
app.use(session({
name: 'sessionId', // Custom session cookie name (avoids default 'connect.sid')
secret: process.env.SESSION_SECRET || 'dev-session-secret-change-in-production',
resave: false,
saveUninitialized: false,
cookie: {
secure: isProduction, // Secure cookies in production (HTTPS required)
httpOnly: true, // Prevent XSS attacks by blocking client-side access
sameSite: 'lax', // CSRF protection while allowing some cross-site requests
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}))
app.use(express.json())
// CSRF protection using Lusca
app.use(lusca({
csrf: {
secret: process.env.CSRF_SECRET || 'dev-csrf-secret-change-in-production',
cookie: true, // Store CSRF token in cookie
header: 'x-csrf-token', // Accept token in header
whitelist: ['/api/app/auth/callback', '/api/app/auth/token'] // Skip CSRF for OAuth callback and token exchange
},
csp: false, // Disable Lusca's CSP (we use Helmet)
xframe: false, // Disable Lusca's X-Frame-Options (we use Helmet)
xssProtection: false, // Disable Lusca's XSS protection (we use Helmet)
nosniff: false, // Disable Lusca's nosniff (we use Helmet)
hsts: false // Disable Lusca's HSTS (we use Helmet)
}))
// OAuth configuration
const OAUTH_ISSUER_INTERNAL = process.env.OAUTH_ISSUER_INTERNAL || 'http://user-management:8080'
const OAUTH_CLIENT_ID = process.env.OAUTH_CLIENT_ID || 'web-ui'
const OAUTH_CLIENT_SECRET = process.env.OAUTH_CLIENT_SECRET || 'stickerlandia-web-ui-secret-2025'
const DEPLOYMENT_HOST_URL = process.env.DEPLOYMENT_HOST_URL || 'http://localhost:8080'
let client = null
let issuerMetadata = null
// Initialize OIDC client
async function initializeOIDC() {
try {
// Discover from internal endpoint
console.info('Discovering OIDC metadata from:', OAUTH_ISSUER_INTERNAL)
const discoveredIssuer = await Issuer.discover(OAUTH_ISSUER_INTERNAL)
console.info('Discovered issuer:', discoveredIssuer.issuer)
console.info('Discovered token endpoint:', discoveredIssuer.token_endpoint)
// Override endpoints to use internal URLs for server-to-server communication
// The discovered metadata has external URLs (OPENIDDICT_ISSUER), but we need
// internal Docker network URLs for token exchange, userinfo, and JWKS calls
issuerMetadata = new Issuer({
...discoveredIssuer.metadata,
token_endpoint: `${OAUTH_ISSUER_INTERNAL}/api/users/v1/connect/token`,
userinfo_endpoint: `${OAUTH_ISSUER_INTERNAL}/api/users/v1/connect/userinfo`,
jwks_uri: `${OAUTH_ISSUER_INTERNAL}/.well-known/jwks`,
})
console.info('Using internal token endpoint:', issuerMetadata.token_endpoint)
client = new issuerMetadata.Client({
client_id: OAUTH_CLIENT_ID,
client_secret: OAUTH_CLIENT_SECRET,
response_types: ['code']
})
console.info('OIDC client initialized')
} catch (error) {
console.error('Failed to initialize OIDC client:', error)
}
}
// Initialize OIDC on startup
initializeOIDC()
// CSRF token endpoint
app.get('/api/app/csrf-token', (req, res) => {
res.json({ csrfToken: res.locals._csrf })
})
// Rate limiting for login attempts
const loginRateLimit = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute window
max: 5, // Max 5 login attempts per IP per minute
message: {
error: 'Too many login attempts, please try again later',
retryAfter: '1 minute'
},
standardHeaders: true, // Include rate limit info in headers
legacyHeaders: false, // Disable legacy X-RateLimit-* headers
skip: (req) => {
// Skip rate limiting in development for easier testing
return process.env.NODE_ENV === 'development' && process.env.SKIP_RATE_LIMIT === 'true'
}
})
// BFF Auth Endpoints
// Step 1: Frontend calls POST /login, BFF generates PKCE and redirects to IdP
app.post('/api/app/auth/login', loginRateLimit, (req, res) => {
if (!client) {
return res.status(500).json({ error: 'OIDC client not initialized' })
}
// Generate PKCE codes
const code_verifier = generators.codeVerifier()
const code_challenge = generators.codeChallenge(code_verifier)
const state = generators.state()
const nonce = generators.nonce()
// Use environment-configured redirect URI
const redirectUri = `${DEPLOYMENT_HOST_URL}/api/app/auth/callback`
// Store PKCE verifier, state, and redirect URI in server session
req.session.oauth = {
code_verifier,
state,
nonce,
redirect_uri: redirectUri
}
// Generate authorization URL using configured redirect URI
const internalAuthUrl = client.authorizationUrl({
scope: 'openid profile email roles',
code_challenge,
code_challenge_method: 'S256',
state,
nonce,
redirect_uri: redirectUri
})
// Extract just the path and query from the authorization URL
const authUrlObj = new URL(internalAuthUrl)
const authUrl = authUrlObj.pathname + authUrlObj.search
// Redirect browser to IdP
res.redirect(authUrl)
})
// Step 3: IdP redirects back to BFF with authorization code
app.get('/api/app/auth/callback', async (req, res) => {
try {
if (!client) {
return res.status(500).send('OIDC client not initialized')
}
const { code, state, iss } = req.query
// If iss parameter is missing but we know what it should be, add it
// This works around the openid-client validation issue
const callbackParams = { code, state }
if (iss) {
callbackParams.iss = iss
} else {
// Add the expected issuer to satisfy openid-client validation
callbackParams.iss = client.issuer.issuer
}
const sessionOAuth = req.session.oauth
if (!sessionOAuth) {
return res.status(400).send('No OAuth session found')
}
// Verify state matches
if (state !== sessionOAuth.state) {
return res.status(400).send('Invalid state parameter')
}
// Exchange authorization code for tokens
const checks = {
code_verifier: sessionOAuth.code_verifier,
nonce: sessionOAuth.nonce,
state: sessionOAuth.state
}
const tokenSet = await client.callback(sessionOAuth.redirect_uri, callbackParams, checks)
console.info('Token exchange successful, received tokens')
// Store tokens in server session
req.session.tokens = {
access_token: tokenSet.access_token,
refresh_token: tokenSet.refresh_token,
id_token: tokenSet.id_token,
expires_at: tokenSet.expires_at
}
// Get user info and store in session
const userinfo = await client.userinfo(tokenSet.access_token)
req.session.user = userinfo
// Clear OAuth temp data
delete req.session.oauth
// Flag that a token is ready for pickup via /api/app/auth/token
req.session.pendingToken = {
access_token: tokenSet.access_token,
expires_at: tokenSet.expires_at
}
res.redirect('/?auth=complete')
} catch (error) {
console.error('OAuth callback failed:', error)
res.status(400).send('Authentication failed')
}
})
// Get current user info (frontend checks this)
app.get('/api/app/auth/user', async (req, res) => {
// Check for Bearer token in Authorization header
const authHeader = req.headers.authorization
if (authHeader && authHeader.startsWith('Bearer ')) {
const token = authHeader.slice(7)
try {
// Validate token with OIDC provider
if (client) {
const userinfo = await client.userinfo(token)
res.json({
user: userinfo,
authenticated: true
})
return
}
} catch (error) {
console.error('Token validation failed:', error)
res.json({ authenticated: false })
return
}
}
// Fallback to session-based auth
if (req.session.user && req.session.tokens) {
res.json({
user: req.session.user,
authenticated: true
})
} else {
res.json({ authenticated: false })
}
})
// One-time token exchange — frontend calls this after ?auth=complete redirect
app.post('/api/app/auth/token', (req, res) => {
const pending = req.session.pendingToken
if (!pending) {
return res.status(404).json({ error: 'No pending token' })
}
// Clear immediately so the token can only be retrieved once
delete req.session.pendingToken
res.json({
access_token: pending.access_token,
expires_at: pending.expires_at
})
})
// Logout - clear session and optionally call IdP logout
app.post('/api/app/auth/logout', (req, res) => {
// Capture id_token before destroying session
const idToken = req.session?.tokens?.id_token
// Clear session and cookie
req.session.destroy((err) => {
if (err) console.error('Session destroy error:', err)
res.clearCookie('sessionId', { path: '/' })
if (idToken && client) {
// Generate IdP logout URL with correct deployment host
const logoutUrl = client.endSessionUrl({
id_token_hint: idToken,
post_logout_redirect_uri: `${DEPLOYMENT_HOST_URL.replace(/\/$/, '')}/`
})
res.redirect(logoutUrl)
} else {
// No IdP session, just redirect to home
res.redirect('/')
}
})
})
// Service mapping table based on docker-compose routes
const SERVICE_MAPPING = {
'/api/users': 'http://user-management:8080',
'/api/awards': 'http://sticker-award:8080',
'/api/stickers': 'http://sticker-catalogue:8080'
}
// Middleware to check authentication
function requireAuth(req, res, next) {
if (!req.session.tokens || !req.session.tokens.access_token) {
return res.status(401).json({ error: 'Not authenticated' })
}
next()
}
// Proxy endpoint for authenticated API calls
// NOTE: This proxy system may be useful for future server-side token management
app.use('/api/app/proxy', requireAuth, async (req, res) => {
try {
const originalPath = req.originalUrl.replace('/api/app/proxy', '')
let targetService = null
// Find matching service based on path prefix
for (const [prefix, service] of Object.entries(SERVICE_MAPPING)) {
if (originalPath.startsWith(prefix)) {
targetService = service
break
}
}
if (!targetService) {
return res.status(404).json({ error: 'Service not found' })
}
const targetUrl = `${targetService}${originalPath}`
// Forward request with Bearer token
const response = await fetch(targetUrl, {
method: req.method,
headers: {
'Authorization': `Bearer ${req.session.tokens.access_token}`,
'Content-Type': req.headers['content-type'] || 'application/json',
'Accept': req.headers.accept || 'application/json',
'Document Policy': 'js-profiling'
},
body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : undefined
})
const data = await response.text()
// Forward response
res.status(response.status)
res.set('Content-Type', response.headers.get('content-type'))
res.send(data)
} catch (error) {
console.error('Proxy request failed:', error)
res.status(500).json({ error: 'Proxy request failed' })
}
})
app.get('/api/app', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.info(`BFF listening on port ${port}`)
})