New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug Fix: CreatingTicket not using properly initialized event #600
Conversation
Thanks for raising this - could you elaborate more on the use case which lead you to find this (preferably with a code snippet)? We have an existing test case that explicitly checks that the AspNet.Security.OAuth.Providers/test/AspNet.Security.OAuth.Providers.Tests/OAuthTests`1.cs Line 138 in 0d9f3f2
|
Sure - by calling Whereas the By calling This can be replicated with following example (not exact code I used, heavily stripped off stuff just to show the idea): DiscordOAuthEvents.cs: public class DiscordOAuthEvents : OAuthEvents
{
public DiscordOAuthEvents()
{
// usual constructor DI goes here
}
public override async Task CreatingTicket(OAuthCreatingTicketContext context)
{
// set breakpoint below to see if it's getting hit
;
}
} DiscordDependencyInjectionExtensions.cs: public static class DiscordDependencyInjectionExtensions
{
public static IServiceCollection AddDiscord(this IServiceCollection services)
{
services.TryAddScoped<DiscordOAuthEvents>();
services.AddAuthentication()
.AddDiscord(options =>
{
options.ClientSecret = "client-secret";
options.ClientId = "client-id";
options.EventsType = typeof(DiscordOAuthEvents);
});
return services;
}
} Using the code above, the breakpoint in This class-based approach can be useful in multiple cases - it makes DI easier to use, and also decouples events code from service registration code. |
Great, thanks @TehGM - I understand the bug and the use case now. I've replicated this with a test locally and verified it passes with this change. I'll merge this and then add the test, and then push the fix to NuGet.org. |
Add a unit test to validate aspnet-contrib#600 for all providers.
Add a unit test to validate #600 for all providers.
Thanks again @TehGM - this fix has now been pushed to NuGet.org: https://www.nuget.org/packages/AspNet.Security.OAuth.Discord/5.0.13 |
The bug
Today as I was working on my own project, I noticed that after assigning
DiscordAuthenticationOptions.EventsType
, the custom events class gets properly initialized with a scope, butCreatingTicket
method is not triggered - whileDiscordAuthenticationOptions.Events.OnCreatingTicket
delegate is.Cause
After some investigation I noticed that the cause for this is the fact that each of the providers in the library calls
Options.Events.CreatingTicket
directly. Doing it that way completely ignores the instance generated fromEventsType
property.Solution
Looking into source code of base class AuhenticationHandler, I noticed that
InitializeEventsAsync
async method properly creates an instance and assigns it toEvents
property. Therefore, usingEvents.OnCreatingTicket
instead ofOptions.Events.OnCreatingTicket
in the Provider solves the issue.Unit Tests
Unit tests all ran successfully.