Exemplo n.º 1
0
        public static void Register(IServiceCollection services, IConfiguration configuration)
        {
            var apiKeyConfiguration = new ApiKeyConfiguration();

            configuration.Bind(nameof(ApiKeyConfiguration), apiKeyConfiguration);
            services.AddSingleton(apiKeyConfiguration);

            services.AddSingleton(typeof(IApiKeyRepository), typeof(ApiKeyRepository));
        }
Exemplo n.º 2
0
        private static Mock <HttpContext> SetupMockHttpContext(ApiKeyConfiguration expectedApiKeyConfiguration, StringValues expectedApiKeyIdInHeaders,
                                                               bool tryGetApiKeyIdFromHeadersReturns)
        {
            var mockHttpContext = new Mock <HttpContext>();

            mockHttpContext.Setup(context => context.Request.Headers.TryGetValue(expectedApiKeyConfiguration.ApiHeader, out expectedApiKeyIdInHeaders))
            .Returns(tryGetApiKeyIdFromHeadersReturns);
            return(mockHttpContext);
        }
Exemplo n.º 3
0
        public static void AddSwaggerWithApiKeySecurity(this IServiceCollection services, IConfiguration configuration, string assemblyName)
        {
            var apiKeyConfiguration = new ApiKeyConfiguration();

            configuration.Bind(nameof(ApiKeyConfiguration), apiKeyConfiguration);

            if (string.IsNullOrEmpty(apiKeyConfiguration?.ApiHeader))
            {
                throw new InvalidOperationException("ApiKeyConfiguration.ApiHeader is null or empty.");
            }

            // https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0&tabs=visual-studio
            services.AddSwaggerGen(c =>
            {
                const string securityDefinition = "ApiKey";

                // https://stackoverflow.com/questions/36975389/api-key-in-header-with-swashbuckle
                c.AddSecurityDefinition(securityDefinition, new OpenApiSecurityScheme
                {
                    In   = ParameterLocation.Header,
                    Name = apiKeyConfiguration.ApiHeader,
                    Type = SecuritySchemeType.ApiKey
                });
                // https://stackoverflow.com/questions/57227912/swaggerui-not-adding-apikey-to-header-with-swashbuckle-5-x
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Name      = apiKeyConfiguration.ApiHeader,
                            Type      = SecuritySchemeType.ApiKey,
                            In        = ParameterLocation.Header,
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme, Id = securityDefinition
                            }
                        },
                        new List <string>()
                    }
                });

                var xmlFile = $"{assemblyName}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }
Exemplo n.º 4
0
 private static ApiKey GetExpectedApiKey(ApiKeyConfiguration expectedApiKeyConfiguration, int?expectedApiKeyId) =>
 expectedApiKeyId != null?expectedApiKeyConfiguration?.ApiKeys?.ToList()[(int)expectedApiKeyId] : null;