コード例 #1
0
        public void StoreExceptionTest()
        {
            var dataConnection = MockRepository.GenerateStub<IDataConnection>();
            var dateHelper = new DateHelper();

            var tokenStorage = new TokenStorage(dataConnection, dateHelper);

            var userId = 1;
            var userIdSalt = new byte[24];
            var tokenSalt = new byte[24];
            var encryptedGuid = "encrypted-guid";
            var encryptedUserId = "dummy-encrypted-user-id";
            var issuedAt = new DateTime(2014, 11, 21, 14, 54, 33);

            var hashedToken = new PasswordHash.HashInfo
            {
                Hash = encryptedGuid,
                Iterations = 1000,
                Method = "sha1",
                Salt = Convert.ToBase64String(tokenSalt)
            };

            // stub fake calls
            dataConnection.Stub(x => x.Execute(null, null)).Throw(new Exception()).IgnoreArguments();

            Assert.Throws<Exception>(() => tokenStorage.StoreToken(userId, hashedToken, encryptedUserId, userIdSalt, issuedAt));
        }
コード例 #2
0
        public void StoreAndRetrieveTokenTest()
        {
            TestHelper.DeleteTestData();
            TestHelper.InsertTestData();

            var dataConnection = new DataConnection();
            var dateHelper = new DateHelper();

            var tokenStorage = new TokenStorage(dataConnection, dateHelper);

            var userId = 1;
            var userIdSalt = new byte[24];
            var tokenSalt = new byte[24];
            var encryptedGuid = "encrypted-guid";
            var encryptedUserId = "dummy-encrypted-user-id";
            var issuedAt = new DateTime(2014, 11, 21, 14, 54, 33);

            
            var hashedToken = new PasswordHash.HashInfo
            {
                Hash = encryptedGuid,
                Iterations = 1000,
                Method = "sha1",
                Salt = Convert.ToBase64String(tokenSalt)
            };

            Assert.IsTrue(tokenStorage.StoreToken(userId, hashedToken, encryptedUserId, userIdSalt, issuedAt));
            
            PasswordHash.HashInfo hashedTokenResult;
            string encryptedUserIdResult;
            byte[] userIdSaltResult;
            DateTime issuedAtResult;
            DateTime refreshedResult;
            var retrieveTokenSuccess = tokenStorage.RetrieveToken(encryptedUserId, out hashedTokenResult, out encryptedUserIdResult, out userIdSaltResult, out issuedAtResult, out refreshedResult);

            Assert.IsTrue(retrieveTokenSuccess);
            Assert.AreEqual(hashedToken.Salt, hashedTokenResult.Salt);
            Assert.AreEqual(hashedToken.Hash, hashedTokenResult.Hash);
            Assert.AreEqual(hashedToken.Method, hashedTokenResult.Method);
            Assert.AreEqual(encryptedUserId, encryptedUserIdResult);
            Assert.AreEqual(userIdSalt, userIdSaltResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 14, 54, 33), issuedAtResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 14, 54, 33), refreshedResult);            
           
        }
コード例 #3
0
        public void VerifyTokenExistenceTest()
        {
            TestHelper.DeleteTestData();
            TestHelper.InsertTestData();

            //VerifyTokenExistence
            var dataConnection = new DataConnection();
            var dateHelper = new DateHelper();

            var tokenStorage = new TokenStorage(dataConnection, dateHelper);

            var userId = 101;
            var userIdSalt = new byte[24];
            var tokenSalt = new byte[24];
            var encryptedGuid = "encrypted-guid";
            var encryptedUserId = "dummy-encrypted-user-id";
            var issuedAt = new DateTime(2014, 11, 21, 14, 54, 33);
            
            var hashedToken = new PasswordHash.HashInfo
            {
                Hash = encryptedGuid,
                Iterations = 1000,
                Method = "sha1",
                Salt = Convert.ToBase64String(tokenSalt)
            };

            Assert.IsTrue(tokenStorage.StoreToken(userId, hashedToken, encryptedUserId, userIdSalt, issuedAt));

            string hashedTokenResult;
            DateTime issuedAtResult;
            DateTime refreshedResult;
            var verifyTokenSuccess = tokenStorage.VerifyTokenExistence(userId, out hashedTokenResult, out issuedAtResult, out refreshedResult);

            Assert.IsTrue(verifyTokenSuccess);
            Assert.AreEqual(encryptedGuid, hashedTokenResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 14, 54, 33), issuedAtResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 14, 54, 33), refreshedResult);

            verifyTokenSuccess = tokenStorage.VerifyTokenExistence(102, out hashedTokenResult, out issuedAtResult, out refreshedResult);

            Assert.IsFalse(verifyTokenSuccess);
            Assert.AreEqual(null, hashedTokenResult);
            Assert.AreEqual(new DateTime(), issuedAtResult);
            Assert.AreEqual(new DateTime(), refreshedResult);
        }
