From 4f206558505b32c5ec0a686764ec30a8f912860e Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 20 Apr 2017 14:19:32 -0700 Subject: [PATCH] Set DisplayName for auth --- samples/SocialSample/Startup.cs | 2 +- .../FacebookExtensions.cs | 2 +- .../GoogleExtensions.cs | 2 +- .../MicrosoftAccountExtensions.cs | 2 +- .../OAuthExtensions.cs | 2 +- .../OpenIdConnectExtensions.cs | 2 +- .../TwitterExtensions.cs | 2 +- .../AuthenticationServiceCollectionExtensions.cs | 10 ++++++++-- .../CookieTests.cs | 12 ++++++++++++ .../DynamicSchemeTests.cs | 2 +- .../FacebookTests.cs | 12 ++++++++++++ .../GoogleTests.cs | 12 ++++++++++++ .../JwtBearerTests.cs | 12 ++++++++++++ .../MicrosoftAccountTests.cs | 12 ++++++++++++ .../OAuthTests.cs | 12 ++++++++++++ .../TwitterTests.cs | 12 ++++++++++++ 16 files changed, 100 insertions(+), 10 deletions(-) diff --git a/samples/SocialSample/Startup.cs b/samples/SocialSample/Startup.cs index 3f64d813da..7c1dd9d8db 100644 --- a/samples/SocialSample/Startup.cs +++ b/samples/SocialSample/Startup.cs @@ -238,7 +238,7 @@ namespace SocialSample foreach (var provider in await schemeProvider.GetAllSchemesAsync()) { // REVIEW: we lost access to display name (which is buried in the handler options) - await context.Response.WriteAsync("" + (provider.Name ?? "(suppressed)") + "
"); + await context.Response.WriteAsync("" + (provider.DisplayName ?? "(suppressed)") + "
"); } await context.Response.WriteAsync(""); }); diff --git a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs index bcfa95c0ad..79d9ac66ca 100644 --- a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs @@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddFacebookAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) { - return services.AddScheme(authenticationScheme, configureOptions); + return services.AddScheme(authenticationScheme, authenticationScheme, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs index d85e3a2d6f..420d14030a 100644 --- a/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs @@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddGoogleAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) { - return services.AddScheme(authenticationScheme, configureOptions); + return services.AddScheme(authenticationScheme, authenticationScheme, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs index 1f8884ab2e..509016ff29 100644 --- a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs @@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddMicrosoftAccountAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) { - return services.AddScheme(authenticationScheme, configureOptions); + return services.AddScheme(authenticationScheme, authenticationScheme, configureOptions); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs index aa7c59f03f..6fd0f57f4b 100644 --- a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs @@ -10,6 +10,6 @@ namespace Microsoft.AspNetCore.Builder public static class OAuthExtensions { public static IServiceCollection AddOAuthAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) => - services.AddScheme>(authenticationScheme, configureOptions); + services.AddScheme>(authenticationScheme, authenticationScheme, configureOptions); } } diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs index 89581b201f..64737a9ad8 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs @@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddOpenIdConnectAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) { - return services.AddScheme(authenticationScheme, configureOptions); + return services.AddScheme(authenticationScheme, authenticationScheme, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs index 2170be9028..1e126d4c4a 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs @@ -26,7 +26,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddTwitterAuthentication(this IServiceCollection services, string authenticationScheme, Action configureOptions) { - return services.AddScheme(authenticationScheme, configureOptions); + return services.AddScheme(authenticationScheme, authenticationScheme, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication/AuthenticationServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Authentication/AuthenticationServiceCollectionExtensions.cs index 074f45b5fb..0315562ffb 100644 --- a/src/Microsoft.AspNetCore.Authentication/AuthenticationServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication/AuthenticationServiceCollectionExtensions.cs @@ -45,7 +45,7 @@ namespace Microsoft.Extensions.DependencyInjection return services; } - public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, Action configureScheme, Action configureOptions) + public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, string displayName, Action configureScheme, Action configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler { @@ -53,6 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection { o.AddScheme(authenticationScheme, scheme => { scheme.HandlerType = typeof(THandler); + scheme.DisplayName = displayName; configureScheme?.Invoke(scheme); }); }); @@ -67,6 +68,11 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, Action configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler - => services.AddScheme(authenticationScheme, configureScheme: null, configureOptions: configureOptions); + => services.AddScheme(authenticationScheme, displayName: null, configureScheme: null, configureOptions: configureOptions); + + public static IServiceCollection AddScheme(this IServiceCollection services, string authenticationScheme, string displayName, Action configureOptions) + where TOptions : AuthenticationSchemeOptions, new() + where THandler : AuthenticationHandler + => services.AddScheme(authenticationScheme, displayName, configureScheme: null, configureOptions: configureOptions); } } diff --git a/test/Microsoft.AspNetCore.Authentication.Test/CookieTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/CookieTests.cs index 55dd054269..419d82493d 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/CookieTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/CookieTests.cs @@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.Cookies { private TestClock _clock = new TestClock(); + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddCookieAuthentication(); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(CookieAuthenticationDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("CookieAuthenticationHandler", scheme.HandlerType.Name); + Assert.Null(scheme.DisplayName); + } + [Fact] public async Task NormalRequestPassesThrough() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/DynamicSchemeTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/DynamicSchemeTests.cs index a152c735bb..d239d85f81 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/DynamicSchemeTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/DynamicSchemeTests.cs @@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.Authentication { var name = remainder.Value.Substring(1); var auth = context.RequestServices.GetRequiredService(); - var scheme = new AuthenticationScheme(name, typeof(TestHandler)); + var scheme = new AuthenticationScheme(name, name, typeof(TestHandler)); auth.AddScheme(scheme); } else if (req.Path.StartsWithSegments(new PathString("/auth"), out remainder)) diff --git a/test/Microsoft.AspNetCore.Authentication.Test/FacebookTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/FacebookTests.cs index 79066e48b5..edd9eb5788 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/FacebookTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/FacebookTests.cs @@ -27,6 +27,18 @@ namespace Microsoft.AspNetCore.Authentication.Facebook { public class FacebookTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddFacebookAuthentication().AddSingleton(new ConfigurationBuilder().Build()); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(FacebookDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("FacebookHandler", scheme.HandlerType.Name); + Assert.Equal(FacebookDefaults.AuthenticationScheme, scheme.DisplayName); + } + [Fact] public void AddCanBindAgainstDefaultConfig() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/GoogleTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/GoogleTests.cs index 77ddcc7efc..0ab3e44938 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/GoogleTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/GoogleTests.cs @@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.Google { public class GoogleTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddGoogleAuthentication().AddSingleton(new ConfigurationBuilder().Build()); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(GoogleDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("GoogleHandler", scheme.HandlerType.Name); + Assert.Equal(GoogleDefaults.AuthenticationScheme, scheme.DisplayName); + } + [Fact] public void AddCanBindAgainstDefaultConfig() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/JwtBearerTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/JwtBearerTests.cs index 08098622ca..42efe00dd3 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/JwtBearerTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/JwtBearerTests.cs @@ -26,6 +26,18 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer { public class JwtBearerTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddJwtBearerAuthentication().AddSingleton(new ConfigurationBuilder().Build()); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(JwtBearerDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("JwtBearerHandler", scheme.HandlerType.Name); + Assert.Null(scheme.DisplayName); + } + [Fact] public void AddCanBindAgainstDefaultConfig() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/MicrosoftAccountTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/MicrosoftAccountTests.cs index 26110e9fee..1f0f394f3c 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/MicrosoftAccountTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/MicrosoftAccountTests.cs @@ -27,6 +27,18 @@ namespace Microsoft.AspNetCore.Authentication.Tests.MicrosoftAccount { public class MicrosoftAccountTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddMicrosoftAccountAuthentication().AddSingleton(new ConfigurationBuilder().Build()); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(MicrosoftAccountDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("MicrosoftAccountHandler", scheme.HandlerType.Name); + Assert.Equal(MicrosoftAccountDefaults.AuthenticationScheme, scheme.DisplayName); + } + [Fact] public void AddCanBindAgainstDefaultConfig() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs index 95c086c805..dd48f7c956 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs @@ -15,6 +15,18 @@ namespace Microsoft.AspNetCore.Authentication.OAuth { public class OAuthTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddOAuthAuthentication("oauth", o => { }); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync("oauth"); + Assert.NotNull(scheme); + Assert.Equal("OAuthHandler`1", scheme.HandlerType.Name); + Assert.Equal("oauth", scheme.DisplayName); + } + [Fact] public async Task ThrowsIfClientIdMissing() { diff --git a/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs index 432227aed0..9993559f69 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/TwitterTests.cs @@ -20,6 +20,18 @@ namespace Microsoft.AspNetCore.Authentication.Twitter { public class TwitterTests { + [Fact] + public async Task VerifySchemeDefaults() + { + var services = new ServiceCollection().AddTwitterAuthentication().AddSingleton(new ConfigurationBuilder().Build()); + var sp = services.BuildServiceProvider(); + var schemeProvider = sp.GetRequiredService(); + var scheme = await schemeProvider.GetSchemeAsync(TwitterDefaults.AuthenticationScheme); + Assert.NotNull(scheme); + Assert.Equal("TwitterHandler", scheme.HandlerType.Name); + Assert.Equal(TwitterDefaults.AuthenticationScheme, scheme.DisplayName); + } + [Fact] public void AddCanBindAgainstDefaultConfig() {