From 479173601928d25e17f6bacf453c2f555bfa9c7f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 29 Sep 2021 17:10:49 +0200 Subject: [PATCH] Swagger: Creating a basic security definition --- src/Kyoo.Swagger/SwaggerModule.cs | 48 ++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Kyoo.Swagger/SwaggerModule.cs b/src/Kyoo.Swagger/SwaggerModule.cs index 4cbff4a1..1bf05ec3 100644 --- a/src/Kyoo.Swagger/SwaggerModule.cs +++ b/src/Kyoo.Swagger/SwaggerModule.cs @@ -52,41 +52,61 @@ namespace Kyoo.Swagger public void Configure(IServiceCollection services) { services.AddTransient(); - services.AddOpenApiDocument(options => + services.AddOpenApiDocument(document => { - options.Title = "Kyoo API"; + document.Title = "Kyoo API"; // TODO use a real multi-line description in markdown. - options.Description = "The Kyoo's public API"; - options.Version = "1.0.0"; - options.DocumentName = "v1"; - options.UseControllerSummaryAsTagDescription = true; - options.GenerateExamples = true; - options.PostProcess = postProcess => + document.Description = "The Kyoo's public API"; + document.Version = "1.0.0"; + document.DocumentName = "v1"; + document.UseControllerSummaryAsTagDescription = true; + document.GenerateExamples = true; + document.PostProcess = options => { - postProcess.Info.Contact = new OpenApiContact + options.Info.Contact = new OpenApiContact { Name = "Kyoo's github", Url = "https://github.com/AnonymusRaccoon/Kyoo" }; - postProcess.Info.License = new OpenApiLicense + options.Info.License = new OpenApiLicense { Name = "GPL-3.0-or-later", Url = "https://github.com/AnonymusRaccoon/Kyoo/blob/master/LICENSE" }; }; - options.UseApiTags(); - options.SortApis(); - options.AddOperationFilter(x => + document.UseApiTags(); + document.SortApis(); + document.AddOperationFilter(x => { if (x is AspNetCoreOperationProcessorContext ctx) return ctx.ApiDescription.ActionDescriptor.AttributeRouteInfo?.Order != AlternativeRoute; return true; }); - options.SchemaGenerator.Settings.TypeMappers.Add(new PrimitiveTypeMapper(typeof(Identifier), x => + document.SchemaGenerator.Settings.TypeMappers.Add(new PrimitiveTypeMapper(typeof(Identifier), x => { x.IsNullableRaw = false; x.Type = JsonObjectType.String | JsonObjectType.Integer; })); + + document.AddSecurity("Kyoo", new OpenApiSecurityScheme() + { + Type = OpenApiSecuritySchemeType.OpenIdConnect, + Description = "Kyoo's OpenID Authentication", + Flow = OpenApiOAuth2Flow.AccessCode, + Flows = new OpenApiOAuthFlows() + { + Implicit = new OpenApiOAuthFlow() + { + Scopes = new Dictionary + { + { "read", "Read access to protected resources" }, + { "write", "Write access to protected resources" } + }, + AuthorizationUrl = "https://localhost:44333/core/connect/authorize", + TokenUrl = "https://localhost:44333/core/connect/token" + }, + } + }); }); }