예제 #1
0
        public static ICollection <User> Users()
        {
            var johnSmith = MappingService.Map <User>(new UserDto
            {
                Key   = new Guid("636f1f93-2585-4edc-bbfa-8e5de3778afc"),
                Email = new EmailDto
                {
                    Address = "*****@*****.**"
                },
                HashedPassword = CryptoService.CreateHash("123"),
                IsAdmin        = false,
                Mobile         = new PhoneNumberDto
                {
                    CountryCode             = 61,
                    LocalNumberWithAreaCode = "0421116066",
                    Verified = true
                },
                PersonName = new PersonNameDto {
                    FamilyName = "Smith", GivenName = "John"
                },
                Username  = "******",
                Nickname  = "john_s",
                UserState = UserState.Active
            });

            var mariaGarcia = MappingService.Map <User>(new UserDto
            {
                Key   = new Guid("5a9731cd-3e38-478a-befa-73ab6b45b07e"),
                Email = new EmailDto
                {
                    Address = "*****@*****.**"
                },
                HashedPassword = CryptoService.CreateHash("123"),
                IsAdmin        = false,
                Mobile         = new PhoneNumberDto
                {
                    CountryCode             = 61,
                    LocalNumberWithAreaCode = "0433058893",
                    Verified = true
                },
                PersonName = new PersonNameDto {
                    FamilyName = "Garcia", GivenName = "Maria"
                },
                Username  = "******",
                Nickname  = "maria",
                UserState = UserState.Active
            });

            return(new List <User>
            {
                johnSmith,
                mariaGarcia
            });
        }
예제 #2
0
        public void Register(RegisterUserRequest register)
        {
            using (var database = DatabaseFactory.GetDatabase())
            {
                // Let's do this in a transaction, so we cannot register two users
                // with the same name. Seems to be a useful requirement.
                using (var tran = database.GetTransaction())
                {
                    string hashBase64;
                    string saltBase64;

                    cryptoService.CreateHash(register.Password, out hashBase64, out saltBase64);

                    User user = new User()
                    {
                        Name         = register.UserName,
                        PasswordHash = hashBase64,
                        PasswordSalt = saltBase64
                    };

                    database.Insert(user);

                    tran.Complete();
                }
            }
        }
        public static void CreateHash(this ICryptoService cryptoService, string data, out string hash, out string salt)
        {
            var dataBytes = Encoding.UTF8.GetBytes(data);

            cryptoService.CreateHash(dataBytes, out byte[] hashBytes, out byte[] saltBytes);

            hash = Convert.ToBase64String(hashBytes);
            salt = Convert.ToBase64String(saltBytes);
        }
예제 #4
0
        public void Register(RegisterUserRequest register)
        {
            // Let's do this in a transaction, so we cannot register two users
            // with the same name. Seems to be a useful requirement.
            string hashBase64;
            string saltBase64;

            cryptoService.CreateHash(register.Password, out hashBase64, out saltBase64);

            CHXUser user = new CHXUser()
            {
                Name         = register.UserName,
                PasswordHash = hashBase64,
                PasswordSalt = saltBase64
            };
        }
예제 #5
0
        public VerificationCodeCreatedEvent CreatePasswordResetVerificationCode(
            CreatePasswordResetVerificationCodeRequest request)
        {
            var expiry                 = DateTimeOffset.UtcNow.AddMinutes(5);
            var verificationCode       = new ShortGuid(Guid.NewGuid());
            var hashedVerificationCode = _cryptoService.CreateHash(verificationCode);
            var userDto                = _userUowFactory.SetVerificationCode(request.Email, VerificationPurpose.ResetPassword,
                                                                             hashedVerificationCode, expiry);
            var verificationCodeEvent = new VerificationCodeCreatedEvent
            {
                UserKey   = userDto.Key,
                Recipient = new EmailParticipant
                {
                    EmailAddress = userDto.Email.Address,
                    Name         = userDto.PersonName.FullName()
                },
                VerificationPurpose       = VerificationPurpose.ResetPassword,
                PlainTextVerificationCode = verificationCode,
                ExpirationTime            = expiry
            }.LinkTo(request);

            return(verificationCodeEvent);
        }
예제 #6
0
        public ProcessResult ResetPassword(Guid userKey, VerificationPurpose verificationPurpose,
                                           string plainTextVerificationCode, string newPlainTextPassword)
        {
            var verificationSucceeds = false;
            var currentTime          = DateTimeOffset.UtcNow;
            var result = ExecuteWithProcessResult(uow =>
            {
                var user = uow.Store.UpdatePropertiesOnly <User>(
                    x => x.Key == userKey && x.VerificationPurpose.Name == verificationPurpose.Name,
                    x =>
                {
                    verificationSucceeds =
                        _cryptoService.Validate(plainTextVerificationCode, x.HashedVerificationCode) &&
                        x.TimeVerificationCodeExpires.HasValue &&
                        x.TimeVerificationCodeExpires.Value > currentTime;

                    if (verificationSucceeds)
                    {
                        var hashedPassword = _cryptoService.CreateHash(newPlainTextPassword);
                        x.SetHashedPassword(hashedPassword);
                    }

                    x.ResetVerificationCode();
                });
                if (user == null)
                {
                    throw new KeyNotFoundException($"User {userKey} is not found.");
                }

                if (!verificationSucceeds)
                {
                    throw new Exception("Password reset failed.");
                }
            });

            return(result);
        }
예제 #7
0
        public void Register(RegisterUserRequest register)
        {
            using (var database = DatabaseFactory.GetDatabase())
            {
                using (var tran = database.GetTransaction())
                {
                    string hashBase64;
                    string saltBase64;

                    cryptoService.CreateHash(register.Password, out hashBase64, out saltBase64);

                    User user = new User()
                    {
                        Name         = register.UserName,
                        PasswordHash = hashBase64,
                        PasswordSalt = saltBase64
                    };

                    database.Insert(user);

                    tran.Complete();
                }
            }
        }