From 081577e4f4996082a4fc28ef77c84f54c7d04183 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Fri, 18 Sep 2015 12:24:33 -0700 Subject: [PATCH] Remove usage IOptions from middleware ctors --- samples/OpenIdConnectSample/Startup.cs | 13 +--- samples/SocialSample/Startup.cs | 14 +--- .../CookieAppBuilderExtensions.cs | 30 +++++--- .../CookieAuthenticationMiddleware.cs | 6 +- .../FacebookAppBuilderExtensions.cs | 22 +++++- .../FacebookMiddleware.cs | 5 +- .../FacebookServiceCollectionExtensions.cs | 26 ------- .../GoogleAppBuilderExtensions.cs | 22 +++++- .../GoogleMiddleware.cs | 5 +- .../GoogleServiceCollectionExtensions.cs | 26 ------- .../JwtBearerAppBuilderExtensions.cs | 26 ++++++- .../JwtBearerMiddleware.cs | 5 +- .../JwtBearerServiceCollectionExtensions.cs | 26 ------- .../MicrosoftAccountAppBuilderExtensions.cs | 16 +++- .../MicrosoftAccountMiddleware.cs | 5 +- ...osoftAccountServiceCollectionExtensions.cs | 26 ------- .../OAuthExtensions.cs | 22 +++++- .../OAuthMiddleware.cs | 5 +- .../OAuthOptions.cs | 11 +-- .../OpenIdConnectExtensions.cs | 15 ++-- .../OpenIdConnectMiddleware.cs | 10 +-- ...penIdConnectServiceCollectionExtensions.cs | 26 ------- .../TwitterAppBuilderExtensions.cs | 15 +++- .../TwitterMiddleware.cs | 5 +- .../TwitterServiceCollectionExtensions.cs | 26 ------- .../AuthenticationMiddleware.cs | 12 +-- ...thenticationServiceCollectionExtensions.cs | 17 ----- ...laimsTransformationAppBuilderExtensions.cs | 35 +++++++-- ...dler.cs => ClaimsTransformationHandler.cs} | 1 - .../ClaimsTransformationMiddleware.cs | 10 +-- .../ClaimsTransformationOptions.cs | 34 --------- .../ClaimsTransformer.cs | 19 +++++ .../HttpContextExtensions.cs | 2 +- .../IClaimsTransformer.cs | 13 ++++ ...thorizationServiceCollectionExtensions.cs} | 2 +- .../Cookies/CookieMiddlewareTests.cs | 28 +------ .../Facebook/FacebookMiddlewareTests.cs | 75 +++++++------------ .../Google/GoogleMiddlewareTests.cs | 15 ++-- .../OpenIdConnectHandlerTests.cs | 7 +- ...ConnectMiddlewareForTestingAuthenticate.cs | 5 +- 40 files changed, 262 insertions(+), 421 deletions(-) delete mode 100644 src/Microsoft.AspNet.Authentication.Facebook/FacebookServiceCollectionExtensions.cs delete mode 100644 src/Microsoft.AspNet.Authentication.Google/GoogleServiceCollectionExtensions.cs delete mode 100644 src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerServiceCollectionExtensions.cs delete mode 100644 src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountServiceCollectionExtensions.cs delete mode 100644 src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectServiceCollectionExtensions.cs delete mode 100644 src/Microsoft.AspNet.Authentication.Twitter/TwitterServiceCollectionExtensions.cs rename src/Microsoft.AspNet.Authentication/{ClaimsTransformationAuthenticationHandler.cs => ClaimsTransformationHandler.cs} (99%) create mode 100644 src/Microsoft.AspNet.Authentication/ClaimsTransformer.cs create mode 100644 src/Microsoft.AspNet.Authentication/IClaimsTransformer.cs rename src/Microsoft.AspNet.Authorization/{ServiceCollectionExtensions.cs => AuthorizationServiceCollectionExtensions.cs} (94%) diff --git a/samples/OpenIdConnectSample/Startup.cs b/samples/OpenIdConnectSample/Startup.cs index bd1f45d4c3..3a0a289e72 100644 --- a/samples/OpenIdConnectSample/Startup.cs +++ b/samples/OpenIdConnectSample/Startup.cs @@ -1,10 +1,9 @@ using System.Linq; +using Microsoft.AspNet.Authentication.Cookies; +using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Authentication.Cookies; -using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; @@ -14,11 +13,7 @@ namespace OpenIdConnectSample { public void ConfigureServices(IServiceCollection services) { - services.AddAuthentication(); - services.Configure(options => - { - options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; - }); + services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) @@ -51,8 +46,6 @@ namespace OpenIdConnectSample context.Response.ContentType = "text/plain"; await context.Response.WriteAsync("Hello Authenticated User"); }); - - } } } diff --git a/samples/SocialSample/Startup.cs b/samples/SocialSample/Startup.cs index f47aea5bbf..ee397f272c 100644 --- a/samples/SocialSample/Startup.cs +++ b/samples/SocialSample/Startup.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Claims; -using Microsoft.AspNet.Authentication; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.Google; using Microsoft.AspNet.Authentication.MicrosoftAccount; @@ -21,18 +20,7 @@ namespace CookieSample { public void ConfigureServices(IServiceCollection services) { - services.AddAuthentication(); - services.Configure(options => - { - options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; - }); - services.AddClaimsTransformation(p => - { - var id = new ClaimsIdentity("xform"); - id.AddClaim(new Claim("ClaimsTransformation", "TransformAddedClaim")); - p.AddIdentity(id); - return p; - }); + services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) diff --git a/src/Microsoft.AspNet.Authentication.Cookies/CookieAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.Cookies/CookieAppBuilderExtensions.cs index e8b56f4bce..2ed5f99827 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/CookieAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/CookieAppBuilderExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -17,13 +16,10 @@ namespace Microsoft.AspNet.Builder /// Adds a cookie-based authentication middleware to your web application pipeline. /// /// The IApplicationBuilder passed to your configuration method - /// Used to configure the options for the middleware - /// The name of the options class that controls the middleware behavior, null will use the default options /// The original app parameter - public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null) + public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + return app.UseCookieAuthentication(new CookieAuthenticationOptions()); } /// @@ -31,12 +27,26 @@ namespace Microsoft.AspNet.Builder /// /// The IApplicationBuilder passed to your configuration method /// Used to configure the options for the middleware - /// The name of the options class that controls the middleware behavior, null will use the default options /// The original app parameter - public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, IOptions options) + public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) { - return app.UseMiddleware(options, - new ConfigureOptions(o => { })); + var options = new CookieAuthenticationOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseCookieAuthentication(options); + } + + /// + /// Adds a cookie-based authentication middleware to your web application pipeline. + /// + /// The IApplicationBuilder passed to your configuration method + /// Used to configure the middleware + /// The original app parameter + public static IApplicationBuilder UseCookieAuthentication([NotNull] this IApplicationBuilder app, [NotNull] CookieAuthenticationOptions options) + { + return app.UseMiddleware(options); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs index d7de7fbecd..900ad5505d 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationMiddleware.cs @@ -6,7 +6,6 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; using Microsoft.Framework.Internal; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.WebEncoders; namespace Microsoft.AspNet.Authentication.Cookies @@ -18,9 +17,8 @@ namespace Microsoft.AspNet.Authentication.Cookies [NotNull] IDataProtectionProvider dataProtectionProvider, [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder urlEncoder, - [NotNull] IOptions options, - ConfigureOptions configureOptions) - : base(next, options, loggerFactory, urlEncoder, configureOptions) + [NotNull] CookieAuthenticationOptions options) + : base(next, options, loggerFactory, urlEncoder) { if (Options.Events == null) { diff --git a/src/Microsoft.AspNet.Authentication.Facebook/FacebookAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.Facebook/FacebookAppBuilderExtensions.cs index 9d0dc44e1b..0e6fc8a971 100644 --- a/src/Microsoft.AspNet.Authentication.Facebook/FacebookAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.Facebook/FacebookAppBuilderExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNet.Authentication.Facebook; using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -18,10 +17,25 @@ namespace Microsoft.AspNet.Builder /// /// The passed to the configure method. /// The updated . - public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null) + public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, [NotNull] FacebookOptions options) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + return app.UseMiddleware(options); + } + + /// + /// Authenticate users using Facebook. + /// + /// The passed to the configure method. + /// Configures the options. + /// The updated . + public static IApplicationBuilder UseFacebookAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) + { + var options = new FacebookOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseFacebookAuthentication(options); } } } diff --git a/src/Microsoft.AspNet.Authentication.Facebook/FacebookMiddleware.cs b/src/Microsoft.AspNet.Authentication.Facebook/FacebookMiddleware.cs index 8e97d9d158..aa518d5df2 100644 --- a/src/Microsoft.AspNet.Authentication.Facebook/FacebookMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.Facebook/FacebookMiddleware.cs @@ -34,9 +34,8 @@ namespace Microsoft.AspNet.Authentication.Facebook [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions) + [NotNull] FacebookOptions options) + : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options) { if (string.IsNullOrEmpty(Options.AppId)) { diff --git a/src/Microsoft.AspNet.Authentication.Facebook/FacebookServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.Facebook/FacebookServiceCollectionExtensions.cs deleted file mode 100644 index ccffd7de44..0000000000 --- a/src/Microsoft.AspNet.Authentication.Facebook/FacebookServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.Facebook; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods for using . - /// - public static class FacebookServiceCollectionExtensions - { - public static IServiceCollection AddFacebookAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddFacebookAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.Google/GoogleAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.Google/GoogleAppBuilderExtensions.cs index 55e3ba23a0..7d1599ff52 100644 --- a/src/Microsoft.AspNet.Authentication.Google/GoogleAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.Google/GoogleAppBuilderExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNet.Authentication.Google; using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -13,6 +12,17 @@ namespace Microsoft.AspNet.Builder /// public static class GoogleAppBuilderExtensions { + /// + /// Authenticate users using Google OAuth 2.0. + /// + /// The passed to the configure method. + /// The Middleware options. + /// The updated . + public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, [NotNull] GoogleOptions options) + { + return app.UseMiddleware(options); + } + /// /// Authenticate users using Google OAuth 2.0. /// @@ -20,10 +30,14 @@ namespace Microsoft.AspNet.Builder /// Used to configure Middleware options. /// Name of the options instance to be used /// The updated . - public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null, string optionsName = "") + public static IApplicationBuilder UseGoogleAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + var options = new GoogleOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseGoogleAuthentication(options); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.Google/GoogleMiddleware.cs b/src/Microsoft.AspNet.Authentication.Google/GoogleMiddleware.cs index 5ceb214494..43dd9ba80e 100644 --- a/src/Microsoft.AspNet.Authentication.Google/GoogleMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.Google/GoogleMiddleware.cs @@ -34,9 +34,8 @@ namespace Microsoft.AspNet.Authentication.Google [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions) + [NotNull] GoogleOptions options) + : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options) { if (Options.Scope.Count == 0) { diff --git a/src/Microsoft.AspNet.Authentication.Google/GoogleServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.Google/GoogleServiceCollectionExtensions.cs deleted file mode 100644 index 0c329dc5cb..0000000000 --- a/src/Microsoft.AspNet.Authentication.Google/GoogleServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.Google; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods for using . - /// - public static class GoogleServiceCollectionExtensions - { - public static IServiceCollection AddGoogleAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddGoogleAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAppBuilderExtensions.cs index bdc91c71d2..55f6cb5b2b 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAppBuilderExtensions.cs @@ -24,10 +24,30 @@ namespace Microsoft.AspNet.Builder /// The application builder /// Options which control the processing of the bearer header. /// The application builder - public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null, string optionsName = "") + public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, [NotNull] JwtBearerOptions options) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + return app.UseMiddleware(options); + } + + /// + /// Adds Bearer token processing to an HTTP application pipeline. This middleware understands appropriately + /// formatted and secured tokens which appear in the request header. If the Options.AuthenticationMode is Active, the + /// claims within the bearer token are added to the current request's IPrincipal User. If the Options.AuthenticationMode + /// is Passive, then the current request is not modified, but IAuthenticationManager AuthenticateAsync may be used at + /// any time to obtain the claims from the request's bearer token. + /// See also http://tools.ietf.org/html/rfc6749 + /// + /// The application builder + /// Used to configure Middleware options. + /// The application builder + public static IApplicationBuilder UseJwtBearerAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) + { + var options = new JwtBearerOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseJwtBearerAuthentication(options); } } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerMiddleware.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerMiddleware.cs index 7884a675b8..0c67c632e7 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerMiddleware.cs @@ -29,9 +29,8 @@ namespace Microsoft.AspNet.Authentication.JwtBearer [NotNull] RequestDelegate next, [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, - [NotNull] IOptions options, - ConfigureOptions configureOptions) - : base(next, options, loggerFactory, encoder, configureOptions) + [NotNull] JwtBearerOptions options) + : base(next, options, loggerFactory, encoder) { if (Options.Events == null) { diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerServiceCollectionExtensions.cs deleted file mode 100644 index 30a99c277b..0000000000 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.JwtBearer; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods to add Jwt Bearer authentication capabilities to an HTTP application pipeline - /// - public static class JwtBearerServiceCollectionExtensions - { - public static IServiceCollection AddJwtBearerAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddJwtBearerAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} diff --git a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountAppBuilderExtensions.cs index 86c4ae040e..9fe1887bbf 100644 --- a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountAppBuilderExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNet.Authentication.MicrosoftAccount; using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -13,10 +12,19 @@ namespace Microsoft.AspNet.Builder /// public static class MicrosoftAccountAuthenticationExtensions { - public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null) + public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, [NotNull] MicrosoftAccountOptions options) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + return app.UseMiddleware(options); + } + + public static IApplicationBuilder UseMicrosoftAccountAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) + { + var options = new MicrosoftAccountOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseMicrosoftAccountAuthentication(options); } } } diff --git a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountMiddleware.cs b/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountMiddleware.cs index 7e5830922e..9f2c5d96e8 100644 --- a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountMiddleware.cs @@ -32,9 +32,8 @@ namespace Microsoft.AspNet.Authentication.MicrosoftAccount [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options, configureOptions) + [NotNull] MicrosoftAccountOptions options) + : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options) { if (Options.Scope.Count == 0) { diff --git a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountServiceCollectionExtensions.cs deleted file mode 100644 index 83e52461c7..0000000000 --- a/src/Microsoft.AspNet.Authentication.MicrosoftAccount/MicrosoftAccountServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.MicrosoftAccount; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods for using - /// - public static class MicrosoftAccountServiceCollectionExtensions - { - public static IServiceCollection AddMicrosoftAccountAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddMicrosoftAccountAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} diff --git a/src/Microsoft.AspNet.Authentication.OAuth/OAuthExtensions.cs b/src/Microsoft.AspNet.Authentication.OAuth/OAuthExtensions.cs index 08dc3d52d3..813f3cdfeb 100644 --- a/src/Microsoft.AspNet.Authentication.OAuth/OAuthExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.OAuth/OAuthExtensions.cs @@ -13,17 +13,31 @@ namespace Microsoft.AspNet.Builder /// public static class OAuthExtensions { + /// + /// Authenticate users using OAuth. + /// + /// The passed to the configure method. + /// Configures the middleware options. + /// The updated . + public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] Action configureOptions) + { + var options = new OAuthOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseOAuthAuthentication(options); + } + /// /// Authenticate users using OAuth. /// /// The passed to the configure method. /// The middleware configuration options. /// The updated . - public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] IOptions options) + public static IApplicationBuilder UseOAuthAuthentication([NotNull] this IApplicationBuilder app, [NotNull] OAuthOptions options) { - return app.UseMiddleware>( - options, - new ConfigureOptions(o => { })); + return app.UseMiddleware>(options); } } } diff --git a/src/Microsoft.AspNet.Authentication.OAuth/OAuthMiddleware.cs b/src/Microsoft.AspNet.Authentication.OAuth/OAuthMiddleware.cs index 9822963cc5..5030fd9673 100644 --- a/src/Microsoft.AspNet.Authentication.OAuth/OAuthMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.OAuth/OAuthMiddleware.cs @@ -33,9 +33,8 @@ namespace Microsoft.AspNet.Authentication.OAuth [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, options, loggerFactory, encoder, configureOptions) + [NotNull] TOptions options) + : base(next, options, loggerFactory, encoder) { // todo: review error handling if (string.IsNullOrEmpty(Options.AuthenticationScheme)) diff --git a/src/Microsoft.AspNet.Authentication.OAuth/OAuthOptions.cs b/src/Microsoft.AspNet.Authentication.OAuth/OAuthOptions.cs index b20ada53ca..c87026dc7d 100644 --- a/src/Microsoft.AspNet.Authentication.OAuth/OAuthOptions.cs +++ b/src/Microsoft.AspNet.Authentication.OAuth/OAuthOptions.cs @@ -6,14 +6,13 @@ using System.Collections.Generic; using System.Net.Http; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Authentication.OAuth { /// /// Configuration options for . /// - public class OAuthOptions : AuthenticationOptions, IOptions + public class OAuthOptions : AuthenticationOptions { /// /// Gets or sets the provider-assigned client id. @@ -102,13 +101,5 @@ namespace Microsoft.AspNet.Authentication.OAuth /// authentication cookie. Note that social providers set this property to false by default. /// public bool SaveTokensAsClaims { get; set; } = true; - - OAuthOptions IOptions.Value - { - get - { - return this; - } - } } } diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs index 18b83580bc..e8bcf935df 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectExtensions.cs @@ -3,7 +3,7 @@ using System; using Microsoft.AspNet.Authentication.OpenIdConnect; -using Microsoft.Framework.OptionsModel; +using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Builder { @@ -18,10 +18,15 @@ namespace Microsoft.AspNet.Builder /// The application builder /// Options which control the processing of the OpenIdConnect protocol and token validation. /// The application builder - public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, Action configureOptions = null) + public static IApplicationBuilder UseOpenIdConnectAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + + var options = new OpenIdConnectOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseOpenIdConnectAuthentication(options); } /// @@ -30,7 +35,7 @@ namespace Microsoft.AspNet.Builder /// The application builder /// Options which control the processing of the OpenIdConnect protocol and token validation. /// The application builder - public static IApplicationBuilder UseOpenIdConnectAuthentication(this IApplicationBuilder app, IOptions options) + public static IApplicationBuilder UseOpenIdConnectAuthentication([NotNull] this IApplicationBuilder app, [NotNull] OpenIdConnectOptions options) { return app.UseMiddleware(options); } diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectMiddleware.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectMiddleware.cs index 76819648ff..7aeca5efba 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectMiddleware.cs @@ -31,10 +31,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect /// /// /// - /// a instance that will supply - /// if configureOptions is null. - /// a instance that will be passed to an instance of - /// that is retrieved by calling where string == provides runtime configuration. + /// [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Managed by caller")] public OpenIdConnectMiddleware( [NotNull] RequestDelegate next, @@ -43,9 +40,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect [NotNull] IUrlEncoder encoder, [NotNull] IServiceProvider services, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, options, loggerFactory, encoder, configureOptions) + [NotNull] OpenIdConnectOptions options) + : base(next, options, loggerFactory, encoder) { if (string.IsNullOrEmpty(Options.SignInScheme) && !string.IsNullOrEmpty(sharedOptions.Value.SignInScheme)) { diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectServiceCollectionExtensions.cs deleted file mode 100644 index c6f4b6ec48..0000000000 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.OpenIdConnect; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods to configure OpenIdConnect authentication options - /// - public static class OpenIdConnectServiceCollectionExtensions - { - public static IServiceCollection AddOpenIdConnectAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddOpenIdConnectAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} diff --git a/src/Microsoft.AspNet.Authentication.Twitter/TwitterAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication.Twitter/TwitterAppBuilderExtensions.cs index 286e79f22f..5a274c2413 100644 --- a/src/Microsoft.AspNet.Authentication.Twitter/TwitterAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication.Twitter/TwitterAppBuilderExtensions.cs @@ -4,7 +4,6 @@ using System; using Microsoft.AspNet.Authentication.Twitter; using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -15,8 +14,18 @@ namespace Microsoft.AspNet.Builder { public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, Action configureOptions = null) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions ?? (o => { }))); + var options = new TwitterOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseTwitterAuthentication(options); } + + public static IApplicationBuilder UseTwitterAuthentication([NotNull] this IApplicationBuilder app, [NotNull] TwitterOptions options) + { + return app.UseMiddleware(options); + } + } } diff --git a/src/Microsoft.AspNet.Authentication.Twitter/TwitterMiddleware.cs b/src/Microsoft.AspNet.Authentication.Twitter/TwitterMiddleware.cs index 8a16dc09b2..b00ca44219 100644 --- a/src/Microsoft.AspNet.Authentication.Twitter/TwitterMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication.Twitter/TwitterMiddleware.cs @@ -38,9 +38,8 @@ namespace Microsoft.AspNet.Authentication.Twitter [NotNull] ILoggerFactory loggerFactory, [NotNull] IUrlEncoder encoder, [NotNull] IOptions sharedOptions, - [NotNull] IOptions options, - ConfigureOptions configureOptions = null) - : base(next, options, loggerFactory, encoder, configureOptions) + [NotNull] TwitterOptions options) + : base(next, options, loggerFactory, encoder) { if (string.IsNullOrEmpty(Options.ConsumerSecret)) { diff --git a/src/Microsoft.AspNet.Authentication.Twitter/TwitterServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication.Twitter/TwitterServiceCollectionExtensions.cs deleted file mode 100644 index fdc48d26b8..0000000000 --- a/src/Microsoft.AspNet.Authentication.Twitter/TwitterServiceCollectionExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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. - -using System; -using Microsoft.AspNet.Authentication.Twitter; -using Microsoft.Framework.Configuration; -using Microsoft.Framework.Internal; - -namespace Microsoft.Framework.DependencyInjection -{ - /// - /// Extension methods for using - /// - public static class TwitterAuthenticationExtensions - { - public static IServiceCollection AddTwitterAuthentication([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddTwitterAuthentication([NotNull] this IServiceCollection services, [NotNull] IConfiguration config) - { - return services.Configure(config); - } - } -} diff --git a/src/Microsoft.AspNet.Authentication/AuthenticationMiddleware.cs b/src/Microsoft.AspNet.Authentication/AuthenticationMiddleware.cs index c0eee7e1a0..9736325570 100644 --- a/src/Microsoft.AspNet.Authentication/AuthenticationMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication/AuthenticationMiddleware.cs @@ -7,7 +7,6 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.Framework.Internal; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.WebEncoders; namespace Microsoft.AspNet.Authentication @@ -18,16 +17,11 @@ namespace Microsoft.AspNet.Authentication protected AuthenticationMiddleware( [NotNull] RequestDelegate next, - [NotNull] IOptions options, + [NotNull] TOptions options, [NotNull] ILoggerFactory loggerFactory, - [NotNull] IUrlEncoder encoder, - ConfigureOptions configureOptions) + [NotNull] IUrlEncoder encoder) { - Options = options.Value; - if (configureOptions != null) - { - configureOptions.Configure(Options); - } + Options = options; Logger = loggerFactory.CreateLogger(this.GetType().FullName); UrlEncoder = encoder; diff --git a/src/Microsoft.AspNet.Authentication/AuthenticationServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authentication/AuthenticationServiceCollectionExtensions.cs index 9634fd97b7..fbe65663e2 100644 --- a/src/Microsoft.AspNet.Authentication/AuthenticationServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Authentication/AuthenticationServiceCollectionExtensions.cs @@ -2,8 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Security.Claims; -using System.Threading.Tasks; using Microsoft.AspNet.Authentication; using Microsoft.Framework.Internal; @@ -23,20 +21,5 @@ namespace Microsoft.Framework.DependencyInjection services.Configure(configureOptions); return services.AddAuthentication(); } - - public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Action configure) - { - return services.Configure(configure); - } - - public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Func transform) - { - return services.Configure(o => o.Transformer = new ClaimsTransformer { TransformSyncDelegate = transform }); - } - - public static IServiceCollection AddClaimsTransformation([NotNull] this IServiceCollection services, [NotNull] Func> asyncTransform) - { - return services.Configure(o => o.Transformer = new ClaimsTransformer { TransformAsyncDelegate = asyncTransform }); - } } } diff --git a/src/Microsoft.AspNet.Authentication/ClaimsTransformationAppBuilderExtensions.cs b/src/Microsoft.AspNet.Authentication/ClaimsTransformationAppBuilderExtensions.cs index ff29ffdbe7..def2c935ba 100644 --- a/src/Microsoft.AspNet.Authentication/ClaimsTransformationAppBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Authentication/ClaimsTransformationAppBuilderExtensions.cs @@ -2,9 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Security.Claims; +using System.Threading.Tasks; using Microsoft.AspNet.Authentication; -using Microsoft.Framework.Internal; -using Microsoft.Framework.OptionsModel; namespace Microsoft.AspNet.Builder { @@ -16,11 +16,28 @@ namespace Microsoft.AspNet.Builder /// /// Adds a claims transformation middleware to your web application pipeline. /// + /// The options for the middleware /// The IApplicationBuilder passed to your configuration method /// The original app parameter - public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app) + public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, ClaimsTransformationOptions options) { - return app.UseClaimsTransformation(configureOptions: o => { }); + return app.UseMiddleware(options); + } + + /// + /// Adds a claims transformation middleware to your web application pipeline. + /// + /// The options for the middleware + /// The IApplicationBuilder passed to your configuration method + /// The original app parameter + public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Func> transform) + { + var options = new ClaimsTransformationOptions(); + options.Transformer = new ClaimsTransformer + { + OnTransform = transform + }; + return app.UseClaimsTransformation(options); } /// @@ -29,10 +46,14 @@ namespace Microsoft.AspNet.Builder /// The IApplicationBuilder passed to your configuration method /// Used to configure the options for the middleware /// The original app parameter - public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, [NotNull] Action configureOptions) + public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Action configureOptions) { - return app.UseMiddleware( - new ConfigureOptions(configureOptions)); + var options = new ClaimsTransformationOptions(); + if (configureOptions != null) + { + configureOptions(options); + } + return app.UseClaimsTransformation(options); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication/ClaimsTransformationAuthenticationHandler.cs b/src/Microsoft.AspNet.Authentication/ClaimsTransformationHandler.cs similarity index 99% rename from src/Microsoft.AspNet.Authentication/ClaimsTransformationAuthenticationHandler.cs rename to src/Microsoft.AspNet.Authentication/ClaimsTransformationHandler.cs index d5c4da165e..954f26032f 100644 --- a/src/Microsoft.AspNet.Authentication/ClaimsTransformationAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Authentication/ClaimsTransformationHandler.cs @@ -80,6 +80,5 @@ namespace Microsoft.AspNet.Authentication { auth.Handler = PriorHandler; } - } } diff --git a/src/Microsoft.AspNet.Authentication/ClaimsTransformationMiddleware.cs b/src/Microsoft.AspNet.Authentication/ClaimsTransformationMiddleware.cs index e72ab3ab12..b4c95310c4 100644 --- a/src/Microsoft.AspNet.Authentication/ClaimsTransformationMiddleware.cs +++ b/src/Microsoft.AspNet.Authentication/ClaimsTransformationMiddleware.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Authentication @@ -15,14 +14,9 @@ namespace Microsoft.AspNet.Authentication public ClaimsTransformationMiddleware( [NotNull] RequestDelegate next, - [NotNull] IOptions options, - ConfigureOptions configureOptions) + [NotNull] ClaimsTransformationOptions options) { - Options = options.Value; - if (configureOptions != null) - { - configureOptions.Configure(Options); - } + Options = options; _next = next; } diff --git a/src/Microsoft.AspNet.Authentication/ClaimsTransformationOptions.cs b/src/Microsoft.AspNet.Authentication/ClaimsTransformationOptions.cs index acc1a5e499..19475ba023 100644 --- a/src/Microsoft.AspNet.Authentication/ClaimsTransformationOptions.cs +++ b/src/Microsoft.AspNet.Authentication/ClaimsTransformationOptions.cs @@ -1,44 +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. -using System; -using System.Security.Claims; -using System.Threading.Tasks; - namespace Microsoft.AspNet.Authentication { public class ClaimsTransformationOptions { public IClaimsTransformer Transformer { get; set; } } - - public interface IClaimsTransformer - { - Task TransformAsync(ClaimsPrincipal principal); - ClaimsPrincipal Transform(ClaimsPrincipal principal); - } - - public class ClaimsTransformer : IClaimsTransformer - { - public Func> TransformAsyncDelegate { get; set; } - public Func TransformSyncDelegate { get; set; } - - public virtual ClaimsPrincipal Transform(ClaimsPrincipal principal) - { - if (TransformSyncDelegate != null) - { - return TransformSyncDelegate(principal); - } - return principal; - } - - public virtual Task TransformAsync(ClaimsPrincipal principal) - { - if (TransformAsyncDelegate != null) - { - return TransformAsyncDelegate(principal); - } - return Task.FromResult(Transform(principal)); - } - } } diff --git a/src/Microsoft.AspNet.Authentication/ClaimsTransformer.cs b/src/Microsoft.AspNet.Authentication/ClaimsTransformer.cs new file mode 100644 index 0000000000..e824689d1c --- /dev/null +++ b/src/Microsoft.AspNet.Authentication/ClaimsTransformer.cs @@ -0,0 +1,19 @@ +// 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. + +using System; +using System.Security.Claims; +using System.Threading.Tasks; + +namespace Microsoft.AspNet.Authentication +{ + public class ClaimsTransformer : IClaimsTransformer + { + public Func> OnTransform { get; set; } + + public virtual Task TransformAsync(ClaimsPrincipal principal) + { + return OnTransform?.Invoke(principal) ?? Task.FromResult(principal); + } + } +} diff --git a/src/Microsoft.AspNet.Authentication/HttpContextExtensions.cs b/src/Microsoft.AspNet.Authentication/HttpContextExtensions.cs index d50b6055d3..0234f22cdb 100644 --- a/src/Microsoft.AspNet.Authentication/HttpContextExtensions.cs +++ b/src/Microsoft.AspNet.Authentication/HttpContextExtensions.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Authentication if (auth == null) { auth = new HttpAuthenticationFeature(); - context.Features.Set(auth); + context.Features.Set(auth); } return auth; } diff --git a/src/Microsoft.AspNet.Authentication/IClaimsTransformer.cs b/src/Microsoft.AspNet.Authentication/IClaimsTransformer.cs new file mode 100644 index 0000000000..a4e4e2f25e --- /dev/null +++ b/src/Microsoft.AspNet.Authentication/IClaimsTransformer.cs @@ -0,0 +1,13 @@ +// 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. + +using System.Security.Claims; +using System.Threading.Tasks; + +namespace Microsoft.AspNet.Authentication +{ + public interface IClaimsTransformer + { + Task TransformAsync(ClaimsPrincipal principal); + } +} diff --git a/src/Microsoft.AspNet.Authorization/ServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Authorization/AuthorizationServiceCollectionExtensions.cs similarity index 94% rename from src/Microsoft.AspNet.Authorization/ServiceCollectionExtensions.cs rename to src/Microsoft.AspNet.Authorization/AuthorizationServiceCollectionExtensions.cs index d0a60edf2e..2d085a7902 100644 --- a/src/Microsoft.AspNet.Authorization/ServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Authorization/AuthorizationServiceCollectionExtensions.cs @@ -8,7 +8,7 @@ using Microsoft.Framework.Internal; namespace Microsoft.Framework.DependencyInjection { - public static class ServiceCollectionExtensions + public static class AuthorizationServiceCollectionExtensions { public static IServiceCollection AddAuthorization([NotNull] this IServiceCollection services) { diff --git a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs index 84d8e1a872..668f6a7ace 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs @@ -232,17 +232,7 @@ namespace Microsoft.AspNet.Authentication.Cookies baseAddress: null, claimsTransform: o => o.Transformer = new ClaimsTransformer { - TransformSyncDelegate = p => - { - if (!p.Identities.Any(i => i.AuthenticationType == "xform")) - { - var id = new ClaimsIdentity("xform"); - id.AddClaim(new Claim("sync", "no")); - p.AddIdentity(id); - } - return p; - }, - TransformAsyncDelegate = p => + OnTransform = p => { if (!p.Identities.Any(i => i.AuthenticationType == "xform")) { @@ -745,22 +735,6 @@ namespace Microsoft.AspNet.Authentication.Cookies Assert.True(transaction.SetCookie[0].StartsWith(".AspNet.Cookies=")); } - [Fact] - public async Task UseCookieWithOutInstanceDoesUseSharedOptions() - { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.CookieName = "One"); - app.UseCookieAuthentication(options => options.AuthenticationScheme = "Two"); - app.Run(context => context.Authentication.SignInAsync("Two", new ClaimsPrincipal(new ClaimsIdentity()))); - }, services => services.AddAuthentication()); - - var transaction = await server.SendAsync("http://example.com"); - - Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode); - Assert.True(transaction.SetCookie[0].StartsWith("One=")); - } - [Fact] public async Task MapWithSignInOnlyRedirectToReturnUrlOnLoginPath() { diff --git a/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs index fa7978e03d..ed1723f3a4 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs @@ -29,16 +29,7 @@ namespace Microsoft.AspNet.Authentication.Facebook var server = CreateServer( app => { - app.UseFacebookAuthentication(); - app.UseCookieAuthentication(); - }, - services => - { - services.AddAuthentication(options => - { - options.SignInScheme = "External"; - }); - services.AddFacebookAuthentication(options => + app.UseFacebookAuthentication(options => { options.AppId = "Test App Id"; options.AppSecret = "Test App Secret"; @@ -51,12 +42,19 @@ namespace Microsoft.AspNet.Authentication.Facebook } }; }); - services.AddCookieAuthentication(options => + app.UseCookieAuthentication(options => { options.AuthenticationScheme = "External"; options.AutomaticAuthentication = true; }); }, + services => + { + services.AddAuthentication(options => + { + options.SignInScheme = "External"; + }); + }, context => { // REVIEW: Gross. @@ -74,19 +72,15 @@ namespace Microsoft.AspNet.Authentication.Facebook { var server = CreateServer(app => app.Map("/base", map => { - map.UseFacebookAuthentication(); - map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" }))); - }), - services => - { - services.AddAuthentication(); - services.AddFacebookAuthentication(options => + map.UseFacebookAuthentication(options => { options.AppId = "Test App Id"; options.AppSecret = "Test App Secret"; options.SignInScheme = "External"; }); - }, + map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" }))); + }), + services => services.AddAuthentication(), handler: null); var transaction = await server.SendAsync("http://example.com/base/login"); Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); @@ -105,19 +99,15 @@ namespace Microsoft.AspNet.Authentication.Facebook var server = CreateServer( app => { - app.UseFacebookAuthentication(); - app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" }))); - }, - services => - { - services.AddAuthentication(); - services.AddFacebookAuthentication(options => + app.UseFacebookAuthentication(options => { options.AppId = "Test App Id"; options.AppSecret = "Test App Secret"; options.SignInScheme = "External"; }); + app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Facebook", new AuthenticationProperties() { RedirectUri = "/" }))); }, + services => services.AddAuthentication(), handler: null); var transaction = await server.SendAsync("http://example.com/login"); Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); @@ -136,24 +126,16 @@ namespace Microsoft.AspNet.Authentication.Facebook var server = CreateServer( app => { - app.UseFacebookAuthentication(); - app.UseCookieAuthentication(); - }, - services => - { - services.AddAuthentication(options => - { - options.SignInScheme = "External"; - }); - services.AddFacebookAuthentication(options => + app.UseFacebookAuthentication(options => { options.AppId = "Test App Id"; options.AppSecret = "Test App Secret"; }); - services.AddCookieAuthentication(options => - { - options.AuthenticationScheme = "External"; - }); + app.UseCookieAuthentication(options => options.AuthenticationScheme = "External"); + }, + services => + { + services.AddAuthentication(options => options.SignInScheme = "External"); }, context => { @@ -181,13 +163,7 @@ namespace Microsoft.AspNet.Authentication.Facebook var server = CreateServer( app => { - app.UseFacebookAuthentication(); - app.UseCookieAuthentication(); - }, - services => - { - services.AddAuthentication(); - services.AddFacebookAuthentication(options => + app.UseFacebookAuthentication(options => { options.AppId = "Test App Id"; options.AppSecret = "Test App Secret"; @@ -224,6 +200,11 @@ namespace Microsoft.AspNet.Authentication.Facebook } }; }); + app.UseCookieAuthentication(); + }, + services => + { + services.AddAuthentication(); }, handler: null); var properties = new AuthenticationProperties(); diff --git a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs index 86801ccc4e..d6d2b7053a 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs @@ -548,7 +548,13 @@ namespace Microsoft.AspNet.Authentication.Google options.AutomaticAuthentication = true; }); app.UseGoogleAuthentication(configureOptions); - app.UseClaimsTransformation(); + app.UseClaimsTransformation(p => + { + var id = new ClaimsIdentity("xform"); + id.AddClaim(new Claim("xform", "yup")); + p.AddIdentity(id); + return Task.FromResult(p); + }); app.Use(async (context, next) => { var req = context.Request; @@ -601,13 +607,6 @@ namespace Microsoft.AspNet.Authentication.Google services => { services.AddAuthentication(options => options.SignInScheme = TestExtensions.CookieAuthenticationScheme); - services.AddClaimsTransformation(p => - { - var id = new ClaimsIdentity("xform"); - id.AddClaim(new Claim("xform", "yup")); - p.AddIdentity(id); - return p; - }); }); } } diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs index d2bee5035c..2345d74318 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs @@ -15,7 +15,6 @@ using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.TestHost; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.WebEncoders; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Xunit; @@ -58,7 +57,7 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect public async Task AuthenticateCoreState(Action action, OpenIdConnectMessage message) { var handler = new OpenIdConnectHandlerForTestingAuthenticate(); - var server = CreateServer(new ConfigureOptions(action), UrlEncoder.Default, handler); + var server = CreateServer(action, UrlEncoder.Default, handler); await server.CreateClient().PostAsync("http://localhost", new FormUrlEncodedContent(message.Parameters.Where(pair => pair.Value != null))); } @@ -116,11 +115,13 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect }; } - private static TestServer CreateServer(ConfigureOptions options, IUrlEncoder encoder, OpenIdConnectHandler handler = null) + private static TestServer CreateServer(Action configureOptions, IUrlEncoder encoder, OpenIdConnectHandler handler = null) { return TestServer.Create( app => { + var options = new OpenIdConnectOptions(); + configureOptions(options); app.UseMiddleware(options, encoder, handler); app.Use(async (context, next) => { diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareForTestingAuthenticate.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareForTestingAuthenticate.cs index e76c213259..46c04ab7a9 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareForTestingAuthenticate.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareForTestingAuthenticate.cs @@ -27,11 +27,10 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect IUrlEncoder encoder, IServiceProvider services, IOptions sharedOptions, - IOptions options, - ConfigureOptions configureOptions = null, + OpenIdConnectOptions options, OpenIdConnectHandler handler = null ) - : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options, configureOptions) + : base(next, dataProtectionProvider, loggerFactory, encoder, services, sharedOptions, options) { _handler = handler; var customFactory = loggerFactory as InMemoryLoggerFactory;