Closed
Description
DESCRIPTION
I'm implementing a custom AuthenticationHandler to support API Key based authentication. However, I cannot find how to stay compliant with the JSON:API specification since I don't have access to the services from JADNC at the authentication stage, within the middleware pipeline.
My question is how I should handle the authentication (error) responses through JADNC in order to stay compliant with the specification.
STEPS TO REPRODUCE
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Can be any implementation which inherits from AuthenticationHandler:
services.AddAuthentication("ApiKey").AddApiKey();
...
services.AddJsonApi<DbContext>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseAuthentication();
...
app.UseJsonApi();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
I had a look at the source code and found two "problems" related to my issue:
- The serializer/deserializer is not separated and cannot be added to the pipeline before authentication (Separation of JsonApi serialization/deserialization and asp.net core #661). Meaning that I cannot return a compliant response in my custom AuthenticationHandler.
- The ExceptionHandler of JADNC is not added to the pipeline before authentication. Meaning, obviously, that exceptions in my custom AuthenticationHandler won't be handled by JADNC. Thus, the response won't be in proper JSON:API format.
EXPECTED BEHAVIOR
Being able to handle responses, outside of regular resource controllers, through JADNC.
ACTUAL BEHAVIOR
Responses (outside of regular resource controllers) being unhandled resulting in responses which aren't compliant with the JSON:API specification.
VERSIONS USED
- JsonApiDotNetCore version: 4.0.3
- ASP.NET Core version: 3.1
- Entity Framework Core version: 3.1.12
- Database provider: PostgreSQL