Files
server/src/Api/Utilities/ServiceCollectionExtensions.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
3.0 KiB
C#
Raw Normal View History

2020-01-10 09:36:12 -05:00
using System;
using System.Collections.Generic;
2019-03-01 09:34:07 -05:00
using System.IO;
using Bit.Core.Settings;
2019-02-28 14:20:14 -05:00
using Microsoft.Extensions.DependencyInjection;
2020-01-10 09:36:12 -05:00
using Microsoft.OpenApi.Models;
2019-02-28 14:20:14 -05:00
namespace Bit.Api.Utilities
{
public static class ServiceCollectionExtensions
{
public static void AddSwagger(this IServiceCollection services, GlobalSettings globalSettings)
{
services.AddSwaggerGen(config =>
{
2020-01-10 09:36:12 -05:00
config.SwaggerDoc("public", new OpenApiInfo
2019-03-07 14:06:02 -05:00
{
Title = "Bitwarden Public API",
Version = "latest",
2020-01-10 09:36:12 -05:00
Contact = new OpenApiContact
2019-03-07 14:06:02 -05:00
{
Name = "Bitwarden Support",
2020-01-10 09:36:12 -05:00
Url = new Uri("https://bitwarden.com"),
2019-03-07 14:06:02 -05:00
Email = "support@bitwarden.com"
},
Description = "The Bitwarden public APIs.",
2020-01-10 09:36:12 -05:00
License = new OpenApiLicense
2019-03-07 14:06:02 -05:00
{
Name = "GNU Affero General Public License v3.0",
2020-01-10 09:36:12 -05:00
Url = new Uri("https://github.com/bitwarden/server/blob/master/LICENSE.txt")
2019-03-07 14:06:02 -05:00
}
});
2020-01-10 09:36:12 -05:00
config.SwaggerDoc("internal", new OpenApiInfo { Title = "Bitwarden Internal API", Version = "latest" });
2019-02-28 14:20:14 -05:00
2020-01-10 09:36:12 -05:00
config.AddSecurityDefinition("OAuth2 Client Credentials", new OpenApiSecurityScheme
2019-02-28 14:20:14 -05:00
{
2020-01-10 09:36:12 -05:00
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
2019-02-28 14:20:14 -05:00
{
2020-01-10 09:36:12 -05:00
ClientCredentials = new OpenApiOAuthFlow
{
TokenUrl = new Uri($"{globalSettings.BaseServiceUri.Identity}/connect/token"),
Scopes = new Dictionary<string, string>
{
{ "api.organization", "Organization APIs" },
},
}
2019-02-28 14:20:14 -05:00
},
});
2020-01-10 09:36:12 -05:00
config.AddSecurityRequirement(new OpenApiSecurityRequirement
2019-02-28 14:20:14 -05:00
{
2020-01-10 09:36:12 -05:00
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "OAuth2 Client Credentials"
},
},
new[] { "api.organization" }
}
2019-02-28 14:20:14 -05:00
});
2019-02-28 20:50:40 -05:00
config.DescribeAllParametersInCamelCase();
// config.UseReferencedDefinitionsForEnums();
2019-03-01 09:34:07 -05:00
2020-01-10 09:36:12 -05:00
var apiFilePath = Path.Combine(AppContext.BaseDirectory, "Api.xml");
2019-03-01 09:34:07 -05:00
config.IncludeXmlComments(apiFilePath, true);
2020-01-10 09:36:12 -05:00
var coreFilePath = Path.Combine(AppContext.BaseDirectory, "Core.xml");
2019-03-01 09:34:07 -05:00
config.IncludeXmlComments(coreFilePath);
2019-02-28 14:20:14 -05:00
});
}
}
}