mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 13:38:13 -05:00
swagger specs for public api
This commit is contained in:
parent
b6696df055
commit
1c71af47bb
@ -14,6 +14,11 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="4.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -14,6 +14,7 @@ using Stripe;
|
||||
using Bit.Core.Utilities;
|
||||
using IdentityModel;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Swashbuckle.AspNetCore.Swagger;
|
||||
|
||||
namespace Bit.Api
|
||||
{
|
||||
@ -110,10 +111,13 @@ namespace Bit.Api
|
||||
// MVC
|
||||
services.AddMvc(config =>
|
||||
{
|
||||
config.Conventions.Add(new ApiExplorerGroupConvention());
|
||||
config.Filters.Add(new ExceptionHandlerFilterAttribute());
|
||||
config.Filters.Add(new ModelStateValidationFilterAttribute());
|
||||
}).AddJsonOptions(o => o.SerializerSettings.ContractResolver = new DefaultContractResolver());
|
||||
|
||||
services.AddSwagger(globalSettings);
|
||||
|
||||
if(globalSettings.SelfHosted)
|
||||
{
|
||||
// Jobs service
|
||||
@ -182,6 +186,21 @@ namespace Bit.Api
|
||||
|
||||
// Add MVC to the request pipeline.
|
||||
app.UseMvc();
|
||||
|
||||
if(globalSettings.SelfHosted)
|
||||
{
|
||||
app.UseSwagger(config =>
|
||||
{
|
||||
config.RouteTemplate = "specs/{documentName}/swagger.json";
|
||||
});
|
||||
app.UseSwaggerUI(config =>
|
||||
{
|
||||
config.RoutePrefix = "docs";
|
||||
config.SwaggerEndpoint("/specs/public/swagger.json", "Bitwarden Public API");
|
||||
config.OAuthClientId("accountType.id");
|
||||
config.OAuthClientSecret("secretKey");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
src/Api/Utilities/ApiExplorerGroupConvention.cs
Normal file
13
src/Api/Utilities/ApiExplorerGroupConvention.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
|
||||
namespace Bit.Api.Utilities
|
||||
{
|
||||
public class ApiExplorerGroupConvention : IControllerModelConvention
|
||||
{
|
||||
public void Apply(ControllerModel controller)
|
||||
{
|
||||
var controllerNamespace = controller.ControllerType.Namespace;
|
||||
controller.ApiExplorer.GroupName = controllerNamespace.Contains(".Public.") ? "public" : "internal";
|
||||
}
|
||||
}
|
||||
}
|
35
src/Api/Utilities/ServiceCollectionExtensions.cs
Normal file
35
src/Api/Utilities/ServiceCollectionExtensions.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Swashbuckle.AspNetCore.Swagger;
|
||||
|
||||
namespace Bit.Api.Utilities
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static void AddSwagger(this IServiceCollection services, GlobalSettings globalSettings)
|
||||
{
|
||||
services.AddSwaggerGen(config =>
|
||||
{
|
||||
config.SwaggerDoc("public", new Info { Title = "Bitwarden Public API", Version = "latest" });
|
||||
// config.SwaggerDoc("internal", new Info { Title = "Bitwarden Internal API", Version = "latest" });
|
||||
|
||||
config.AddSecurityDefinition("OAuth2 Client Credentials", new OAuth2Scheme
|
||||
{
|
||||
Type = "oauth2",
|
||||
Flow = "application",
|
||||
TokenUrl = $"{globalSettings.BaseServiceUri.Identity}/connect/token",
|
||||
Scopes = new Dictionary<string, string>
|
||||
{
|
||||
{ "api.organization", "Organization APIs" },
|
||||
},
|
||||
});
|
||||
|
||||
config.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
|
||||
{
|
||||
{ "OAuth2 Client Credentials", new[] { "api.organization" } }
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user