Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 24x 24x 2x 2x 2x 2x 2x 2x 2x 2x 70x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 15x 15x 15x 64x 2x 64x 2x 63x 2x 4x 21x 21x 21x 21x 59x 2x 2x 2x 21x 21x 2x 2x 1x 1x 1x 58x 21x 1x 1x 21x 19x 19x 19x 19x 57x 19x 19x 57x 57x 1x 57x 1x 4x 1x 4x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 21x 18x 21x 18x 18x 18x 18x 18x 18x 18x 18x 53x 18x 18x 18x 18x 18x 53x 2x 2x 53x 53x 53x 1x 1x 24x 2x 2x 2x 2x 2x 2x 24x 24x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 2x 24x 2x 24x 24x 24x 2x 2x 24x 9x 296x 9x 6x 6x 296x 6x 2x 296x 1x 1x 293x 1x 296x 292x 2x 2x 143x 143x 2x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 4x 2x 2x 2x 4x 2x 2x 2x 2x 2x 4x 2x 2x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 47x 2x 47x 24x 2x 2x 23x 23x 23x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 15x 2x 2x 2x 15x 2x 2x 2x 2x 2x 14x 2x 4x 2x 14x 2x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 625x 2x 2x 2x 624x 2x 2x 2x 2x 625x 2x 2x 2x | // src/config/passport.ts
import passport from 'passport';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { Strategy as LocalStrategy } from 'passport-local';
import { Strategy as GoogleStrategy, Profile as GoogleProfile } from 'passport-google-oauth20';
import { Strategy as GitHubStrategy, Profile as GitHubProfile } from 'passport-github2';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
import * as bcrypt from 'bcrypt';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { Request, Response, NextFunction } from 'express';
import * as db from '../services/db/index.db';
import { logger } from '../services/logger.server';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { UserProfile } from '../types';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { createMockUserProfile } from '../tests/utils/mockFactories';
// All route handlers now use req.log (request-scoped logger) as per ADR-004
import { ForbiddenError } from '../services/db/errors.db';
const JWT_SECRET = process.env.JWT_SECRET!;
const MAX_FAILED_ATTEMPTS = 5;
const LOCKOUT_DURATION_MINUTES = 15;
/**
* A type guard to check if an object is a UserProfile.
* This is useful for safely accessing properties on `req.user`.
* @param user The user object to check.
* @returns True if the object is a UserProfile, false otherwise.
*/
function isUserProfile(user: unknown): user is UserProfile {
return (
typeof user === 'object' &&
user !== null &&
'role' in user &&
'user' in user &&
typeof (user as { user: unknown }).user === 'object' &&
(user as { user: unknown }).user !== null &&
'user_id' in ((user as { user: unknown }).user as object)
);
}
// --- Passport Local Strategy (for email/password login) ---
passport.use(
new LocalStrategy(
{
usernameField: 'email',
passReqToCallback: true, // Pass the request object to the callback
},
async (req: Request, email, password, done) => {
try {
// 1. Find the user by email, including their profile data for the JWT payload.
const userprofile = await db.userRepo.findUserWithProfileByEmail(email, req.log);
if (!userprofile) {
// User not found
req.log.warn(`Login attempt failed for non-existent user: ${email}`);
return done(null, false, { message: 'Incorrect email or password.' });
}
// Check if the account is currently locked.
if (
userprofile.failed_login_attempts >= MAX_FAILED_ATTEMPTS &&
userprofile.last_failed_login
) {
const lockoutTime = new Date(userprofile.last_failed_login).getTime();
const timeSinceLockout = Date.now() - lockoutTime;
const lockoutDurationMs = LOCKOUT_DURATION_MINUTES * 60 * 1000;
if (timeSinceLockout < lockoutDurationMs) {
req.log.warn(`Login attempt for locked account: ${email}`);
// Refresh the lockout timestamp on each attempt to prevent probing.
await db.adminRepo.incrementFailedLoginAttempts(userprofile.user.user_id, req.log);
return done(null, false, {
message: `Account is temporarily locked. Please try again in ${LOCKOUT_DURATION_MINUTES} minutes.`,
});
}
}
if (!userprofile.password_hash) {
// User exists but signed up via OAuth, so they don't have a password.
req.log.warn(`Password login attempt for OAuth user: ${email}`);
return done(null, false, {
message:
'This account was created using a social login. Please use Google or GitHub to sign in.',
});
}
// 2. Compare the submitted password with the hashed password in your DB.
req.log.debug(
`[Passport] Verifying password for ${email}. Hash length: ${userprofile.password_hash.length}`,
);
const isMatch = await bcrypt.compare(password, userprofile.password_hash);
req.log.debug(`[Passport] Password match result: ${isMatch}`);
if (!isMatch) {
// Password does not match
req.log.warn(`Login attempt failed for user ${email} due to incorrect password.`);
// Increment failed attempts and get the new count.
const newAttemptCount = await db.adminRepo.incrementFailedLoginAttempts(
userprofile.user.user_id,
req.log,
);
// Log this security event.
await db.adminRepo.logActivity(
{
userId: userprofile.user.user_id,
action: 'login_failed_password',
displayText: `Failed login attempt for user ${userprofile.user.email}.`,
icon: 'shield-alert',
details: { source_ip: req.ip ?? null, new_attempt_count: newAttemptCount }, // The user.email is correct here as it's part of the Omit type
},
req.log,
);
// If this attempt just locked the account, inform the user immediately.
if (newAttemptCount >= MAX_FAILED_ATTEMPTS) {
return done(null, false, {
message: `Account is temporarily locked. Please try again in ${LOCKOUT_DURATION_MINUTES} minutes.`,
});
}
return done(null, false, { message: 'Incorrect email or password.' });
}
// 3. Success! Return the user object (without password_hash for security).
// Reset failed login attempts upon successful login.
await db.adminRepo.resetFailedLoginAttempts(
userprofile.user.user_id,
req.ip ?? 'unknown',
req.log,
);
req.log.info(`User successfully authenticated: ${email}`);
// The `user` object from `findUserWithProfileByEmail` is now a fully formed
// UserProfile object with additional authentication fields. We must strip these
// sensitive fields before passing the profile to the session.
// The `...userProfile` rest parameter will contain the clean UserProfile object,
// which no longer has a top-level email property.
// SECURITY: password_hash and refresh_token are intentionally discarded - never send to client.
const {
password_hash: _password_hash,
failed_login_attempts,
last_failed_login,
refresh_token: _refresh_token,
...cleanUserProfile
} = userprofile;
// Log login metadata for audit purposes (non-sensitive fields only)
req.log.debug(
{ failed_login_attempts, last_failed_login },
'User login metadata stripped from session',
);
return done(null, cleanUserProfile);
} catch (err: unknown) {
req.log.error({ error: err }, 'Error during local authentication strategy:');
return done(err);
}
},
),
);
// --- Passport Google OAuth 2.0 Strategy ---
// Only register the strategy if the required environment variables are set.
Iif (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/api/auth/google/callback',
scope: ['profile', 'email'],
},
async (
_accessToken: string,
_refreshToken: string,
profile: GoogleProfile,
done: (error: Error | null, user?: UserProfile | false) => void,
) => {
try {
const email = profile.emails?.[0]?.value;
if (!email) {
return done(new Error('No email found in Google profile.'), false);
}
// Check if user already exists in our database
const existingUserProfile = await db.userRepo.findUserWithProfileByEmail(email, logger);
if (existingUserProfile) {
// User exists, proceed to log them in.
logger.info(`Google OAuth successful for existing user: ${email}`);
// Strip sensitive fields before returning
const {
password_hash: _password_hash,
failed_login_attempts: _failed_login_attempts,
last_failed_login: _last_failed_login,
refresh_token: _refresh_token,
...cleanUserProfile
} = existingUserProfile;
return done(null, cleanUserProfile);
} else {
// User does not exist, create a new account for them.
logger.info(`Google OAuth: creating new user for email: ${email}`);
// Since this is an OAuth user, they don't have a password.
// We pass `null` for the password hash.
const newUserProfile = await db.userRepo.createUser(
email,
null, // No password for OAuth users
{
full_name: profile.displayName,
avatar_url: profile.photos?.[0]?.value,
},
logger,
);
return done(null, newUserProfile);
}
} catch (err) {
logger.error({ error: err }, 'Error during Google authentication strategy');
return done(err as Error, false);
}
},
),
);
logger.info('[Passport] Google OAuth strategy registered.');
} else {
logger.warn(
'[Passport] Google OAuth strategy NOT registered: GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET not set.',
);
}
// --- Passport GitHub OAuth 2.0 Strategy ---
// Only register the strategy if the required environment variables are set.
Iif (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
passport.use(
new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: '/api/auth/github/callback',
scope: ['user:email'],
},
async (
_accessToken: string,
_refreshToken: string,
profile: GitHubProfile,
done: (error: Error | null, user?: UserProfile | false) => void,
) => {
try {
const email = profile.emails?.[0]?.value;
if (!email) {
return done(
new Error(
'No public email found in GitHub profile. Please ensure your primary email is public or add one.',
),
false,
);
}
// Check if user already exists in our database
const existingUserProfile = await db.userRepo.findUserWithProfileByEmail(email, logger);
if (existingUserProfile) {
// User exists, proceed to log them in.
logger.info(`GitHub OAuth successful for existing user: ${email}`);
// Strip sensitive fields before returning
const {
password_hash: _password_hash,
failed_login_attempts: _failed_login_attempts,
last_failed_login: _last_failed_login,
refresh_token: _refresh_token,
...cleanUserProfile
} = existingUserProfile;
return done(null, cleanUserProfile);
} else {
// User does not exist, create a new account for them.
logger.info(`GitHub OAuth: creating new user for email: ${email}`);
// Since this is an OAuth user, they don't have a password.
// We pass `null` for the password hash.
const newUserProfile = await db.userRepo.createUser(
email,
null, // No password for OAuth users
{
full_name: profile.displayName || profile.username, // GitHub profile might not have displayName
avatar_url: profile.photos?.[0]?.value,
},
logger,
);
return done(null, newUserProfile);
}
} catch (err) {
logger.error({ error: err }, 'Error during GitHub authentication strategy');
return done(err as Error, false);
}
},
),
);
logger.info('[Passport] GitHub OAuth strategy registered.');
} else {
logger.warn(
'[Passport] GitHub OAuth strategy NOT registered: GITHUB_CLIENT_ID or GITHUB_CLIENT_SECRET not set.',
);
}
// --- Passport JWT Strategy (for protecting API routes) ---
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), // Expect JWT in 'Authorization: Bearer <token>' header
secretOrKey: JWT_SECRET,
};
// --- DEBUG LOGGING FOR JWT SECRET ---
Iif (!JWT_SECRET) {
logger.fatal(
'[Passport] CRITICAL: JWT_SECRET is missing or empty in environment variables! JwtStrategy will fail.',
);
} else {
logger.info(`[Passport] JWT_SECRET loaded successfully (length: ${JWT_SECRET.length}).`);
}
passport.use(
new JwtStrategy(jwtOptions, async (jwt_payload, done) => {
logger.debug(
{ jwt_payload: jwt_payload ? { user_id: jwt_payload.user_id } : 'null' },
'[JWT Strategy] Verifying token payload:',
);
try {
// The jwt_payload contains the data you put into the token during login (e.g., { user_id: user.user_id, email: user.email }).
// We re-fetch the user from the database here to ensure they are still active and valid.
const userProfile = await db.userRepo.findUserProfileById(jwt_payload.user_id, logger);
// --- JWT STRATEGY DEBUG LOGGING ---
logger.debug(
`[JWT Strategy] DB lookup for user ID ${jwt_payload.user_id} result: ${userProfile ? 'FOUND' : 'NOT FOUND'}`,
);
if (userProfile) {
return done(null, userProfile); // User profile object will be available as req.user in protected routes
} else {
logger.warn(`JWT authentication failed: user with ID ${jwt_payload.user_id} not found.`);
return done(null, false); // User not found or invalid token
}
} catch (err: unknown) {
logger.error({ error: err }, 'Error during JWT authentication strategy:');
return done(err, false);
}
}),
);
// --- Custom Error Class for Unauthorized Access ---
class UnauthorizedError extends Error {
status: number;
constructor(message: string) {
super(message);
this.name = 'UnauthorizedError';
this.status = 401;
}
}
/**
* A required authentication middleware that returns standardized error responses.
* Unlike the default passport.authenticate(), this middleware ensures that 401 responses
* follow our API response format with { success: false, error: { code, message } }.
*
* Use this instead of `passport.authenticate('jwt', { session: false })` to ensure
* consistent error responses per ADR-028.
*/
export const requireAuth = (req: Request, res: Response, next: NextFunction) => {
passport.authenticate(
'jwt',
{ session: false },
(err: Error | null, user: UserProfile | false, info: { message: string } | Error) => {
Iif (err) {
// An actual error occurred during authentication
req.log.error({ error: err }, 'Authentication error');
return next(err);
}
if (!user) {
// Authentication failed - return standardized error through error handler
const message =
info instanceof Error ? info.message : info?.message || 'Authentication required.';
req.log.warn({ info: message }, 'JWT authentication failed');
return next(new UnauthorizedError(message));
}
// Authentication succeeded - attach user and proceed
req.user = user;
next();
},
)(req, res, next);
};
// --- Middleware for Admin Role Check ---
export const isAdmin = (req: Request, res: Response, next: NextFunction) => {
// Use the type guard for safer access to req.user
const userProfile = req.user;
if (isUserProfile(userProfile) && userProfile.role === 'admin') {
next();
} else {
// Check if userProfile is a valid UserProfile before accessing its properties for logging.
const userIdForLog = isUserProfile(userProfile) ? userProfile.user.user_id : 'unknown';
req.log.warn(`Admin access denied for user: ${userIdForLog}`);
next(new ForbiddenError('Forbidden: Administrator access required.'));
}
};
/**
* A flexible authentication middleware. It attempts to authenticate via JWT but does NOT
* reject the request if authentication fails. This allows routes to handle both
* authenticated and anonymous users. If a valid token is present, `req.user` will be
* populated; otherwise, it will be undefined, and the request proceeds.
*/
export const optionalAuth = (req: Request, res: Response, next: NextFunction) => {
// The custom callback for passport.authenticate gives us access to `err`, `user`, and `info`.
passport.authenticate(
'jwt',
{ session: false },
(err: Error | null, user: Express.User | false, info: { message: string } | Error) => {
if (err) {
// An actual error occurred during authentication (e.g., malformed token).
// For optional auth, we log this but still proceed without a user.
req.log.warn({ error: err }, 'Optional auth encountered an error, proceeding anonymously.');
return next();
}
if (info) {
// The patch requested this specific error handling.
req.log.info({ info: info.message || info.toString() }, 'Optional auth info:');
}
if (user) (req as Express.Request).user = user; // Attach user if authentication succeeds.
next(); // Always proceed to the next middleware
},
)(req, res, next);
};
/**
* Mock Authentication Middleware for Testing
*
* This middleware is ONLY active when `process.env.NODE_ENV` is 'test'.
* It bypasses the entire JWT authentication flow and directly injects a
* mock user object into `req.user`. This is essential for integration tests,
* allowing protected routes to be tested without needing to generate valid JWTs
* or mock the passport strategy.
*
* In any environment other than 'test', it does nothing and immediately passes
* control to the next middleware.
*/
export const mockAuth = (req: Request, res: Response, next: NextFunction) => {
if (process.env.NODE_ENV === 'test') {
// In a test environment, attach a mock user to the request.
// We use the mock factory to create a consistent, type-safe user profile.
// We override the default role to 'admin' for broad access in tests.
req.user = createMockUserProfile({
role: 'admin',
});
}
// In production or development, this middleware does nothing.
next();
};
export default passport;
|