Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Failing after 4m5s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 1s
68 lines
3.0 KiB
C#
68 lines
3.0 KiB
C#
using System.Security.Cryptography;
|
|
using Heimdall.Application.Common;
|
|
using Heimdall.Application.Common.Persistence;
|
|
using Heimdall.Application.DTOs;
|
|
using Heimdall.Domain.Entities;
|
|
using MediatR;
|
|
using Heimdall.Application.Services;
|
|
|
|
namespace Heimdall.Application.Users.Commands.ResetPassword;
|
|
|
|
public class ResetPasswordHandler : IRequestHandler<ResetPasswordCommand, ServiceResult<ResetPasswordResponse>>
|
|
{
|
|
private readonly IIdentityAccountService _userManager;
|
|
private readonly IUserRepository _repository;
|
|
|
|
public ResetPasswordHandler(IIdentityAccountService userManager, IUserRepository repository)
|
|
{
|
|
_userManager = userManager;
|
|
_repository = repository;
|
|
}
|
|
|
|
public async Task<ServiceResult<ResetPasswordResponse>> Handle(ResetPasswordCommand command, CancellationToken ct)
|
|
{
|
|
var user = await _userManager.FindByIdAsync(command.UserId, ct).ConfigureAwait(false);
|
|
if (user is null)
|
|
return ServiceResult<ResetPasswordResponse>.NotFound("User not found");
|
|
|
|
var temporaryPassword = GenerateTemporaryPassword();
|
|
var token = await _userManager.GeneratePasswordResetTokenAsync(user, ct).ConfigureAwait(false);
|
|
var result = await _userManager.ResetPasswordAsync(user, token, temporaryPassword, ct).ConfigureAwait(false);
|
|
|
|
if (!result.Succeeded)
|
|
return ServiceResult<ResetPasswordResponse>.Failure("Password reset failed");
|
|
|
|
user.MustChangePassword = true;
|
|
var updated = await _userManager.UpdateAsync(user, ct).ConfigureAwait(false);
|
|
if (!updated.Succeeded) return ServiceResult<ResetPasswordResponse>.Failure("Password reset failed");
|
|
var stamp = await _userManager.UpdateSecurityStampAsync(user, ct).ConfigureAwait(false);
|
|
if (!stamp) return ServiceResult<ResetPasswordResponse>.Failure("Password reset failed");
|
|
|
|
await _repository.AddAuditLogAsync(command.ActorId, "UserPasswordReset", "User",
|
|
command.UserId.ToString(), null, null, ct);
|
|
|
|
return ServiceResult<ResetPasswordResponse>.Success(
|
|
new ResetPasswordResponse(temporaryPassword));
|
|
}
|
|
|
|
private static string GenerateTemporaryPassword()
|
|
{
|
|
const string lower = "abcdefghijklmnopqrstuvwxyz";
|
|
const string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
const string digits = "0123456789";
|
|
const string special = "!@#$%";
|
|
|
|
var chars = (lower + upper + digits + special).ToCharArray();
|
|
var password = new char[16];
|
|
password[0] = digits[RandomNumberGenerator.GetInt32(digits.Length)];
|
|
password[1] = lower[RandomNumberGenerator.GetInt32(lower.Length)];
|
|
password[2] = upper[RandomNumberGenerator.GetInt32(upper.Length)];
|
|
password[3] = special[RandomNumberGenerator.GetInt32(special.Length)];
|
|
for (int i = 4; i < password.Length; i++)
|
|
password[i] = chars[RandomNumberGenerator.GetInt32(chars.Length)];
|
|
|
|
RandomNumberGenerator.Shuffle(password.AsSpan());
|
|
return new string(password);
|
|
}
|
|
}
|