From a80d41feaba46a59efc49bc68f71495fb99c720e Mon Sep 17 00:00:00 2001 From: javiermengual Date: Sat, 3 Jan 2026 18:12:40 +0100 Subject: [PATCH] feat: Primera parte de la implementacion de autentificacion, para obtener la DOLIAPIKEY del cliente la cual cachearemos asociada a un Id que devolveremos en el JWT de nuestro servidor. --- DoliMiddlewareApi.sln | 29 +++++++++++++++++ .../Dtos/Dolibarr/TokenResponse.cs | 6 ++++ .../Dtos/command/CreateTokenDto.cs | 16 ++++++++++ .../Services/Auth/DolibarrAuthService.cs | 31 +++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs create mode 100644 DoliMiddlewareApi/Dtos/command/CreateTokenDto.cs create mode 100644 DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs diff --git a/DoliMiddlewareApi.sln b/DoliMiddlewareApi.sln index aa12ddd..fefbb3e 100644 --- a/DoliMiddlewareApi.sln +++ b/DoliMiddlewareApi.sln @@ -7,15 +7,44 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution compose.yaml = compose.yaml EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoliMiddlewareApi.Tests", "DoliMiddlewareApi.Tests\DoliMiddlewareApi.Tests.csproj", "{356EF403-AF00-493B-AC69-6DD7C2D10DF5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|x64.ActiveCfg = Debug|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|x64.Build.0 = Debug|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|x86.ActiveCfg = Debug|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Debug|x86.Build.0 = Debug|Any CPU {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|Any CPU.Build.0 = Release|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|x64.ActiveCfg = Release|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|x64.Build.0 = Release|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|x86.ActiveCfg = Release|Any CPU + {9A700A1E-080F-4B0D-BD3D-3E5B31B488EC}.Release|x86.Build.0 = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|x64.ActiveCfg = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|x64.Build.0 = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|x86.ActiveCfg = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Debug|x86.Build.0 = Debug|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|Any CPU.Build.0 = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|x64.ActiveCfg = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|x64.Build.0 = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|x86.ActiveCfg = Release|Any CPU + {356EF403-AF00-493B-AC69-6DD7C2D10DF5}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection EndGlobal diff --git a/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs b/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs new file mode 100644 index 0000000..813e9d9 --- /dev/null +++ b/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs @@ -0,0 +1,6 @@ +namespace DoliMiddlewareApi.Dtos.Dolibarr; + +public class TokenResponse +{ + public required string AccessToken { get; set; } +} \ No newline at end of file diff --git a/DoliMiddlewareApi/Dtos/command/CreateTokenDto.cs b/DoliMiddlewareApi/Dtos/command/CreateTokenDto.cs new file mode 100644 index 0000000..a915c62 --- /dev/null +++ b/DoliMiddlewareApi/Dtos/command/CreateTokenDto.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace DoliMiddlewareApi.Dtos.command; + +public class CreateTokenDto +{ + [Required] + [StringLength(100)] + public string Username{get;set;}=""; + [Required] + [StringLength(255)] + public string Password{get;set;}=""; + + + +} \ No newline at end of file diff --git a/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs b/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs new file mode 100644 index 0000000..b7cfdec --- /dev/null +++ b/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs @@ -0,0 +1,31 @@ +using DoliMiddlewareApi.Dtos.command; +using DoliMiddlewareApi.Dtos.Dolibarr; +using DoliMiddlewareApi.Services.Clients; +using System.Text.Json; + +namespace DoliMiddlewareApi.Services.Auth; + +public sealed class DolibarrAuthService(IDolibarrApiClient apiClient) +{ + public async Task AuthenticateAsync(CreateTokenDto createTokenDto) + { + var responseString = await apiClient.PostAsync("login", new + { + login = createTokenDto.Username, + password = createTokenDto.Password + }); + + if (string.IsNullOrEmpty(responseString)) + { + throw new UnauthorizedAccessException("Credenciales inválidas"); + } + + var response = JsonSerializer.Deserialize(responseString); + if (response == null || string.IsNullOrEmpty(response.AccessToken)) + { + throw new UnauthorizedAccessException("Respuesta inválida de Dolibarr"); + } + + return response.AccessToken; + } +} \ No newline at end of file