Esempio n. 1
0
        public void Test_password_length()
        {
            // Arrange

            // Act
            string password = otpGenerator.Create();

            // Assert
            Assert.IsNotNull(password);
            Assert.IsTrue(password.Length == 24);
        }
Esempio n. 2
0
        public void Test_check_otp_ok_password_and_time()
        {
            // Arrange
            string test_user = "******";
            string test_pass = otpGenerator.Create();

            CleanTables();
            CreateOtp(CreateUser(test_user), test_pass, DateTime.Now);
            application.ResetRules();

            // Act
            Sleep(5000);
            bool check   = application.CheckOtp(test_user, test_pass);
            var  koRules = application.ApplicationRules.Where(r => !r.Result);

            // Assert
            Assert.IsTrue(check);
            Assert.IsTrue(koRules.Count() == 0);
        }
Esempio n. 3
0
 public string CreateOtp(string userId)
 {
     try
     {
         // datetime to check against
         DateTime now         = DateTime.Now;
         string   newPassword = _otpGenerator.Create();
         // Verify rules on userId
         // if ok generates password else null
         Func <IRepository <User>, bool> ok = repo =>
         {
             Func <User, bool> duplicate = u =>
             {
                 bool checkUserId  = u?.UserId.ToLower() == userId.ToLower();
                 bool checkExpired = checkUserId && ((TimeSpan)(now - u.Otp?.StartDate)).TotalMilliseconds <= Settings.Default.ValidityMsec;
                 return(checkExpired);
             };
             ApplicationRule existentRule    = new ApplicationRule(this, repo.SingleOrDefault(u => duplicate(u)) == null, ReasonEnum.ElementDuplication);
             ApplicationRule validationRule1 = new ApplicationRule(this, userId.Length <= Settings.Default.UserIdLength, ReasonEnum.UserIdLength);
             ApplicationRule validationRule2 = new ApplicationRule(this, !string.IsNullOrEmpty(userId) && !string.IsNullOrWhiteSpace(userId), ReasonEnum.ElementValidation);
             ApplicationRule validationRule3 = new ApplicationRule(this, !userId.Contains(' '), ReasonEnum.ElementValidation);
             return(existentRule & validationRule1 & validationRule2 & validationRule3);
         };
         if (ok(_userRepo))
         {
             // check if element exists and update or insert
             Func <User, bool> expiredDuplicate = u =>
             {
                 bool checkUserId  = u.UserId.ToLower() == userId.ToLower();
                 bool checkExpired = checkUserId && ((TimeSpan)(now - u.Otp?.StartDate)).TotalMilliseconds > Settings.Default.ValidityMsec;
                 return(checkExpired);
             };
             User user = _userRepo.SingleOrDefault(u => expiredDuplicate(u));
             if (user != null)
             {
                 user.Otp.Password  = newPassword;
                 user.Otp.StartDate = now;
             }
             else
             {
                 Otp newOtp = new Otp {
                     Password = newPassword, StartDate = now
                 };
                 User newUser = _userRepo.Create();
                 newUser.UserId = userId;
                 newUser.Otp    = newOtp;
                 _userRepo.Add(newUser);
             }
             _userRepo.SaveChanges();
             return(newPassword);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception ex)
     {
         _logger.LogError(ex);
         new ApplicationRule(this, false, ReasonEnum.Error);
         return(null);
     }
 }