Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading default scheme from config #41987

Merged
merged 5 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public class AzureADAuthenticationBuilderExtensionsTests
public void AddAzureAD_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand All @@ -39,8 +37,7 @@ public void AddAzureAD_AddsAllAuthenticationHandlers()
public void AddAzureAD_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -89,8 +86,7 @@ public void AddAzureAD_ConfiguresAllOptions()
public void AddAzureAD_AllowsOverridingCookiesAndOpenIdConnectSettings()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -134,8 +130,7 @@ public void AddAzureAD_AllowsOverridingCookiesAndOpenIdConnectSettings()
public void AddAzureAD_RegisteringAddCookiesAndAddOpenIdConnectHasNoImpactOnAzureAAExtensions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -181,8 +176,7 @@ public void AddAzureAD_RegisteringAddCookiesAndAddOpenIdConnectHasNoImpactOnAzur
public void AddAzureAD_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { })
Expand All @@ -202,8 +196,7 @@ public void AddAzureAD_ThrowsForDuplicatedSchemes()
public void AddAzureAD_ThrowsWhenOpenIdSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { })
Expand All @@ -226,8 +219,7 @@ public void AddAzureAD_ThrowsWhenOpenIdSchemeIsAlreadyInUse()
public void AddAzureAD_ThrowsWhenCookieSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { })
Expand All @@ -250,8 +242,7 @@ public void AddAzureAD_ThrowsWhenCookieSchemeIsAlreadyInUse()
public void AddAzureAD_ThrowsWhenInstanceIsNotSet()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { });
Expand All @@ -271,8 +262,7 @@ public void AddAzureAD_ThrowsWhenInstanceIsNotSet()
[Fact]
public void AddAzureAD_SkipsOptionsValidationForNonAzureCookies()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { })
Expand All @@ -288,9 +278,7 @@ public void AddAzureAD_SkipsOptionsValidationForNonAzureCookies()
public void AddAzureADBearer_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand All @@ -306,9 +294,7 @@ public void AddAzureADBearer_AddsAllAuthenticationHandlers()
public void AddAzureADBearer_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -342,9 +328,7 @@ public void AddAzureADBearer_ConfiguresAllOptions()
public void AddAzureADBearer_CanOverrideJwtBearerOptionsConfiguration()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -376,9 +360,7 @@ public void AddAzureADBearer_CanOverrideJwtBearerOptionsConfiguration()
public void AddAzureADBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -411,8 +393,7 @@ public void AddAzureADBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensions(
public void AddAzureADBearer_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADBearer(o => { })
Expand All @@ -432,8 +413,7 @@ public void AddAzureADBearer_ThrowsForDuplicatedSchemes()
public void AddAzureADBearer_ThrowsWhenBearerSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADBearer(o => { })
Expand All @@ -456,8 +436,7 @@ public void AddAzureADBearer_ThrowsWhenBearerSchemeIsAlreadyInUse()
public void AddAzureADBearer_ThrowsWhenInstanceIsNotSet()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADBearer(o => { });
Expand All @@ -477,9 +456,7 @@ public void AddAzureADBearer_ThrowsWhenInstanceIsNotSet()
[Fact]
public void AddAzureADBearer_SkipsOptionsValidationForNonAzureCookies()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADBearer(o => { })
Expand All @@ -494,8 +471,7 @@ public void AddAzureADBearer_SkipsOptionsValidationForNonAzureCookies()
[Fact]
public void AddAzureAD_SkipsOptionsValidationForNonAzureOpenIdConnect()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureAD(o => { })
Expand All @@ -510,4 +486,12 @@ public void AddAzureAD_SkipsOptionsValidationForNonAzureOpenIdConnect()

Assert.NotNull(openIdConnectOptions.Get("other"));
}

