Code improvements of JwtBearer module (#1742)

* Use pattern matching to check exception type in JwtBearerHandler

* Use expression body to configure events in JwtBearerHandler
This commit is contained in:
Alexey Malinin 2018-05-01 23:22:27 +07:00 committed by Chris Ross
parent 7921fa4c49
commit a2cb92b358
1 changed files with 28 additions and 34 deletions

View File

@ -32,8 +32,8 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
/// </summary> /// </summary>
protected new JwtBearerEvents Events protected new JwtBearerEvents Events
{ {
get { return (JwtBearerEvents)base.Events; } get => (JwtBearerEvents)base.Events;
set { base.Events = value; } set => base.Events = value;
} }
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new JwtBearerEvents()); protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new JwtBearerEvents());
@ -267,9 +267,8 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
private static string CreateErrorDescription(Exception authFailure) private static string CreateErrorDescription(Exception authFailure)
{ {
IEnumerable<Exception> exceptions; IEnumerable<Exception> exceptions;
if (authFailure is AggregateException) if (authFailure is AggregateException agEx)
{ {
var agEx = authFailure as AggregateException;
exceptions = agEx.InnerExceptions; exceptions = agEx.InnerExceptions;
} }
else else
@ -283,37 +282,32 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
{ {
// Order sensitive, some of these exceptions derive from others // Order sensitive, some of these exceptions derive from others
// and we want to display the most specific message possible. // and we want to display the most specific message possible.
if (ex is SecurityTokenInvalidAudienceException) switch (ex)
{ {
messages.Add("The audience is invalid"); case SecurityTokenInvalidAudienceException _:
} messages.Add("The audience is invalid");
else if (ex is SecurityTokenInvalidIssuerException) break;
{ case SecurityTokenInvalidIssuerException _:
messages.Add("The issuer is invalid"); messages.Add("The issuer is invalid");
} break;
else if (ex is SecurityTokenNoExpirationException) case SecurityTokenNoExpirationException _:
{ messages.Add("The token has no expiration");
messages.Add("The token has no expiration"); break;
} case SecurityTokenInvalidLifetimeException _:
else if (ex is SecurityTokenInvalidLifetimeException) messages.Add("The token lifetime is invalid");
{ break;
messages.Add("The token lifetime is invalid"); case SecurityTokenNotYetValidException _:
} messages.Add("The token is not valid yet");
else if (ex is SecurityTokenNotYetValidException) break;
{ case SecurityTokenExpiredException _:
messages.Add("The token is not valid yet"); messages.Add("The token is expired");
} break;
else if (ex is SecurityTokenExpiredException) case SecurityTokenSignatureKeyNotFoundException _:
{ messages.Add("The signature key was not found");
messages.Add("The token is expired"); break;
} case SecurityTokenInvalidSignatureException _:
else if (ex is SecurityTokenSignatureKeyNotFoundException) messages.Add("The signature is invalid");
{ break;
messages.Add("The signature key was not found");
}
else if (ex is SecurityTokenInvalidSignatureException)
{
messages.Add("The signature is invalid");
} }
} }