@@ -85,7 +85,6 @@ public static void AddSwagger(this IServiceCollection services, IConfiguration c
8585 c . OperationFilter < FileResponseTypeFilter > ( ) ;
8686 c . OperationFilter < OptionalParametersFilter > ( ) ;
8787 c . OperationFilter < ArrayInQueryParametersFilter > ( ) ;
88- c . OperationFilter < SecurityRequirementsOperationFilter > ( ) ;
8988 c . OperationFilter < ModuleInfoFilter > ( ) ;
9089 c . OperationFilter < OpenIDEndpointDescriptionFilter > ( ) ;
9190 c . SchemaFilter < EnumSchemaFilter > ( ) ;
@@ -94,18 +93,8 @@ public static void AddSwagger(this IServiceCollection services, IConfiguration c
9493 c . AddModulesXmlComments ( provider ) ;
9594 c . CustomOperationIds ( apiDesc =>
9695 apiDesc . TryGetMethodInfo ( out var methodInfo ) ? $ "{ ( ( ControllerActionDescriptor ) apiDesc . ActionDescriptor ) . ControllerName } _{ methodInfo . Name } " : null ) ;
97- c . AddSecurityDefinition ( "oauth2" , new OpenApiSecurityScheme
98- {
99- Type = SecuritySchemeType . OAuth2 ,
100- Description = "OAuth2 Resource Owner Password Grant flow" ,
101- Flows = new OpenApiOAuthFlows
102- {
103- Password = new OpenApiOAuthFlow
104- {
105- TokenUrl = new Uri ( "/connect/token" , UriKind . Relative )
106- }
107- } ,
108- } ) ;
96+
97+ c . AddSecuritySchemes ( ) ;
10998
11099 c . DocInclusionPredicate ( ( docName , apiDesc ) => DocInclusionPredicateCustomStrategy ( modules , docName , apiDesc ) ) ;
111100 c . ResolveConflictingActions ( apiDescriptions => apiDescriptions . First ( ) ) ;
@@ -250,5 +239,62 @@ private static void AddModulesXmlComments(this SwaggerGenOptions options, Servic
250239 }
251240 }
252241 }
242+
243+ /// <summary>
244+ /// Add security schemes definitions and operation filters for authentication
245+ /// </summary>
246+ private static void AddSecuritySchemes ( this SwaggerGenOptions options )
247+ {
248+ options . AddSecurityDefinition ( "oauth2" , new OpenApiSecurityScheme
249+ {
250+ Type = SecuritySchemeType . OAuth2 ,
251+ Description = "OAuth2 Resource Owner Password Grant flow" ,
252+ Flows = new OpenApiOAuthFlows
253+ {
254+ ClientCredentials = new OpenApiOAuthFlow
255+ {
256+ TokenUrl = new Uri ( "/connect/token" , UriKind . Relative )
257+ } ,
258+ Password = new OpenApiOAuthFlow
259+ {
260+ TokenUrl = new Uri ( "/connect/token" , UriKind . Relative )
261+ }
262+ } ,
263+ } ) ;
264+ options . AddSecurityDefinition ( "api_key" , new OpenApiSecurityScheme
265+ {
266+ Type = SecuritySchemeType . ApiKey ,
267+ Description = "API Key authentication" ,
268+ In = ParameterLocation . Query ,
269+ Name = "api_key" ,
270+ } ) ;
271+ options . AddSecurityDefinition ( "api_key_header" , new OpenApiSecurityScheme
272+ {
273+ Type = SecuritySchemeType . ApiKey ,
274+ Description = "API Key authentication (alternative via header)" ,
275+ In = ParameterLocation . Header ,
276+ Name = "api_key" ,
277+ } ) ;
278+ options . AddSecurityDefinition ( "http-signature" , new OpenApiSecurityScheme
279+ {
280+ Type = SecuritySchemeType . Http ,
281+ Scheme = "signature" ,
282+ Description = "HTTP Signature authentication using Authorization header" ,
283+ } ) ;
284+ options . AddSecurityDefinition ( "basic" , new OpenApiSecurityScheme
285+ {
286+ Type = SecuritySchemeType . Http ,
287+ Scheme = "basic" ,
288+ Description = "Basic authentication using username and password" ,
289+ } ) ;
290+
291+ // Register SecurityRequirementsOperationFilter for each security scheme
292+ // This allows API clients to use any of the supported authentication methods
293+ options . OperationFilter < SecurityRequirementsOperationFilter > ( true , "oauth2" ) ;
294+ options . OperationFilter < SecurityRequirementsOperationFilter > ( true , "api_key" ) ;
295+ options . OperationFilter < SecurityRequirementsOperationFilter > ( true , "api_key_header" ) ;
296+ options . OperationFilter < SecurityRequirementsOperationFilter > ( true , "http-signature" ) ;
297+ options . OperationFilter < SecurityRequirementsOperationFilter > ( true , "basic" ) ;
298+ }
253299 }
254300}
0 commit comments