From 852f44a3691c02c379b586fe7ef3dc0cb4cea6ab Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 24 Sep 2015 14:53:31 -0700 Subject: [PATCH] Rename JwtBearer events --- ...ectBearerEvents.cs => IJwtBearerEvents.cs} | 8 ++--- ...ontext.cs => JwtBearerChallengeContext.cs} | 4 +-- ...nectBearerEvents.cs => JwtBearerEvents.cs} | 16 ++++----- ...ivedContext.cs => ReceivedTokenContext.cs} | 6 ++-- ...vedContext.cs => ReceivingTokenContext.cs} | 4 +-- ...ngeContext.cs => TokenValidatedContext.cs} | 4 +-- .../JwtBearerHandler.cs | 36 +++++++++---------- .../JwtBearer/JwtBearerMiddlewareTests.cs | 14 ++++---- 8 files changed, 46 insertions(+), 46 deletions(-) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{IOpenIdConnectBearerEvents.cs => IJwtBearerEvents.cs} (82%) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{SecurityTokenValidatedContext.cs => JwtBearerChallengeContext.cs} (64%) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{OpenIdConnectBearerEvents.cs => JwtBearerEvents.cs} (64%) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{SecurityTokenReceivedContext.cs => ReceivedTokenContext.cs} (58%) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{MessageReceivedContext.cs => ReceivingTokenContext.cs} (76%) rename src/Microsoft.AspNet.Authentication.JwtBearer/Events/{AuthenticationChallengeContext.cs => TokenValidatedContext.cs} (64%) diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IOpenIdConnectBearerEvents.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerEvents.cs similarity index 82% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/IOpenIdConnectBearerEvents.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerEvents.cs index d4d280484d..cf47ab47ea 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IOpenIdConnectBearerEvents.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/IJwtBearerEvents.cs @@ -21,21 +21,21 @@ namespace Microsoft.AspNet.Authentication.JwtBearer /// /// Invoked when a protocol message is first received. /// - Task MessageReceived(MessageReceivedContext context); + Task ReceivingToken(ReceivingTokenContext context); /// /// Invoked with the security token that has been extracted from the protocol message. /// - Task SecurityTokenReceived(SecurityTokenReceivedContext context); + Task ReceivedToken(ReceivedTokenContext context); /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - Task SecurityTokenValidated(SecurityTokenValidatedContext context); + Task ValidatedToken(ValidatedTokenContext context); /// /// Invoked to apply a challenge sent back to the caller. /// - Task ApplyChallenge(AuthenticationChallengeContext context); + Task Challenge(JwtBearerChallengeContext context); } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerChallengeContext.cs similarity index 64% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerChallengeContext.cs index 488e8e6b02..b67c5b9e55 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenValidatedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerChallengeContext.cs @@ -5,9 +5,9 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { - public class SecurityTokenValidatedContext : BaseControlContext + public class JwtBearerChallengeContext : BaseControlContext { - public SecurityTokenValidatedContext(HttpContext context, JwtBearerOptions options) + public JwtBearerChallengeContext(HttpContext context, JwtBearerOptions options) : base(context, options) { } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/OpenIdConnectBearerEvents.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerEvents.cs similarity index 64% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/OpenIdConnectBearerEvents.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerEvents.cs index e4aa6ed8e0..e9832862b2 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/OpenIdConnectBearerEvents.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/JwtBearerEvents.cs @@ -23,22 +23,22 @@ namespace Microsoft.AspNet.Authentication.JwtBearer /// /// Invoked when a protocol message is first received. /// - public Func OnMessageReceived { get; set; } = context => Task.FromResult(0); + public Func OnReceivingToken { get; set; } = context => Task.FromResult(0); /// /// Invoked with the security token that has been extracted from the protocol message. /// - public Func OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0); + public Func OnReceivedToken { get; set; } = context => Task.FromResult(0); /// /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// - public Func OnSecurityTokenValidated { get; set; } = context => Task.FromResult(0); + public Func OnValidatedToken { get; set; } = context => Task.FromResult(0); /// /// Invoked to apply a challenge sent back to the caller. /// - public Func OnApplyChallenge { get; set; } = context => + public Func OnChallenge { get; set; } = context => { context.HttpContext.Response.Headers.Append("WWW-Authenticate", context.Options.Challenge); return Task.FromResult(0); @@ -46,12 +46,12 @@ namespace Microsoft.AspNet.Authentication.JwtBearer public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); - public virtual Task MessageReceived(MessageReceivedContext context) => OnMessageReceived(context); + public virtual Task ReceivingToken(ReceivingTokenContext context) => OnReceivingToken(context); - public virtual Task SecurityTokenReceived(SecurityTokenReceivedContext context) => OnSecurityTokenReceived(context); + public virtual Task ReceivedToken(ReceivedTokenContext context) => OnReceivedToken(context); - public virtual Task SecurityTokenValidated(SecurityTokenValidatedContext context) => OnSecurityTokenValidated(context); + public virtual Task ValidatedToken(ValidatedTokenContext context) => OnValidatedToken(context); - public virtual Task ApplyChallenge(AuthenticationChallengeContext context) => OnApplyChallenge(context); + public virtual Task Challenge(JwtBearerChallengeContext context) => OnChallenge(context); } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivedTokenContext.cs similarity index 58% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivedTokenContext.cs index 5aedda1d84..1cf0eefb1b 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/SecurityTokenReceivedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivedTokenContext.cs @@ -5,13 +5,13 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { - public class SecurityTokenReceivedContext : BaseControlContext + public class ReceivedTokenContext : BaseControlContext { - public SecurityTokenReceivedContext(HttpContext context, JwtBearerOptions options) + public ReceivedTokenContext(HttpContext context, JwtBearerOptions options) : base(context, options) { } - public string SecurityToken { get; set; } + public string Token { get; set; } } } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivingTokenContext.cs similarity index 76% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivingTokenContext.cs index cd940ef679..3f85c55214 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/MessageReceivedContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/ReceivingTokenContext.cs @@ -5,9 +5,9 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { - public class MessageReceivedContext : BaseControlContext + public class ReceivingTokenContext : BaseControlContext { - public MessageReceivedContext(HttpContext context, JwtBearerOptions options) + public ReceivingTokenContext(HttpContext context, JwtBearerOptions options) : base(context, options) { } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/TokenValidatedContext.cs similarity index 64% rename from src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs rename to src/Microsoft.AspNet.Authentication.JwtBearer/Events/TokenValidatedContext.cs index 44f44d5b3e..3d95e4acc0 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/Events/AuthenticationChallengeContext.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/Events/TokenValidatedContext.cs @@ -5,9 +5,9 @@ using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Authentication.JwtBearer { - public class AuthenticationChallengeContext : BaseControlContext + public class ValidatedTokenContext : BaseControlContext { - public AuthenticationChallengeContext(HttpContext context, JwtBearerOptions options) + public ValidatedTokenContext(HttpContext context, JwtBearerOptions options) : base(context, options) { } diff --git a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerHandler.cs b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerHandler.cs index 2577f897eb..cc8a838331 100644 --- a/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerHandler.cs +++ b/src/Microsoft.AspNet.Authentication.JwtBearer/JwtBearerHandler.cs @@ -26,22 +26,22 @@ namespace Microsoft.AspNet.Authentication.JwtBearer try { // Give application opportunity to find from a different location, adjust, or reject token - var messageReceivedContext = new MessageReceivedContext(Context, Options); + var receivingTokenContext = new ReceivingTokenContext(Context, Options); // event can set the token - await Options.Events.MessageReceived(messageReceivedContext); - if (messageReceivedContext.HandledResponse) + await Options.Events.ReceivingToken(receivingTokenContext); + if (receivingTokenContext.HandledResponse) { - return messageReceivedContext.AuthenticationTicket; + return receivingTokenContext.AuthenticationTicket; } - if (messageReceivedContext.Skipped) + if (receivingTokenContext.Skipped) { return null; } // If application retrieved token from somewhere else, use that. - token = messageReceivedContext.Token; + token = receivingTokenContext.Token; if (string.IsNullOrEmpty(token)) { @@ -66,18 +66,18 @@ namespace Microsoft.AspNet.Authentication.JwtBearer } // notify user token was received - var securityTokenReceivedContext = new SecurityTokenReceivedContext(Context, Options) + var receivedTokenContext = new ReceivedTokenContext(Context, Options) { - SecurityToken = token, + Token = token, }; - await Options.Events.SecurityTokenReceived(securityTokenReceivedContext); - if (securityTokenReceivedContext.HandledResponse) + await Options.Events.ReceivedToken(receivedTokenContext); + if (receivedTokenContext.HandledResponse) { - return securityTokenReceivedContext.AuthenticationTicket; + return receivedTokenContext.AuthenticationTicket; } - if (securityTokenReceivedContext.Skipped) + if (receivedTokenContext.Skipped) { return null; } @@ -110,18 +110,18 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { var principal = validator.ValidateToken(token, validationParameters, out validatedToken); var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme); - var securityTokenValidatedContext = new SecurityTokenValidatedContext(Context, Options) + var validatedTokenContext = new ValidatedTokenContext(Context, Options) { AuthenticationTicket = ticket }; - await Options.Events.SecurityTokenValidated(securityTokenValidatedContext); - if (securityTokenValidatedContext.HandledResponse) + await Options.Events.ValidatedToken(validatedTokenContext); + if (validatedTokenContext.HandledResponse) { - return securityTokenValidatedContext.AuthenticationTicket; + return validatedTokenContext.AuthenticationTicket; } - if (securityTokenValidatedContext.Skipped) + if (validatedTokenContext.Skipped) { return null; } @@ -165,7 +165,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer protected override async Task HandleUnauthorizedAsync(ChallengeContext context) { Response.StatusCode = 401; - await Options.Events.ApplyChallenge(new AuthenticationChallengeContext(Context, Options)); + await Options.Events.Challenge(new JwtBearerChallengeContext(Context, Options)); return false; } diff --git a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs index 24bc6ccec4..96ea604ec5 100644 --- a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs @@ -68,7 +68,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer options.Events = new JwtBearerEvents() { - OnMessageReceived = context => + OnReceivingToken = context => { var claims = new[] { @@ -118,7 +118,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer options.Events = new JwtBearerEvents() { - OnSecurityTokenReceived = context => + OnReceivedToken = context => { var claims = new[] { @@ -152,7 +152,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer options.Events = new JwtBearerEvents() { - OnSecurityTokenValidated = context => + OnValidatedToken = context => { // Retrieve the NameIdentifier claim from the identity // returned by the custom security token validator. @@ -189,12 +189,12 @@ namespace Microsoft.AspNet.Authentication.JwtBearer options.Events = new JwtBearerEvents() { - OnMessageReceived = context => + OnReceivingToken = context => { context.Token = "CustomToken"; return Task.FromResult(null); }, - OnSecurityTokenReceived = context => + OnReceivedToken = context => { var claims = new[] { @@ -226,7 +226,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.Events = new JwtBearerEvents() { - OnSecurityTokenReceived = context => + OnReceivedToken = context => { var claims = new[] { @@ -257,7 +257,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer { options.Events = new JwtBearerEvents() { - OnSecurityTokenReceived = context => + OnReceivedToken = context => { var claims = new[] {