Exemplo n.º 1
0
        private static LoginViewModel CreateSut(
            IHudUtility hudUtility                   = null,
            IAlertUtility alertUtility               = null,
            ICredentialStorage credentialStorage     = null,
            ICredentialValidator credentialValidator = null,
            IBackgroundSyncUtility backgroundSync    = null
            )
        {
            if (credentialValidator == null)
            {
                credentialValidator = Substitute.For <ICredentialValidator>();

                credentialValidator.ValidateAsync(Arg.Any <Credentials>())
                .Returns(Task.FromResult(Result.Success));
            }

            if (credentialStorage == null)
            {
                credentialStorage = Substitute.For <ICredentialStorage>();

                credentialStorage.SaveCredentialsAsync(Arg.Any <Credentials>())
                .Returns(Task.FromResult(Result.Success));
            }

            return(new LoginViewModel(
                       credentialValidator,
                       credentialStorage,
                       hudUtility ?? Substitute.For <IHudUtility>(),
                       alertUtility ?? Substitute.For <IAlertUtility>(),
                       backgroundSync ?? Substitute.For <IBackgroundSyncUtility>()
                       ));
        }
Exemplo n.º 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            var credentials = await GetCredentialsFromBody();

            if (credentials == null)
            {
                return(AuthenticateResult.Fail("Authentication failed."));
            }

            UserDto user = await _userRepository.GetUserAsync(credentials.Username, WellKnownUserIds.TokenApi);

            if (user == null)
            {
                return(AuthenticateResult.Fail("Authentication failed."));
            }

            var credentialsValid = await _credentialValidator.ValidateAsync(
                user.Id,
                credentials.Password,
                WellKnownUserIds.TokenApi);

            if (!credentialsValid)
            {
                return(AuthenticateResult.Fail("Authentication failed."));
            }

            RegisteredApplicationDto registeredApplication = await _userRepository.GetRegisteredApplicationAsync(credentials.Username, credentials.Audience);

            return(registeredApplication == null?AuthenticateResult.Fail("Authentication failed.") :
                       AuthenticateResult.Success(CreateTicket(credentials.Audience, registeredApplication.RegisteredOwner)));
        }
Exemplo n.º 3
0
        public async Task ValidateAsync_WhenCredentialsValid_Returns_True()
        {
            var userDto = new UserDto {
                Username = Guid.NewGuid().ToString("N")
            };
            var password = _hashingProvider.Hash("Hello123!!", true);

            try
            {
                await AddUserAsync(userDto);

                var addedUserDto = await _userRepository.GetUserAsync(userDto.Username, _userId);
                await AddCredentialAsync(addedUserDto.Id, password);

                var isValid = await _sqlCredentialValidator.ValidateAsync(addedUserDto.Id, password, _userId);

                isValid.Should().BeTrue();
            }
            finally
            {
            }
        }
Exemplo n.º 4
0
        internal async Task SignInAsync()
        {
            try
            {
                IsBusy = true;

                _hud.Show(LoginMessage);

                var credentials = new Credentials(UserName, Password);

                Result validationResult = await _credentialValidator.ValidateAsync(credentials)
                                          .ConfigureAwait(false);

                if (!validationResult.IsSuccessful)
                {
                    _alerts.ShowError(validationResult.ErrorMessage);
                    return;
                }

                var saveResult = await _credentialStorage.SaveCredentialsAsync(credentials)
                                 .ConfigureAwait(false);

                if (!saveResult.IsSuccessful)
                {
                    _alerts.ShowError(saveResult.ErrorMessage);
                    return;
                }

                _backgroundSync.Enable();

                SignedIn?.Invoke(this, EventArgs.Empty);
            }
            finally
            {
                _hud.Dismiss();

                IsBusy = false;
            }
        }