private IServiceCollection GenerateServicesForTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really nitty (don't feel obligated to do this): GenerateTestServices? But I'm not a huge fan of method names as sentences without spaces.

{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class AzureADB2CAuthenticationBuilderExtensionsTests
public void AddAzureADB2C_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand All @@ -38,8 +37,7 @@ public void AddAzureADB2C_AddsAllAuthenticationHandlers()
public void AddAzureADB2C_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -102,8 +100,7 @@ public void AddAzureADB2C_ConfiguresAllOptions()
public void AddAzureADB2C_AllowsOverridingCookiesAndOpenIdConnectSettings()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -146,8 +143,7 @@ public void AddAzureADB2C_AllowsOverridingCookiesAndOpenIdConnectSettings()
public void AddAzureADB2C_RegisteringAddCookiesAndAddOpenIdConnectHasNoImpactOnAzureAAExtensions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -192,8 +188,7 @@ public void AddAzureADB2C_RegisteringAddCookiesAndAddOpenIdConnectHasNoImpactOnA
public void AddAzureADB2C_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADB2C(o => { })
Expand All @@ -213,8 +208,7 @@ public void AddAzureADB2C_ThrowsForDuplicatedSchemes()
public void AddAzureADB2C_ThrowsWhenOpenIdSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADB2C(o => { })
Expand All @@ -237,8 +231,7 @@ public void AddAzureADB2C_ThrowsWhenOpenIdSchemeIsAlreadyInUse()
public void AddAzureADB2C_ThrowsWhenCookieSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADB2C(o => { })
Expand All @@ -261,9 +254,7 @@ public void AddAzureADB2C_ThrowsWhenCookieSchemeIsAlreadyInUse()
public void AddAzureADB2CBearer_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand All @@ -279,9 +270,7 @@ public void AddAzureADB2CBearer_AddsAllAuthenticationHandlers()
public void AddAzureADB2CBearer_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -316,9 +305,7 @@ public void AddAzureADB2CBearer_ConfiguresAllOptions()
public void AddAzureADB2CBearer_CanOverrideJwtBearerOptionsConfiguration()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -350,9 +337,7 @@ public void AddAzureADB2CBearer_CanOverrideJwtBearerOptionsConfiguration()
public void AddAzureADB2CBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
var services = GenerateServicesForTest();

// Act
services.AddAuthentication()
Expand Down Expand Up @@ -385,8 +370,7 @@ public void AddAzureADB2CBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensio
public void AddAzureADB2CBearer_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADB2CBearer(o => { })
Expand All @@ -406,8 +390,7 @@ public void AddAzureADB2CBearer_ThrowsForDuplicatedSchemes()
public void AddAzureADB2CBearer_ThrowsWhenBearerSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
var services = GenerateServicesForTest();

services.AddAuthentication()
.AddAzureADB2CBearer(o => { })
Expand All @@ -425,4 +408,12 @@ public void AddAzureADB2CBearer_ThrowsWhenBearerSchemeIsAlreadyInUse()

Assert.Equal(expectedMessage, exception.Message);
}

private IServiceCollection GenerateServicesForTest()
{
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddSingleton<IConfiguration>(new ConfigurationManager());
return services;
}
}
12 changes: 6 additions & 6 deletions src/DefaultBuilder/src/WebApplicationAuthenticationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ public WebApplicationAuthenticationBuilder(IServiceCollection services) : base(s

public override AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, Action<PolicySchemeOptions> configureOptions)
{
RegisterServices(authenticationScheme);
RegisterServices();
return base.AddPolicyScheme(authenticationScheme, displayName, configureOptions);
}

public override AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
{
RegisterServices(authenticationScheme);
RegisterServices();
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
}

public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
{
RegisterServices(authenticationScheme);
RegisterServices();
return base.AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
}

public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
{
RegisterServices(authenticationScheme);
RegisterServices();
return base.AddScheme<TOptions, THandler>(authenticationScheme, configureOptions);
}

private void RegisterServices(string authenticationScheme)
private void RegisterServices()
{
if (!IsAuthenticationConfigured)
{
IsAuthenticationConfigured = true;
Services.AddAuthentication(authenticationScheme);
Services.AddAuthentication();
Services.AddAuthorization();
}
}
Expand Down
Loading