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
@@ -5,6 +5,13 @@ namespace Helpdesk.Web.Api;
|
||||
/// <summary>Endpointy API pro přihlášeného uživatele; access token přidává <see cref="BearerTokenHandler"/>.</summary>
|
||||
public sealed class HelpdeskApiClient(HttpClient http)
|
||||
{
|
||||
public async Task<string> GetOpenApiAsync(CancellationToken ct)
|
||||
{
|
||||
using var response = await http.GetAsync("api/v1/admin/openapi/v1.json", ct);
|
||||
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||||
return await response.Content.ReadAsStringAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<UserDto> GetMeAsync(CancellationToken ct)
|
||||
{
|
||||
using var response = await http.GetAsync("api/v1/users/me", ct);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using Helpdesk.Web.Api;
|
||||
using Helpdesk.Web.Auth;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Helpdesk.Web.Controllers;
|
||||
|
||||
[Authorize(Roles = UserRoles.Admin)]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
public sealed class SwaggerController(HelpdeskApiClient api) : Controller
|
||||
{
|
||||
[HttpGet("/swagger/v1/swagger.json")]
|
||||
public async Task<IActionResult> Document(CancellationToken ct) =>
|
||||
Content(await api.GetOpenApiAsync(ct), "application/json");
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Helpdesk.Web.Tests" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -115,6 +115,13 @@ app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapSwaggerUI("swagger", options =>
|
||||
{
|
||||
options.SwaggerEndpoint("v1/swagger.json", "Helpdesk API v1");
|
||||
options.DocumentTitle = "Helpdesk API";
|
||||
options.CacheLifetime = null;
|
||||
}).RequireAuthorization(policy => policy.RequireRole(UserRoles.Admin));
|
||||
|
||||
app.MapStaticAssets();
|
||||
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}").WithStaticAssets();
|
||||
|
||||
|
||||
@@ -9,6 +9,10 @@
|
||||
<ul class="dropdown-menu dropdown-menu-end">
|
||||
<li><span class="dropdown-item-text small text-body-secondary">@User.FindFirst(ClaimTypes.Email)?.Value</span></li>
|
||||
<li><a class="dropdown-item" asp-controller="Profile" asp-action="Index">Můj profil</a></li>
|
||||
@if (User.IsInRole(UserRoles.Admin))
|
||||
{
|
||||
<li><a class="dropdown-item" href="~/swagger">Swagger API</a></li>
|
||||
}
|
||||
<li><hr class="dropdown-divider" /></li>
|
||||
<li>
|
||||
<form method="post" asp-controller="Account" asp-action="Logout">
|
||||
|
||||
Reference in new issue
Block a user