Added authentication
This commit is contained in:
+63
-1
@@ -4,6 +4,7 @@ using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using EonaCat.DoxaApi.Generation;
|
||||
using EonaCat.DoxaApi.Interop;
|
||||
using EonaCat.DoxaApi.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
@@ -12,6 +13,9 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace EonaCat.DoxaApi.Middleware
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public static class DoxaApiMiddlewareExtensions
|
||||
{
|
||||
private static readonly JsonSerializerOptions _writeOptions = new()
|
||||
@@ -100,6 +104,64 @@ namespace EonaCat.DoxaApi.Middleware
|
||||
});
|
||||
});
|
||||
|
||||
if (options.EnableMockServer)
|
||||
{
|
||||
app.Map(prefix + "/mock", mockApp =>
|
||||
{
|
||||
mockApp.Run(async context =>
|
||||
{
|
||||
var document = GenerateDocument(context, options);
|
||||
var operationId = context.Request.Query["operationId"].FirstOrDefault();
|
||||
|
||||
ApiEndpoint? endpoint = null;
|
||||
foreach (var group in document.Groups)
|
||||
{
|
||||
endpoint = group.Endpoints.FirstOrDefault(e => e.OperationId == operationId);
|
||||
if (endpoint is not null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (endpoint is null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
await context.Response.WriteAsync("Unknown operationId. Pass ?operationId=<id> matching an entry in the spec.");
|
||||
return;
|
||||
}
|
||||
|
||||
var successResponse = endpoint.Responses.FirstOrDefault(r => r.StatusCode.StartsWith('2'))
|
||||
?? endpoint.Responses.FirstOrDefault();
|
||||
|
||||
var mockValue = MockResponseGenerator.Generate(successResponse?.Schema, document);
|
||||
|
||||
context.Response.StatusCode = int.TryParse(successResponse?.StatusCode, out var code) ? code : 200;
|
||||
context.Response.ContentType = "application/json; charset=utf-8";
|
||||
context.Response.Headers["X-DoxaApi-Mock"] = "true";
|
||||
await JsonSerializer.SerializeAsync(context.Response.Body, mockValue, _writeOptions);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (options.EnableConsole)
|
||||
{
|
||||
app.Map(prefix + "/console", consoleApp =>
|
||||
{
|
||||
consoleApp.Run(async context =>
|
||||
{
|
||||
if (!HttpMethods.IsPost(context.Request.Method))
|
||||
{
|
||||
context.Response.StatusCode = 405;
|
||||
context.Response.Headers["Allow"] = "POST";
|
||||
await context.Response.WriteAsync("Method Not Allowed - use POST with a JSON ConsoleRequest body.");
|
||||
return;
|
||||
}
|
||||
|
||||
await ConsoleProxy.HandleAsync(context, _writeOptions);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
app.Map(prefix, uiApp =>
|
||||
{
|
||||
uiApp.Run(async context =>
|
||||
@@ -179,4 +241,4 @@ namespace EonaCat.DoxaApi.Middleware
|
||||
return (ms.ToArray(), contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user