Rename JwtBearer events

This commit is contained in:
Hao Kung 2015-09-24 14:53:31 -07:00
parent 966fa6672f
commit 852f44a369
8 changed files with 46 additions and 46 deletions

View File

@ -21,21 +21,21 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
/// <summary> /// <summary>
/// Invoked when a protocol message is first received. /// Invoked when a protocol message is first received.
/// </summary> /// </summary>
Task MessageReceived(MessageReceivedContext context); Task ReceivingToken(ReceivingTokenContext context);
/// <summary> /// <summary>
/// Invoked with the security token that has been extracted from the protocol message. /// Invoked with the security token that has been extracted from the protocol message.
/// </summary> /// </summary>
Task SecurityTokenReceived(SecurityTokenReceivedContext context); Task ReceivedToken(ReceivedTokenContext context);
/// <summary> /// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary> /// </summary>
Task SecurityTokenValidated(SecurityTokenValidatedContext context); Task ValidatedToken(ValidatedTokenContext context);
/// <summary> /// <summary>
/// Invoked to apply a challenge sent back to the caller. /// Invoked to apply a challenge sent back to the caller.
/// </summary> /// </summary>
Task ApplyChallenge(AuthenticationChallengeContext context); Task Challenge(JwtBearerChallengeContext context);
} }
} }

View File

@ -5,9 +5,9 @@ using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.JwtBearer namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
public class SecurityTokenValidatedContext : BaseControlContext<JwtBearerOptions> public class JwtBearerChallengeContext : BaseControlContext<JwtBearerOptions>
{ {
public SecurityTokenValidatedContext(HttpContext context, JwtBearerOptions options) public JwtBearerChallengeContext(HttpContext context, JwtBearerOptions options)
: base(context, options) : base(context, options)
{ {
} }

View File

@ -23,22 +23,22 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
/// <summary> /// <summary>
/// Invoked when a protocol message is first received. /// Invoked when a protocol message is first received.
/// </summary> /// </summary>
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0); public Func<ReceivingTokenContext, Task> OnReceivingToken { get; set; } = context => Task.FromResult(0);
/// <summary> /// <summary>
/// Invoked with the security token that has been extracted from the protocol message. /// Invoked with the security token that has been extracted from the protocol message.
/// </summary> /// </summary>
public Func<SecurityTokenReceivedContext, Task> OnSecurityTokenReceived { get; set; } = context => Task.FromResult(0); public Func<ReceivedTokenContext, Task> OnReceivedToken { get; set; } = context => Task.FromResult(0);
/// <summary> /// <summary>
/// Invoked after the security token has passed validation and a ClaimsIdentity has been generated. /// Invoked after the security token has passed validation and a ClaimsIdentity has been generated.
/// </summary> /// </summary>
public Func<SecurityTokenValidatedContext, Task> OnSecurityTokenValidated { get; set; } = context => Task.FromResult(0); public Func<ValidatedTokenContext, Task> OnValidatedToken { get; set; } = context => Task.FromResult(0);
/// <summary> /// <summary>
/// Invoked to apply a challenge sent back to the caller. /// Invoked to apply a challenge sent back to the caller.
/// </summary> /// </summary>
public Func<AuthenticationChallengeContext, Task> OnApplyChallenge { get; set; } = context => public Func<JwtBearerChallengeContext, Task> OnChallenge { get; set; } = context =>
{ {
context.HttpContext.Response.Headers.Append("WWW-Authenticate", context.Options.Challenge); context.HttpContext.Response.Headers.Append("WWW-Authenticate", context.Options.Challenge);
return Task.FromResult(0); return Task.FromResult(0);
@ -46,12 +46,12 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context); 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);
} }
} }

View File

@ -5,13 +5,13 @@ using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.JwtBearer namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
public class SecurityTokenReceivedContext : BaseControlContext<JwtBearerOptions> public class ReceivedTokenContext : BaseControlContext<JwtBearerOptions>
{ {
public SecurityTokenReceivedContext(HttpContext context, JwtBearerOptions options) public ReceivedTokenContext(HttpContext context, JwtBearerOptions options)
: base(context, options) : base(context, options)
{ {
} }
public string SecurityToken { get; set; } public string Token { get; set; }
} }
} }

View File

