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.

This commit is contained in:
javiermengual 2026-01-03 18:12:40 +01:00
parent f28461ae0c
commit a80d41feab
4 changed files with 82 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,6 @@
namespace DoliMiddlewareApi.Dtos.Dolibarr;
public class TokenResponse
{
public required string AccessToken { get; set; }
}

View File

@ -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;}="";
}

View File

@ -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<string> 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<TokenResponse>(responseString);
if (response == null || string.IsNullOrEmpty(response.AccessToken))
{
throw new UnauthorizedAccessException("Respuesta inválida de Dolibarr");
}
return response.AccessToken;
}
}