-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched to events model to DI of event type (#495)
* Switch to Events Model to Allow IoC of Event Type * Added, updated tests * Updated changelog * Bumped JWT.Extensions.AspNetCore version to beta3 --------- Co-authored-by: Alexander Batishchev <[email protected]> Co-authored-by: Alex Batishchev <[email protected]>
- Loading branch information
1 parent
31a9366
commit c2434a1
Showing
9 changed files
with
365 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JWT.Extensions.AspNetCore | ||
{ | ||
public class FailedTicketContext | ||
{ | ||
public FailedTicketContext(ILogger logger, Exception exception, HttpContext context, JwtAuthenticationOptions options) | ||
{ | ||
this.Logger = logger; | ||
this.Exception = exception; | ||
this.Context = context; | ||
this.Options = options; | ||
} | ||
|
||
public ILogger Logger { get; } | ||
|
||
public Exception Exception { get; } | ||
|
||
public HttpContext Context { get; } | ||
|
||
public JwtAuthenticationOptions Options { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace JWT.Extensions.AspNetCore | ||
{ | ||
public class JwtAuthenticationEvents | ||
{ | ||
private static readonly Action<ILogger, Exception> _logMissingHeader; | ||
private static readonly Action<ILogger, string, string, Exception> _logIncorrectScheme; | ||
private static readonly Action<ILogger, Exception> _logEmptyHeader; | ||
private static readonly Action<ILogger, Exception> _logSuccessfulTicket; | ||
private static readonly Action<ILogger, string, Exception> _logFailedTicket; | ||
|
||
static JwtAuthenticationEvents() | ||
{ | ||
_logMissingHeader = LoggerMessage.Define( | ||
LogLevel.Information, | ||
10, | ||
$"Header {nameof(HeaderNames.Authorization)} is empty, returning none"); | ||
|
||
_logIncorrectScheme = LoggerMessage.Define<string, string>( | ||
LogLevel.Information, | ||
11, | ||
$"Header {nameof(HeaderNames.Authorization)} scheme is {{ActualScheme}}, expected {{ExpectedScheme}}, returning none"); | ||
|
||
_logEmptyHeader = LoggerMessage.Define( | ||
LogLevel.Information, | ||
12, | ||
$"Token in header {nameof(HeaderNames.Authorization)} is empty, returning none"); | ||
|
||
_logSuccessfulTicket = LoggerMessage.Define( | ||
LogLevel.Information, | ||
1, | ||
"Successfully decoded JWT, returning success"); | ||
|
||
_logFailedTicket = LoggerMessage.Define<string>( | ||
LogLevel.Information, | ||
2, | ||
"Error decoding JWT: {0}, returning failure"); | ||
} | ||
|
||
public Func<ILogger, AuthenticateResult> OnMissingHeader { get; set; } = | ||
logger => | ||
{ | ||
_logMissingHeader(logger, null); | ||
return AuthenticateResult.NoResult(); | ||
}; | ||
|
||
public Func<ILogger, string, string, AuthenticateResult> OnIncorrectScheme { get; set; } = | ||
(logger, actualScheme, expectedScheme) => | ||
{ | ||
_logIncorrectScheme(logger, actualScheme, expectedScheme, null); | ||
return AuthenticateResult.NoResult(); | ||
}; | ||
|
||
public Func<ILogger, string, AuthenticateResult> OnEmptyHeader { get; set; } = (logger, header) => | ||
{ | ||
_logEmptyHeader(logger, null); | ||
return AuthenticateResult.NoResult(); | ||
}; | ||
|
||
public Func<SuccessfulTicketContext, AuthenticateResult> OnSuccessfulTicket { get; set; } = context => | ||
{ | ||
_logSuccessfulTicket(context.Logger, null); | ||
return AuthenticateResult.Success(context.Ticket); | ||
}; | ||
|
||
public Func<FailedTicketContext, AuthenticateResult> OnFailedTicket { get; set; } = context => | ||
{ | ||
_logFailedTicket(context.Logger, context.Exception.Message, context.Exception); | ||
return AuthenticateResult.Fail(context.Exception); | ||
}; | ||
|
||
public virtual AuthenticateResult SuccessfulTicket(SuccessfulTicketContext context) => OnSuccessfulTicket(context); | ||
|
||
public virtual AuthenticateResult FailedTicket(FailedTicketContext context) => OnFailedTicket(context); | ||
|
||
public virtual AuthenticateResult EmptyHeader(ILogger logger, string header) => OnEmptyHeader(logger, header); | ||
|
||
public virtual AuthenticateResult IncorrectScheme(ILogger logger, string actualScheme, string expectedScheme) => OnIncorrectScheme(logger, actualScheme, expectedScheme); | ||
|
||
public virtual AuthenticateResult MissingHeader(ILogger logger) => OnMissingHeader(logger); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JWT.Extensions.AspNetCore | ||
{ | ||
public class SuccessfulTicketContext | ||
{ | ||
public SuccessfulTicketContext(ILogger logger, AuthenticationTicket ticket, HttpContext context, JwtAuthenticationOptions options) | ||
{ | ||
this.Logger = logger; | ||
this.Ticket = ticket; | ||
this.Context = context; | ||
this.Options = options; | ||
} | ||
|
||
public ILogger Logger { get; } | ||
|
||
public AuthenticationTicket Ticket { get; } | ||
|
||
public HttpContext Context { get; } | ||
|
||
public JwtAuthenticationOptions Options { get; } | ||
} | ||
} |
Oops, something went wrong.