Restructure solution layout by module
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace StellaOps.Policy.Gateway.Infrastructure;
|
||||
|
||||
internal sealed record GatewayForwardingContext(string Authorization, string? Dpop, string? Tenant)
|
||||
{
|
||||
private static readonly string[] ForwardedHeaders =
|
||||
{
|
||||
"Authorization",
|
||||
"DPoP",
|
||||
"X-Stella-Tenant"
|
||||
};
|
||||
|
||||
public void Apply(HttpRequestMessage request)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
|
||||
request.Headers.TryAddWithoutValidation(ForwardedHeaders[0], Authorization);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Dpop))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation(ForwardedHeaders[1], Dpop);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Tenant))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation(ForwardedHeaders[2], Tenant);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryCreate(HttpContext context, out GatewayForwardingContext forwardingContext)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var authorization = context.Request.Headers.Authorization.ToString();
|
||||
if (string.IsNullOrWhiteSpace(authorization))
|
||||
{
|
||||
forwardingContext = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
var dpop = context.Request.Headers["DPoP"].ToString();
|
||||
if (string.IsNullOrWhiteSpace(dpop))
|
||||
{
|
||||
dpop = null;
|
||||
}
|
||||
|
||||
var tenant = context.Request.Headers["X-Stella-Tenant"].ToString();
|
||||
if (string.IsNullOrWhiteSpace(tenant))
|
||||
{
|
||||
tenant = null;
|
||||
}
|
||||
|
||||
forwardingContext = new GatewayForwardingContext(authorization.Trim(), dpop, tenant);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user