Ejemplo n.º 1
0
        public async Task IpClaimMissingShouldMarkContextSucceeded()
        {
            ILogger logger = Logger.None;
            var     nameValueCollection = new NameValueCollection
            {
                [DeployerAppConstants.AllowedIpNetworks] = "192.168.0.0/24"
            };
            var configuration = new InMemoryKeyValueConfiguration(nameValueCollection);

            var handler =
                new DefaultAuthorizationHandler(configuration,
                                                logger,
                                                ImmutableArray <AllowedEmail> .Empty,
                                                ImmutableArray <AllowedEmailDomain> .Empty);

            IEnumerable <Claim> claims = ImmutableArray <Claim> .Empty;
            var user = new ClaimsPrincipal(new ClaimsIdentity(claims));
            var authorizationHandlerContext = new AuthorizationHandlerContext(
                new IAuthorizationRequirement[] { new DefaultAuthorizationRequirement() },
                user,
                null);

            await handler.HandleAsync(authorizationHandlerContext);

            Assert.False(authorizationHandlerContext.HasSucceeded);
        }
Ejemplo n.º 2
0
        public async Task IpClaimInRangeForMultipleAllowedNetworksShouldMarkContextSucceeded()
        {
            ILogger logger = Logger.None;
            var     nameValueCollection = new NameValueCollection
            {
                { DeployerAppConstants.AllowedIpNetworks, "192.168.0.0/24" },
                { DeployerAppConstants.AllowedIpNetworks, "192.168.0.0/16" }
            };

            Assert.Equal(2, nameValueCollection.GetValues(DeployerAppConstants.AllowedIpNetworks)?.Length);

            var configuration = new InMemoryKeyValueConfiguration(nameValueCollection);

            var handler =
                new DefaultAuthorizationHandler(configuration,
                                                logger,
                                                ImmutableArray <AllowedEmail> .Empty,
                                                ImmutableArray <AllowedEmailDomain> .Empty);

            IEnumerable <Claim> claims = new[] { new Claim(CustomClaimTypes.IpAddress, "192.168.0.2") };
            var user = new ClaimsPrincipal(new ClaimsIdentity(claims));
            var authorizationHandlerContext = new AuthorizationHandlerContext(
                new IAuthorizationRequirement[] { new DefaultAuthorizationRequirement() },
                user,
                null);

            await handler.HandleAsync(authorizationHandlerContext);

            Assert.True(authorizationHandlerContext.HasSucceeded);
        }
Ejemplo n.º 3
0
        public void Arrange()
        {
            _configuration = new EmployerAccountsConfiguration
            {
                SupportConsoleUsers = SupportConsoleUsers
            };
            _mockAuthenticationService           = new Mock <IAuthenticationService>();
            AuthorizationContextTestsFixture     = new AuthorizationContextTestsFixture();
            MockIAuthorisationResourceRepository = new Mock <IAuthorisationResourceRepository>();
            Options      = new List <string>();
            _userContext = new UserContext(_mockAuthenticationService.Object, _configuration);
            SutDefaultAuthorizationHandler = new DefaultAuthorizationHandler(MockIAuthorisationResourceRepository.Object, _userContext);
            _testAuthorizationResource     = new AuthorizationResource
            {
                Name  = "Test",
                Value = Guid.NewGuid().ToString()
            };
            ResourceList = new List <AuthorizationResource>
            {
                _testAuthorizationResource
            };

            MockIAuthorisationResourceRepository.Setup(x => x.Get(It.IsAny <ClaimsIdentity>())).Returns(ResourceList);
            AuthorizationContext = new AuthorizationContext();
        }
Ejemplo n.º 4
0
        public DefaultAuthorizationHandlerTests()
        {
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            _configurationManagerMock = fixture.Freeze <Mock <IConfigurationManager> >();
            _requirements             = new List <IAuthorizationRequirement> {
                new DefaultAuthorizationRequirement()
            };
            _userManagerMock     = fixture.Freeze <Mock <IUserManager> >();
            _httpContextAccessor = fixture.Freeze <Mock <IHttpContextAccessor> >();

            _sut = fixture.Create <DefaultAuthorizationHandler>();
        }
Ejemplo n.º 5
0
        public async Task IpClaimWithoutNetworksShouldMarkContextNotSucceeded()
        {
            ILogger logger = Logger.None;

            var handler =
                new DefaultAuthorizationHandler(NoConfiguration.Empty,
                                                logger,
                                                ImmutableArray <AllowedEmail> .Empty,
                                                ImmutableArray <AllowedEmailDomain> .Empty);

            IEnumerable <Claim> claims = new[] { new Claim(CustomClaimTypes.IpAddress, "192.168.1.2") };
            var user = new ClaimsPrincipal(new ClaimsIdentity(claims));
            var authorizationHandlerContext = new AuthorizationHandlerContext(
                new IAuthorizationRequirement[] { new DefaultAuthorizationRequirement() },
                user,
                null);

            await handler.HandleAsync(authorizationHandlerContext);

            Assert.False(authorizationHandlerContext.HasSucceeded);
        }