@ -5,9 +5,9 @@ using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.JwtBearer namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
public class MessageReceivedContext : BaseControlContext<JwtBearerOptions> public class ReceivingTokenContext : BaseControlContext<JwtBearerOptions>
{ {
public MessageReceivedContext(HttpContext context, JwtBearerOptions options) public ReceivingTokenContext(HttpContext context, JwtBearerOptions options)
: base(context, options) : base(context, options)
{ {
} }

View File

@ -5,9 +5,9 @@ using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Authentication.JwtBearer namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
public class AuthenticationChallengeContext : BaseControlContext<JwtBearerOptions> public class ValidatedTokenContext : BaseControlContext<JwtBearerOptions>
{ {
public AuthenticationChallengeContext(HttpContext context, JwtBearerOptions options) public ValidatedTokenContext(HttpContext context, JwtBearerOptions options)
: base(context, options) : base(context, options)
{ {
} }

View File

@ -26,22 +26,22 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
try try
{ {
// Give application opportunity to find from a different location, adjust, or reject token // 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 // event can set the token
await Options.Events.MessageReceived(messageReceivedContext); await Options.Events.ReceivingToken(receivingTokenContext);
if (messageReceivedContext.HandledResponse) if (receivingTokenContext.HandledResponse)
{ {
return messageReceivedContext.AuthenticationTicket; return receivingTokenContext.AuthenticationTicket;
} }
if (messageReceivedContext.Skipped) if (receivingTokenContext.Skipped)
{ {
return null; return null;
} }
// If application retrieved token from somewhere else, use that. // If application retrieved token from somewhere else, use that.
token = messageReceivedContext.Token; token = receivingTokenContext.Token;
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
@ -66,18 +66,18 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
} }
// notify user token was received // 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); await Options.Events.ReceivedToken(receivedTokenContext);
if (securityTokenReceivedContext.HandledResponse) if (receivedTokenContext.HandledResponse)
{ {
return securityTokenReceivedContext.AuthenticationTicket; return receivedTokenContext.AuthenticationTicket;
} }
if (securityTokenReceivedContext.Skipped) if (receivedTokenContext.Skipped)
{ {
return null; return null;
} }
@ -110,18 +110,18 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
var principal = validator.ValidateToken(token, validationParameters, out validatedToken); var principal = validator.ValidateToken(token, validationParameters, out validatedToken);
var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme); var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), Options.AuthenticationScheme);
var securityTokenValidatedContext = new SecurityTokenValidatedContext(Context, Options) var validatedTokenContext = new ValidatedTokenContext(Context, Options)
{ {
AuthenticationTicket = ticket AuthenticationTicket = ticket
}; };
await Options.Events.SecurityTokenValidated(securityTokenValidatedContext); await Options.Events.ValidatedToken(validatedTokenContext);
if (securityTokenValidatedContext.HandledResponse) if (validatedTokenContext.HandledResponse)
{ {
return securityTokenValidatedContext.AuthenticationTicket; return validatedTokenContext.AuthenticationTicket;
} }
if (securityTokenValidatedContext.Skipped) if (validatedTokenContext.Skipped)
{ {
return null; return null;
} }
@ -165,7 +165,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
protected override async Task<bool> HandleUnauthorizedAsync(ChallengeContext context) protected override async Task<bool> HandleUnauthorizedAsync(ChallengeContext context)
{ {
Response.StatusCode = 401; Response.StatusCode = 401;
await Options.Events.ApplyChallenge(new AuthenticationChallengeContext(Context, Options)); await Options.Events.Challenge(new JwtBearerChallengeContext(Context, Options));
return false; return false;
} }

View File

@ -68,7 +68,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnMessageReceived = context => OnReceivingToken = context =>
{ {
var claims = new[] var claims = new[]
{ {
@ -118,7 +118,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnSecurityTokenReceived = context => OnReceivedToken = context =>
{ {
var claims = new[] var claims = new[]
{ {
@ -152,7 +152,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnSecurityTokenValidated = context => OnValidatedToken = context =>
{ {
// Retrieve the NameIdentifier claim from the identity // Retrieve the NameIdentifier claim from the identity
// returned by the custom security token validator. // returned by the custom security token validator.
@ -189,12 +189,12 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnMessageReceived = context => OnReceivingToken = context =>
{ {
context.Token = "CustomToken"; context.Token = "CustomToken";
return Task.FromResult<object>(null); return Task.FromResult<object>(null);
}, },
OnSecurityTokenReceived = context => OnReceivedToken = context =>
{ {
var claims = new[] var claims = new[]
{ {
@ -226,7 +226,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnSecurityTokenReceived = context => OnReceivedToken = context =>
{ {
var claims = new[] var claims = new[]
{ {
@ -257,7 +257,7 @@ namespace Microsoft.AspNet.Authentication.JwtBearer
{ {
options.Events = new JwtBearerEvents() options.Events = new JwtBearerEvents()
{ {
OnSecurityTokenReceived = context => OnReceivedToken = context =>
{ {
var claims = new[] var claims = new[]
{ {