From 592e8a1b4230e8c20f81d781dc3e42f687d97d23 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 27 Aug 2024 22:52:07 +0300 Subject: [PATCH] feat: add renew password --- Endpoint/Controllers/V1/AuthController.cs | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Endpoint/Controllers/V1/AuthController.cs b/Endpoint/Controllers/V1/AuthController.cs index 275903b..9ee7f7d 100644 --- a/Endpoint/Controllers/V1/AuthController.cs +++ b/Endpoint/Controllers/V1/AuthController.cs @@ -7,6 +7,7 @@ using Mirea.Api.Dto.Common; using Mirea.Api.Dto.Requests; using Mirea.Api.Dto.Responses; using Mirea.Api.Endpoint.Common.Attributes; +using Mirea.Api.Endpoint.Common.Exceptions; using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Common.Settings; using Mirea.Api.Security.Common.Dto.Requests; @@ -165,4 +166,29 @@ public class AuthController(IOptionsSnapshot user, AuthService auth, Pass [Authorize] [CacheMaxAge(0, 0, 1)] public ActionResult GetRole() => Ok(AuthRoles.Admin); + + [HttpPost("RenewPassword")] + [ApiExplorerSettings(IgnoreApi = true)] + [Localhost] + [BadRequestResponse] + public ActionResult RenewPassword([FromBody] string? password = null) + { + if (string.IsNullOrEmpty(password)) + password = string.Empty; + else if (!PasswordHashService.HasPasswordInPolicySecurity(password)) + throw new ControllerArgumentException("The password must be at least 8 characters long and contain at least one uppercase letter and one special character."); + + while (!PasswordHashService.HasPasswordInPolicySecurity(password)) + password = GeneratorKey.GenerateAlphaNumeric(16, includes: "!@#%^"); + + var (salt, hash) = passwordService.HashPassword(password); + + var admin = user.Value; + + admin.Salt = salt; + admin.PasswordHash = hash; + admin.SaveSetting(); + + return Ok(password); + } }