diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Events/CookieAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.Cookies/Events/CookieAuthenticationEvents.cs index cbe3a71711..6c65bb857d 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Events/CookieAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Events/CookieAuthenticationEvents.cs @@ -14,101 +14,70 @@ namespace Microsoft.AspNet.Authentication.Cookies public class CookieAuthenticationEvents : ICookieAuthenticationEvents { /// - /// Create a new instance of the default events. + /// A delegate assigned to this property will be invoked when the related method is called /// - public CookieAuthenticationEvents() - { - OnValidatePrincipal = context => Task.FromResult(0); - OnResponseSignIn = context => { }; - OnResponseSignedIn = context => { }; - OnResponseSignOut = context => { }; - OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri); - OnException = context => { }; - } + public Func OnValidatePrincipal { get; set; } = context => Task.FromResult(0); /// /// A delegate assigned to this property will be invoked when the related method is called /// - public Func OnValidatePrincipal { get; set; } + public Action OnResponseSignIn { get; set; } = context => { }; /// /// A delegate assigned to this property will be invoked when the related method is called /// - public Action OnResponseSignIn { get; set; } + public Action OnResponseSignedIn { get; set; } = context => { }; /// /// A delegate assigned to this property will be invoked when the related method is called /// - public Action OnResponseSignedIn { get; set; } + public Action OnResponseSignOut { get; set; } = context => { }; /// /// A delegate assigned to this property will be invoked when the related method is called /// - public Action OnResponseSignOut { get; set; } + public Action OnApplyRedirect { get; set; } = context => context.Response.Redirect(context.RedirectUri); /// /// A delegate assigned to this property will be invoked when the related method is called /// - public Action OnApplyRedirect { get; set; } - - /// - /// A delegate assigned to this property will be invoked when the related method is called - /// - public Action OnException { get; set; } + public Action OnException { get; set; } = context => { }; /// /// Implements the interface method by invoking the related delegate method /// /// /// - public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) - { - return OnValidatePrincipal.Invoke(context); - } + public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) => OnValidatePrincipal(context); /// /// Implements the interface method by invoking the related delegate method /// /// - public virtual void ResponseSignIn(CookieResponseSignInContext context) - { - OnResponseSignIn.Invoke(context); - } + public virtual void ResponseSignIn(CookieResponseSignInContext context) => OnResponseSignIn(context); /// /// Implements the interface method by invoking the related delegate method /// /// - public virtual void ResponseSignedIn(CookieResponseSignedInContext context) - { - OnResponseSignedIn.Invoke(context); - } + public virtual void ResponseSignedIn(CookieResponseSignedInContext context) => OnResponseSignedIn(context); /// /// Implements the interface method by invoking the related delegate method /// /// - public virtual void ResponseSignOut(CookieResponseSignOutContext context) - { - OnResponseSignOut.Invoke(context); - } + public virtual void ResponseSignOut(CookieResponseSignOutContext context) => OnResponseSignOut(context); /// /// Implements the interface method by invoking the related delegate method /// /// Contains information about the event - public virtual void ApplyRedirect(CookieApplyRedirectContext context) - { - OnApplyRedirect.Invoke(context); - } + public virtual void ApplyRedirect(CookieApplyRedirectContext context) => OnApplyRedirect(context); /// /// Implements the interface method by invoking the related delegate method /// /// Contains information about the event - public virtual void Exception(CookieExceptionContext context) - { - OnException.Invoke(context); - } + public virtual void Exception(CookieExceptionContext context) => OnException(context); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerAuthenticationEvents.cs new file mode 100644 index 0000000000..cd1a6fc087 --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerAuthenticationEvents.cs @@ -0,0 +1,43 @@ +// 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.Threading.Tasks; +using Microsoft.AspNet.Http; + +/// +/// Specifies events which the invokes to enable developer control over the authentication process. /> +/// +namespace Microsoft.AspNet.Authentication.JwtBearer +{ + /// + /// Jwt bearer token middleware events. + /// + public interface IJwtBearerAuthenticationEvents + { + /// + /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. + /// + Task AuthenticationFailed(AuthenticationFailedContext context); + + /// + /// Invoked when a protocol message is first received. + /// + Task MessageReceived(MessageReceivedContext context); + + /// + /// Invoked with the security token that has been extracted from the protocol message. + /// + Task SecurityTokenReceived(SecurityTokenReceivedContext context); + + /// + /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. + /// + Task SecurityTokenValidated(SecurityTokenValidatedContext context); + + /// + /// Invoked to apply a challenge sent back to the caller. + /// + Task ApplyChallenge(AuthenticationChallengeContext context); + } +} diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs index a8d674d1e9..4b06937a70 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerAuthenticationEvents.cs @@ -11,45 +11,47 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { /// - /// Jwt bearer token middleware provider + /// Jwt bearer token middleware events. /// - public class JwtBearerAuthenticationEvents + public class JwtBearerAuthenticationEvents : IJwtBearerAuthenticationEvents { - /// - /// Initializes a new instance of the class - /// - public JwtBearerAuthenticationEvents() - { - ApplyChallenge = context => { context.HttpContext.Response.Headers.Append("WWW-Authenticate", context.Options.Challenge); return Task.FromResult(0); }; - AuthenticationFailed = context => Task.FromResult(0); - MessageReceived = context => Task.FromResult(0); - SecurityTokenReceived = context => Task.FromResult(0); - SecurityTokenValidated = context => Task.FromResult(0); - } - /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func AuthenticationFailed { get; set; } + public Func OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); /// /// Invoked when a protocol message is first received. /// - public Func MessageReceived { get; set; } + public Func OnMessageReceived { get; set; } = context => Task.FromResult(0); /// /// Invoked with the security token that has been extracted from the protocol message. /// - public Func SecurityTokenReceived { get; set; } + public Func OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0); /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func SecurityTokenValidated { get; set; } + public Func OnSecurityTokenValidated { get; set; } = context => Task.FromResult(0); /// /// Invoked to apply a challenge sent back to the caller. /// - public Func ApplyChallenge { get; set; } + public Func OnApplyChallenge { get; set; } = context => + { + context.HttpContext.Response.Headers.Append("WWW-Authenticate", context.Options.Challenge); + return Task.FromResult(0); + }; + + public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); + + public virtual Task MessageReceived(MessageReceivedContext context) => OnMessageReceived(context); + + public virtual Task SecurityTokenReceived(SecurityTokenReceivedContext context) => OnSecurityTokenReceived(context); + + public virtual Task SecurityTokenValidated(SecurityTokenValidatedContext context) => OnSecurityTokenValidated(context); + + public virtual Task ApplyChallenge(AuthenticationChallengeContext context) => OnApplyChallenge(context); } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationOptions.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationOptions.cs index 07d0859f20..373095f822 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationOptions.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerAuthenticationOptions.cs @@ -52,7 +52,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer /// The application may implement the interface fully, or it may create an instance of JwtBearerAuthenticationEvents /// and assign delegates only to the events it wants to process. /// - public JwtBearerAuthenticationEvents Events { get; set; } = new JwtBearerAuthenticationEvents(); + public IJwtBearerAuthenticationEvents Events { get; set; } = new JwtBearerAuthenticationEvents(); /// /// The HttpMessageHandler used to retrieve metadata. diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/IOpenIdConnectAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/IOpenIdConnectAuthenticationEvents.cs new file mode 100644 index 0000000000..4c69a1b39f --- /dev/null +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/IOpenIdConnectAuthenticationEvents.cs @@ -0,0 +1,48 @@ +// 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.Threading.Tasks; + +namespace Microsoft.AspNet.Authentication.OpenIdConnect +{ + /// + /// Specifies events which the invokes to enable developer control over the authentication process. + /// + public interface IOpenIdConnectAuthenticationEvents + { + /// + /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. + /// + Task AuthenticationFailed(AuthenticationFailedContext context); + + /// + /// Invoked after security token validation if an authorization code is present in the protocol message. + /// + Task AuthorizationCodeReceived(AuthorizationCodeReceivedContext context); + + /// + /// Invoked after "authorization code" is redeemed for tokens at the token endpoint. + /// + Task AuthorizationCodeRedeemed(AuthorizationCodeRedeemedContext context); + + /// + /// Invoked when a protocol message is first received. + /// + Task MessageReceived(MessageReceivedContext context); + + /// + /// Invoked to manipulate redirects to the identity provider for SignIn, SignOut, or Challenge. + /// + Task RedirectToIdentityProvider(RedirectToIdentityProviderContext context); + + /// + /// Invoked with the security token that has been extracted from the protocol message. + /// + Task SecurityTokenReceived(SecurityTokenReceivedContext context); + + /// + /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. + /// + Task SecurityTokenValidated(SecurityTokenValidatedContext context); + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs index 134ce48929..9f21f74537 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/Events/OpenIdConnectAuthenticationEvents.cs @@ -9,55 +9,55 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect /// /// Specifies events which the invokes to enable developer control over the authentication process. /// - public class OpenIdConnectAuthenticationEvents + public class OpenIdConnectAuthenticationEvents : IOpenIdConnectAuthenticationEvents { - /// - /// Creates a new set of events. Each event has a default no-op behavior unless otherwise documented. - /// - public OpenIdConnectAuthenticationEvents() - { - AuthenticationFailed = context => Task.FromResult(0); - AuthorizationCodeReceived = context => Task.FromResult(0); - AuthorizationCodeRedeemed = context => Task.FromResult(0); - MessageReceived = context => Task.FromResult(0); - RedirectToIdentityProvider = context => Task.FromResult(0); - SecurityTokenReceived = context => Task.FromResult(0); - SecurityTokenValidated = context => Task.FromResult(0); - } - /// /// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed. /// - public Func AuthenticationFailed { get; set; } + public Func OnAuthenticationFailed { get; set; } = context => Task.FromResult(0); /// /// Invoked after security token validation if an authorization code is present in the protocol message. /// - public Func AuthorizationCodeReceived { get; set; } + public Func OnAuthorizationCodeReceived { get; set; } = context => Task.FromResult(0); /// /// Invoked after "authorization code" is redeemed for tokens at the token endpoint. /// - public Func AuthorizationCodeRedeemed { get; set; } + public Func OnAuthorizationCodeRedeemed { get; set; } = context => Task.FromResult(0); /// /// Invoked when a protocol message is first received. /// - public Func MessageReceived { get; set; } + public Func OnMessageReceived { get; set; } = context => Task.FromResult(0); /// /// Invoked to manipulate redirects to the identity provider for SignIn, SignOut, or Challenge. /// - public Func RedirectToIdentityProvider { get; set; } + public Func OnRedirectToIdentityProvider { get; set; } = context => Task.FromResult(0); /// /// Invoked with the security token that has been extracted from the protocol message. /// - public Func SecurityTokenReceived { get; set; } + public Func OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0); /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func SecurityTokenValidated { get; set; } + public Func OnSecurityTokenValidated { get; set; } = context => Task.FromResult(0); + + public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); + + public virtual Task AuthorizationCodeReceived(AuthorizationCodeReceivedContext context) => OnAuthorizationCodeReceived(context); + + public virtual Task AuthorizationCodeRedeemed(AuthorizationCodeRedeemedContext context) => OnAuthorizationCodeRedeemed(context); + + public virtual Task MessageReceived(MessageReceivedContext context) => OnMessageReceived(context); + + public virtual Task RedirectToIdentityProvider(RedirectToIdentityProviderContext context) => OnRedirectToIdentityProvider(context); + + public virtual Task SecurityTokenReceived(SecurityTokenReceivedContext context) => OnSecurityTokenReceived(context); + + public virtual Task SecurityTokenValidated(SecurityTokenValidatedContext context) => OnSecurityTokenValidated(context); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs index 045b279cfd..6809b571a7 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenIdConnectAuthenticationOptions.cs @@ -160,9 +160,9 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect public bool CacheNonces { get; set; } /// - /// Gets or sets the to notify when processing OpenIdConnect messages. + /// Gets or sets the to notify when processing OpenIdConnect messages. /// - public OpenIdConnectAuthenticationEvents Events { get; set; } = new OpenIdConnectAuthenticationEvents(); + public IOpenIdConnectAuthenticationEvents Events { get; set; } = new OpenIdConnectAuthenticationEvents(); /// /// Gets or sets the that is used to ensure that the 'id_token' received diff --git a/src/Microsoft.AspNet.Authentication.Twitter/Events/TwitterAuthenticationEvents.cs b/src/Microsoft.AspNet.Authentication.Twitter/Events/TwitterAuthenticationEvents.cs index 3fcd6fe84b..deae986da7 100644 --- a/src/Microsoft.AspNet.Authentication.Twitter/Events/TwitterAuthenticationEvents.cs +++ b/src/Microsoft.AspNet.Authentication.Twitter/Events/TwitterAuthenticationEvents.cs @@ -11,58 +11,39 @@ namespace Microsoft.AspNet.Authentication.Twitter /// public class TwitterAuthenticationEvents : ITwitterAuthenticationEvents { - /// - /// Initializes a - /// - public TwitterAuthenticationEvents() - { - OnAuthenticated = context => Task.FromResult(null); - OnReturnEndpoint = context => Task.FromResult(null); - OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri); - } - /// /// Gets or sets the function that is invoked when the Authenticated method is invoked. /// - public Func OnAuthenticated { get; set; } + public Func OnAuthenticated { get; set; } = context => Task.FromResult(0); /// /// Gets or sets the function that is invoked when the ReturnEndpoint method is invoked. /// - public Func OnReturnEndpoint { get; set; } + public Func OnReturnEndpoint { get; set; } = context => Task.FromResult(0); /// /// Gets or sets the delegate that is invoked when the ApplyRedirect method is invoked. /// - public Action OnApplyRedirect { get; set; } + public Action OnApplyRedirect { get; set; } = context => context.Response.Redirect(context.RedirectUri); /// - /// Invoked whenever Twitter succesfully authenticates a user + /// Invoked whenever Twitter successfully authenticates a user /// /// Contains information about the login session as well as the user . /// A representing the completed operation. - public virtual Task Authenticated(TwitterAuthenticatedContext context) - { - return OnAuthenticated(context); - } + public virtual Task Authenticated(TwitterAuthenticatedContext context) => OnAuthenticated(context); /// /// Invoked prior to the being saved in a local cookie and the browser being redirected to the originally requested URL. /// /// /// A representing the completed operation. - public virtual Task ReturnEndpoint(TwitterReturnEndpointContext context) - { - return OnReturnEndpoint(context); - } + public virtual Task ReturnEndpoint(TwitterReturnEndpointContext context) => OnReturnEndpoint(context); /// /// Called when a Challenge causes a redirect to authorize endpoint in the Twitter middleware /// /// Contains redirect URI and of the challenge - public virtual void ApplyRedirect(TwitterApplyRedirectContext context) - { - OnApplyRedirect(context); - } + public virtual void ApplyRedirect(TwitterApplyRedirectContext context) => OnApplyRedirect(context); } } diff --git a/src/Microsoft.AspNet.Authorization/DelegateRequirement.cs b/src/Microsoft.AspNet.Authorization/DelegateRequirement.cs index 11e30c3e6b..ea985d5e91 100644 --- a/src/Microsoft.AspNet.Authorization/DelegateRequirement.cs +++ b/src/Microsoft.AspNet.Authorization/DelegateRequirement.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Authorization protected override void Handle(AuthorizationContext context, DelegateRequirement requirement) { - Handler.Invoke(context, requirement); + Handler(context, requirement); } } } diff --git a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs index cc125fb4e2..aebeb06170 100644 --- a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs @@ -67,22 +67,25 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.AutomaticAuthentication = true; - options.Events.MessageReceived = context => + options.Events = new JwtBearerAuthenticationEvents() { - var claims = new[] + OnMessageReceived = context => { - new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), - new Claim(ClaimTypes.Email, "bob@contoso.com"), - new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") - }; + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), + new Claim(ClaimTypes.Email, "bob@contoso.com"), + new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") + }; - context.AuthenticationTicket = new AuthenticationTicket( - new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), - new AuthenticationProperties(), context.Options.AuthenticationScheme); + context.AuthenticationTicket = new AuthenticationTicket( + new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), + new AuthenticationProperties(), context.Options.AuthenticationScheme); - context.HandleResponse(); - - return Task.FromResult(null); + context.HandleResponse(); + + return Task.FromResult(null); + } }; }); @@ -114,22 +117,25 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.AutomaticAuthentication = true; - options.Events.SecurityTokenReceived = context => + options.Events = new JwtBearerAuthenticationEvents() { - var claims = new[] + OnSecurityTokenReceived = context => { - new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), - new Claim(ClaimTypes.Email, "bob@contoso.com"), - new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") - }; + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), + new Claim(ClaimTypes.Email, "bob@contoso.com"), + new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") + }; - context.AuthenticationTicket = new AuthenticationTicket( - new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), - new AuthenticationProperties(), context.Options.AuthenticationScheme); + context.AuthenticationTicket = new AuthenticationTicket( + new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), + new AuthenticationProperties(), context.Options.AuthenticationScheme); - context.HandleResponse(); - - return Task.FromResult(null); + context.HandleResponse(); + + return Task.FromResult(null); + } }; }); @@ -145,23 +151,26 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.AutomaticAuthentication = true; - options.Events.SecurityTokenValidated = context => + options.Events = new JwtBearerAuthenticationEvents() { - // Retrieve the NameIdentifier claim from the identity - // returned by the custom security token validator. - var identity = (ClaimsIdentity)context.AuthenticationTicket.Principal.Identity; - var identifier = identity.FindFirst(ClaimTypes.NameIdentifier); + OnSecurityTokenValidated = context => + { + // Retrieve the NameIdentifier claim from the identity + // returned by the custom security token validator. + var identity = (ClaimsIdentity)context.AuthenticationTicket.Principal.Identity; + var identifier = identity.FindFirst(ClaimTypes.NameIdentifier); - identifier.Value.ShouldBe("Bob le Tout Puissant"); + identifier.Value.ShouldBe("Bob le Tout Puissant"); - // Remove the existing NameIdentifier claim and replace it - // with a new one containing a different value. - identity.RemoveClaim(identifier); - // Make sure to use a different name identifier - // than the one defined by BlobTokenValidator. - identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique")); + // Remove the existing NameIdentifier claim and replace it + // with a new one containing a different value. + identity.RemoveClaim(identifier); + // Make sure to use a different name identifier + // than the one defined by BlobTokenValidator. + identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique")); - return Task.FromResult(null); + return Task.FromResult(null); + } }; options.SecurityTokenValidators.Add(new BlobTokenValidator(options.AuthenticationScheme)); @@ -179,28 +188,30 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.AutomaticAuthentication = true; - options.Events.MessageReceived = context => + options.Events = new JwtBearerAuthenticationEvents() { - context.Token = "CustomToken"; - return Task.FromResult(null); - }; - - options.Events.SecurityTokenReceived = context => - { - var claims = new[] + OnMessageReceived = context => { - new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), - new Claim(ClaimTypes.Email, "bob@contoso.com"), - new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") - }; + context.Token = "CustomToken"; + return Task.FromResult(null); + }, + OnSecurityTokenReceived = context => + { + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), + new Claim(ClaimTypes.Email, "bob@contoso.com"), + new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") + }; - context.AuthenticationTicket = new AuthenticationTicket( - new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), - new AuthenticationProperties(), context.Options.AuthenticationScheme); + context.AuthenticationTicket = new AuthenticationTicket( + new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), + new AuthenticationProperties(), context.Options.AuthenticationScheme); - context.HandleResponse(); + context.HandleResponse(); - return Task.FromResult(null); + return Task.FromResult(null); + } }; }); @@ -214,22 +225,25 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { var server = CreateServer(options => { - options.Events.SecurityTokenReceived = context => + options.Events = new JwtBearerAuthenticationEvents() { - var claims = new[] + OnSecurityTokenReceived = context => { - new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), - new Claim(ClaimTypes.Email, "bob@contoso.com"), - new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") - }; + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), + new Claim(ClaimTypes.Email, "bob@contoso.com"), + new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") + }; - context.AuthenticationTicket = new AuthenticationTicket( - new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), - new AuthenticationProperties(), context.Options.AuthenticationScheme); + context.AuthenticationTicket = new AuthenticationTicket( + new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), + new AuthenticationProperties(), context.Options.AuthenticationScheme); - context.HandleResponse(); + context.HandleResponse(); - return Task.FromResult(null); + return Task.FromResult(null); + } }; }); @@ -242,22 +256,25 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { var server = CreateServer(options => { - options.Events.SecurityTokenReceived = context => + options.Events = new JwtBearerAuthenticationEvents() { - var claims = new[] + OnSecurityTokenReceived = context => { - new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), - new Claim(ClaimTypes.Email, "bob@contoso.com"), - new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") - }; + var claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, "Bob le Magnifique"), + new Claim(ClaimTypes.Email, "bob@contoso.com"), + new Claim(ClaimsIdentity.DefaultNameClaimType, "bob") + }; - context.AuthenticationTicket = new AuthenticationTicket( - new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), - new AuthenticationProperties(), context.Options.AuthenticationScheme); + context.AuthenticationTicket = new AuthenticationTicket( + new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)), + new AuthenticationProperties(), context.Options.AuthenticationScheme); - context.HandleResponse(); + context.HandleResponse(); - return Task.FromResult(null); + return Task.FromResult(null); + } }; }); @@ -267,7 +284,6 @@ namespace Microsoft.AspNet.Authentication.JwtBearer class BlobTokenValidator : ISecurityTokenValidator { - public BlobTokenValidator(string authenticationScheme) { AuthenticationScheme = authenticationScheme; @@ -283,7 +299,6 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { throw new NotImplementedException(); } - set { throw new NotImplementedException(); diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs index 80c77ac535..2e1ab50b5a 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs @@ -102,9 +102,9 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect options.ConfigurationManager = TestUtilities.DefaultOpenIdConnectConfigurationManager; options.ClientId = Guid.NewGuid().ToString(); options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue(); - options.Events = new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() { - AuthorizationCodeRedeemed = context => + OnAuthorizationCodeRedeemed = context => { context.HandleResponse(); if (context.ProtocolMessage.State == null && !context.ProtocolMessage.Parameters.ContainsKey(ExpectedStateParameter)) @@ -274,15 +274,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.SecurityTokenValidator = MockSecurityTokenValidator(); options.ProtocolValidator = MockProtocolValidator(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthorizationCodeReceived = (context) => { - AuthorizationCodeReceived = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void AuthorizationCodeReceivedSkippedOptions(OpenIdConnectAuthenticationOptions options) @@ -290,15 +289,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.SecurityTokenValidator = MockSecurityTokenValidator(); options.ProtocolValidator = MockProtocolValidator(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthorizationCodeReceived = (context) => { - AuthorizationCodeReceived = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static void AuthenticationErrorHandledOptions(OpenIdConnectAuthenticationOptions options) @@ -306,15 +304,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.SecurityTokenValidator = MockSecurityTokenValidator(); options.ProtocolValidator = MockProtocolValidator(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthenticationFailed = (context) => { - AuthenticationFailed = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void AuthenticationErrorSkippedOptions(OpenIdConnectAuthenticationOptions options) @@ -322,29 +319,27 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.SecurityTokenValidator = MockSecurityTokenValidator(); options.ProtocolValidator = MockProtocolValidator(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthenticationFailed = (context) => { - AuthenticationFailed = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static void MessageReceivedHandledOptions(OpenIdConnectAuthenticationOptions options) { DefaultOptions(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnMessageReceived = (context) => { - MessageReceived = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void CodeReceivedAndRedeemedHandledOptions(OpenIdConnectAuthenticationOptions options) @@ -352,15 +347,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.ResponseType = OpenIdConnectResponseTypes.Code; options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthorizationCodeRedeemed = (context) => { - AuthorizationCodeRedeemed = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void CodeReceivedAndRedeemedSkippedOptions(OpenIdConnectAuthenticationOptions options) @@ -368,15 +362,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect DefaultOptions(options); options.ResponseType = OpenIdConnectResponseTypes.Code; options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnAuthorizationCodeRedeemed = (context) => { - AuthorizationCodeRedeemed = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static void GetUserInfoFromUIEndpoint(OpenIdConnectAuthenticationOptions options) @@ -387,30 +380,28 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect options.StateDataFormat = new AuthenticationPropertiesFormaterKeyValue(); options.GetClaimsFromUserInfoEndpoint = true; options.SecurityTokenValidator = MockSecurityTokenValidator(); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnSecurityTokenValidated = (context) => { - SecurityTokenValidated = (context) => - { - var claimValue = context.AuthenticationTicket.Principal.FindFirst("test claim"); - Assert.Equal(claimValue.Value, "test value"); - context.HandleResponse(); - return Task.FromResult(null); - } - }; + var claimValue = context.AuthenticationTicket.Principal.FindFirst("test claim"); + Assert.Equal(claimValue.Value, "test value"); + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void MessageReceivedSkippedOptions(OpenIdConnectAuthenticationOptions options) { DefaultOptions(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnMessageReceived = (context) => { - MessageReceived = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static void MessageWithErrorOptions(OpenIdConnectAuthenticationOptions options) @@ -421,29 +412,27 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect private static void SecurityTokenReceivedHandledOptions(OpenIdConnectAuthenticationOptions options) { DefaultOptions(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnSecurityTokenReceived = (context) => { - SecurityTokenReceived = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void SecurityTokenReceivedSkippedOptions(OpenIdConnectAuthenticationOptions options) { DefaultOptions(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnSecurityTokenReceived = (context) => { - SecurityTokenReceived = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static ISecurityTokenValidator MockSecurityTokenValidator() @@ -492,29 +481,27 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect private static void SecurityTokenValidatedHandledOptions(OpenIdConnectAuthenticationOptions options) { SecurityTokenValidatorValidatesAllTokens(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnSecurityTokenValidated = (context) => { - SecurityTokenValidated = (context) => - { - context.HandleResponse(); - return Task.FromResult(null); - } - }; + context.HandleResponse(); + return Task.FromResult(null); + } + }; } private static void SecurityTokenValidatedSkippedOptions(OpenIdConnectAuthenticationOptions options) { SecurityTokenValidatorValidatesAllTokens(options); - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnSecurityTokenValidated = (context) => { - SecurityTokenValidated = (context) => - { - context.SkipToNextMiddleware(); - return Task.FromResult(null); - } - }; + context.SkipToNextMiddleware(); + return Task.FromResult(null); + } + }; } private static void StateNullOptions(OpenIdConnectAuthenticationOptions options) diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs index e8d724be2a..971301c19b 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs @@ -130,15 +130,14 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect mockOpenIdConnectMessage.Setup(m => m.CreateAuthenticationRequestUrl()).Returns(ExpectedAuthorizeRequest); mockOpenIdConnectMessage.Setup(m => m.CreateLogoutRequestUrl()).Returns(ExpectedLogoutRequest); options.AutomaticAuthentication = true; - options.Events = - new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() + { + OnRedirectToIdentityProvider = (context) => { - RedirectToIdentityProvider = (context) => - { - context.ProtocolMessage = mockOpenIdConnectMessage.Object; - return Task.FromResult(null); - } - }; + context.ProtocolMessage = mockOpenIdConnectMessage.Object; + return Task.FromResult(null); + } + }; } /// @@ -163,9 +162,9 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect { SetOptions(options, DefaultParameters(new string[] { OpenIdConnectParameterNames.State }), queryValues, stateDataFormat); options.AutomaticAuthentication = challenge.Equals(ChallengeWithOutContext); - options.Events = new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() { - RedirectToIdentityProvider = context => + OnRedirectToIdentityProvider = context => { context.ProtocolMessage.State = userState; return Task.FromResult(null); @@ -214,9 +213,9 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect var server = CreateServer(options => { SetOptions(options, DefaultParameters(), queryValues); - options.Events = new OpenIdConnectAuthenticationEvents + options.Events = new OpenIdConnectAuthenticationEvents() { - RedirectToIdentityProvider = context => + OnRedirectToIdentityProvider = context => { context.ProtocolMessage.ClientId = queryValuesSetInEvent.ClientId; context.ProtocolMessage.RedirectUri = queryValuesSetInEvent.RedirectUri;