| | | 1 | | using System.Reflection; |
| | | 2 | | using ClutterStock.Api.Filters; |
| | | 3 | | using ClutterStock.Contracts.Items; |
| | | 4 | | using Microsoft.OpenApi; |
| | | 5 | | |
| | | 6 | | namespace ClutterStock.Api.Extensions; |
| | | 7 | | |
| | | 8 | | internal static class OpenApiServiceExtensions |
| | | 9 | | { |
| | | 10 | | public static IServiceCollection AddOpenApiDocumentation(this IServiceCollection services) |
| | | 11 | | { |
| | 3 | 12 | | services.AddOpenApi(); |
| | 3 | 13 | | services.AddSwaggerGen(static options => |
| | 3 | 14 | | { |
| | 3 | 15 | | options.CustomSchemaIds(static type => type.FullName?.Replace("+", ".") ?? type.Name); |
| | 3 | 16 | | |
| | 3 | 17 | | const string intro = |
| | 3 | 18 | | "Because \"somewhere in the garage\" isn't a location. Home inventory for your actual clutter—electronic |
| | 3 | 19 | | |
| | 3 | 20 | | options.SwaggerDoc("v1", |
| | 3 | 21 | | new OpenApiInfo |
| | 3 | 22 | | { |
| | 3 | 23 | | Version = "v1", |
| | 3 | 24 | | Title = "ClutterStock API", |
| | 3 | 25 | | Description = ApiBuildMetadata.BuildOpenApiDescription(intro) |
| | 3 | 26 | | }); |
| | 3 | 27 | | |
| | 3 | 28 | | var apiXml = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml") |
| | 3 | 29 | | if (File.Exists(apiXml)) |
| | 3 | 30 | | options.IncludeXmlComments(apiXml); |
| | 3 | 31 | | |
| | 3 | 32 | | var contractsAssembly = typeof(AddItemRequest).Assembly; |
| | 3 | 33 | | var contractsXml = Path.Combine(AppContext.BaseDirectory, $"{contractsAssembly.GetName().Name}.xml"); |
| | 3 | 34 | | if (File.Exists(contractsXml)) |
| | 3 | 35 | | options.IncludeXmlComments(contractsXml); |
| | 3 | 36 | | |
| | 3 | 37 | | options.OperationFilter<GlobalResponsesOperationFilter>(); |
| | 3 | 38 | | options.SwaggerGeneratorOptions.XmlCommentEndOfLine = "\n"; |
| | 3 | 39 | | |
| | 3 | 40 | | options.AddSecurityDefinition("oidc", new OpenApiSecurityScheme |
| | 3 | 41 | | { |
| | 3 | 42 | | Type = SecuritySchemeType.OAuth2, |
| | 3 | 43 | | Flows = new OpenApiOAuthFlows |
| | 3 | 44 | | { |
| | 3 | 45 | | AuthorizationCode = new OpenApiOAuthFlow |
| | 3 | 46 | | { |
| | 3 | 47 | | AuthorizationUrl = new Uri("https://auth.wsh.no/api/oidc/authorization"), |
| | 3 | 48 | | TokenUrl = new Uri("https://auth.wsh.no/api/oidc/token"), |
| | 3 | 49 | | Scopes = new Dictionary<string, string> |
| | 3 | 50 | | { |
| | 3 | 51 | | ["openid"] = "OpenID", |
| | 3 | 52 | | ["profile"] = "Profile", |
| | 3 | 53 | | ["email"] = "Email", |
| | 3 | 54 | | ["groups"] = "Groups", |
| | 3 | 55 | | ["offline_access"] = "Offline access", |
| | 3 | 56 | | } |
| | 3 | 57 | | } |
| | 3 | 58 | | } |
| | 3 | 59 | | }); |
| | 3 | 60 | | |
| | 3 | 61 | | options.AddSecurityRequirement(static doc => new OpenApiSecurityRequirement |
| | 3 | 62 | | { |
| | 3 | 63 | | { new OpenApiSecuritySchemeReference("oidc", doc), [] } |
| | 3 | 64 | | }); |
| | 3 | 65 | | }); |
| | | 66 | | |
| | 3 | 67 | | return services; |
| | | 68 | | } |
| | | 69 | | } |