backend + web: Protect Swagger UI behind admin login
This commit is contained in:
1 parent
d8de714481
commit
eddf5837eb
16 files changed
+158
-28
No files matched your search
@@ -15,7 +15,7 @@
|
||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.14" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.14" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.14" />
|
||||
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.3.1" />
|
||||
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.3.1" />
|
||||
<PackageVersion Include="BCrypt.Net-Next" Version="4.2.0" />
|
||||
<PackageVersion Include="MailKit" Version="5.0.0" />
|
||||
<PackageVersion Include="FirebaseAdmin" Version="3.7.1" />
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -9,7 +9,7 @@ public static class SwaggerSetup
|
||||
{
|
||||
private const string SchemeId = "Bearer";
|
||||
|
||||
/// <summary>Swagger UI na <c>/swagger</c> včetně přihlášení access tokenem (NFR-04).</summary>
|
||||
/// <summary>Generování OpenAPI dokumentu pro administrátorské Swagger UI (NFR-04).</summary>
|
||||
public static IServiceCollection AddHelpdeskSwagger(this IServiceCollection services)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
|
||||
@@ -78,17 +78,12 @@ if (trustedProxies.Length > 0)
|
||||
app.UseExceptionHandler();
|
||||
app.UseStatusCodePages();
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Helpdesk API v1");
|
||||
options.DocumentTitle = "Helpdesk API";
|
||||
});
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseRateLimiter();
|
||||
|
||||
app.MapSwagger(ApiRoutes.V1 + "/admin/openapi/{documentName}.json")
|
||||
.RequireAuthorization(policy => policy.RequireRole(Roles.Admin));
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Helpdesk.Api.Controllers;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Helpdesk.Api.Controllers;
|
||||
using Helpdesk.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -44,10 +45,18 @@ public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<Backen
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OpenApiDescribesMultipartIssueRequests()
|
||||
public async Task OnlyAdministratorsCanReadTheOpenApiDocument()
|
||||
{
|
||||
using var client = fixture.CreateClient();
|
||||
using var document = await ReadAsync<JsonDocument>(client.GetAsync("/swagger/v1/swagger.json", Cancellation));
|
||||
const string path = "/api/v1/admin/openapi/v1.json";
|
||||
await ExpectAsync(client.GetAsync(path, Cancellation), HttpStatusCode.Unauthorized);
|
||||
var student = await fixture.RegisterAsync(client);
|
||||
using var studentClient = fixture.CreateClient((await LoginAsync(client, student.Email)).AccessToken);
|
||||
await ExpectAsync(studentClient.GetAsync(path, Cancellation), HttpStatusCode.Forbidden);
|
||||
using var admin = fixture.CreateClient((await LoginAsync(client, AdminEmail)).AccessToken);
|
||||
await ExpectAsync(admin.GetAsync("/swagger/index.html", Cancellation), HttpStatusCode.NotFound);
|
||||
await ExpectAsync(admin.GetAsync("/swagger/v1/swagger.json", Cancellation), HttpStatusCode.NotFound);
|
||||
using var document = await ReadAsync<JsonDocument>(admin.GetAsync(path, Cancellation));
|
||||
var paths = document.RootElement.GetProperty("paths");
|
||||
var create = paths.GetProperty("/api/v1/issues").GetProperty("post").GetProperty("requestBody").GetProperty("content");
|
||||
Assert.True(create.TryGetProperty("application/json", out _));
|
||||
@@ -66,7 +75,7 @@ public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<Backen
|
||||
{
|
||||
await using var scope = fixture.Services.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
||||
var reporterId = await db.Users.Select(user => user.Id).SingleAsync(Cancellation);
|
||||
var reporterId = await db.Users.Where(user => user.Email == AdminEmail).Select(user => user.Id).SingleAsync(Cancellation);
|
||||
var categoryId = await db.IssueCategories.MinAsync(category => category.Id, Cancellation);
|
||||
|
||||
var error = await Assert.ThrowsAsync<MySqlException>(() => db.Database.ExecuteSqlInterpolatedAsync($"""
|
||||
|
||||
Reference in new issue
Block a user