Skip to content

Commit db855a5

Browse files
Remove support for setting default scheme from user-jwts (#42578)
* Revert "Fix up reading auth schemes and setting default scheme (#42452)" This reverts commit f0e1d1e. * Bring back non-default scheme related changes * Remove AuthenticationConfigureOptions * Update src/Tools/dotnet-user-jwts/src/Helpers/JwtAuthenticationSchemeSettings.cs Co-authored-by: Chris Ross <Tratcher@Outlook.com> Co-authored-by: Chris Ross <Tratcher@Outlook.com>
1 parent 33e011e commit db855a5

File tree

8 files changed

+21
-270
lines changed

8 files changed

+21
-270
lines changed

src/Http/Authentication.Core/src/AuthenticationService.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Microsoft.AspNetCore.Authentication;
1414
public class AuthenticationService : IAuthenticationService
1515
{
1616
private HashSet<ClaimsPrincipal>? _transformCache;
17-
private const string defaultSchemesOptionsMsg = "The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions) or by setting the Authentication:DefaultScheme property in configuration.";
1817

1918
/// <summary>
2019
/// Constructor.
@@ -65,7 +64,7 @@ public virtual async Task<AuthenticateResult> AuthenticateAsync(HttpContext cont
6564
scheme = defaultScheme?.Name;
6665
if (scheme == null)
6766
{
68-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultAuthenticateScheme found. {defaultSchemesOptionsMsg}");
67+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultAuthenticateScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
6968
}
7069
}
7170

@@ -113,7 +112,7 @@ public virtual async Task ChallengeAsync(HttpContext context, string? scheme, Au
113112
scheme = defaultChallengeScheme?.Name;
114113
if (scheme == null)
115114
{
116-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultChallengeScheme found. {defaultSchemesOptionsMsg}");
115+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
117116
}
118117
}
119118

@@ -141,7 +140,7 @@ public virtual async Task ForbidAsync(HttpContext context, string? scheme, Authe
141140
scheme = defaultForbidScheme?.Name;
142141
if (scheme == null)
143142
{
144-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultForbidScheme found. {defaultSchemesOptionsMsg}");
143+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultForbidScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
145144
}
146145
}
147146

@@ -187,7 +186,7 @@ public virtual async Task SignInAsync(HttpContext context, string? scheme, Claim
187186
scheme = defaultScheme?.Name;
188187
if (scheme == null)
189188
{
190-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultSignInScheme found. {defaultSchemesOptionsMsg}");
189+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultSignInScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
191190
}
192191
}
193192

@@ -221,7 +220,7 @@ public virtual async Task SignOutAsync(HttpContext context, string? scheme, Auth
221220
scheme = defaultScheme?.Name;
222221
if (scheme == null)
223222
{
224-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultSignOutScheme found. {defaultSchemesOptionsMsg}");
223+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultSignOutScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
225224
}
226225
}
227226

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

-29
This file was deleted.

src/Security/Authentication/Core/src/AuthenticationServiceCollectionExtensions.cs

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Microsoft.AspNetCore.Authentication;
55
using Microsoft.Extensions.DependencyInjection.Extensions;
6-
using Microsoft.Extensions.Options;
76

87
namespace Microsoft.Extensions.DependencyInjection;
98

@@ -29,7 +28,6 @@ public static AuthenticationBuilder AddAuthentication(this IServiceCollection se
2928
services.AddWebEncoders();
3029
services.TryAddSingleton<ISystemClock, SystemClock>();
3130
services.TryAddSingleton<IAuthenticationConfigurationProvider, DefaultAuthenticationConfigurationProvider>();
32-
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, AuthenticationConfigureOptions>());
3331

3432
return new AuthenticationBuilder(services);
3533
}

src/Security/Authentication/test/JwtBearerTests.cs

-35
Original file line numberDiff line numberDiff line change
@@ -885,41 +885,6 @@ public async Task ExpirationAndIssuedNullWhenMinOrMaxValue()
885885
Assert.Equal(JsonValueKind.Null, dom.RootElement.GetProperty("issued").ValueKind);
886886
}
887887

