1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 21:48:12 -05:00

upgrade swagger

This commit is contained in:
Kyle Spearrin 2020-01-10 09:36:12 -05:00
parent f71433d09a
commit e13f022c90
3 changed files with 61 additions and 39 deletions

View File

@ -24,7 +24,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -14,6 +14,8 @@ using IdentityModel;
using System.Globalization; using System.Globalization;
using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Logging;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;
namespace Bit.Api namespace Bit.Api
{ {
@ -122,7 +124,7 @@ namespace Bit.Api
} }
}); });
//services.AddSwagger(globalSettings); services.AddSwagger(globalSettings);
if(globalSettings.SelfHosted) if(globalSettings.SelfHosted)
{ {
@ -181,25 +183,29 @@ namespace Bit.Api
app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
// Add Swagger // Add Swagger
//if(Environment.IsDevelopment() || globalSettings.SelfHosted) if(Environment.IsDevelopment() || globalSettings.SelfHosted)
//{ {
// app.UseSwagger(config => app.UseSwagger(config =>
// { {
// config.RouteTemplate = "specs/{documentName}/swagger.json"; config.RouteTemplate = "specs/{documentName}/swagger.json";
// var host = globalSettings.BaseServiceUri.Api.Replace("https://", string.Empty) var host = globalSettings.BaseServiceUri.Api.Replace("https://", string.Empty)
// .Replace("http://", string.Empty); .Replace("http://", string.Empty);
// config.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = host); config.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
// }); swaggerDoc.Servers = new List<OpenApiServer>
// app.UseSwaggerUI(config => {
// { new OpenApiServer { Url = $"{httpReq.Scheme}://{host}" }
// config.DocumentTitle = "Bitwarden API Documentation"; });
// config.RoutePrefix = "docs"; });
// config.SwaggerEndpoint($"{globalSettings.BaseServiceUri.Api}/specs/public/swagger.json", app.UseSwaggerUI(config =>
// "Bitwarden Public API"); {
// config.OAuthClientId("accountType.id"); config.DocumentTitle = "Bitwarden API Documentation";
// config.OAuthClientSecret("secretKey"); config.RoutePrefix = "docs";
// }); config.SwaggerEndpoint($"{globalSettings.BaseServiceUri.Api}/specs/public/swagger.json",
//} "Bitwarden Public API");
config.OAuthClientId("accountType.id");
config.OAuthClientSecret("secretKey");
});
}
// Log startup // Log startup
logger.LogInformation(Constants.BypassFiltersEventId, globalSettings.ProjectName + " started."); logger.LogInformation(Constants.BypassFiltersEventId, globalSettings.ProjectName + " started.");

View File

@ -1,8 +1,9 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Bit.Core; using Bit.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger; using Microsoft.OpenApi.Models;
namespace Bit.Api.Utilities namespace Bit.Api.Utilities
{ {
@ -12,47 +13,62 @@ namespace Bit.Api.Utilities
{ {
services.AddSwaggerGen(config => services.AddSwaggerGen(config =>
{ {
config.SwaggerDoc("public", new Info config.SwaggerDoc("public", new OpenApiInfo
{ {
Title = "Bitwarden Public API", Title = "Bitwarden Public API",
Version = "latest", Version = "latest",
Contact = new Contact Contact = new OpenApiContact
{ {
Name = "Bitwarden Support", Name = "Bitwarden Support",
Url = "https://bitwarden.com", Url = new Uri("https://bitwarden.com"),
Email = "support@bitwarden.com" Email = "support@bitwarden.com"
}, },
Description = "The Bitwarden public APIs.", Description = "The Bitwarden public APIs.",
License = new License License = new OpenApiLicense
{ {
Name = "GNU Affero General Public License v3.0", Name = "GNU Affero General Public License v3.0",
Url = "https://github.com/bitwarden/server/blob/master/LICENSE.txt" Url = new Uri("https://github.com/bitwarden/server/blob/master/LICENSE.txt")
} }
}); });
// config.SwaggerDoc("internal", new Info { Title = "Bitwarden Internal API", Version = "latest" }); config.SwaggerDoc("internal", new OpenApiInfo { Title = "Bitwarden Internal API", Version = "latest" });
config.AddSecurityDefinition("OAuth2 Client Credentials", new OAuth2Scheme config.AddSecurityDefinition("OAuth2 Client Credentials", new OpenApiSecurityScheme
{ {
Type = "oauth2", Type = SecuritySchemeType.OAuth2,
Flow = "application", Flows = new OpenApiOAuthFlows
TokenUrl = $"{globalSettings.BaseServiceUri.Identity}/connect/token",
Scopes = new Dictionary<string, string>
{ {
{ "api.organization", "Organization APIs" }, ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri($"{globalSettings.BaseServiceUri.Identity}/connect/token"),
Scopes = new Dictionary<string, string>
{
{ "api.organization", "Organization APIs" },
},
}
}, },
}); });
config.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> config.AddSecurityRequirement(new OpenApiSecurityRequirement
{ {
{ "OAuth2 Client Credentials", new[] { "api.organization" } } {
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "OAuth2 Client Credentials"
},
},
new[] { "api.organization" }
}
}); });
config.DescribeAllParametersInCamelCase(); config.DescribeAllParametersInCamelCase();
// config.UseReferencedDefinitionsForEnums(); // config.UseReferencedDefinitionsForEnums();
var apiFilePath = Path.Combine(System.AppContext.BaseDirectory, "Api.xml"); var apiFilePath = Path.Combine(AppContext.BaseDirectory, "Api.xml");
config.IncludeXmlComments(apiFilePath, true); config.IncludeXmlComments(apiFilePath, true);
var coreFilePath = Path.Combine(System.AppContext.BaseDirectory, "Core.xml"); var coreFilePath = Path.Combine(AppContext.BaseDirectory, "Core.xml");
config.IncludeXmlComments(coreFilePath); config.IncludeXmlComments(coreFilePath);
}); });
} }