1
0
mirror of https://github.com/bitwarden/server synced 2025-12-22 03:03:33 +00:00

[BEEEP] [PM-28808] Fix invalid identity URL in Swagger (#6653)

- in generated JSON (used in help center), only show cloud options
  (with corrected identity URL)
- in self-host and dev, only show local option
This commit is contained in:
Thomas Rittson
2025-12-03 09:20:56 +10:00
committed by GitHub
parent 89a2eab32a
commit ee26a701e9
4 changed files with 126 additions and 36 deletions

View File

@@ -85,7 +85,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using StackExchange.Redis;
using Swashbuckle.AspNetCore.SwaggerGen;
using ZiggyCreatures.Caching.Fusion;
using NoopRepos = Bit.Core.Repositories.Noop;
using Role = Bit.Core.Entities.Role;
@@ -1067,4 +1069,61 @@ public static class ServiceCollectionExtensions
CoreHelpers.SettingHasValue(settings.EventLogging.RabbitMq.Password) &&
CoreHelpers.SettingHasValue(settings.EventLogging.RabbitMq.EventExchangeName);
}
/// <summary>
/// Adds a server with its corresponding OAuth2 client credentials security definition and requirement.
/// </summary>
/// <param name="config">The SwaggerGen configuration</param>
/// <param name="serverId">Unique identifier for this server (e.g., "us-server", "eu-server")</param>
/// <param name="serverUrl">The API server URL</param>
/// <param name="identityTokenUrl">The identity server token URL</param>
/// <param name="serverDescription">Human-readable description for the server</param>
public static void AddSwaggerServerWithSecurity(
this SwaggerGenOptions config,
string serverId,
string serverUrl,
string identityTokenUrl,
string serverDescription)
{
// Add server
config.AddServer(new OpenApiServer
{
Url = serverUrl,
Description = serverDescription
});
// Add security definition
config.AddSecurityDefinition(serverId, new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Description = $"**Use this option if you've selected the {serverDescription}**",
Flows = new OpenApiOAuthFlows
{
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri(identityTokenUrl),
Scopes = new Dictionary<string, string>
{
{ ApiScopes.ApiOrganization, $"Organization APIs ({serverDescription})" },
},
}
},
});
// Add security requirement
config.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = serverId
},
},
[ApiScopes.ApiOrganization]
}
});
}
}