public void PostConfigure_AuthenticationOptions_Sets_DefaultScheme(RestApiExtendedOptions apiOptions) { var options = new AuthenticationOptions(); new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options); options.DefaultScheme.Should().Be(JwtBearerDefaults.AuthenticationScheme); }
public ConfigureSwaggerOptions( RestApiExtendedOptions options, IWebHostEnvironment env) { this.apiOptions = options ?? throw new ArgumentNullException(nameof(options)); this.env = env ?? throw new ArgumentNullException(nameof(env)); }
public static IServiceCollection AddRestApi <TStartup>( this IServiceCollection services, RestApiExtendedOptions restApiOptions, IConfiguration configuration) { return(services.AddRestApi <TStartup>(mvc => { }, restApiOptions, configuration)); }
public void PostConfigure_JwtBearerOptions_Sets_Authority(RestApiExtendedOptions apiOptions) { var options = new JwtBearerOptions(); new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options); options.Authority.Should().NotBeNullOrWhiteSpace(); }
public void PostConfigure_JwtBearerOptions_Sets_TokenValidationParameters(RestApiExtendedOptions apiOptions) { var options = new JwtBearerOptions(); new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options); options.TokenValidationParameters.Should().NotBeNull(); }
public Startup(IConfiguration configuration) { Configuration = configuration; restApiOptions = new RestApiExtendedOptions { // Base AllowAnonymousAccessForDevelopment = true, UseApplicationInsights = true, UseAutoRegistrateServices = true, UseEnumAsStringInSerialization = true, UseHttpContextAccessor = true, ErrorHandlingExceptionFilter = new RestApiOptionsErrorHandlingExceptionFilter { Enable = true, UseProblemDetailsAsResponseBody = true, IncludeExceptionDetails = true }, UseRequireHttpsPermanent = true, UseJsonSerializerOptionsIgnoreNullValues = true, JsonSerializerCasingStyle = CasingStyle.CamelCase, UseValidateServiceRegistrations = true, // Extended UseApiVersioning = true, UseFluentValidation = true, UseOpenApiSpec = true }; restApiOptions.AddAssemblyPairs( Assembly.GetAssembly(typeof(ApiRegistration)), Assembly.GetAssembly(typeof(DomainRegistration))); }
public static IApplicationBuilder UseOpenApiSpec( this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions) { if (env == null) { throw new ArgumentNullException(nameof(env)); } if (restApiOptions == null) { throw new ArgumentNullException(nameof(restApiOptions)); } if (!restApiOptions.UseOpenApiSpec) { return(app); } app.UseSwagger(); if (env.IsDevelopment()) { app.UseSwaggerUI(); } return(app); }
public static IApplicationBuilder ConfigureRestApi( this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions) { return(app.ConfigureRestApi(env, restApiOptions, _ => { })); }
public void PostConfigure_JwtBearerOptions_ValidateIssuer_True_With_Issuer(RestApiExtendedOptions apiOptions) { apiOptions.Authorization.ValidIssuers = new List <string>(); var options = new JwtBearerOptions(); new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options); options.TokenValidationParameters.ValidateIssuer.Should().BeTrue(); }
public static IServiceCollection AddOpenApiSpec <TStartup>( this IServiceCollection services, RestApiExtendedOptions restApiOptions, Action <SwaggerGenOptions> setupAction) { services.AddSwaggerGen(options => { options.TagActionsBy(api => { if (api.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { return(new[] { controllerActionDescriptor.ControllerName }); } if (api.HttpMethod != null) { return(new[] { api.HttpMethod }); } throw new InvalidOperationException("Unable to determine tag for endpoint."); }); options.OrderActionsBy((apiDesc) => $"{apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.HttpMethod}"); options.IgnoreObsoleteActions(); options.IgnoreObsoleteProperties(); options.DefaultResponseForSecuredOperations(); options.TreatBadRequestAsDefaultResponse(); if (!restApiOptions.AllowAnonymousAccessForDevelopment) { options.OperationFilter <SecurityRequirementsOperationFilter>(); } options.DocumentFilter <SwaggerEnumDescriptionsDocumentFilter>(); if (restApiOptions.UseApiVersioning) { options.ApplyApiVersioningFilters(); options.ApplyApiVersioningSwaggerDocuments(services, typeof(TStartup).GetApiName()); } options.IncludeXmlComments(Path.ChangeExtension(typeof(TStartup).Assembly.Location, "xml")); foreach (var assemblyPairOptions in restApiOptions.AssemblyPairs) { options.IncludeXmlComments(Path.ChangeExtension(assemblyPairOptions.ApiAssembly.Location, "xml")); } setupAction(options); }); return(services); }
public static IServiceCollection AddRestApi <TStartup>( this IServiceCollection services, Action <IMvcBuilder> setupMvcAction, RestApiExtendedOptions restApiOptions, IConfiguration configuration) { if (setupMvcAction == null) { throw new ArgumentNullException(nameof(setupMvcAction)); } if (restApiOptions == null) { throw new ArgumentNullException(nameof(restApiOptions)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } services.AddSingleton(restApiOptions); services.AddSingleton <RestApiOptions>(restApiOptions); if (restApiOptions.UseApiVersioning) { services.ConfigureOptions <ConfigureApiVersioningOptions>(); services.AddApiVersioning(); services.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VV"); } if (restApiOptions.UseOpenApiSpec) { services.ConfigureOptions <ConfigureSwaggerOptions>(); services.AddOpenApiSpec <TStartup>(restApiOptions); } services.AddRestApi <TStartup>(setupMvcAction, restApiOptions); if (restApiOptions.UseFluentValidation) { services.AddFluentValidation <TStartup>(restApiOptions.UseAutoRegistrateServices, restApiOptions.AssemblyPairs); } configuration.Bind(AuthorizationOptions.ConfigurationSectionName, restApiOptions.Authorization); services.ConfigureOptions <ConfigureAuthorizationOptions>(); services.AddAuthentication().AddJwtBearer(); return(services); }
public static IApplicationBuilder ConfigureRestApi( this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions, Action <IApplicationBuilder> setupAction) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (env == null) { throw new ArgumentNullException(nameof(env)); } if (restApiOptions == null) { throw new ArgumentNullException(nameof(restApiOptions)); } if (setupAction == null) { throw new ArgumentNullException(nameof(setupAction)); } if (env.IsDevelopment()) { IdentityModelEventSource.ShowPII = true; } // Cast to base-restApiOptions to force to use ConfigureRestApi in RestApiBuilderExtensions app.ConfigureRestApi(env, restApiOptions as RestApiOptions, setupAction); if (restApiOptions.UseOpenApiSpec) { app.UseOpenApiSpec(env, restApiOptions); } return(app); }
public void PostConfigure_JwtBearerOptions_ValidateIssuer_False_With_ValidIssuers_Null(RestApiExtendedOptions apiOptions) { apiOptions.Authorization.Issuer = null; apiOptions.Authorization.ValidIssuers = null; var options = new JwtBearerOptions(); new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options); options.TokenValidationParameters.ValidateIssuer.Should().BeFalse(); }
public Startup(IConfiguration configuration) { Configuration = configuration; restApiOptions = new RestApiExtendedOptions(); restApiOptions.AddAssemblyPairs(Assembly.GetAssembly(typeof(ApiRegistration)), Assembly.GetAssembly(typeof(DomainRegistration))); }
public static IServiceCollection AddOpenApiSpec <TStartup>( this IServiceCollection services, RestApiExtendedOptions restApiOptions) { return(services.AddOpenApiSpec <TStartup>(restApiOptions, _ => { })); }