Merge pull request #1384 from Kukks/api/authorize-ui

Make api docs only available after login
This commit is contained in:
Nicolas Dorier
2020-03-12 12:00:00 +09:00
committed by GitHub
3 changed files with 23 additions and 3 deletions

View File

@@ -292,7 +292,6 @@ namespace BTCPayServer.Hosting
public static IApplicationBuilder UsePayServer(this IApplicationBuilder app)
{
app.UseMiddleware<BTCPayMiddleware>();
app.UseBTCPayOpenApi();
return app;
}
public static IApplicationBuilder UseHeadersOverride(this IApplicationBuilder app)

View File

@@ -1,15 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Configuration;
using BTCPayServer.Data;
using BTCPayServer.Payments;
using BTCPayServer.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using NJsonSchema;
using NJsonSchema.Generation.TypeMappers;
using NSwag;
using NSwag.Generation.Processors.Security;
using Org.BouncyCastle.Asn1.Ocsp;
namespace BTCPayServer.Hosting.OpenApi
{
@@ -17,7 +22,6 @@ namespace BTCPayServer.Hosting.OpenApi
{
public static IServiceCollection AddBTCPayOpenApi(this IServiceCollection serviceCollection)
{
return serviceCollection.AddOpenApiDocument(config =>
{
config.PostProcess = document =>
@@ -67,8 +71,23 @@ namespace BTCPayServer.Hosting.OpenApi
public static IApplicationBuilder UseBTCPayOpenApi(this IApplicationBuilder builder)
{
var roothPath = builder.ApplicationServices.GetService<BTCPayServerOptions>().RootPath;
var matched = new PathString($"{roothPath}docs");
return builder.UseOpenApi()
.UseReDoc(settings => settings.Path = "/docs");
.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments(matched, StringComparison.InvariantCultureIgnoreCase) && !context.User.Claims.Any())
{
context.Response.Redirect( $"{context.Request.GetRelativePath(roothPath)}account/login?returnUrl={context.Request.Path}");
return;
}
await next.Invoke();
})
.UseReDoc(settings =>
{
settings.Path = "/docs";
});
}

View File

@@ -19,6 +19,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using BTCPayServer.Security;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Net;
using BTCPayServer.Hosting.OpenApi;
using BTCPayServer.PaymentRequest;
using BTCPayServer.Services.Apps;
using BTCPayServer.Storage;
@@ -183,6 +184,7 @@ namespace BTCPayServer.Hosting
app.UseProviderStorage(options);
app.UseAuthentication();
app.UseAuthorization();
app.UseBTCPayOpenApi();
app.UseSession();
app.UseWebSockets();