888-
[Fact]
889-
public async Task ForwardSchemeOverridesSchemeFromConfig()
890-
{
891-
// Arrange
892-
var defaultSchemeFromConfig = "DefaultSchemeFromConfig";
893-
var defaultSchemeFromForward = "DefaultSchemeFromForward";
894-
var services = new ServiceCollection().AddLogging();
895-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
896-
{
897-
new KeyValuePair<string, string>("Authentication:DefaultScheme", defaultSchemeFromConfig)
898-
}).Build();
899-
services.AddSingleton<IConfiguration>(config);
900-
901-
// Act
902-
var builder = services.AddAuthentication(o =>
903-
{
904-
o.AddScheme<TestHandler>(defaultSchemeFromForward, defaultSchemeFromForward);
905-
});
906-
builder.AddJwtBearer(defaultSchemeFromConfig, o => o.ForwardAuthenticate = defaultSchemeFromForward);
907-
var forwardAuthentication = new TestHandler();
908-
services.AddSingleton(forwardAuthentication);
909-
910-
var sp = services.BuildServiceProvider();
911-
var context = new DefaultHttpContext();
912-
context.RequestServices = sp;
913-
914-
// Assert
915-
Assert.Equal(0, forwardAuthentication.AuthenticateCount);
916-
await context.AuthenticateAsync();
917-
Assert.Equal(1, forwardAuthentication.AuthenticateCount);
918-
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
919-
var defaultSchemeFromServices = await schemeProvider.GetDefaultAuthenticateSchemeAsync();
920-
Assert.Equal(defaultSchemeFromConfig, defaultSchemeFromServices.Name);
921-
}
922-
923888
class InvalidTokenValidator : ISecurityTokenValidator
924889
{
925890
public InvalidTokenValidator()

src/Security/Authentication/test/SharedAuthenticationTests.cs

-88
Original file line numberDiff line numberDiff line change
@@ -545,92 +545,4 @@ public async Task VerifySchemeDefaults()
545545
Assert.Equal(HandlerType, scheme.HandlerType);
546546
Assert.Equal(DisplayName, scheme.DisplayName);
547547
}
548-
549-
[Fact]
550-
public async Task RespectsDefaultSchemeInConfig()
551-
{
552-
// Arrange
553-
var defaultSchemeFromConfig = "DefaultSchemeFromConfig";
554-
var services = new ServiceCollection();
555-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
556-
{
557-
new KeyValuePair<string, string>("Authentication:DefaultScheme", defaultSchemeFromConfig)
558-
}).Build();
559-
services.AddSingleton<IConfiguration>(config);
560-
561-
// Act
562-
var builder = services.AddAuthentication(o =>
563-
{
564-
o.AddScheme<TestHandler>(defaultSchemeFromConfig, defaultSchemeFromConfig);
565-
});
566-
RegisterAuth(builder, _ => { });
567-
var sp = services.BuildServiceProvider();
568-
569-
// Assert
570-
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
571-
var defaultSchemeFromServices = await schemeProvider.GetDefaultAuthenticateSchemeAsync();
572-
Assert.Equal(defaultSchemeFromConfig, defaultSchemeFromServices.Name);
573-
}
574-
575-
[Fact]
576-
public async Task CanOverrideDefaultInConfigViaAddAuthentication()
577-
{
578-
// Arrange
579-
var defaultSchemeFromConfig = "DefaultSchemeFromConfig";
580-
var defaultSchemeFromAddAuth = "DefaultSchemeFromAddAuth";
581-
var services = new ServiceCollection();
582-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
583-
{
584-
new KeyValuePair<string, string>("Authentication:DefaultScheme", defaultSchemeFromConfig)
585-
}).Build();
586-
services.AddSingleton<IConfiguration>(config);
587-
588-
// Act
589-
var builder = services.AddAuthentication(o =>
590-
{
591-
o.DefaultScheme = defaultSchemeFromAddAuth;
592-
o.AddScheme<TestHandler>(defaultSchemeFromConfig, defaultSchemeFromConfig);
593-
o.AddScheme<TestHandler>(defaultSchemeFromAddAuth, defaultSchemeFromAddAuth);
594-
});
595-
RegisterAuth(builder, _ => { });
596-
var sp = services.BuildServiceProvider();
597-
598-
// Assert
599-
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
600-
var defaultSchemeFromServices = await schemeProvider.GetDefaultAuthenticateSchemeAsync();
601-
Assert.Equal(defaultSchemeFromAddAuth, defaultSchemeFromServices.Name);
602-
}
603-
604-
[Fact]
605-
public async Task DoesNotOverrideDefaultSchemeSetViaOptions()
606-
{
607-
// Arrange
608-
var defaultSchemeFromConfig = "DefaultSchemeFromConfig";
609-
var defaultSchemeFromOptions = "DefaultSchemeFromOptions";
610-
var services = new ServiceCollection();
611-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
612-
{
613-
new KeyValuePair<string, string>("Authentication:DefaultScheme", defaultSchemeFromConfig)
614-
}).Build();
615-
services.AddSingleton<IConfiguration>(config);
616-
617-
// Act
618-
services.Configure<AuthenticationOptions>(options =>
619-
{
620-
options.DefaultScheme = defaultSchemeFromOptions;
621-
});
622-
var builder = services.AddAuthentication(o =>
623-
{
624-
o.AddScheme<TestHandler>(defaultSchemeFromConfig, defaultSchemeFromConfig);
625-
o.AddScheme<TestHandler>(defaultSchemeFromOptions, defaultSchemeFromOptions);
626-
});
627-
RegisterAuth(builder, _ => { });
628-
var sp = services.BuildServiceProvider();
629-
630-
// Assert
631-
var schemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
632-
var defaultSchemeFromServices = await schemeProvider.GetDefaultAuthenticateSchemeAsync();
633-
Assert.Equal(defaultSchemeFromOptions, defaultSchemeFromServices.Name);
634-
635-
}
636548
}

