private async Task LogResultAsync(
            UserCredentialsAuthenticationResult result,
            AuthenticateUserCredentialsQuery query,
            string uniqueUsername,
            IExecutionContext executionContext
            )
        {
            ICommand command;

            if (result.IsSuccess)
            {
                command = new LogSuccessfulAuthenticationCommand()
                {
                    UserId = result.User.UserId
                };
            }
            else
            {
                command = new LogFailedAuthenticationAttemptCommand(query.UserAreaCode, uniqueUsername);
            }

            await _domainRepository
            .WithContext(executionContext)
            .ExecuteCommandAsync(command);
        }
Esempio n. 2
0
        private async Task AuthenticateAsync(UpdateCurrentUserPasswordCommand command, User user)
        {
            if (_userAuthenticationHelper.VerifyPassword(user, command.OldPassword) == PasswordVerificationResult.Failed)
            {
                var logFailedAttemptCommand = new LogFailedAuthenticationAttemptCommand(user.UserAreaCode, user.Username);
                await _domainRepository.ExecuteCommandAsync(logFailedAttemptCommand);

                UserValidationErrors.Authentication.InvalidPassword.Throw(nameof(command.OldPassword));
            }
        }
        public async Task CanClearStaleAuthenticationFailData()
        {
            var uniqueData = UNIQUE_PREFIX + "CCStaleAuthF";
            var seedDate   = new DateTime(1994, 8, 2, 0, 0, 0, DateTimeKind.Utc);

            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions();
            var dbContext         = app.Services.GetRequiredService <CofoundryDbContext>();

            var logCommand = new LogFailedAuthenticationAttemptCommand()
            {
                UserAreaCode = TestUserArea1.Code,
                Username     = uniqueData
            };

            app.Mocks.MockDateTime(seedDate);
            await contentRepository.ExecuteCommandAsync(logCommand);

            app.Mocks.MockDateTime(seedDate.AddDays(50));
            await contentRepository.ExecuteCommandAsync(logCommand);

            app.Mocks.MockDateTime(seedDate.AddDays(70));
            await contentRepository
            .ExecuteCommandAsync(new CleanupUsersCommand()
            {
                UserAreaCode           = TestUserArea1.Code,
                DefaultRetentionPeriod = TimeSpan.FromDays(30)
            });

            var logs = await dbContext
                       .UserAuthenticationFailLogs
                       .AsNoTracking()
                       .Where(u => u.Username == uniqueData)
                       .ToListAsync();

            using (new AssertionScope())
            {
                logs.Should().HaveCount(1);
                logs.Single().CreateDate.Should().Be(seedDate.AddDays(50));
            }
        }