@@ -195,12 +195,29 @@ public static class EndpointExtensions
195195
196196 public static void MapCacheRefreshEndpoint ( this WebApplication app )
197197 {
198- app . MapPost ( "/api/cache/refresh" , async ( IContentService contentService , ILogger < Program > logger , IWebHostEnvironment environment ) =>
198+ app . MapPost ( "/api/cache/refresh" , async ( HttpContext context , IContentService contentService , ILogger < Program > logger , IWebHostEnvironment environment , IConfiguration configuration ) =>
199199 {
200200 try
201201 {
202202 logger . LogInformation ( "Cache refresh requested via API endpoint" ) ;
203203
204+ // Validate API key for security
205+ var expectedApiKey = configuration [ "CacheRefresh:ApiKey" ] ;
206+ if ( ! string . IsNullOrEmpty ( expectedApiKey ) )
207+ {
208+ var providedApiKey = context . Request . Headers [ "X-API-Key" ] . FirstOrDefault ( ) ;
209+ if ( string . IsNullOrEmpty ( providedApiKey ) || providedApiKey != expectedApiKey )
210+ {
211+ logger . LogWarning ( "Cache refresh request rejected: Invalid or missing API key" ) ;
212+ return Results . Unauthorized ( ) ;
213+ }
214+ }
215+ else if ( ! environment . IsDevelopment ( ) )
216+ {
217+ logger . LogWarning ( "Cache refresh API key not configured in production environment" ) ;
218+ return Results . Problem ( "API key not configured" , statusCode : 500 ) ;
219+ }
220+
204221 // Only perform cache refresh in non-development environments
205222 // In development, the cache isn't as critical and may require Azure Table Storage
206223 if ( environment . IsDevelopment ( ) )
@@ -226,6 +243,6 @@ public static void MapCacheRefreshEndpoint(this WebApplication app)
226243 } )
227244 . WithName ( "RefreshCache" )
228245 . WithSummary ( "Refresh the content cache" )
229- . WithDescription ( "Triggers a refresh of the in-memory content cache from Azure Table Storage" ) ;
246+ . WithDescription ( "Triggers a refresh of the in-memory content cache from Azure Table Storage. Requires X-API-Key header for authentication. " ) ;
230247 }
231248}
0 commit comments