src/Tools/dotnet-user-jwts/src/Helpers/JwtAuthenticationSchemeSettings.cs

-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools;
1010
internal sealed record JwtAuthenticationSchemeSettings(string SchemeName, List<string> Audiences, string ClaimsIssuer)
1111
{
1212
private const string AuthenticationKey = "Authentication";
13-
private const string DefaultSchemeKey = "DefaultScheme";
1413
private const string SchemesKey = "Schemes";
1514

1615
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
@@ -57,15 +56,6 @@ public void Save(string filePath)
5756
};
5857
}
5958

60-
// Set the DefaultScheme if it has not already been set
61-
// and only a single scheme has been configured thus far
62-
if (config[AuthenticationKey][DefaultSchemeKey] is null
63-
&& config[AuthenticationKey][SchemesKey] is JsonObject setSchemes
64-
&& setSchemes.Count == 1)
65-
{
66-
config[AuthenticationKey][DefaultSchemeKey] = SchemeName;
67-
}
68-
6959
using var writer = new FileStream(filePath, FileMode.Open, FileAccess.Write);
7060
JsonSerializer.Serialize(writer, config, _jsonSerializerOptions);
7161
}
@@ -80,11 +70,6 @@ public static void RemoveScheme(string filePath, string name)
8070
authentication[SchemesKey] is JsonObject schemes)
8171
{
8272
schemes.Remove(name);
83-
if (authentication[DefaultSchemeKey] is JsonValue defaultScheme
84-
&& defaultScheme.GetValue<string>() == name)
85-
{
86-
authentication.Remove(DefaultSchemeKey);
87-
}
8873
}
8974

9075
using var writer = new FileStream(filePath, FileMode.Create, FileAccess.Write);

src/Tools/dotnet-user-jwts/test/UserJwtsTestFixture.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class UserJwtsTestFixture : IDisposable
6262
}
6363
}";
6464

65-
public string CreateProject(bool hasSecret = true, string appSettingsContent = "{}")
65+
public string CreateProject(bool hasSecret = true)
6666
{
6767
var projectPath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "userjwtstest", Guid.NewGuid().ToString()));
6868
Directory.CreateDirectory(Path.Combine(projectPath.FullName, "Properties"));
@@ -81,7 +81,7 @@ public string CreateProject(bool hasSecret = true, string appSettingsContent = "
8181

8282
File.WriteAllText(
8383
Path.Combine(projectPath.FullName, "appsettings.Development.json"),
84-
appSettingsContent);
84+
"{}");
8585

8686
if (hasSecret)
8787
{

0 commit comments

Comments
 (0)