/// <exception cref="InvalidEmailFormatException"></exception>
        /// <exception cref="UserAlreadyExistsException"></exception>
        /// <exception cref="InvalidNameFormatException"></exception>
        /// <exception cref="WeakPasswordException"></exception>
        public async Task <UserModel> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
        {
            // Check is email valid
            if (!AuthUtils.ValidateEmail(request.Email))
            {
                throw new InvalidEmailFormatException(request.Email);
            }

            // Check is user already exists
            var isExists = await _usersRepository.IsUserIdentityExists(request.Email);

            if (isExists)
            {
                throw new UserAlreadyExistsException(request.Email);
            }

            // Check name
            if (!AuthUtils.ValidateName(request.Name))
            {
                throw new InvalidNameFormatException(request.Name);
            }

            // Check password stronger
            if (!AuthUtils.CheckPasswordComplexity(request.Password))
            {
                throw new WeakPasswordException();
            }

            // Generate password hash
            var passwordHash = AuthUtils.GetMd5Hash(request.Password);

            // User registration instant
            var registrationInstant = Clock.GetCurrentInstant();

            var user = new UserIdentityModel(
                Guid.NewGuid(),
                request.Email,
                request.Name,
                "user",
                passwordHash,
                registrationInstant
                );

            // Register user
            var registrationResult = await _usersRepository.RegisterUser(user);

            return(registrationResult);
        }
Exemple #2
0
        public void CheckPasswordComplexityMethod_StrongPasswords_ReturnsTrue()
        {
            var strongPasswords = new List <string>()
            {
                "7hGob5~y2l@Q",
                "?b%WVEvh6c8*sr",
                "JR2hkaVQMVmLFB2m",
                "ImOb0Iz74krSM",
                "0MwV9RBZLH#|A}%iqyTacvYkKFrXQYqdV8kx1ufa",
                "PwVivpX8DLUmC5e7agqbIA1lHOxQrMuhenLsJetF"
            };

            foreach (var password in strongPasswords)
            {
                Assert.IsTrue(AuthUtils.CheckPasswordComplexity(password));
            }
        }
Exemple #3
0
        public void CheckPasswordComplexityMethod_WeakPasswords_ReturnsFalse()
        {
            var weakPasswords = new List <string>()
            {
                "",
                "820646",
                "837949205512",
                "#15$@2",
                "}0231@2*8|93",
                "mmxhfu",
                "TFEDEG",
                "taraomqcnppa",
                "TXBURONYUTOW",
                "m@@cyqk?zw~w",
                "RZ*I}#T{XZHQ"
            };

            foreach (var password in weakPasswords)
            {
                Assert.IsFalse(AuthUtils.CheckPasswordComplexity(password));
            }
        }