From 36d3d97b9935b2cefb22731084adc4754d1e61e9 Mon Sep 17 00:00:00 2001 From: Chris R Date: Thu, 13 Jul 2017 12:25:36 -0700 Subject: [PATCH] #1319 Add DisplayName overloads --- samples/OpenIdConnect.AzureAdSample/Startup.cs | 2 +- samples/SocialSample/Startup.cs | 6 +++--- .../CookieExtensions.cs | 5 ++++- .../FacebookDefaults.cs | 2 ++ .../FacebookExtensions.cs | 5 ++++- .../GoogleDefaults.cs | 2 ++ .../GoogleExtensions.cs | 5 ++++- .../JwtBearerExtensions.cs | 5 ++++- .../MicrosoftAccountDefaults.cs | 2 ++ .../MicrosoftAccountExtensions.cs | 5 ++++- .../OAuthDefaults.cs | 10 ++++++++++ .../OAuthExtensions.cs | 10 +++++++++- .../OpenIdConnectDefaults.cs | 4 ++-- .../OpenIdConnectExtensions.cs | 5 ++++- .../OpenIdConnectOptions.cs | 1 - .../breakingchanges.netcore.json | 5 +++++ .../TwitterDefaults.cs | 2 ++ .../TwitterExtensions.cs | 5 ++++- .../OAuthTests.cs | 2 +- 19 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Authentication.OAuth/OAuthDefaults.cs diff --git a/samples/OpenIdConnect.AzureAdSample/Startup.cs b/samples/OpenIdConnect.AzureAdSample/Startup.cs index 6a6abd7c34..c3fa3c719b 100644 --- a/samples/OpenIdConnect.AzureAdSample/Startup.cs +++ b/samples/OpenIdConnect.AzureAdSample/Startup.cs @@ -49,7 +49,7 @@ namespace OpenIdConnect.AzureAdSample sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) .AddCookie() - .AddOpenIdConnect(o => + .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, "AAD", o => { o.ClientId = ClientId; o.ClientSecret = ClientSecret; // for code flow diff --git a/samples/SocialSample/Startup.cs b/samples/SocialSample/Startup.cs index dcf76263d5..8a59928c41 100644 --- a/samples/SocialSample/Startup.cs +++ b/samples/SocialSample/Startup.cs @@ -70,7 +70,7 @@ namespace SocialSample }) // You must first create an app with Google and add its ID and Secret to your user-secrets. // https://console.developers.google.com/project - .AddOAuth("Google-AccessToken", o => + .AddOAuth("Google-AccessToken", "Google AccessToken only", o => { o.ClientId = Configuration["google:clientid"]; o.ClientSecret = Configuration["google:clientsecret"]; @@ -128,7 +128,7 @@ namespace SocialSample */ // You must first create an app with Microsoft Account and add its ID and Secret to your user-secrets. // https://apps.dev.microsoft.com/ - .AddOAuth("Microsoft-AccessToken", o => + .AddOAuth("Microsoft-AccessToken", "Microsoft AccessToken only", o => { o.ClientId = Configuration["microsoftaccount:clientid"]; o.ClientSecret = Configuration["microsoftaccount:clientsecret"]; @@ -148,7 +148,7 @@ namespace SocialSample }) // You must first create an app with GitHub and add its ID and Secret to your user-secrets. // https://github.com/settings/applications/ - .AddOAuth("GitHub-AccessToken", o => + .AddOAuth("GitHub-AccessToken", "GitHub AccessToken only", o => { o.ClientId = Configuration["github-token:clientid"]; o.ClientSecret = Configuration["github-token:clientsecret"]; diff --git a/src/Microsoft.AspNetCore.Authentication.Cookies/CookieExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Cookies/CookieExtensions.cs index 61f72e361f..4c41f54a9c 100644 --- a/src/Microsoft.AspNetCore.Authentication.Cookies/CookieExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Cookies/CookieExtensions.cs @@ -21,9 +21,12 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + => builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions); + + public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, PostConfigureCookieAuthenticationOptions>()); - return builder.AddScheme(authenticationScheme, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookDefaults.cs b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookDefaults.cs index 012f95dcce..6143a4f235 100644 --- a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookDefaults.cs +++ b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookDefaults.cs @@ -7,6 +7,8 @@ namespace Microsoft.AspNetCore.Authentication.Facebook { public const string AuthenticationScheme = "Facebook"; + public static readonly string DisplayName = "Facebook"; + public static readonly string AuthorizationEndpoint = "https://www.facebook.com/v2.6/dialog/oauth"; public static readonly string TokenEndpoint = "https://graph.facebook.com/v2.6/oauth/access_token"; diff --git a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs index 1cb1ef03eb..2273724a42 100644 --- a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookExtensions.cs @@ -16,6 +16,9 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddFacebook(FacebookDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) - => builder.AddOAuth(authenticationScheme, configureOptions); + => builder.AddFacebook(authenticationScheme, FacebookDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddFacebook(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } } diff --git a/src/Microsoft.AspNetCore.Authentication.Google/GoogleDefaults.cs b/src/Microsoft.AspNetCore.Authentication.Google/GoogleDefaults.cs index 77d68aed93..e4bd666157 100644 --- a/src/Microsoft.AspNetCore.Authentication.Google/GoogleDefaults.cs +++ b/src/Microsoft.AspNetCore.Authentication.Google/GoogleDefaults.cs @@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Authentication.Google { public const string AuthenticationScheme = "Google"; + public static readonly string DisplayName = "Google"; + public static readonly string AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/auth"; public static readonly string TokenEndpoint = "https://www.googleapis.com/oauth2/v4/token"; diff --git a/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs index 7bb9b7bebd..95547014ca 100644 --- a/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Google/GoogleExtensions.cs @@ -16,6 +16,9 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddGoogle(GoogleDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) - => builder.AddOAuth(authenticationScheme, configureOptions); + => builder.AddGoogle(authenticationScheme, GoogleDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddGoogle(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } } diff --git a/src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerExtensions.cs b/src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerExtensions.cs index 698c0118e0..334407c0da 100644 --- a/src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.JwtBearer/JwtBearerExtensions.cs @@ -18,9 +18,12 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + => builder.AddJwtBearer(authenticationScheme, displayName: null, configureOptions: configureOptions); + + public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, JwtBearerPostConfigureOptions>()); - return builder.AddScheme(authenticationScheme, configureOptions); + return builder.AddScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountDefaults.cs b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountDefaults.cs index 0d272f9792..1b0859c5b7 100644 --- a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountDefaults.cs +++ b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountDefaults.cs @@ -7,6 +7,8 @@ namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount { public const string AuthenticationScheme = "Microsoft"; + public static readonly string DisplayName = "Microsoft"; + public static readonly string AuthorizationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"; public static readonly string TokenEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"; diff --git a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs index 8da6daaac8..7f24e5af77 100644 --- a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountExtensions.cs @@ -16,6 +16,9 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddMicrosoftAccount(MicrosoftAccountDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) - => builder.AddOAuth(authenticationScheme, configureOptions); + => builder.AddMicrosoftAccount(authenticationScheme, MicrosoftAccountDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddMicrosoftAccount(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + => builder.AddOAuth(authenticationScheme, displayName, configureOptions); } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthDefaults.cs b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthDefaults.cs new file mode 100644 index 0000000000..376f8ab01a --- /dev/null +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthDefaults.cs @@ -0,0 +1,10 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Authentication.OAuth +{ + public static class OAuthDefaults + { + public static readonly string DisplayName = "OAuth"; + } +} diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs index 8c9d34c5f9..22c541a0ac 100644 --- a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthExtensions.cs @@ -14,12 +14,20 @@ namespace Microsoft.Extensions.DependencyInjection public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) => builder.AddOAuth>(authenticationScheme, configureOptions); + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + => builder.AddOAuth>(authenticationScheme, displayName, configureOptions); + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) where TOptions : OAuthOptions, new() where THandler : OAuthHandler + => builder.AddOAuth(authenticationScheme, OAuthDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddOAuth(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) + where TOptions : OAuthOptions, new() + where THandler : OAuthHandler { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, OAuthPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, authenticationScheme, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectDefaults.cs b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectDefaults.cs index c5baca4db9..f98ba87e02 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectDefaults.cs +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectDefaults.cs @@ -19,9 +19,9 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect public const string AuthenticationScheme = "OpenIdConnect"; /// - /// The default value for OpenIdConnectOptions.Caption. + /// The default value for the display name. /// - public static readonly string Caption = "OpenIdConnect"; + public static readonly string DisplayName = "OpenIdConnect"; /// /// The prefix used to for the nonce in the cookie. diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs index 43059b3ffe..f427bebaff 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs @@ -18,9 +18,12 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + => builder.AddOpenIdConnect(authenticationScheme, OpenIdConnectDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddOpenIdConnect(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, OpenIdConnectPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, authenticationScheme, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectOptions.cs b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectOptions.cs index 23169b5bcd..a8545e35a6 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectOptions.cs +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectOptions.cs @@ -27,7 +27,6 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect /// Defaults: /// AddNonceToRequest: true. /// BackchannelTimeout: 1 minute. - /// Caption: . /// ProtocolValidator: new . /// RefreshOnIssuerKeyNotFound: true /// ResponseType: diff --git a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/breakingchanges.netcore.json b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/breakingchanges.netcore.json index 0f50b12103..931a7b079d 100644 --- a/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/breakingchanges.netcore.json +++ b/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/breakingchanges.netcore.json @@ -59,5 +59,10 @@ { "TypeId": "public class Microsoft.AspNetCore.Authentication.OpenIdConnect.UserInformationReceivedContext : Microsoft.AspNetCore.Authentication.OpenIdConnect.BaseOpenIdConnectContext", "Kind": "Removal" + }, + { + "TypeId": "public static class Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectDefaults", + "MemberId": "public static readonly System.String Caption", + "Kind": "Removal" } ] \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterDefaults.cs b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterDefaults.cs index 0610ccfc9d..a39a3f0367 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterDefaults.cs +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterDefaults.cs @@ -6,5 +6,7 @@ namespace Microsoft.AspNetCore.Authentication.Twitter public static class TwitterDefaults { public const string AuthenticationScheme = "Twitter"; + + public static readonly string DisplayName = "Twitter"; } } diff --git a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs index bd0a3b1e20..7243805692 100644 --- a/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.Twitter/TwitterExtensions.cs @@ -18,9 +18,12 @@ namespace Microsoft.Extensions.DependencyInjection => builder.AddTwitter(TwitterDefaults.AuthenticationScheme, configureOptions); public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, Action configureOptions) + => builder.AddTwitter(authenticationScheme, TwitterDefaults.DisplayName, configureOptions); + + public static AuthenticationBuilder AddTwitter(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action configureOptions) { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, TwitterPostConfigureOptions>()); - return builder.AddRemoteScheme(authenticationScheme, authenticationScheme, configureOptions); + return builder.AddRemoteScheme(authenticationScheme, displayName, configureOptions); } } } diff --git a/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs index ea0c941c91..aeb313daa3 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/OAuthTests.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Authentication.OAuth var scheme = await schemeProvider.GetSchemeAsync("oauth"); Assert.NotNull(scheme); Assert.Equal("OAuthHandler`1", scheme.HandlerType.Name); - Assert.Equal("oauth", scheme.DisplayName); + Assert.Equal(OAuthDefaults.DisplayName, scheme.DisplayName); } [Fact]