#47 Standardize on I*Events pattern.

This commit is contained in:
Chris R 2015-09-03 11:02:19 -07:00
parent 2aba485263
commit 47520e126e
12 changed files with 357 additions and 313 deletions

View File

@ -14,101 +14,70 @@ namespace Microsoft.AspNet.Authentication.Cookies
public class CookieAuthenticationEvents : ICookieAuthenticationEvents
{
/// <summary>
/// Create a new instance of the default events.
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public CookieAuthenticationEvents()
{
OnValidatePrincipal = context => Task.FromResult(0);
OnResponseSignIn = context => { };
OnResponseSignedIn = context => { };
OnResponseSignOut = context => { };
OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri);
OnException = context => { };
}
public Func<CookieValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => Task.FromResult(0);
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Func<CookieValidatePrincipalContext, Task> OnValidatePrincipal { get; set; }
public Action<CookieResponseSignInContext> OnResponseSignIn { get; set; } = context => { };
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieResponseSignInContext> OnResponseSignIn { get; set; }
public Action<CookieResponseSignedInContext> OnResponseSignedIn { get; set; } = context => { };
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieResponseSignedInContext> OnResponseSignedIn { get; set; }
public Action<CookieResponseSignOutContext> OnResponseSignOut { get; set; } = context => { };
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieResponseSignOutContext> OnResponseSignOut { get; set; }
public Action<CookieApplyRedirectContext> OnApplyRedirect { get; set; } = context => context.Response.Redirect(context.RedirectUri);
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieApplyRedirectContext> OnApplyRedirect { get; set; }
/// <summary>
/// A delegate assigned to this property will be invoked when the related method is called
/// </summary>
public Action<CookieExceptionContext> OnException { get; set; }
public Action<CookieExceptionContext> OnException { get; set; } = context => { };
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context)
{
return OnValidatePrincipal.Invoke(context);
}
public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) => OnValidatePrincipal(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context"></param>
public virtual void ResponseSignIn(CookieResponseSignInContext context)
{
OnResponseSignIn.Invoke(context);
}
public virtual void ResponseSignIn(CookieResponseSignInContext context) => OnResponseSignIn(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context"></param>
public virtual void ResponseSignedIn(CookieResponseSignedInContext context)
{
OnResponseSignedIn.Invoke(context);
}
public virtual void ResponseSignedIn(CookieResponseSignedInContext context) => OnResponseSignedIn(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context"></param>
public virtual void ResponseSignOut(CookieResponseSignOutContext context)
{
OnResponseSignOut.Invoke(context);
}
public virtual void ResponseSignOut(CookieResponseSignOutContext context) => OnResponseSignOut(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual void ApplyRedirect(CookieApplyRedirectContext context)
{
OnApplyRedirect.Invoke(context);
}
public virtual void ApplyRedirect(CookieApplyRedirectContext context) => OnApplyRedirect(context);
/// <summary>
/// Implements the interface method by invoking the related delegate method
/// </summary>
/// <param name="context">Contains information about the event</param>
public virtual void Exception(CookieExceptionContext context)
{
OnException.Invoke(context);
}
public virtual void Exception(CookieExceptionContext context) => OnException(context);
}
}

View File

@ -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;
/// <summary>
/// Specifies events which the <see cref="JwtBearerAuthenticationMiddleware"></see> invokes to enable developer control over the authentication process. />
/// </summary>
namespace Microsoft.AspNet.Authentication.JwtBearer
{
/// <summary>
/// Jwt bearer token middleware events.
/// </summary>
public interface IJwtBearerAuthenticationEvents
{
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
Task AuthenticationFailed(AuthenticationFailedContext context);
/// <summary>
/// Invoked when a protocol message is first received.
/// </summary>
Task MessageReceived(MessageReceivedContext context);
/// <summary>
/// Invoked with the security token that has been extracted from the protocol message.
/// </summary>
Task SecurityTokenReceived(SecurityTokenReceivedContext context);
/// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary>
Task SecurityTokenValidated(SecurityTokenValidatedContext context);
/// <summary>
/// Invoked to apply a challenge sent back to the caller.
/// </summary>
Task ApplyChallenge(AuthenticationChallengeContext context);
}
}

View File

@ -11,45 +11,47 @@ using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.JwtBearer
{
/// <summary>
/// Jwt bearer token middleware provider
/// Jwt bearer token middleware events.
/// </summary>
public class JwtBearerAuthenticationEvents
public class JwtBearerAuthenticationEvents : IJwtBearerAuthenticationEvents
{
/// <summary>
/// Initializes a new instance of the <see cref="JwtBearerAuthenticationProvider"/> class
/// </summary>
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);
}
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
public Func<AuthenticationFailedContext, Task> AuthenticationFailed { get; set; }
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked when a protocol message is first received.
/// </summary>
public Func<MessageReceivedContext, Task> MessageReceived { get; set; }
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked with the security token that has been extracted from the protocol message.
/// </summary>
public Func<SecurityTokenReceivedContext, Task> SecurityTokenReceived { get; set; }
public Func<SecurityTokenReceivedContext, Task> OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary>
public Func<SecurityTokenValidatedContext, Task> SecurityTokenValidated { get; set; }
public Func<SecurityTokenValidatedContext, Task> OnSecurityTokenValidated { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked to apply a challenge sent back to the caller.
/// </summary>
public Func<AuthenticationChallengeContext, Task> ApplyChallenge { get; set; }
public Func<AuthenticationChallengeContext, Task> 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);
}
}

