diff options
author | Safia Abdalla <safia@safia.rocks> | 2022-10-25 07:19:19 +0300 |
---|---|---|
committer | Safia Abdalla <safia@safia.rocks> | 2022-10-25 07:19:19 +0300 |
commit | 941d5edcf4f0a555489168086bf530d9452ab84c (patch) | |
tree | 0c0f5793d52486a0e52e2bfaa5769475c6b26de1 | |
parent | cf9a95babc30e890fe606feda6e8bef006d98d73 (diff) |
6 files changed, 79 insertions, 98 deletions
diff --git a/src/Http/samples/MinimalSample/MinimalSample.csproj b/src/Http/samples/MinimalSample/MinimalSample.csproj index eadd5323ad..c9e197bc8d 100644 --- a/src/Http/samples/MinimalSample/MinimalSample.csproj +++ b/src/Http/samples/MinimalSample/MinimalSample.csproj @@ -16,4 +16,9 @@ <Reference Include="Microsoft.AspNetCore.Mvc.Core" /> </ItemGroup> + <ItemGroup> + <Reference Include="Microsoft.AspNetCore.Authentication.Google" /> + <Reference Include="Microsoft.AspNetCore.Authentication.OAuth" /> + </ItemGroup> + </Project> diff --git a/src/Http/samples/MinimalSample/Program.cs b/src/Http/samples/MinimalSample/Program.cs index 9f18aab0f3..b2e9a53356 100644 --- a/src/Http/samples/MinimalSample/Program.cs +++ b/src/Http/samples/MinimalSample/Program.cs @@ -10,51 +10,20 @@ using Microsoft.OpenApi; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Writers; +using Microsoft.AspNetCore.OpenApi; var builder = WebApplication.CreateBuilder(args); -builder.Services.UseOpenApi(); // Can we auto-add the options here -builder.Services.Configure<OpenApiDocument>(document => -{ - document.Tags = new List<OpenApiTag>() { new OpenApiTag { Name = "test-tag" } }; -}); +builder.Services.AddAuthentication(); +builder.Services.AddAuthorization(); + +builder.UseOpenApi(); var app = builder.Build(); string Plaintext() => "Hello, World!"; -app.MapGet("/plaintext", Plaintext); - -object Json() => new { message = "Hello, World!" }; -app.MapGet("/json", Json).WithTags("json"); - -string SayHello(string name) => $"Hello, {name}!"; -app.MapGet("/hello/{name}", SayHello); - -app.MapGet("/null-result", IResult () => null!); -app.MapGet("/todo/{id}", Results<Ok<Todo>, NotFound, BadRequest> (int id) => id switch - { - <= 0 => TypedResults.BadRequest(), - >= 1 and <= 10 => TypedResults.Ok(new Todo(id, "Walk the dog")), - _ => TypedResults.NotFound() - }); - -app.MapGet("/swagger", ([FromServices] IOptions<OpenApiDocument> openApiDocument) => -{ - return openApiDocument.Value.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); -}); - -app.Run(); - -internal record Todo(int Id, string Title); -public class TodoBindable : IBindableFromHttpContext<TodoBindable> -{ - public int Id { get; set; } - public string Title { get; set; } = string.Empty; - public bool IsComplete { get; set; } +app.MapGet("/plaintext", Plaintext); +app.MapGet("/plaintext-secured", Plaintext); - public static ValueTask<TodoBindable?> BindAsync(HttpContext context, ParameterInfo parameter) - { - return ValueTask.FromResult<TodoBindable?>(new TodoBindable { Id = 1, Title = "I was bound from IBindableFromHttpContext<TodoBindable>.BindAsync!" }); - } -} +app.Run();
\ No newline at end of file diff --git a/src/Http/samples/MinimalSample/appsettings.json b/src/Http/samples/MinimalSample/appsettings.json index d9d9a9bff6..d3f5e3ef51 100644 --- a/src/Http/samples/MinimalSample/appsettings.json +++ b/src/Http/samples/MinimalSample/appsettings.json @@ -6,5 +6,11 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "Authentication": { + "Google": { + "ClientId": "736712084808-g5vhrqm4rnt4vquoa0elos6fphrio2gl.apps.googleusercontent.com", + "ClientSecret": "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" + } + }, "AllowedHosts": "*" } diff --git a/src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj b/src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj index c414f7c680..c1dc8ed5d2 100644 --- a/src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj +++ b/src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj @@ -9,6 +9,7 @@ </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.AspNetCore" /> <Reference Include="Microsoft.OpenApi" /> <Reference Include="Microsoft.AspNetCore.Http.Abstractions" /> <Reference Include="Microsoft.AspNetCore.Routing" /> diff --git a/src/OpenApi/src/OpenApiApplicationBuilderExtensions.cs b/src/OpenApi/src/OpenApiApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..34eddb03e6 --- /dev/null +++ b/src/OpenApi/src/OpenApiApplicationBuilderExtensions.cs @@ -0,0 +1,58 @@ +using System; +using System.Linq; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Authentication; +using Microsoft.OpenApi; +using Microsoft.OpenApi.Models; +using Microsoft.Extensions.Options; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.AspNetCore.OpenApi; + +public static class OpenApiApplicationBuilderExtensions +{ + public static WebApplicationBuilder UseOpenApi(this WebApplicationBuilder builder) + { + builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<OpenApiDocument>, OpenApiDocumentConfigureOptions>()); + // builder.Services.Configure<OpenApiDocument>(); + return builder; + } +} + +internal sealed class OpenApiDocumentConfigureOptions : IConfigureOptions<OpenApiDocument> +{ + private readonly IHostEnvironment _hostEnvironment; + private readonly IServiceProviderIsService _serviceProviderIsService; + private readonly EndpointDataSource _endpointDataSource; + private readonly IAuthenticationSchemeProvider? _authenticationSchemeProvider; + private readonly OpenApiGenerator _generator; + + public OpenApiDocumentConfigureOptions(IHostEnvironment hostEnvironment, IServiceProviderIsService serviceProviderIsService, EndpointDataSource endpointDataSource, IAuthenticationSchemeProvider? authenticationSchemeProvider) + { + _hostEnvironment = hostEnvironment; + _serviceProviderIsService = serviceProviderIsService; + _endpointDataSource = endpointDataSource; + _authenticationSchemeProvider = authenticationSchemeProvider; + _generator = new OpenApiGenerator(hostEnvironment, serviceProviderIsService); + } + + public void Configure(OpenApiDocument document) + { + document = _generator.GetOpenApiDocument(document, _endpointDataSource.Endpoints); + var authSchemes = _authenticationSchemeProvider?.GetAllSchemesAsync().Result ?? Enumerable.Empty<AuthenticationScheme>(); + foreach (var scheme in authSchemes) + { + document.Components.SecuritySchemes.Add(scheme.Name, new OpenApiSecurityScheme + { + Type = SecuritySchemeType.Http, + Scheme = "bearer", + In = ParameterLocation.Header, + }); + } + } +} + + diff --git a/src/OpenApi/src/OpenApiEndpointConventionBuilderExtensions.cs b/src/OpenApi/src/OpenApiEndpointConventionBuilderExtensions.cs index 3d7b2cf869..a6211e22f9 100644 --- a/src/OpenApi/src/OpenApiEndpointConventionBuilderExtensions.cs +++ b/src/OpenApi/src/OpenApiEndpointConventionBuilderExtensions.cs @@ -22,31 +22,6 @@ namespace Microsoft.AspNetCore.Builder; public static class OpenApiEndpointConventionBuilderExtensions { /// <summary> - /// - /// </summary> - /// <param name="builder"></param> - /// <returns></returns> - public static IEndpointRouteBuilder WithOpenApi(this IEndpointRouteBuilder builder, Func<OpenApiDocument, OpenApiDocument> configureDocument = null) - { - var applicationServices = builder.ServiceProvider; - var hostEnvironment = applicationServices.GetService<IHostEnvironment>(); - var serviceProviderIsService = applicationServices.GetService<IServiceProviderIsService>(); - var generator = new OpenApiGenerator(hostEnvironment, serviceProviderIsService); - //var document = generator.GetOpenApiDocument(builder.DataSources); - //if (configureDocument != null) - //{ - // document = configureDocument(document); - //} - return builder; - } - - public static IServiceCollection UseOpenApi(this IServiceCollection services) - { - services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<OpenApiDocument>, OpenApiDocumentConfigureOptions>()); - return services; - } - - /// <summary> /// Adds an OpenAPI annotation to <see cref="Endpoint.Metadata" /> associated /// with the current endpoint. /// </summary> @@ -133,37 +108,4 @@ public static class OpenApiEndpointConventionBuilderExtensions } } } -} - -internal sealed class OpenApiDocumentConfigureOptions : IConfigureOptions<OpenApiDocument> -{ - private readonly IHostEnvironment _hostEnvironment; - private readonly IServiceProviderIsService _serviceProviderIsService; - private readonly EndpointDataSource _endpointDataSource; - private readonly IAuthenticationSchemeProvider? _authenticationSchemeProvider; - private readonly OpenApiGenerator _generator; - - public OpenApiDocumentConfigureOptions(IHostEnvironment hostEnvironment, IServiceProviderIsService serviceProviderIsService, EndpointDataSource endpointDataSource, IAuthenticationSchemeProvider? authenticationSchemeProvider) - { - _hostEnvironment = hostEnvironment; - _serviceProviderIsService = serviceProviderIsService; - _endpointDataSource = endpointDataSource; - _authenticationSchemeProvider = authenticationSchemeProvider; - _generator = new OpenApiGenerator(hostEnvironment, serviceProviderIsService); - } - - public void Configure(OpenApiDocument document) - { - document = _generator.GetOpenApiDocument(document, _endpointDataSource.Endpoints); - var authSchemes = _authenticationSchemeProvider?.GetAllSchemesAsync().Result ?? Enumerable.Empty<AuthenticationScheme>(); - foreach (var scheme in authSchemes) - { - document.Components.SecuritySchemes.Add(scheme.Name, new OpenApiSecurityScheme - { - Type = SecuritySchemeType.Http, - Scheme = "bearer", - In = ParameterLocation.Header, - }); - } - } -} +}
\ No newline at end of file |