Allow overriding forbid handler

This commit is contained in:
Shkar T. Noori 2023-04-11 23:42:08 +03:00
parent 7ad90411ff
commit b5aa1a6bc0
No known key found for this signature in database
GPG Key ID: E7AD76088FB6FE02
2 changed files with 20 additions and 0 deletions

View File

@ -1,7 +1,14 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
namespace DIT.Authentication.GatewayAuth.Abstractions;
public interface IForbidResponseHandler
{
Task HandleForbiddenAsync(HttpContext context, AuthenticationProperties properties);
}
public interface ISignatureValidator
{

View File

@ -4,6 +4,7 @@ using System.Security.Claims;
using System.Text.Encodings.Web;
using DIT.Authentication.GatewayAuth.Abstractions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -14,9 +15,11 @@ public class GatewayAuthHandler : AuthenticationHandler<GatewayAuthOptions>
private readonly IClaimsProvider _claimsProvider;
private readonly ISignatureValidator _signatureValidator;
private readonly IForbidResponseHandler? _forbidResponseHandler;
public GatewayAuthHandler(
IClaimsProvider claimsProvider,
IServiceProvider serviceProvider,
UrlEncoder encoder,
IOptionsMonitor<GatewayAuthOptions> options,
ILoggerFactory logger,
@ -25,6 +28,8 @@ public class GatewayAuthHandler : AuthenticationHandler<GatewayAuthOptions>
{
_claimsProvider = claimsProvider;
_signatureValidator = signatureValidator;
_forbidResponseHandler = serviceProvider.GetService<IForbidResponseHandler>();
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
@ -59,6 +64,14 @@ public class GatewayAuthHandler : AuthenticationHandler<GatewayAuthOptions>
}
}
protected override Task HandleForbiddenAsync(AuthenticationProperties properties)
{
if (_forbidResponseHandler != null)
return _forbidResponseHandler.HandleForbiddenAsync(Context, properties);
return base.HandleForbiddenAsync(properties);
}
private static bool ExtractSignatureValue(string signatureHeader, [NotNullWhen(true)] out string? signature)
{
const string signaturePrefix = "signature=";