@@ -56,8 +56,8 @@ public sealed class CrudApiEntraAuth
5656public sealed class CrudApiApiKeyAuth
5757{
5858 public string ApiKey { get ; set ; } = string . Empty ;
59- public string HeaderName { get ; set ; } = "x-api-key" ;
60- public string QueryParameterName { get ; set ; } = string . Empty ;
59+ public string ? HeaderName { get ; set ; }
60+ public string ? QueryParameterName { get ; set ; }
6161}
6262
6363public sealed class CrudApiAction
@@ -140,6 +140,26 @@ Configuration.ApiKeyAuthConfig is not null &&
140140 Configuration . Auth = CrudApiAuthType . None ;
141141 }
142142
143+ foreach ( var action in Configuration . Actions )
144+ {
145+ if ( action . Auth == CrudApiAuthType . ApiKey &&
146+ action . ApiKeyAuthConfig is null &&
147+ Configuration . ApiKeyAuthConfig is null )
148+ {
149+ Logger . LogError ( "API Key auth is enabled for action {Action} but no configuration is provided. Action will work anonymously." , action . Action ) ;
150+ action . Auth = CrudApiAuthType . None ;
151+ }
152+
153+ var effectiveApiKeyConfig = action . ApiKeyAuthConfig ?? Configuration . ApiKeyAuthConfig ;
154+ if ( action . Auth == CrudApiAuthType . ApiKey &&
155+ effectiveApiKeyConfig is not null &&
156+ string . IsNullOrEmpty ( effectiveApiKeyConfig . ApiKey ) )
157+ {
158+ Logger . LogError ( "API Key auth is enabled for action {Action} but no API key is configured. Action will work anonymously." , action . Action ) ;
159+ action . Auth = CrudApiAuthType . None ;
160+ }
161+ }
162+
143163 if ( ! ProxyUtils . MatchesUrlToWatch ( UrlsToWatch , Configuration . BaseUrl , true ) )
144164 {
145165 Logger . LogWarning (
@@ -326,13 +346,19 @@ private void AddCORSHeaders(Request request, List<HttpHeader> headers)
326346 if ( Configuration . ApiKeyAuthConfig is not null ||
327347 Configuration . Actions . Any ( a => a . Auth == CrudApiAuthType . ApiKey ) )
328348 {
329- var apiKeyHeader = Configuration . ApiKeyAuthConfig ? . HeaderName
330- ?? Configuration . Actions
331- . FirstOrDefault ( a => a . ApiKeyAuthConfig is not null ) ? . ApiKeyAuthConfig ? . HeaderName
332- ?? "x-api-key" ;
333- if ( ! allowHeaders . Contains ( apiKeyHeader , StringComparer . OrdinalIgnoreCase ) )
349+ var apiKeyHeaders = new List < string ? > { Configuration . ApiKeyAuthConfig ? . HeaderName }
350+ . Concat ( Configuration . Actions
351+ . Where ( a => a . ApiKeyAuthConfig is not null )
352+ . Select ( a => a . ApiKeyAuthConfig ! . HeaderName ) )
353+ . Where ( h => ! string . IsNullOrEmpty ( h ) )
354+ . Distinct ( StringComparer . OrdinalIgnoreCase ) ;
355+
356+ foreach ( var apiKeyHeader in apiKeyHeaders )
334357 {
335- allowHeaders . Add ( apiKeyHeader ) ;
358+ if ( ! allowHeaders . Contains ( apiKeyHeader ! , StringComparer . OrdinalIgnoreCase ) )
359+ {
360+ allowHeaders . Add ( apiKeyHeader ! ) ;
361+ }
336362 }
337363 }
338364
@@ -369,18 +395,20 @@ private bool AuthorizeRequest(ProxyRequestArgs e, CrudApiAction? action = null)
369395
370396 private bool AuthorizeApiKeyRequest ( ProxyRequestArgs e , CrudApiAction ? action = null )
371397 {
372- var apiKeyAuthConfig = action is null ? Configuration . ApiKeyAuthConfig : action . ApiKeyAuthConfig ;
398+ var apiKeyAuthConfig = action ? . ApiKeyAuthConfig ?? Configuration . ApiKeyAuthConfig ;
373399
374400 Debug . Assert ( apiKeyAuthConfig is not null , "ApiKeyAuthConfig is null when API key auth is required." ) ;
375401
376402 // Check header
377- var headerName = apiKeyAuthConfig . HeaderName ;
378- var headerValue = e . Session . HttpClient . Request . Headers
379- . FirstOrDefault ( h => h . Name . Equals ( headerName , StringComparison . OrdinalIgnoreCase ) ) ? . Value ;
380-
381- if ( ! string . IsNullOrEmpty ( headerValue ) && headerValue == apiKeyAuthConfig . ApiKey )
403+ if ( ! string . IsNullOrEmpty ( apiKeyAuthConfig . HeaderName ) )
382404 {
383- return true ;
405+ var headerValue = e . Session . HttpClient . Request . Headers
406+ . FirstOrDefault ( h => h . Name . Equals ( apiKeyAuthConfig . HeaderName , StringComparison . OrdinalIgnoreCase ) ) ? . Value ;
407+
408+ if ( ! string . IsNullOrEmpty ( headerValue ) && headerValue == apiKeyAuthConfig . ApiKey )
409+ {
410+ return true ;
411+ }
384412 }
385413
386414 // Check query parameter
0 commit comments