Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }