Beispiel #1
0
        private async Task <ApiKeyAuthenticationHandler> BuildHandlerAsync(HttpContext httpContext)
        {
            var authHandler = new ApiKeyAuthenticationHandler(_apiKeyAuthenticationOptions,
                                                              _authorizedKeysProviderMock.Object, _loggerFactory, _urlEncoder, _systemClock);

            var authScheme = new AuthenticationScheme(ApiKeyConstants.SecurityScheme, ApiKeyConstants.SecurityScheme,
                                                      typeof(ApiKeyAuthenticationHandler));
            await authHandler.InitializeAsync(authScheme, httpContext);

            return(authHandler);
        }
Beispiel #2
0
        public async Task Authenticate_With_Whitespace_Api_Key_In_Header_Content()
        {
            var options = new Mock <IOptionsMonitor <ApiKeyAuthenticationOptions> >();

            options.Setup(x => x.Get(It.IsAny <string>())).Returns(new ApiKeyAuthenticationOptions());
            var loggerFactory      = new Mock <ILoggerFactory>();
            var encoder            = new Mock <UrlEncoder>();
            var clock              = new Mock <ISystemClock>();
            var apiKeyCacheService = new Mock <ApiKeyCacheService>();
            var roleService        = new Mock <IRoleService>();
            var daprClient         = new Mock <DaprClient>();
            var logger             = new Mock <ILogger <ApiKeyAuthenticationHandler> >();

            loggerFactory.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            var apiKeyAuthenticationHandler = new ApiKeyAuthenticationHandler(
                options.Object,
                loggerFactory.Object,
                encoder.Object,
                clock.Object,
                apiKeyCacheService.Object,
                roleService.Object,
                daprClient.Object,
                logger.Object
                );

            var context = Fixture.CreateHttpContext(
                tenantId: "global",
                userId: "Id1",
                userName: "******",
                roles: new() { RoleConstant.TENANT_MANAGER },
                headers: new()
            {
                { HttpHeaderConstant.TenantId, new StringValues("global") },
                { HttpHeaderConstant.ApiKey, new StringValues("       ") }
            }
Beispiel #3
0
        public async Task Authenticate_Without_Api_Key()
        {
            var options = new Mock <IOptionsMonitor <ApiKeyAuthenticationOptions> >();

            options.Setup(x => x.Get(It.IsAny <string>())).Returns(new ApiKeyAuthenticationOptions());
            var loggerFactory      = new Mock <ILoggerFactory>();
            var encoder            = new Mock <UrlEncoder>();
            var clock              = new Mock <ISystemClock>();
            var apiKeyCacheService = new Mock <ApiKeyCacheService>();
            var roleService        = new Mock <IRoleService>();
            var daprClient         = new Mock <DaprClient>();
            var logger             = new Mock <ILogger <ApiKeyAuthenticationHandler> >();

            loggerFactory.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            var apiKeyAuthenticationHandler = new ApiKeyAuthenticationHandler(
                options.Object,
                loggerFactory.Object,
                encoder.Object,
                clock.Object,
                apiKeyCacheService.Object,
                roleService.Object,
                daprClient.Object,
                logger.Object
                );

            var context = new DefaultHttpContext();

            await apiKeyAuthenticationHandler.InitializeAsync(new AuthenticationScheme(ApiKeyAuthenticationOptions.DefaultScheme, null, typeof(ApiKeyAuthenticationHandler)), context);

            var authenticateResult = await apiKeyAuthenticationHandler.AuthenticateAsync();

            Assert.NotNull(authenticateResult);
            Assert.False(authenticateResult.Succeeded);
            Assert.True(authenticateResult.None);
        }