Exemplo n.º 1
0
        public void Authenticate_CorrectlyAuthenticates(string otherSigningKey, bool expectAuthenticated)
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            AppServiceAuthenticationOptions optionsDefault = CreateTestOptions(config);

            optionsDefault.SigningKey = SigningKeyAlpha;

            AppServiceAuthenticationOptions optionsOtherSigningKey = CreateTestOptions(config);

            optionsOtherSigningKey.SigningKey = otherSigningKey;

            var mock    = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken());

            // Act
            AuthenticationTicket authTicket = mock.Authenticate(request, optionsOtherSigningKey);

            // Assert
            if (expectAuthenticated)
            {
                // ensure the AuthenticationTicket is set correctly
                Assert.NotNull(authTicket);
                Assert.NotNull(authTicket.Identity);
                Assert.True(authTicket.Identity.IsAuthenticated);
            }
            else
            {
                Assert.NotNull(authTicket);
                Assert.NotNull(authTicket.Identity);
                Assert.False(authTicket.Identity.IsAuthenticated);
            }
        }
Exemplo n.º 2
0
        public void Authenticate_Fails_WithInvalidIssuer()
        {
            // Arrange
            AppServiceAuthenticationOptions options = CreateTestOptions(new HttpConfiguration());
            var mock    = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken(issuer: "https://invalidIssuer/"));

            // Act
            AuthenticationTicket authticket = mock.Authenticate(request, options);

            // Assert
            Assert.NotNull(authticket);
            Assert.NotNull(authticket.Identity);
            Assert.False(authticket.Identity.IsAuthenticated, "Expected Authenticate to fail with invalid issuer");
        }