View File

@ -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.
/// </summary>
public JwtBearerAuthenticationEvents Events { get; set; } = new JwtBearerAuthenticationEvents();
public IJwtBearerAuthenticationEvents Events { get; set; } = new JwtBearerAuthenticationEvents();
/// <summary>
/// The HttpMessageHandler used to retrieve metadata.

View File

@ -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
{
/// <summary>
/// Specifies events which the <see cref="OpenIdConnectAuthenticationMiddleware" />invokes to enable developer control over the authentication process.
/// </summary>
public interface IOpenIdConnectAuthenticationEvents
{
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
Task AuthenticationFailed(AuthenticationFailedContext context);
/// <summary>
/// Invoked after security token validation if an authorization code is present in the protocol message.
/// </summary>
Task AuthorizationCodeReceived(AuthorizationCodeReceivedContext context);
/// <summary>
/// Invoked after "authorization code" is redeemed for tokens at the token endpoint.
/// </summary>
Task AuthorizationCodeRedeemed(AuthorizationCodeRedeemedContext context);
/// <summary>
/// Invoked when a protocol message is first received.
/// </summary>
Task MessageReceived(MessageReceivedContext context);
/// <summary>
/// Invoked to manipulate redirects to the identity provider for SignIn, SignOut, or Challenge.
/// </summary>
Task RedirectToIdentityProvider(RedirectToIdentityProviderContext context);
/// <summary>
/// Invoked with the security token that has been extracted from the protocol message.
/// </summary>
Task SecurityTokenReceived(SecurityTokenReceivedContext context);
/// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary>
Task SecurityTokenValidated(SecurityTokenValidatedContext context);
}
}

View File

@ -9,55 +9,55 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
/// <summary>
/// Specifies events which the <see cref="OpenIdConnectAuthenticationMiddleware" />invokes to enable developer control over the authentication process.
/// </summary>
public class OpenIdConnectAuthenticationEvents
public class OpenIdConnectAuthenticationEvents : IOpenIdConnectAuthenticationEvents
{
/// <summary>
/// Creates a new set of events. Each event has a default no-op behavior unless otherwise documented.
/// </summary>
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);
}
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
public Func<AuthenticationFailedContext, Task> AuthenticationFailed { get; set; }
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked after security token validation if an authorization code is present in the protocol message.
/// </summary>
public Func<AuthorizationCodeReceivedContext, Task> AuthorizationCodeReceived { get; set; }
public Func<AuthorizationCodeReceivedContext, Task> OnAuthorizationCodeReceived { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked after "authorization code" is redeemed for tokens at the token endpoint.
/// </summary>
public Func<AuthorizationCodeRedeemedContext, Task> AuthorizationCodeRedeemed { get; set; }
public Func<AuthorizationCodeRedeemedContext, Task> OnAuthorizationCodeRedeemed { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked when a protocol message is first received.
/// </summary>
public Func<MessageReceivedContext, Task> MessageReceived { get; set; }
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked to manipulate redirects to the identity provider for SignIn, SignOut, or Challenge.
/// </summary>
public Func<RedirectToIdentityProviderContext, Task> RedirectToIdentityProvider { get; set; }
public Func<RedirectToIdentityProviderContext, Task> OnRedirectToIdentityProvider { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked with the security token that has been extracted from the protocol message.
/// </summary>
public Func<SecurityTokenReceivedContext, Task> SecurityTokenReceived { get; set; }
public Func<SecurityTokenReceivedContext, Task> OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary>
public Func<SecurityTokenValidatedContext, Task> SecurityTokenValidated { get; set; }
public Func<SecurityTokenValidatedContext, Task> 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);
}
}

View File

