Skip to content

Commit f3d5d7c

Browse files
committed
Address feedback and update API
1 parent 2a1e476 commit f3d5d7c

10 files changed

+45
-90
lines changed

src/Http/Authentication.Abstractions/src/IAuthenticationConfigurationProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public interface IAuthenticationConfigurationProvider
1515
/// Returns the specified <see cref="ConfigurationSection"/> object.
1616
/// </summary>
1717
/// <param name="authenticationScheme">The path to the section to be returned.</param>
18-
/// <returns>The specified <see cref="ConfigurationSection"/> object, or null if the requested section does not exist.</returns>
18+
/// <returns>The specified <see cref="IConfiguration"/> object, or null if the requested section does not exist.</returns>
1919
IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme);
2020

2121
/// <summary>
2222
/// Returns the <see cref="ConfigurationSection"/> where authentication
2323
/// options are stored.
2424
/// </summary>
25-
/// <returns>The specified <see cref="ConfigurationSection"/> object, or null if the requested section does not exist.</returns>
25+
/// <returns>The specified <see cref="IConfiguration"/> object, or null if the requested section does not exist.</returns>
2626
IConfiguration GetAuthenticationConfiguration();
2727
}

src/Security/Authentication/Core/src/AuthenticationConfigurationConstants.cs

-24
This file was deleted.

