示例#1
1
        public void ValidateTokens_Success_AuthenticatedUserWithUsername()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            var identity = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken = sessionToken.SecurityToken,
                Username = "******",
                IsSessionToken = false,
                AdditionalData = "some-additional-data"
            };

            var mockAdditionalDataProvider = new Mock<IAntiForgeryAdditionalDataProvider>();
            mockAdditionalDataProvider.Setup(o => o.ValidateAdditionalData(httpContext, "some-additional-data"))
                                      .Returns(true);

            var config = new AntiForgeryOptions();
            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: new Mock<IClaimUidExtractor>().Object,
                additionalDataProvider: mockAdditionalDataProvider.Object);

            // Act
            tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken);

            // Assert
            // Nothing to assert - if we got this far, success!
        }
示例#2
0
        public void ValidateTokens_Success_ClaimsBasedUser()
        {
            // Arrange
            var httpContext  = new Mock <HttpContext>().Object;
            var identity     = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken  = sessionToken.SecurityToken,
                IsSessionToken = false,
                ClaimUid       = new BinaryBlob(256)
            };

            var mockClaimUidExtractor = new Mock <IClaimUidExtractor>();

            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
            .Returns(Convert.ToBase64String(fieldtoken.ClaimUid.GetData()));

            var config = new AntiForgeryOptions();

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act
            tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken);

            // Assert
            // Nothing to assert - if we got this far, success!
        }
示例#3
0
        public void ValidateTokens_AdditionalDataRejected()
        {
            // Arrange
            var httpContext  = new Mock <HttpContext>().Object;
            var identity     = new ClaimsIdentity();
            var sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken  = sessionToken.SecurityToken,
                Username       = String.Empty,
                IsSessionToken = false,
                AdditionalData = "some-additional-data"
            };

            var mockAdditionalDataProvider = new Mock <IAntiForgeryAdditionalDataProvider>();

            mockAdditionalDataProvider.Setup(o => o.ValidateAdditionalData(httpContext, "some-additional-data"))
            .Returns(false);

            var config        = new AntiForgeryOptions();
            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: mockAdditionalDataProvider.Object);

            // Act & assert
            var ex =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));

            Assert.Equal(@"The provided anti-forgery token failed a custom data check.", ex.Message);
        }
示例#4
0
        public void ValidateTokens_Success_AuthenticatedUserWithUsername()
        {
            // Arrange
            var httpContext  = new Mock <HttpContext>().Object;
            var identity     = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken  = sessionToken.SecurityToken,
                Username       = "******",
                IsSessionToken = false,
                AdditionalData = "some-additional-data"
            };

            var mockAdditionalDataProvider = new Mock <IAntiForgeryAdditionalDataProvider>();

            mockAdditionalDataProvider.Setup(o => o.ValidateAdditionalData(httpContext, "some-additional-data"))
            .Returns(true);

            var config        = new AntiForgeryOptions();
            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: new Mock <IClaimUidExtractor>().Object,
                additionalDataProvider: mockAdditionalDataProvider.Object);

            // Act
            tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken);

            // Assert
            // Nothing to assert - if we got this far, success!
        }
