Skip to content

Commit 086be81

Browse files
committed
Refactor API Key authentication configuration to allow null values for header and query parameter names
1 parent 5616879 commit 086be81

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

DevProxy.Plugins/Mocking/CrudApiPlugin.cs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public sealed class CrudApiEntraAuth
5656
public 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

6363
public 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

schemas/v3.0.0/crudapiplugin.apifile.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
},
7878
"headerName": {
7979
"type": "string",
80-
"description": "The HTTP header name to read the API key from. Default is x-api-key."
80+
"description": "The HTTP header name to read the API key from."
8181
},
8282
"queryParameterName": {
8383
"type": "string",
@@ -146,7 +146,7 @@
146146
},
147147
"headerName": {
148148
"type": "string",
149-
"description": "The HTTP header name to read the API key from. Default is x-api-key."
149+
"description": "The HTTP header name to read the API key from."
150150
},
151151
"queryParameterName": {
152152
"type": "string",

0 commit comments

Comments
 (0)