All files / src/middleware oauth-auth.ts

0% Statements 0/82
0% Branches 0/28
0% Functions 0/6
0% Lines 0/82

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/**
 * OAuth Authentication Middleware
 *
 * Validates Bearer tokens on MCP endpoints and sets up the token context
 * for per-request GitLab API access.
 *
 * This middleware:
 * 1. Extracts and validates the Bearer token from Authorization header
 * 2. Verifies the JWT signature and expiration
 * 3. Loads the associated session
 * 4. Refreshes GitLab token if needed
 * 5. Sets up token context for the request
 */
 
import { Request, Response, NextFunction } from "express";
import { loadOAuthConfig } from "../oauth/config";
import { sessionStore } from "../oauth/session-store";
import { verifyMCPToken, isTokenExpiringSoon, calculateTokenExpiry } from "../oauth/token-utils";
import { runWithTokenContext } from "../oauth/token-context";
import { refreshGitLabToken } from "../oauth/gitlab-device-flow";
import { logger } from "../logger";
import { OAuthErrorResponse } from "../oauth/types";
 
/**
 * OAuth authentication middleware for Express
 *
 * Apply this middleware to routes that require OAuth authentication.
 * It validates the Bearer token and sets up the token context.
 *
 * @param req - Express request
 * @param res - Express response
 * @param next - Express next function
 */
export async function oauthAuthMiddleware(
  req: Request,
  res: Response,
  next: NextFunction
): Promise<void> {
  const config = loadOAuthConfig();
  if (!config) {
    sendUnauthorized(res, "server_error", "OAuth not configured");
    return;
  }
 
  // Extract Bearer token from Authorization header
  const authHeader = req.headers.authorization;
  if (!authHeader) {
    sendUnauthorized(res, "unauthorized", "Missing Authorization header");
    return;
  }
 
  if (!authHeader.startsWith("Bearer ")) {
    sendUnauthorized(
      res,
      "unauthorized",
      "Invalid Authorization header format. Expected: Bearer <token>"
    );
    return;
  }
 
  const token = authHeader.slice(7); // Remove "Bearer " prefix
 
  if (!token) {
    sendUnauthorized(res, "unauthorized", "Empty Bearer token");
    return;
  }
 
  // Verify JWT token
  const payload = verifyMCPToken(token, config.sessionSecret);
  if (!payload) {
    sendUnauthorized(res, "invalid_token", "Token is invalid or expired");
    return;
  }
 
  // Get session from token
  const sessionId = payload.sid;
  const session = sessionStore.getSession(sessionId);
 
  if (!session) {
    sendUnauthorized(res, "invalid_token", "Session not found or expired");
    return;
  }
 
  // Verify token matches session
  if (session.mcpAccessToken !== token) {
    // Token might have been rotated
    sendUnauthorized(res, "invalid_token", "Token has been superseded");
    return;
  }
 
  // Refresh GitLab token if it's expiring soon (5 minute buffer)
  if (isTokenExpiringSoon(session.gitlabTokenExpiry)) {
    try {
      const newTokens = await refreshGitLabToken(session.gitlabRefreshToken, config);
 
      sessionStore.updateSession(sessionId, {
        gitlabAccessToken: newTokens.access_token,
        gitlabRefreshToken: newTokens.refresh_token,
        gitlabTokenExpiry: calculateTokenExpiry(newTokens.expires_in),
      });
 
      logger.debug(
        { sessionId: sessionId.substring(0, 8) + "..." },
        "GitLab token refreshed during request"
      );
    } catch (error: unknown) {
      logger.error({ err: error as Error }, "Failed to refresh GitLab token during request");
      sendUnauthorized(
        res,
        "invalid_token",
        "GitLab token refresh failed. Please re-authenticate."
      );
      return;
    }
  }
 
  // Get potentially updated session
  const updatedSession = sessionStore.getSession(sessionId);
  if (!updatedSession) {
    sendUnauthorized(res, "invalid_token", "Session lost during token refresh");
    return;
  }
 
  // Set up token context and continue
  // All code in the request handler will have access to the GitLab token via getGitLabTokenFromContext()
  try {
    await runWithTokenContext(
      {
        gitlabToken: updatedSession.gitlabAccessToken,
        gitlabUserId: updatedSession.gitlabUserId,
        gitlabUsername: updatedSession.gitlabUsername,
        sessionId: updatedSession.id,
      },
      async () => {
        // Call next() inside the context so all downstream code has access to the token
        next();
      }
    );
  } catch (error: unknown) {
    logger.error({ err: error as Error }, "Error in OAuth-authenticated request");
    throw error;
  }
}
 
/**
 * Create OAuth middleware for specific routes
 *
 * Returns the middleware function configured for OAuth authentication.
 * Use this when you need to programmatically apply the middleware.
 */
export function createOAuthMiddleware(): typeof oauthAuthMiddleware {
  return oauthAuthMiddleware;
}
 
/**
 * Optional OAuth middleware
 *
 * Like oauthAuthMiddleware, but doesn't require authentication.
 * If a valid token is provided, sets up the context.
 * If no token or invalid token, continues without context.
 *
 * Useful for endpoints that work with or without authentication.
 */
export async function optionalOAuthMiddleware(
  req: Request,
  res: Response,
  next: NextFunction
): Promise<void> {
  const config = loadOAuthConfig();
  if (!config) {
    // OAuth not configured, continue without context
    next();
    return;
  }
 
  const authHeader = req.headers.authorization;
  if (!authHeader?.startsWith("Bearer ")) {
    // No token provided, continue without context
    next();
    return;
  }
 
  const token = authHeader.slice(7);
  if (!token) {
    next();
    return;
  }
 
  // Try to validate token
  const payload = verifyMCPToken(token, config.sessionSecret);
  if (!payload) {
    // Invalid token, but this is optional auth, so continue
    next();
    return;
  }
 
  const session = sessionStore.getSession(payload.sid);
  if (session?.mcpAccessToken !== token) {
    next();
    return;
  }
 
  // Valid token - set up context
  try {
    await runWithTokenContext(
      {
        gitlabToken: session.gitlabAccessToken,
        gitlabUserId: session.gitlabUserId,
        gitlabUsername: session.gitlabUsername,
        sessionId: session.id,
      },
      async () => {
        next();
      }
    );
  } catch (error: unknown) {
    logger.error({ err: error as Error }, "Error in optional OAuth request");
    throw error;
  }
}
 
/**
 * Send unauthorized response with OAuth error format
 */
function sendUnauthorized(res: Response, error: string, description: string): void {
  const response: OAuthErrorResponse = {
    error,
    error_description: description,
  };
 
  // Set WWW-Authenticate header as per OAuth spec
  res.setHeader("WWW-Authenticate", 'Bearer realm="gitlab-mcp"');
  res.status(401).json(response);
}