mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
App domain redirect (#4391)
* Fix duplicates in GetAllApps with allowNoUser * Use domain mapping as canonical reference and redirect to it * Revert domain mapping to hostname instead of URL
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Abstractions.Contracts;
|
||||
using BTCPayServer.HostedServices;
|
||||
using BTCPayServer.Services;
|
||||
using BTCPayServer.Services.Apps;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -15,45 +12,62 @@ namespace BTCPayServer.Filters
|
||||
{
|
||||
public DomainMappingConstraintAttribute()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DomainMappingConstraintAttribute(AppType appType)
|
||||
{
|
||||
AppType = appType;
|
||||
}
|
||||
|
||||
public int Order => 100;
|
||||
public AppType? AppType { get; set; }
|
||||
private AppType? AppType { get; }
|
||||
|
||||
public bool Accept(ActionConstraintContext context)
|
||||
{
|
||||
if (context.RouteContext.RouteData.Values.ContainsKey("appId"))
|
||||
return true;
|
||||
var hasAppId = context.RouteContext.RouteData.Values.ContainsKey("appId");
|
||||
var policies = context.RouteContext.HttpContext.RequestServices.GetService<PoliciesSettings>();
|
||||
if (policies?.DomainToAppMapping is { } mapping)
|
||||
var mapping = policies?.DomainToAppMapping;
|
||||
var hasDomainMapping = mapping is { Count: > 0 };
|
||||
|
||||
if (hasAppId && hasDomainMapping)
|
||||
{
|
||||
var matchedDomainMapping = mapping.FirstOrDefault(item =>
|
||||
item.Domain.Equals(context.RouteContext.HttpContext.Request.Host.Host, StringComparison.InvariantCultureIgnoreCase));
|
||||
var appId = (string)context.RouteContext.RouteData.Values["appId"];
|
||||
var matchedDomainMapping = mapping.FirstOrDefault(item => item.AppId == appId);
|
||||
|
||||
// App is accessed via path, redirect to canonical domain
|
||||
if (matchedDomainMapping != null)
|
||||
{
|
||||
if (!(AppType is { } appType))
|
||||
var req = context.RouteContext.HttpContext.Request;
|
||||
var url = new UriBuilder(req.Scheme, matchedDomainMapping.Domain).ToString();
|
||||
context.RouteContext.HttpContext.Response.Redirect(url);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDomainMapping)
|
||||
{
|
||||
var matchedDomainMapping = mapping.FirstOrDefault(item =>
|
||||
item.Domain.Equals(context.RouteContext.HttpContext.Request.Host.Host,
|
||||
StringComparison.InvariantCultureIgnoreCase));
|
||||
if (matchedDomainMapping != null)
|
||||
{
|
||||
if (AppType is not { } appType)
|
||||
return false;
|
||||
if (appType != matchedDomainMapping.AppType)
|
||||
return false;
|
||||
context.RouteContext.RouteData.Values.Add("appId", matchedDomainMapping.AppId);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (AppType == policies.RootAppType)
|
||||
{
|
||||
context.RouteContext.RouteData.Values.Add("appId", policies.RootAppId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return AppType is null;
|
||||
}
|
||||
|
||||
return AppType is null;
|
||||
if (AppType == policies.RootAppType)
|
||||
{
|
||||
context.RouteContext.RouteData.Values.Add("appId", policies.RootAppId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return hasAppId || AppType is null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user