src/Security/Authentication/Core/src/AuthenticationConfigureOptions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.AspNetCore.Authentication;
88
internal sealed class AuthenticationConfigureOptions : IConfigureOptions<AuthenticationOptions>
99
{
1010
private readonly IAuthenticationConfigurationProvider _authenticationConfigurationProvider;
11+
private const string DefaultSchemeKey = "DefaultScheme";
1112

1213
public AuthenticationConfigureOptions(IAuthenticationConfigurationProvider configurationProvider)
1314
{
@@ -17,8 +18,8 @@ public AuthenticationConfigureOptions(IAuthenticationConfigurationProvider confi
1718
public void Configure(AuthenticationOptions options)
1819
{
1920
var authenticationConfig = _authenticationConfigurationProvider.GetAuthenticationConfiguration();
20-
var defaultScheme = authenticationConfig[AuthenticationConfigurationConstants.DefaultScheme];
21-
if (defaultScheme is not null)
21+
var defaultScheme = authenticationConfig[DefaultSchemeKey];
22+
if (!string.IsNullOrEmpty(defaultScheme))
2223
{
2324
options.DefaultScheme = defaultScheme;
2425
}

src/Security/Authentication/Core/src/DefaultAuthenticationConfigurationProvider.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Microsoft.AspNetCore.Authentication;
88
internal sealed class DefaultAuthenticationConfigurationProvider : IAuthenticationConfigurationProvider
99
{
1010
private readonly IConfiguration _configuration;
11+
private const string AuthenticationKey = "Authentication";
12+
private const string AuthenticationSchemesKey = "Authentication:Schemes";
1113

1214
public DefaultAuthenticationConfigurationProvider(IConfiguration configuration)
1315
{
@@ -16,11 +18,11 @@ public DefaultAuthenticationConfigurationProvider(IConfiguration configuration)
1618

1719
public IConfiguration GetAuthenticationConfiguration()
1820
{
19-
return _configuration.GetSection(AuthenticationConfigurationConstants.Authentication);
21+
return _configuration.GetSection(AuthenticationKey);
2022
}
2123

2224
public IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme)
2325
{
24-
return _configuration.GetSection($"{AuthenticationConfigurationConstants.Authentication}:{AuthenticationConfigurationConstants.Schemes}:{authenticationScheme}");
26+
return _configuration.GetSection($"{AuthenticationSchemesKey}:{authenticationScheme}");
2527
}
2628
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Authentication.AuthenticationConfigurationConstants
3-
const Microsoft.AspNetCore.Authentication.AuthenticationConfigurationConstants.Authentication = "Authentication" -> string!
4-
const Microsoft.AspNetCore.Authentication.AuthenticationConfigurationConstants.DefaultScheme = "DefaultScheme" -> string!
5-
const Microsoft.AspNetCore.Authentication.AuthenticationConfigurationConstants.Schemes = "Schemes" -> string!

src/Security/Authentication/test/CertificateTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public class ClientCertificateAuthenticationTests
2525
[Fact]
2626
public async Task VerifySchemeDefaults()
2727
{
28-
var services = new ServiceCollection();
29-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
28+
var services = new ServiceCollection().ConfigureAuthTestServices();
3029
services.AddAuthentication().AddCertificate();
3130
var sp = services.BuildServiceProvider();
3231
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();

src/Security/Authentication/test/OpenIdConnect/OpenIdConnectConfigurationTests.cs

+9-22
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ private void ConfigureDefaults(OpenIdConnectOptions o)
2828
[Fact]
2929
public async Task CanForwardDefault()
3030
{
31-
var services = new ServiceCollection().AddLogging();
32-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
33-
31+
var services = new ServiceCollection().ConfigureAuthTestServices();
3432
services.AddAuthentication(o =>
3533
{
3634
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -73,8 +71,7 @@ public async Task CanForwardDefault()
7371
[Fact]
7472
public async Task ForwardSignInThrows()
7573
{
76-
var services = new ServiceCollection().AddLogging();
77-
74+
var services = new ServiceCollection().ConfigureAuthTestServices();
7875
services.AddAuthentication(o =>
7976
{
8077
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -103,9 +100,7 @@ public async Task ForwardSignInThrows()
103100
[Fact]
104101
public async Task ForwardSignOutWinsOverDefault()
105102
{
106-
var services = new ServiceCollection().AddLogging();
107-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
108-
103+
var services = new ServiceCollection().ConfigureAuthTestServices();
109104
services.AddAuthentication(o =>
110105
{
111106
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -145,9 +140,7 @@ public async Task ForwardSignOutWinsOverDefault()
145140
[Fact]
146141
public async Task ForwardForbidWinsOverDefault()
147142
{
148-
var services = new ServiceCollection().AddLogging();
149-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
150-
143+
var services = new ServiceCollection().ConfigureAuthTestServices();
151144
services.AddAuthentication(o =>
152145
{
153146
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -187,9 +180,7 @@ public async Task ForwardForbidWinsOverDefault()
187180
[Fact]
188181
public async Task ForwardAuthenticateWinsOverDefault()
189182
{
190-
var services = new ServiceCollection().AddLogging();
191-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
192-
183+
var services = new ServiceCollection().ConfigureAuthTestServices();
193184
services.AddAuthentication(o =>
194185
{
195186
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -229,8 +220,7 @@ public async Task ForwardAuthenticateWinsOverDefault()
229220
[Fact]
230221
public async Task ForwardChallengeWinsOverDefault()
231222
{
232-
var services = new ServiceCollection().AddLogging();
233-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
223+
var services = new ServiceCollection().ConfigureAuthTestServices();
234224
services.AddAuthentication(o =>
235225
{
236226
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -270,8 +260,7 @@ public async Task ForwardChallengeWinsOverDefault()
270260
[Fact]
271261
public async Task ForwardSelectorWinsOverDefault()
272262
{
273-
var services = new ServiceCollection().AddLogging();
274-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
263+
var services = new ServiceCollection().ConfigureAuthTestServices();
275264
services.AddAuthentication(o =>
276265
{
277266
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -326,8 +315,7 @@ public async Task ForwardSelectorWinsOverDefault()
326315
[Fact]
327316
public async Task NullForwardSelectorUsesDefault()
328317
{
329-
var services = new ServiceCollection().AddLogging();
330-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
318+
var services = new ServiceCollection().ConfigureAuthTestServices();
331319
services.AddAuthentication(o =>
332320
{
333321
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;
@@ -382,8 +370,7 @@ public async Task NullForwardSelectorUsesDefault()
382370
[Fact]
383371
public async Task SpecificForwardWinsOverSelectorAndDefault()
384372
{
385-
var services = new ServiceCollection().AddLogging();
386-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
373+
var services = new ServiceCollection().ConfigureAuthTestServices();
387374
services.AddAuthentication(o =>
388375
{
389376
o.DefaultScheme = OpenIdConnectDefaults.AuthenticationScheme;

src/Security/Authentication/test/PolicyTests.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public async Task CanDispatch()
5454
[Fact]
5555
public async Task DefaultTargetSelectorWinsOverDefaultTarget()
5656
{
57-
var services = new ServiceCollection().AddOptions().AddLogging();
58-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
57+
var services = new ServiceCollection().ConfigureAuthTestServices();
5958
services.AddAuthentication(o =>
6059
{
6160
o.AddScheme<TestHandler>("auth1", "auth1");
@@ -111,8 +110,7 @@ public async Task DefaultTargetSelectorWinsOverDefaultTarget()
111110
[Fact]
112111
public async Task NullDefaultTargetSelectorFallsBacktoDefaultTarget()
113112
{
114-
var services = new ServiceCollection().AddOptions().AddLogging();
115-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
113+
var services = new ServiceCollection().ConfigureAuthTestServices();
116114
services.AddAuthentication(o =>
117115
{
118116
o.AddScheme<TestHandler>("auth1", "auth1");
@@ -168,8 +166,7 @@ public async Task NullDefaultTargetSelectorFallsBacktoDefaultTarget()
168166
[Fact]
169167
public async Task SpecificTargetAlwaysWinsOverDefaultTarget()
170168
{
171-
var services = new ServiceCollection().AddOptions().AddLogging();
172-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
169+
var services = new ServiceCollection().ConfigureAuthTestServices();
173170
services.AddAuthentication(o =>
174171
{
175172
o.AddScheme<TestHandler>("auth1", "auth1");
@@ -230,8 +227,7 @@ public async Task SpecificTargetAlwaysWinsOverDefaultTarget()
230227
[Fact]
231228
public async Task VirtualSchemeTargetsForwardWithDefaultTarget()
232229
{
233-
var services = new ServiceCollection().AddOptions().AddLogging();
234-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
230+
var services = new ServiceCollection().ConfigureAuthTestServices();
235231
services.AddAuthentication(o =>
236232
{
237233
o.AddScheme<TestHandler>("auth1", "auth1");
@@ -283,8 +279,7 @@ public async Task VirtualSchemeTargetsForwardWithDefaultTarget()
283279
[Fact]
284280
public async Task VirtualSchemeTargetsOverrideDefaultTarget()
285281
{
286-
var services = new ServiceCollection().AddOptions().AddLogging();
287-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
282+
var services = new ServiceCollection().ConfigureAuthTestServices();
288283
services.AddAuthentication(o =>
289284
{
290285
o.AddScheme<TestHandler>("auth1", "auth1");

src/Security/Authentication/test/SharedAuthenticationTests.cs

+11-22
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public abstract class SharedAuthenticationTests<TOptions> where TOptions : Authe
2626
[Fact]
2727
public async Task CanForwardDefault()
2828
{
29-
var services = new ServiceCollection().AddLogging();
30-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
29+
var services = new ServiceCollection().ConfigureAuthTestServices();
3130

3231
var builder = services.AddAuthentication(o =>
3332
{
@@ -84,8 +83,7 @@ public async Task ForwardSignInWinsOverDefault()
8483
{
8584
if (SupportsSignIn)
8685
{
87-
var services = new ServiceCollection().AddLogging();
88-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
86+
var services = new ServiceCollection().ConfigureAuthTestServices();
8987

9088
var builder = services.AddAuthentication(o =>
9189
{
@@ -128,8 +126,7 @@ public async Task ForwardSignOutWinsOverDefault()
128126
{
129127
if (SupportsSignOut)
130128
{
131-
var services = new ServiceCollection().AddLogging();
132-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
129+
var services = new ServiceCollection().ConfigureAuthTestServices();
133130
var builder = services.AddAuthentication(o =>
134131
{
135132
o.DefaultScheme = DefaultScheme;
@@ -169,8 +166,7 @@ public async Task ForwardSignOutWinsOverDefault()
169166
[Fact]
170167
public async Task ForwardForbidWinsOverDefault()
171168
{
172-
var services = new ServiceCollection().AddLogging();
173-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
169+
var services = new ServiceCollection().ConfigureAuthTestServices();
174170
var builder = services.AddAuthentication(o =>
175171
{
176172
o.DefaultScheme = DefaultScheme;
@@ -219,8 +215,7 @@ public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
219215
[Fact]
220216
public async Task ForwardAuthenticateOnlyRunsTransformOnceByDefault()
221217
{
222-
var services = new ServiceCollection().AddLogging();
223-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
218+
var services = new ServiceCollection().ConfigureAuthTestServices();
224219
var transform = new RunOnce();
225220
var builder = services.AddSingleton<IClaimsTransformation>(transform).AddAuthentication(o =>
226221
{
@@ -250,8 +245,7 @@ public async Task ForwardAuthenticateOnlyRunsTransformOnceByDefault()
250245
[Fact]
251246
public async Task ForwardAuthenticateWinsOverDefault()
252247
{
253-
var services = new ServiceCollection().AddLogging();
254-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
248+
var services = new ServiceCollection().ConfigureAuthTestServices();
255249
var builder = services.AddAuthentication(o =>
256250
{
257251
o.DefaultScheme = DefaultScheme;
@@ -290,8 +284,7 @@ public async Task ForwardAuthenticateWinsOverDefault()
290284
[Fact]
291285
public async Task ForwardChallengeWinsOverDefault()
292286
{
293-
var services = new ServiceCollection().AddLogging();
294-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
287+
var services = new ServiceCollection().ConfigureAuthTestServices();
295288
var builder = services.AddAuthentication(o =>
296289
{
297290
o.DefaultScheme = DefaultScheme;
@@ -330,8 +323,7 @@ public async Task ForwardChallengeWinsOverDefault()
330323
[Fact]
331324
public async Task ForwardSelectorWinsOverDefault()
332325
{
333-
var services = new ServiceCollection().AddLogging();
334-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
326+
var services = new ServiceCollection().ConfigureAuthTestServices();
335327
var builder = services.AddAuthentication(o =>
336328
{
337329
o.DefaultScheme = DefaultScheme;
@@ -400,8 +392,7 @@ public async Task ForwardSelectorWinsOverDefault()
400392
[Fact]
401393
public async Task NullForwardSelectorUsesDefault()
402394
{
403-
var services = new ServiceCollection().AddLogging();
404-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
395+
var services = new ServiceCollection().ConfigureAuthTestServices();
405396
var builder = services.AddAuthentication(o =>
406397
{
407398
o.DefaultScheme = DefaultScheme;
@@ -470,8 +461,7 @@ public async Task NullForwardSelectorUsesDefault()
470461
[Fact]
471462
public async Task SpecificForwardWinsOverSelectorAndDefault()
472463
{
473-
var services = new ServiceCollection().AddLogging();
474-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
464+
var services = new ServiceCollection().ConfigureAuthTestServices();
475465
var builder = services.AddAuthentication(o =>
476466
{
477467
o.DefaultScheme = DefaultScheme;
@@ -545,8 +535,7 @@ public async Task SpecificForwardWinsOverSelectorAndDefault()
545535
[Fact]
546536
public async Task VerifySchemeDefaults()
547537
{
548-
var services = new ServiceCollection();
549-
services.AddSingleton<IConfiguration>(new ConfigurationManager());
538+
var services = new ServiceCollection().ConfigureAuthTestServices();
550539
var builder = services.AddAuthentication();
551540
RegisterAuth(builder, o => { });
552541
var sp = services.BuildServiceProvider();

src/Security/Authentication/test/TestExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Xml.Linq;
88
using Microsoft.AspNetCore.Http;
99
using Microsoft.AspNetCore.TestHost;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
1012

1113
namespace Microsoft.AspNetCore.Authentication;
1214

@@ -77,4 +79,12 @@ public static Task DescribeAsync(this HttpResponse res, IEnumerable<Authenticati
7779
return res.Body.WriteAsync(xmlBytes, 0, xmlBytes.Length);
7880
}
7981

82+
public static IServiceCollection ConfigureAuthTestServices(this IServiceCollection services)
83+
{
84+
return services
85+
.AddOptions()
86+
.AddLogging()
87+
.AddSingleton<IConfiguration>(new ConfigurationManager());
88+
}
89+
8090
}

0 commit comments

Comments
 (0)