@@ -25,19 +25,23 @@ public enum AuthPluginAuthType
25
25
OAuth2
26
26
}
27
27
28
- [ Flags ]
29
28
public enum AuthPluginApiKeyIn
30
29
{
31
- Header = 1 ,
32
- Query = 2 ,
33
- Cookie = 4
30
+ Header ,
31
+ Query ,
32
+ Cookie
34
33
}
35
34
36
- public class AuthPluginApiKeyConfiguration
35
+ public class AuthPluginApiKeyParameter
37
36
{
38
37
[ JsonConverter ( typeof ( JsonStringEnumConverter ) ) ]
39
38
public AuthPluginApiKeyIn ? In { get ; set ; }
40
39
public string ? Name { get ; set ; }
40
+ }
41
+
42
+ public class AuthPluginApiKeyConfiguration
43
+ {
44
+ public AuthPluginApiKeyParameter [ ] ? Parameters { get ; set ; }
41
45
public string [ ] ? AllowedKeys { get ; set ; }
42
46
}
43
47
@@ -104,16 +108,20 @@ public override async Task RegisterAsync()
104
108
{
105
109
Debug . Assert ( _configuration . ApiKey is not null ) ;
106
110
107
- if ( _configuration . ApiKey . In == null )
111
+ if ( _configuration . ApiKey . Parameters == null ||
112
+ _configuration . ApiKey . Parameters . Length == 0 )
108
113
{
109
- Logger . LogError ( "ApiKey.In is required when using ApiKey auth type" ) ;
114
+ Logger . LogError ( "ApiKey.Parameters is required when using ApiKey auth type" ) ;
110
115
return ;
111
116
}
112
117
113
- if ( string . IsNullOrWhiteSpace ( _configuration . ApiKey . Name ) )
118
+ foreach ( var parameter in _configuration . ApiKey . Parameters )
114
119
{
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
+ }
117
125
}
118
126
119
127
if ( _configuration . ApiKey . AllowedKeys == null || _configuration . ApiKey . AllowedKeys . Length == 0 )
@@ -474,32 +482,30 @@ private static bool HasPermission(string permission, string permissionString)
474
482
{
475
483
Debug . Assert ( _configuration is not null ) ;
476
484
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 ) ;
478
486
479
487
string ? apiKey = null ;
480
- string name = _configuration . ApiKey . Name ;
481
488
482
- if ( ( _configuration . ApiKey . In & AuthPluginApiKeyIn . Header ) == AuthPluginApiKeyIn . Header )
489
+ foreach ( var parameter in _configuration . ApiKey . Parameters )
483
490
{
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
+ }
488
495
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)" ) ;
496
504
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
+ }
503
509
}
504
510
505
511
return apiKey ;
0 commit comments