public ValidationInfo ValidateFinishReg(User userForUpdate, RegCredentials regCredentials)
        {
            var errorMessages = new List <ErrorMessage>();

            if (userForUpdate.RegistrationToken == null)
            {
                errorMessages.Add(Errors.UserAlreadyRegistered());
            }

            if (userForUpdate.RegistrationToken != regCredentials.RegistrationToken)
            {
                errorMessages.Add(Errors.InvalidRegistrationToken());
            }

            if (userForUpdate.RegistrationToken != null && HashingUtil.IsTokenExpired(userForUpdate.RegistrationToken, hoursToExpire: 336))
            {
                errorMessages.Add(Errors.ExpiredRegistrationToken());
            }

            return(new ValidationInfo(errorMessages));
        }
Example #2
0
        /// <summary>
        /// Finalizes registration for user with provided email.
        /// Will perform validations and test if user is not already registered,
        /// if provided token is not expired and if user exists at all
        /// </summary>
        /// <param name="regCredentials">Credentials used to finish registration, also used for validation</param>
        /// <returns>Updated user entity after successfulregistration</returns>
        /// <exception cref="ValidationFailedException">When user registration fails described validations</exception>
        /// <exception cref="EntityNotFoundException">When user with provided email was not found</exception>
        public User FinishRegistration(RegCredentials regCredentials)
        {
            using (var transaction = new TransactionScope())
            {
                var userToUpdate = usersDao.SelectByRegToken(regCredentials.RegistrationToken);
                if (userToUpdate == null)
                {
                    throw new EntityNotFoundException($"User with token {regCredentials.RegistrationToken} was not found", typeof(User));
                }
                var validationInfo = userValidator.ValidateFinishReg(userToUpdate, regCredentials);
                if (!validationInfo.IsValid)
                {
                    throw new ValidationFailedException(validationInfo);
                }

                usersDao.UpdatePasswordClearToken(HashingUtil.HashPasswordWithSalt(regCredentials.PlainPassword), userToUpdate.UserId);
                transaction.Complete();

                userToUpdate.RegistrationToken = null;
                return(userToUpdate);
            }
        }
Example #3
0
 public IHttpActionResult FinishRegistration([FromBody] RegCredentials regCredentials)
 {
     return(Ok(userLogic.FinishRegistration(regCredentials)));
 }