Skip to content

Commit 78ad354

Browse files
Updates AuthPlugin to support different parameters for API key auth (#883)
1 parent 6844e92 commit 78ad354

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

dev-proxy-plugins/Mocks/AuthPlugin.cs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@ public enum AuthPluginAuthType
2525
OAuth2
2626
}
2727

28-
[Flags]
2928
public enum AuthPluginApiKeyIn
3029
{
31-
Header = 1,
32-
Query = 2,
33-
Cookie = 4
30+
Header,
31+
Query,
32+
Cookie
3433
}
3534

36-
public class AuthPluginApiKeyConfiguration
35+
public class AuthPluginApiKeyParameter
3736
{
3837
[JsonConverter(typeof(JsonStringEnumConverter))]
3938
public AuthPluginApiKeyIn? In { get; set; }
4039
public string? Name { get; set; }
40+
}
41+
42+
public class AuthPluginApiKeyConfiguration
43+
{
44+
public AuthPluginApiKeyParameter[]? Parameters { get; set; }
4145
public string[]? AllowedKeys { get; set; }
4246
}
4347

@@ -104,16 +108,20 @@ public override async Task RegisterAsync()
104108
{
105109
Debug.Assert(_configuration.ApiKey is not null);
106110

107-
if (_configuration.ApiKey.In == null)
111+
if (_configuration.ApiKey.Parameters == null ||
112+
_configuration.ApiKey.Parameters.Length == 0)
108113
{
109-
Logger.LogError("ApiKey.In is required when using ApiKey auth type");
114+
Logger.LogError("ApiKey.Parameters is required when using ApiKey auth type");
110115
return;
111116
}
112117

113-
if (string.IsNullOrWhiteSpace(_configuration.ApiKey.Name))
118+
foreach (var parameter in _configuration.ApiKey.Parameters)
114119
{
115-
Logger.LogError("ApiKey.Name is required when using ApiKey auth type");
116-
return;
120+
if (parameter.In is null || parameter.Name is null)
121+
{
122+
Logger.LogError("ApiKey.In and ApiKey.Name are required for each parameter");
123+
return;
124+
}
117125
}
118126

119127
if (_configuration.ApiKey.AllowedKeys == null || _configuration.ApiKey.AllowedKeys.Length == 0)
@@ -474,32 +482,30 @@ private static bool HasPermission(string permission, string permissionString)
474482
{
475483
Debug.Assert(_configuration is not null);
476484
Debug.Assert(_configuration.ApiKey is not null);
477-
Debug.Assert(_configuration.ApiKey.Name is not null);
485+
Debug.Assert(_configuration.ApiKey.Parameters is not null);
478486

479487
string? apiKey = null;
480-
string name = _configuration.ApiKey.Name;
481488

482-
if ((_configuration.ApiKey.In & AuthPluginApiKeyIn.Header) == AuthPluginApiKeyIn.Header)
489+
foreach (var parameter in _configuration.ApiKey.Parameters)
483490
{
484-
Logger.LogDebug("Getting API key from header");
485-
apiKey = GetApiKeyFromHeader(session.HttpClient.Request, name);
486-
Logger.LogDebug("API key from header: {apiKey}", apiKey ?? "(not found)");
487-
}
491+
if (parameter.In is null || parameter.Name is null)
492+
{
493+
continue;
494+
}
488495

489-
if ((_configuration.ApiKey.In & AuthPluginApiKeyIn.Query) == AuthPluginApiKeyIn.Query &&
490-
apiKey is null)
491-
{
492-
Logger.LogDebug("Getting API key from query");
493-
apiKey = GetApiKeyFromQuery(session.HttpClient.Request, name);
494-
Logger.LogDebug("API key from query: {apiKey}", apiKey ?? "(not found)");
495-
}
496+
Logger.LogDebug("Getting API key from parameter {param} in {in}", parameter.Name, parameter.In);
497+
apiKey = parameter.In switch {
498+
AuthPluginApiKeyIn.Header => GetApiKeyFromHeader(session.HttpClient.Request, parameter.Name),
499+
AuthPluginApiKeyIn.Query => GetApiKeyFromQuery(session.HttpClient.Request, parameter.Name),
500+
AuthPluginApiKeyIn.Cookie => GetApiKeyFromCookie(session.HttpClient.Request, parameter.Name),
501+
_ => null
502+
};
503+
Logger.LogDebug("API key from parameter {param} in {in}: {apiKey}", parameter.Name, parameter.In, apiKey ?? "(not found)");
496504

497-
if ((_configuration.ApiKey.In & AuthPluginApiKeyIn.Cookie) == AuthPluginApiKeyIn.Cookie &&
498-
apiKey is null)
499-
{
500-
Logger.LogDebug("Getting API key from cookie");
501-
apiKey = GetApiKeyFromCookie(session.HttpClient.Request, name);
502-
Logger.LogDebug("API key from cookie: {apiKey}", apiKey ?? "(not found)");
505+
if (apiKey is not null)
506+
{
507+
break;
508+
}
503509
}
504510

505511
return apiKey;

0 commit comments

Comments
 (0)