예제 #1
0
        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);
        }
예제 #2
0
 public ConfigureSwaggerOptions(
     RestApiExtendedOptions options,
     IWebHostEnvironment env)
 {
     this.apiOptions = options ?? throw new ArgumentNullException(nameof(options));
     this.env        = env ?? throw new ArgumentNullException(nameof(env));
 }
예제 #3
0
 public static IServiceCollection AddRestApi <TStartup>(
     this IServiceCollection services,
     RestApiExtendedOptions restApiOptions,
     IConfiguration configuration)
 {
     return(services.AddRestApi <TStartup>(mvc => { }, restApiOptions, configuration));
 }
예제 #4
0
        public void PostConfigure_JwtBearerOptions_Sets_Authority(RestApiExtendedOptions apiOptions)
        {
            var options = new JwtBearerOptions();

            new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options);
            options.Authority.Should().NotBeNullOrWhiteSpace();
        }
예제 #5
0
        public void PostConfigure_JwtBearerOptions_Sets_TokenValidationParameters(RestApiExtendedOptions apiOptions)
        {
            var options = new JwtBearerOptions();

            new ConfigureAuthorizationOptions(apiOptions, null).PostConfigure(string.Empty, options);
            options.TokenValidationParameters.Should().NotBeNull();
        }
예제 #6
0
        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)));
        }
예제 #7
0
        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, _ => { }));
 }
예제 #9
0
        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();
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #13
0
        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();
        }
예제 #14
0
 public Startup(IConfiguration configuration)
 {
     Configuration  = configuration;
     restApiOptions = new RestApiExtendedOptions();
     restApiOptions.AddAssemblyPairs(Assembly.GetAssembly(typeof(ApiRegistration)), Assembly.GetAssembly(typeof(DomainRegistration)));
 }
예제 #15
0
 public static IServiceCollection AddOpenApiSpec <TStartup>(
     this IServiceCollection services,
     RestApiExtendedOptions restApiOptions)
 {
     return(services.AddOpenApiSpec <TStartup>(restApiOptions, _ => { }));
 }