From 85ccc52ca17e7eb749175b2b4b35b0fd887d119e Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 10 Mar 2025 16:58:34 +0100 Subject: [PATCH] =?UTF-8?q?feat(AuthController):=20Aktualisiert,=20um=20di?= =?UTF-8?q?e=20Anmeldung=20=C3=BCber=20die=20Benutzer-ID=20zu=20erm=C3=B6g?= =?UTF-8?q?lichen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AuthController.cs | 43 +++++++++++++------ src/DigitalData.Auth.API/Dto/UserLogin.cs | 6 +++ 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/DigitalData.Auth.API/Dto/UserLogin.cs diff --git a/src/DigitalData.Auth.API/Controllers/AuthController.cs b/src/DigitalData.Auth.API/Controllers/AuthController.cs index ce3a6f3..04fc6cc 100644 --- a/src/DigitalData.Auth.API/Controllers/AuthController.cs +++ b/src/DigitalData.Auth.API/Controllers/AuthController.cs @@ -3,13 +3,13 @@ using DigitalData.Core.Abstractions.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; -using DigitalData.UserManager.Application.DTOs.Auth; using DigitalData.UserManager.Application.Contracts; using DigitalData.UserManager.Application.DTOs.User; using DigitalData.Core.Abstractions.Application; using DigitalData.Auth.API.Dto; using DigitalData.Auth.API.Services.Contracts; using DigitalData.Auth.API.Entities; +using DigitalData.Core.DTO; namespace DigitalData.Auth.API.Controllers { @@ -45,18 +45,37 @@ namespace DigitalData.Auth.API.Controllers _consumerSignatureHandler = apiSignatureHandler; } - private async Task CreateTokenAsync(LogInDto login, string consumerName, bool cookie = true) + private async Task CreateTokenAsync(UserLogin login, string consumerName, bool cookie = true) { - bool isValid = await _dirSearchService.ValidateCredentialsAsync(login.Username, login.Password); + DataResult? uRes; + if (login.Username is not null) + { + bool isValid = await _dirSearchService.ValidateCredentialsAsync(login.Username, login.Password); - if (!isValid) - return Unauthorized(); + if (!isValid) + return Unauthorized(); - //find the user - var uRes = await _userService.ReadByUsernameAsync(login.Username); - if (uRes.IsFailed) - return Unauthorized(); + uRes = await _userService.ReadByUsernameAsync(login.Username); + if (uRes.IsFailed) + return Unauthorized(); + } + else if(login.Id is int userId) + { + uRes = await _userService.ReadByIdAsync(userId); + if (uRes.IsFailed) + return Unauthorized(); + + bool isValid = await _dirSearchService.ValidateCredentialsAsync(uRes.Data.Username, login.Password); + if (!isValid) + return Unauthorized(); + } + else + { + return BadRequest("One of user ID or username should be provided."); + } + + //find the user var consumer = await _consumerService.ReadByNameAsync(consumerName); if (consumer is null) return Unauthorized(); @@ -64,7 +83,7 @@ namespace DigitalData.Auth.API.Controllers if (!_cryptoFactory.TokenDescriptors.TryGet(_apiParams.Issuer, consumer.Audience, out var descriptor)) return StatusCode(StatusCodes.Status500InternalServerError); - var token = _userSignatureHandler.WriteToken(uRes.Data, descriptor); + var token = _userSignatureHandler.WriteToken(uRes!.Data, descriptor); //set cookie if (cookie) @@ -102,7 +121,7 @@ namespace DigitalData.Auth.API.Controllers //TODO: Add role depends on group name [HttpPost("{consumerName}/login")] [AllowAnonymous] - public async Task Login([FromForm] LogInDto login, [FromRoute] string consumerName) + public async Task Login([FromForm] UserLogin login, [FromRoute] string consumerName) { try { @@ -146,7 +165,7 @@ namespace DigitalData.Auth.API.Controllers } [HttpPost("{consumerName}")] - public async Task CreateTokenViaBody([FromBody] LogInDto login, [FromRoute] string consumerName, [FromQuery] bool cookie = false) + public async Task CreateTokenViaBody([FromBody] UserLogin login, [FromRoute] string consumerName, [FromQuery] bool cookie = false) { try { diff --git a/src/DigitalData.Auth.API/Dto/UserLogin.cs b/src/DigitalData.Auth.API/Dto/UserLogin.cs new file mode 100644 index 0000000..17e4737 --- /dev/null +++ b/src/DigitalData.Auth.API/Dto/UserLogin.cs @@ -0,0 +1,6 @@ +namespace DigitalData.Auth.API.Dto; + +public record UserLogin(string Password, int? Id = null, string? Username = null) +{ + public bool Valid => Id is not null || !string.IsNullOrWhiteSpace(Username); +}; \ No newline at end of file