示例#5
0
        public void ValidateTokens_UsernameMismatch(string identityUsername, string embeddedUsername)
        {
            // Arrange
            var httpContext  = new Mock <HttpContext>().Object;
            var identity     = GetAuthenticatedIdentity(identityUsername);
            var sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken  = sessionToken.SecurityToken,
                Username       = embeddedUsername,
                IsSessionToken = false
            };

            var mockClaimUidExtractor = new Mock <IClaimUidExtractor>();

            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
            .Returns((string)null);

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));

            Assert.Equal(
                @"The provided anti-forgery token was meant for user """ + embeddedUsername +
                @""", but the current user is """ + identityUsername + @""".", ex.Message);
        }
示例#6
0
        public void ValidateTokens_FieldAndSessionTokensHaveDifferentSecurityKeys()
        {
            // Arrange
            var            httpContext  = new Mock <HttpContext>().Object;
            ClaimsIdentity identity     = new Mock <ClaimsIdentity>().Object;
            var            sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                IsSessionToken = false
            };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));

            Assert.Equal(@"The anti-forgery cookie token and form field token do not match.", ex.Message);
        }
示例#7
0
        public void ValidateTokens_FieldTokenMissing()
        {
            // Arrange
            var            httpContext  = new Mock <HttpContext>().Object;
            ClaimsIdentity identity     = new Mock <ClaimsIdentity>().Object;
            var            sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            AntiForgeryToken fieldtoken = null;

            var config = new AntiForgeryOptions()
            {
                FormFieldName = "my-form-field-name"
            };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));

            Assert.Equal(@"The required anti-forgery form field ""my-form-field-name"" is not present.", ex.Message);
        }
示例#8
0
        public void ValidateTokens_FieldAndSessionTokensSwapped()
        {
            // Arrange
            var            httpContext  = new Mock <HttpContext>().Object;
            ClaimsIdentity identity     = new Mock <ClaimsIdentity>().Object;
            var            sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                IsSessionToken = false
            };

            var config = new AntiForgeryOptions()
            {
                CookieName    = "my-cookie-name",
                FormFieldName = "my-form-field-name"
            };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex1 =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, fieldtoken, fieldtoken));

            Assert.Equal(
                "Validation of the provided anti-forgery token failed. " +
                @"The cookie ""my-cookie-name"" and the form field ""my-form-field-name"" were swapped.",
                ex1.Message);

            var ex2 =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, sessionToken));

            Assert.Equal(
                "Validation of the provided anti-forgery token failed. " +
                @"The cookie ""my-cookie-name"" and the form field ""my-form-field-name"" were swapped.",
                ex2.Message);
        }
示例#9
0
        public void ValidateTokens_ClaimUidMismatch()
        {
            // Arrange
            var httpContext  = new Mock <HttpContext>().Object;
            var identity     = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken()
            {
                IsSessionToken = true
            };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken  = sessionToken.SecurityToken,
                IsSessionToken = false,
                ClaimUid       = new BinaryBlob(256)
            };

            var differentToken        = new BinaryBlob(256);
            var mockClaimUidExtractor = new Mock <IClaimUidExtractor>();

            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
            .Returns(Convert.ToBase64String(differentToken.GetData()));

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws <InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));

            Assert.Equal(
                @"The provided anti-forgery token was meant for a different claims-based user than the current user.",
                ex.Message);
        }
示例#10
0
        public void ValidateTokens_Success_ClaimsBasedUser()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            var identity = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken = sessionToken.SecurityToken,
                IsSessionToken = false,
                ClaimUid = new BinaryBlob(256)
            };

            var mockClaimUidExtractor = new Mock<IClaimUidExtractor>();
            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
                                 .Returns(Convert.ToBase64String(fieldtoken.ClaimUid.GetData()));

            var config = new AntiForgeryOptions();

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act
            tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken);

            // Assert
            // Nothing to assert - if we got this far, success!
        }
示例#11
0
        public void ValidateTokens_AdditionalDataRejected()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            var identity = new ClaimsIdentity();
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken = sessionToken.SecurityToken,
                Username = String.Empty,
                IsSessionToken = false,
                AdditionalData = "some-additional-data"
            };

            var mockAdditionalDataProvider = new Mock<IAntiForgeryAdditionalDataProvider>();
            mockAdditionalDataProvider.Setup(o => o.ValidateAdditionalData(httpContext, "some-additional-data"))
                                      .Returns(false);

            var config = new AntiForgeryOptions();
            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: mockAdditionalDataProvider.Object);

            // Act & assert
            var ex =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));
            Assert.Equal(@"The provided anti-forgery token failed a custom data check.", ex.Message);
        }
示例#12
0
        public void ValidateTokens_ClaimUidMismatch()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            var identity = GetAuthenticatedIdentity("the-user");
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken = sessionToken.SecurityToken,
                IsSessionToken = false,
                ClaimUid = new BinaryBlob(256)
            };

            var differentToken = new BinaryBlob(256);
            var mockClaimUidExtractor = new Mock<IClaimUidExtractor>();
            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
                                 .Returns(Convert.ToBase64String(differentToken.GetData()));

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));
            Assert.Equal(
                @"The provided anti-forgery token was meant for a different claims-based user than the current user.",
                ex.Message);
        }
示例#13
0
        public void ValidateTokens_UsernameMismatch(string identityUsername, string embeddedUsername)
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            var identity = GetAuthenticatedIdentity(identityUsername);
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken()
            {
                SecurityToken = sessionToken.SecurityToken,
                Username = embeddedUsername,
                IsSessionToken = false
            };

            var mockClaimUidExtractor = new Mock<IClaimUidExtractor>();
            mockClaimUidExtractor.Setup(o => o.ExtractClaimUid(identity))
                                 .Returns((string)null);

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: mockClaimUidExtractor.Object,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));
            Assert.Equal(
                @"The provided anti-forgery token was meant for user """ + embeddedUsername +
                @""", but the current user is """ + identityUsername + @""".", ex.Message);
        }
示例#14
0
        public void ValidateTokens_FieldAndSessionTokensHaveDifferentSecurityKeys()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            ClaimsIdentity identity = new Mock<ClaimsIdentity>().Object;
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken() { IsSessionToken = false };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: null,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));
            Assert.Equal(@"The anti-forgery cookie token and form field token do not match.", ex.Message);
        }
示例#15
0
        public void ValidateTokens_FieldAndSessionTokensSwapped()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            ClaimsIdentity identity = new Mock<ClaimsIdentity>().Object;
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            var fieldtoken = new AntiForgeryToken() { IsSessionToken = false };

            var config = new AntiForgeryOptions()
            {
                CookieName = "my-cookie-name",
                FormFieldName = "my-form-field-name"
            };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex1 =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, fieldtoken, fieldtoken));
            Assert.Equal(
                "Validation of the provided anti-forgery token failed. " +
                @"The cookie ""my-cookie-name"" and the form field ""my-form-field-name"" were swapped.",
                ex1.Message);

            var ex2 =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, sessionToken));
            Assert.Equal(
                "Validation of the provided anti-forgery token failed. " +
                @"The cookie ""my-cookie-name"" and the form field ""my-form-field-name"" were swapped.",
                ex2.Message);
        }
示例#16
0
        public void ValidateTokens_FieldTokenMissing()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>().Object;
            ClaimsIdentity identity = new Mock<ClaimsIdentity>().Object;
            var sessionToken = new AntiForgeryToken() { IsSessionToken = true };
            AntiForgeryToken fieldtoken = null;

            var config = new AntiForgeryOptions()
            {
                FormFieldName = "my-form-field-name"
            };

            var tokenProvider = new AntiForgeryTokenProvider(
                config: config,
                claimUidExtractor: null,
                additionalDataProvider: null);

            // Act & assert
            var ex =
                Assert.Throws<InvalidOperationException>(
                    () => tokenProvider.ValidateTokens(httpContext, identity, sessionToken, fieldtoken));
            Assert.Equal(@"The required anti-forgery form field ""my-form-field-name"" is not present.", ex.Message);
        }