Exemplo n.º 3
0
        public void Authenticate_FailsToAuthenticate_ValidIdentity_WithoutSigningKey()
        {
            // Arrange
            AppServiceAuthenticationOptions options = CreateTestOptions(new HttpConfiguration());

            var mock    = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken());

            options.SigningKey = null;

            // Act
            AuthenticationTicket authticket = mock.Authenticate(request, options);

            // Assert
            Assert.NotNull(authticket);
            Assert.NotNull(authticket.Identity);
            Assert.False(authticket.Identity.IsAuthenticated, "Expected Authenticate to fail without signing key specified in MobileAppAuthenticationOptions");
        }
        public void Authenticate_LeavesUserNull_IfException()
        {
            // Arrange
            var mockTokenHandler = new Mock <MobileAppTokenHandler>(this.config);

            mockTokenHandler.CallBase = true;
            mockTokenHandler
            .Setup(t => t.CreateServiceUser(It.IsAny <ClaimsIdentity>(), It.IsAny <string>()))
            .Throws(new InvalidOperationException())
            .Verifiable();
            var mock    = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object, mockTokenHandler.Object);
            var request = CreateAuthRequest("signing_key");

            request.User = new ClaimsPrincipal();

            // Act
            mock.Authenticate(request, CreateOptions(false, "signing_key"));

            // Assert
            mockTokenHandler.VerifyAll();
            Assert.Null(request.User);
        }
        public void Authenticate_CorrectlyAuthenticates(MobileAppAuthenticationOptions options, bool expectAuthenticated)
        {
            // Arrange
            var mock    = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object, this.tokenHandler);
            var request = CreateAuthRequest("signing_key");

            request.User = new ClaimsPrincipal();

            // Act
            mock.Authenticate(request, options);

            // Assert
            if (expectAuthenticated)
            {
                Assert.NotNull(request.User.Identity);
                Assert.True(request.User.Identity.IsAuthenticated);
                Assert.IsType(typeof(MobileAppUser), request.User);
            }
            else
            {
                Assert.Null(request.User);
            }
        }
        public void Authenticate_CorrectlyAuthenticates(string otherSigningKey, bool expectAuthenticated)
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            AppServiceAuthenticationOptions optionsDefault = CreateTestOptions(config);
            optionsDefault.SigningKey = SigningKeyAlpha;

            AppServiceAuthenticationOptions optionsOtherSigningKey = CreateTestOptions(config);
            optionsOtherSigningKey.SigningKey = otherSigningKey;

            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken());

            // Act
            AuthenticationTicket authTicket = mock.Authenticate(request, optionsOtherSigningKey);

            // Assert
            if (expectAuthenticated)
            {
                // ensure the AuthenticationTicket is set correctly
                Assert.NotNull(authTicket);
                Assert.NotNull(authTicket.Identity);
                Assert.True(authTicket.Identity.IsAuthenticated);
            }
            else
            {
                Assert.NotNull(authTicket);
                Assert.NotNull(authTicket.Identity);
                Assert.False(authTicket.Identity.IsAuthenticated);
            }
        }
        public void Authenticate_Fails_WithInvalidIssuer()
        {
            // Arrange
            AppServiceAuthenticationOptions options = CreateTestOptions(new HttpConfiguration());
            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken(issuer: "https://invalidIssuer/"));

            // Act
            AuthenticationTicket authticket = mock.Authenticate(request, options);

            // Assert
            Assert.NotNull(authticket);
            Assert.NotNull(authticket.Identity);
            Assert.False(authticket.Identity.IsAuthenticated, "Expected Authenticate to fail with invalid issuer");
        }
        public void Authenticate_FailsToAuthenticate_ValidIdentity_WithoutSigningKey()
        {
            // Arrange
            AppServiceAuthenticationOptions options = CreateTestOptions(new HttpConfiguration());

            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken());

            options.SigningKey = null;

            // Act
            AuthenticationTicket authticket = mock.Authenticate(request, options);

            // Assert
            Assert.NotNull(authticket);
            Assert.NotNull(authticket.Identity);
            Assert.False(authticket.Identity.IsAuthenticated, "Expected Authenticate to fail without signing key specified in MobileAppAuthenticationOptions");
        }
        public void Authenticate_Fails_WithInvalidAudience()
        {
            // Arrange
            MobileAppAuthenticationOptions options = CreateTestOptions();
            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object, this.tokenHandler);
            var request = CreateAuthRequest(new Uri(TestWebsiteUrl), GetTestToken(audience: "https://invalidAudience/"));

            // Act
            AuthenticationTicket authticket = mock.Authenticate(request, options);

            // Assert            
            Assert.NotNull(authticket);
            Assert.NotNull(authticket.Identity);
            Assert.False(authticket.Identity.IsAuthenticated, "Expected Authenticate to fail with invalid audience");
        }
        public void Authenticate_LeavesUserNull_IfException()
        {
            // Arrange
            var mockTokenHandler = new Mock<MobileAppTokenHandler>(this.config);
            mockTokenHandler.CallBase = true;
            mockTokenHandler
                .Setup(t => t.CreateServiceUser(It.IsAny<ClaimsIdentity>(), It.IsAny<string>()))
                .Throws(new InvalidOperationException())
                .Verifiable();
            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object, mockTokenHandler.Object);
            var request = CreateAuthRequest("signing_key");
            request.User = new ClaimsPrincipal();

            // Act
            mock.Authenticate(request, CreateOptions(false, "signing_key"));

            // Assert            
            mockTokenHandler.VerifyAll();
            Assert.Null(request.User);
        }
        public void Authenticate_CorrectlyAuthenticates(MobileAppAuthenticationOptions options, bool expectAuthenticated)
        {
            // Arrange
            var mock = new MobileAppAuthenticationHandlerMock(this.loggerMock.Object, this.tokenHandler);
            var request = CreateAuthRequest("signing_key");
            request.User = new ClaimsPrincipal();

            // Act
            mock.Authenticate(request, options);

            // Assert            
            if (expectAuthenticated)
            {
                Assert.NotNull(request.User.Identity);
                Assert.True(request.User.Identity.IsAuthenticated);
                Assert.IsType(typeof(MobileAppUser), request.User);
            }
            else
            {
                Assert.Null(request.User);
            }
        }