13
13
using Swashbuckle . AspNetCore . TestSupport ;
14
14
using Xunit ;
15
15
using System . Threading . Tasks ;
16
- using Microsoft . AspNetCore . Authentication ;
17
16
using Microsoft . AspNetCore . Server . HttpSys ;
17
+ using Microsoft . AspNetCore . Authentication ;
18
18
19
19
namespace Swashbuckle . AspNetCore . SwaggerGen . Test
20
20
{
@@ -1081,76 +1081,70 @@ public void GetSwagger_SupportsOption_SecuritySchemes()
1081
1081
1082
1082
var document = subject . GetSwagger ( "v1" ) ;
1083
1083
1084
- Assert . Equal ( new [ ] { "basic" , "Bearer" } , document . Components . SecuritySchemes . Keys ) ;
1085
- }
1086
-
1087
- [ Fact ]
1088
- public async Task GetSwagger_SupportsSecuritySchemesSelector ( )
1089
- {
1090
- var subject = Subject (
1091
- apiDescriptions : new ApiDescription [ ] { } ,
1092
- options : new SwaggerGeneratorOptions
1093
- {
1094
- SwaggerDocs = new Dictionary < string , OpenApiInfo >
1095
- {
1096
- [ "v1" ] = new OpenApiInfo { Version = "V1" , Title = "Test API" }
1097
- } ,
1098
- SecuritySchemesSelector = ( schemes ) => new Dictionary < string , OpenApiSecurityScheme >
1099
- {
1100
- [ "basic" ] = new OpenApiSecurityScheme { Type = SecuritySchemeType . Http , Scheme = "basic" }
1101
- }
1102
- }
1103
- ) ;
1104
-
1105
- var document = await subject . GetSwaggerAsync ( "v1" ) ;
1106
-
1107
- // Overrides the default set of [basic, bearer] with just [basic]
1108
1084
Assert . Equal ( new [ ] { "basic" } , document . Components . SecuritySchemes . Keys ) ;
1109
1085
}
1110
1086
1111
- [ Fact ]
1112
- public async Task GetSwagger_DefaultSecuritySchemeSelectorAddsBearerByDefault ( )
1087
+ [ Theory ]
1088
+ [ InlineData ( false , new string [ ] { } ) ]
1089
+ [ InlineData ( true , new string [ ] { "Bearer" } ) ]
1090
+ public async Task GetSwagger_SupportsOption_InferSecuritySchemes (
1091
+ bool inferSecuritySchemes ,
1092
+ string [ ] expectedSecuritySchemeNames )
1093
+
1113
1094
{
1114
1095
var subject = Subject (
1115
1096
apiDescriptions : new ApiDescription [ ] { } ,
1097
+ authenticationSchemes : new [ ] {
1098
+ new AuthenticationScheme ( "Bearer" , null , typeof ( IAuthenticationHandler ) ) ,
1099
+ new AuthenticationScheme ( "Cookies" , null , typeof ( IAuthenticationHandler ) )
1100
+ } ,
1116
1101
options : new SwaggerGeneratorOptions
1117
1102
{
1118
1103
SwaggerDocs = new Dictionary < string , OpenApiInfo >
1119
1104
{
1120
1105
[ "v1" ] = new OpenApiInfo { Version = "V1" , Title = "Test API" }
1121
1106
} ,
1107
+ InferSecuritySchemes = inferSecuritySchemes
1122
1108
}
1123
1109
) ;
1124
1110
1125
1111
var document = await subject . GetSwaggerAsync ( "v1" ) ;
1126
1112
1127
- Assert . Equal ( new [ ] { "Bearer" } , document . Components . SecuritySchemes . Keys ) ;
1113
+ Assert . Equal ( expectedSecuritySchemeNames , document . Components . SecuritySchemes . Keys ) ;
1128
1114
}
1129
1115
1130
- [ Fact ]
1131
- public async Task GetSwagger_DefaultSecuritySchemesSelectorDoesNotOverrideBearer ( )
1116
+ [ Theory ]
1117
+ [ InlineData ( false , new string [ ] { } ) ]
1118
+ [ InlineData ( true , new string [ ] { "Bearer" , "Cookies" } ) ]
1119
+ public async Task GetSwagger_SupportsOption_SecuritySchemesSelector (
1120
+ bool inferSecuritySchemes ,
1121
+ string [ ] expectedSecuritySchemeNames )
1122
+
1132
1123
{
1133
1124
var subject = Subject (
1134
1125
apiDescriptions : new ApiDescription [ ] { } ,
1126
+ authenticationSchemes : new [ ] {
1127
+ new AuthenticationScheme ( "Bearer" , null , typeof ( IAuthenticationHandler ) ) ,
1128
+ new AuthenticationScheme ( "Cookies" , null , typeof ( IAuthenticationHandler ) )
1129
+ } ,
1135
1130
options : new SwaggerGeneratorOptions
1136
1131
{
1137
1132
SwaggerDocs = new Dictionary < string , OpenApiInfo >
1138
1133
{
1139
1134
[ "v1" ] = new OpenApiInfo { Version = "V1" , Title = "Test API" }
1140
1135
} ,
1141
- SecuritySchemes = new Dictionary < string , OpenApiSecurityScheme >
1142
- {
1143
- [ "Bearer" ] = new OpenApiSecurityScheme { Type = SecuritySchemeType . ApiKey , Scheme = "someSpecialOne" }
1144
- }
1136
+ InferSecuritySchemes = inferSecuritySchemes ,
1137
+ SecuritySchemesSelector = ( authenticationSchemes ) =>
1138
+ authenticationSchemes
1139
+ . ToDictionary (
1140
+ ( authScheme ) => authScheme . Name ,
1141
+ ( authScheme ) => new OpenApiSecurityScheme ( ) )
1145
1142
}
1146
1143
) ;
1147
1144
1148
1145
var document = await subject . GetSwaggerAsync ( "v1" ) ;
1149
1146
1150
- var securityScheme = Assert . Single ( document . Components . SecuritySchemes ) ;
1151
- Assert . Equal ( "Bearer" , securityScheme . Key ) ;
1152
- Assert . Equal ( SecuritySchemeType . ApiKey , securityScheme . Value . Type ) ;
1153
- Assert . Equal ( "someSpecialOne" , securityScheme . Value . Scheme ) ;
1147
+ Assert . Equal ( expectedSecuritySchemeNames , document . Components . SecuritySchemes . Keys ) ;
1154
1148
}
1155
1149
1156
1150
[ Fact ]
@@ -1283,13 +1277,16 @@ public void GetSwagger_SupportsOption_DocumentFilters()
1283
1277
Assert . Contains ( "ComplexType" , document . Components . Schemas . Keys ) ;
1284
1278
}
1285
1279
1286
- private SwaggerGenerator Subject ( IEnumerable < ApiDescription > apiDescriptions , SwaggerGeneratorOptions options = null )
1280
+ private SwaggerGenerator Subject (
1281
+ IEnumerable < ApiDescription > apiDescriptions ,
1282
+ SwaggerGeneratorOptions options = null ,
1283
+ IEnumerable < AuthenticationScheme > authenticationSchemes = null )
1287
1284
{
1288
1285
return new SwaggerGenerator (
1289
1286
options ?? DefaultOptions ,
1290
1287
new FakeApiDescriptionGroupCollectionProvider ( apiDescriptions ) ,
1291
1288
new SchemaGenerator ( new SchemaGeneratorOptions ( ) , new JsonSerializerDataContractResolver ( new JsonSerializerOptions ( ) ) ) ,
1292
- new TestAuthenticationSchemeProvider ( )
1289
+ new FakeAuthenticationSchemeProvider ( authenticationSchemes ?? Enumerable . Empty < AuthenticationScheme > ( ) )
1293
1290
) ;
1294
1291
}
1295
1292
@@ -1301,41 +1298,4 @@ private SwaggerGenerator Subject(IEnumerable<ApiDescription> apiDescriptions, Sw
1301
1298
}
1302
1299
} ;
1303
1300
}
1304
-
1305
- class TestAuthenticationSchemeProvider : IAuthenticationSchemeProvider
1306
- {
1307
- private readonly IEnumerable < AuthenticationScheme > _authenticationSchemes = new AuthenticationScheme [ ]
1308
- {
1309
- new AuthenticationScheme ( "Bearer" , null , typeof ( IAuthenticationHandler ) )
1310
- } ;
1311
-
1312
- public void AddScheme ( AuthenticationScheme scheme )
1313
- => throw new NotImplementedException ( ) ;
1314
- public Task < IEnumerable < AuthenticationScheme > > GetAllSchemesAsync ( )
1315
- => Task . FromResult ( _authenticationSchemes ) ;
1316
-
1317
- public Task < AuthenticationScheme > GetDefaultAuthenticateSchemeAsync ( )
1318
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1319
-
1320
- public Task < AuthenticationScheme > GetDefaultChallengeSchemeAsync ( )
1321
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1322
-
1323
- public Task < AuthenticationScheme > GetDefaultForbidSchemeAsync ( )
1324
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1325
-
1326
- public Task < AuthenticationScheme > GetDefaultSignInSchemeAsync ( )
1327
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1328
-
1329
- public Task < AuthenticationScheme > GetDefaultSignOutSchemeAsync ( )
1330
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1331
-
1332
- public Task < IEnumerable < AuthenticationScheme > > GetRequestHandlerSchemesAsync ( )
1333
- => throw new NotImplementedException ( ) ;
1334
-
1335
- public Task < AuthenticationScheme > GetSchemeAsync ( string name )
1336
- => Task . FromResult ( _authenticationSchemes . First ( ) ) ;
1337
-
1338
- public void RemoveScheme ( string name )
1339
- => throw new NotImplementedException ( ) ;
1340
- }
1341
1301
}
0 commit comments