MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / getUserFromRequest

Function getUserFromRequest

server/src/http.ts:467–532  ·  view source on GitHub ↗

* Get user info from request for HTML config injection. * Checks dev mode first, then WorkOS session. * If session is refreshed, updates the cookie in the response.

(
  req: express.Request,
  res?: express.Response
)

Source from the content-addressed store, hash-verified

465 * If session is refreshed, updates the cookie in the response.
466 */
467async function getUserFromRequest(
468 req: express.Request,
469 res?: express.Response
470): Promise<{ id?: string; email: string; firstName?: string | null; lastName?: string | null } | null> {
471 // Check dev mode first
472 if (isDevModeEnabled()) {
473 const devUser = getDevUser(req);
474 if (devUser) {
475 return devUser;
476 }
477 }
478
479 // Then check WorkOS session
480 const sessionCookie = req.cookies?.['wos-session'];
481 // codeql[js/user-controlled-bypass] - session cookie is verified cryptographically by WorkOS sealed session
482 if (sessionCookie && AUTH_ENABLED && workos) {
483 try {
484 const session = workos.userManagement.loadSealedSession({
485 sessionData: sessionCookie,
486 cookiePassword: WORKOS_COOKIE_PASSWORD,
487 });
488
489 // Try to authenticate with the current session
490 let authResult = await session.authenticate();
491
492 // If authentication failed (e.g., expired token), try to refresh
493 if (!authResult.authenticated || !authResult.user) {
494 try {
495 const refreshResult = await session.refresh({
496 cookiePassword: WORKOS_COOKIE_PASSWORD,
497 });
498
499 if (refreshResult.authenticated && refreshResult.sealedSession) {
500 // Update the cookie with the refreshed session
501 if (res) {
502 res.cookie('wos-session', refreshResult.sealedSession, {
503 httpOnly: true,
504 secure: process.env.NODE_ENV === 'production',
505 sameSite: 'lax',
506 path: '/',
507 maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
508 });
509 }
510
511 // Re-authenticate with the new session
512 const newSession = workos.userManagement.loadSealedSession({
513 sessionData: refreshResult.sealedSession,
514 cookiePassword: WORKOS_COOKIE_PASSWORD,
515 });
516 authResult = await newSession.authenticate();
517 }
518 } catch {
519 // Refresh failed - continue without user
520 }
521 }
522
523 if (authResult.authenticated && authResult.user) {
524 return authResult.user;

Callers 4

setupMiddlewareMethod · 0.85
serveHtmlWithConfigMethod · 0.85
setupRoutesMethod · 0.85
serveDashboardPageMethod · 0.85

Calls 2

isDevModeEnabledFunction · 0.50
getDevUserFunction · 0.50

Tested by

no test coverage detected