コード例 #4
0
        public void RefreshTokenTest()
        {
            var dataConnection = new DataConnection();
            var dateHelper = new DateHelper();

            var tokenStorage = new TokenStorage(dataConnection, dateHelper);

            var userId = 101;
            var userIdSalt = new byte[24];
            var tokenSalt = new byte[24];
            var encryptedGuid = "encrypted-guid";
            var encryptedUserId = "dummy-encrypted-user-id";
            var issuedAt = new DateTime(2014, 11, 21, 14, 54, 33);
            
            var hashedToken = new PasswordHash.HashInfo
            {
                Hash = encryptedGuid,
                Iterations = 1000,
                Method = "sha1",
                Salt = Convert.ToBase64String(tokenSalt)
            };

            Assert.IsTrue(tokenStorage.StoreToken(userId, hashedToken, encryptedUserId, userIdSalt, issuedAt));

            Assert.IsTrue(tokenStorage.RefreshToken(userId, hashedToken.Hash, new DateTime(2014, 11, 21, 15, 55, 22)));

            PasswordHash.HashInfo hashedTokenResult;
            string encryptedUserIdResult;
            byte[] userIdSaltResult;
            DateTime issuedAtResult;
            DateTime refreshedResult;
            var retrieveTokenSuccess = tokenStorage.RetrieveToken(encryptedUserId, out hashedTokenResult, out encryptedUserIdResult, out userIdSaltResult, out issuedAtResult, out refreshedResult);

            Assert.IsTrue(retrieveTokenSuccess);
            Assert.AreEqual(hashedToken.Salt, hashedTokenResult.Salt);
            Assert.AreEqual(hashedToken.Hash, hashedTokenResult.Hash);
            Assert.AreEqual(hashedToken.Method, hashedTokenResult.Method);
            Assert.AreEqual(encryptedUserId, encryptedUserIdResult);
            Assert.AreEqual(userIdSalt, userIdSaltResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 14, 54, 33), issuedAtResult);
            Assert.AreEqual(new DateTime(2014, 11, 21, 15, 55, 22), refreshedResult);

            // check if using a wrong id/encryptid fails (it should fail)
            Assert.IsFalse(tokenStorage.RefreshToken(102, hashedToken.Hash, new DateTime(2014, 11, 21, 15, 55, 22)));
            Assert.IsFalse(tokenStorage.RefreshToken(userId, hashedToken.Hash + 'a', new DateTime(2014, 11, 21, 15, 55, 22)));
        }
コード例 #5
0
        public void RetrieveTokenExceptionTest()
        {
            // declare mocks
            var dataConnection = MockRepository.GenerateStub<IDataConnection>();
            
            // declare objects
            var dateHelper = new DateHelper();
            var encryptedUserId = "dummy-encrypted-user-id";
            var tokenStorage = new TokenStorage(dataConnection, dateHelper);
            
            
            // stub fake calls
            dataConnection.Stub(x => x.Query<TokenStorage.TokenStorageDb>(null, null)).Throw(new Exception()).IgnoreArguments();

            PasswordHash.HashInfo hashedTokenResult;
            string encryptedUserIdResult;
            byte[] userIdSaltResult;
            DateTime issuedAtResult;
            DateTime refreshedResult;

            var result = tokenStorage.RetrieveToken(encryptedUserId, out hashedTokenResult, out encryptedUserIdResult, out userIdSaltResult, out issuedAtResult, out refreshedResult);
            
            Assert.IsFalse(result);            
        }