1414 * - Uses same key extraction methods (Bearer, X-API-Key, query param)
1515 */
1616
17+ const crypto = require ( "crypto" ) ;
1718const logger = require ( "./logger" ) ;
1819
1920// Admin Configuration
@@ -152,18 +153,22 @@ function extractAPIKey(req) {
152153}
153154
154155/**
155- * Validate admin API key
156+ * Validate admin API key (ADMIN_API_KEY)
156157 * Uses timing-safe comparison to prevent timing attacks
158+ * @param {string } providedKey - The key to validate
159+ * @returns {boolean } True if key matches ADMIN_API_KEY
157160 */
158- function validateAPIKey ( providedKey ) {
161+ function validateAdminKey ( providedKey ) {
159162 return validateKeyAgainst ( providedKey , ADMIN_API_KEY ) ;
160163}
161164
162165/**
163- * Validate general API key (for rate limit bypass)
166+ * Validate general API key (API_KEY - for rate limit bypass)
164167 * Uses timing-safe comparison to prevent timing attacks
168+ * @param {string } providedKey - The key to validate
169+ * @returns {boolean } True if key matches API_KEY
165170 */
166- function validateApiKey ( providedKey ) {
171+ function validateGeneralApiKey ( providedKey ) {
167172 return validateKeyAgainst ( providedKey , API_KEY ) ;
168173}
169174
@@ -199,7 +204,7 @@ function shouldSkipRateLimit(req) {
199204 const apiKey = extractAPIKey ( req ) ;
200205
201206 // Check API key (either general API_KEY or ADMIN_API_KEY)
202- if ( apiKey && ( validateApiKey ( apiKey ) || validateAPIKey ( apiKey ) ) ) {
207+ if ( apiKey && ( validateGeneralApiKey ( apiKey ) || validateAdminKey ( apiKey ) ) ) {
203208 logger . debug ( `Rate limit bypass: Valid API key from ${ clientIP } ` ) ;
204209 req . apiAuth = { method : "api_key" , ip : clientIP } ;
205210 return true ;
@@ -230,7 +235,7 @@ function apiKeyMiddleware(req, res, next) {
230235 const clientIP = getClientIP ( req ) ;
231236 const apiKey = extractAPIKey ( req ) ;
232237
233- if ( apiKey && ( validateApiKey ( apiKey ) || validateAPIKey ( apiKey ) ) ) {
238+ if ( apiKey && ( validateGeneralApiKey ( apiKey ) || validateAdminKey ( apiKey ) ) ) {
234239 req . apiAuth = { method : "api_key" , ip : clientIP } ;
235240 } else if ( isApiWhitelisted ( clientIP ) ) {
236241 req . apiAuth = { method : "ip_whitelist" , ip : clientIP } ;
@@ -255,7 +260,7 @@ function adminAuth(req, res, next) {
255260
256261 // Method 1: API Key authentication
257262 if ( apiKey ) {
258- if ( validateAPIKey ( apiKey ) ) {
263+ if ( validateAdminKey ( apiKey ) ) {
259264 logger . debug ( `Admin auth: API key accepted from ${ clientIP } ` ) ;
260265 req . adminAuth = { method : "api_key" , ip : clientIP } ;
261266 return next ( ) ;
@@ -318,7 +323,7 @@ function optionalAdminAuth(req, res, next) {
318323
319324 req . isAdmin = false ;
320325
321- if ( apiKey && validateAPIKey ( apiKey ) ) {
326+ if ( apiKey && validateAdminKey ( apiKey ) ) {
322327 req . isAdmin = true ;
323328 req . adminAuth = { method : "api_key" , ip : clientIP } ;
324329 } else if ( isWhitelisted ( clientIP ) ) {
@@ -335,9 +340,10 @@ function optionalAdminAuth(req, res, next) {
335340/**
336341 * Generate a secure random API key
337342 * Can be used to generate keys for .env file
343+ * @param {number } length - Length of key in bytes (default: 32, produces 64 hex chars)
344+ * @returns {string } Random hex string
338345 */
339346function generateAPIKey ( length = 32 ) {
340- const crypto = require ( "crypto" ) ;
341347 return crypto . randomBytes ( length ) . toString ( "hex" ) ;
342348}
343349
0 commit comments