@ -160,9 +160,9 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect
public bool CacheNonces { get; set; }
/// <summary>
/// Gets or sets the <see cref="OpenIdConnectAuthenticationEvents"/> to notify when processing OpenIdConnect messages.
/// Gets or sets the <see cref="IOpenIdConnectAuthenticationEvents"/> to notify when processing OpenIdConnect messages.
/// </summary>
public OpenIdConnectAuthenticationEvents Events { get; set; } = new OpenIdConnectAuthenticationEvents();
public IOpenIdConnectAuthenticationEvents Events { get; set; } = new OpenIdConnectAuthenticationEvents();
/// <summary>
/// Gets or sets the <see cref="OpenIdConnectProtocolValidator"/> that is used to ensure that the 'id_token' received

View File

@ -11,58 +11,39 @@ namespace Microsoft.AspNet.Authentication.Twitter
/// </summary>
public class TwitterAuthenticationEvents : ITwitterAuthenticationEvents
{
/// <summary>
/// Initializes a <see cref="TwitterAuthenticationEvents"/>
/// </summary>
public TwitterAuthenticationEvents()
{
OnAuthenticated = context => Task.FromResult<object>(null);
OnReturnEndpoint = context => Task.FromResult<object>(null);
OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri);
}
/// <summary>
/// Gets or sets the function that is invoked when the Authenticated method is invoked.
/// </summary>
public Func<TwitterAuthenticatedContext, Task> OnAuthenticated { get; set; }
public Func<TwitterAuthenticatedContext, Task> OnAuthenticated { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Gets or sets the function that is invoked when the ReturnEndpoint method is invoked.
/// </summary>
public Func<TwitterReturnEndpointContext, Task> OnReturnEndpoint { get; set; }
public Func<TwitterReturnEndpointContext, Task> OnReturnEndpoint { get; set; } = context => Task.FromResult(0);
/// <summary>
/// Gets or sets the delegate that is invoked when the ApplyRedirect method is invoked.
/// </summary>
public Action<TwitterApplyRedirectContext> OnApplyRedirect { get; set; }
public Action<TwitterApplyRedirectContext> OnApplyRedirect { get; set; } = context => context.Response.Redirect(context.RedirectUri);
/// <summary>
/// Invoked whenever Twitter succesfully authenticates a user
/// Invoked whenever Twitter successfully authenticates a user
/// </summary>
/// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
public virtual Task Authenticated(TwitterAuthenticatedContext context)
{
return OnAuthenticated(context);
}
public virtual Task Authenticated(TwitterAuthenticatedContext context) => OnAuthenticated(context);
/// <summary>
/// Invoked prior to the <see cref="System.Security.Claims.ClaimsIdentity"/> being saved in a local cookie and the browser being redirected to the originally requested URL.
/// </summary>
/// <param name="context"></param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
public virtual Task ReturnEndpoint(TwitterReturnEndpointContext context)
{
return OnReturnEndpoint(context);
}
public virtual Task ReturnEndpoint(TwitterReturnEndpointContext context) => OnReturnEndpoint(context);
/// <summary>
/// Called when a Challenge causes a redirect to authorize endpoint in the Twitter middleware
/// </summary>
/// <param name="context">Contains redirect URI and <see cref="AuthenticationProperties"/> of the challenge </param>
public virtual void ApplyRedirect(TwitterApplyRedirectContext context)
{
OnApplyRedirect(context);
}
public virtual void ApplyRedirect(TwitterApplyRedirectContext context) => OnApplyRedirect(context);
}
}

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Authorization
protected override void Handle(AuthorizationContext context, DelegateRequirement requirement)
{
Handler.Invoke(context, requirement);
Handler(context, requirement);
}
}
}

View File

@ -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<object>(null);
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
return Task.FromResult<object>(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<object>(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<object>(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<object>(null);
return Task.FromResult<object>(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<object>(null);
return Task.FromResult<object>(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<object>(null);
return Task.FromResult<object>(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();

View File

@ -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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(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<object>(null);
}
};
var claimValue = context.AuthenticationTicket.Principal.FindFirst("test claim");
Assert.Equal(claimValue.Value, "test value");
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(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<object>(null);
}
};
context.HandleResponse();
return Task.FromResult<object>(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<object>(null);
}
};
context.SkipToNextMiddleware();
return Task.FromResult<object>(null);
}
};
}
private static void StateNullOptions(OpenIdConnectAuthenticationOptions options)

View File

@ -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<object>(null);
}
};
context.ProtocolMessage = mockOpenIdConnectMessage.Object;
return Task.FromResult<object>(null);
}
};
}
/// <summary>
@ -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<object>(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;