First Commit
This commit is contained in:
142
Apis/ADS.Host/Auth/HostAdsUserCredentialValidator .cs
Normal file
142
Apis/ADS.Host/Auth/HostAdsUserCredentialValidator .cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using ADS.Helpers.Auth.Abstractions;
|
||||
using ADS.Helpers.MultiTenant.Abstractions;
|
||||
using ADS.Helpers.MultiTenant.Context;
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Npgsql;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ADS.Host.Auth;
|
||||
|
||||
public class HostAdsUserCredentialValidator : IAdsUserCredentialValidator
|
||||
{
|
||||
private readonly ITenantContextAccessor _tenantContextAccessor;
|
||||
|
||||
public HostAdsUserCredentialValidator(ITenantContextAccessor tenantContextAccessor)
|
||||
{
|
||||
_tenantContextAccessor = tenantContextAccessor;
|
||||
}
|
||||
|
||||
public async Task<AdsUserValidationResult> ValidateAsync(
|
||||
string userName,
|
||||
string password,
|
||||
string? tenantId,
|
||||
string? scope,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var tenant = _tenantContextAccessor.Current;
|
||||
|
||||
if (tenant == null)
|
||||
{
|
||||
return new AdsUserValidationResult
|
||||
{
|
||||
IsValid = false,
|
||||
Error = "TenantContext no está cargado."
|
||||
};
|
||||
}
|
||||
|
||||
if (tenant.Database == null ||
|
||||
string.IsNullOrWhiteSpace(tenant.Database.ConnectionString))
|
||||
{
|
||||
return new AdsUserValidationResult
|
||||
{
|
||||
IsValid = false,
|
||||
Error = "El tenant no tiene conexión configurada."
|
||||
};
|
||||
}
|
||||
|
||||
using var conn = CreateConnection(
|
||||
tenant.Database.Provider,
|
||||
tenant.Database.ConnectionString);
|
||||
|
||||
var result = await conn.QueryFirstOrDefaultAsync<UsuarioLoginDbResult>(
|
||||
"dbo.PA_Sis_UsuariosGet",
|
||||
new
|
||||
{
|
||||
CveUsuario = -1,
|
||||
CveRol = -1,
|
||||
CveUsuarioControlado = -1,
|
||||
Usuario = userName,
|
||||
Contraseña = EncriptaDES(password),
|
||||
EsLogIn = true,
|
||||
CvePlataforma = -1,
|
||||
CveEmpresa = -1,
|
||||
IP = "",
|
||||
MAC = "",
|
||||
GrupoEmpresa = "",
|
||||
Estatus = -1
|
||||
},
|
||||
commandType: CommandType.StoredProcedure
|
||||
);
|
||||
|
||||
if (result == null || result.CveUsuario <= 0)
|
||||
{
|
||||
return new AdsUserValidationResult
|
||||
{
|
||||
IsValid = false,
|
||||
Error = "Usuario o contraseña inválidos."
|
||||
};
|
||||
}
|
||||
|
||||
return new AdsUserValidationResult
|
||||
{
|
||||
IsValid = true,
|
||||
UserName=userName,
|
||||
CveUser = result.CveUsuario,
|
||||
CveRole = result.CveRol,
|
||||
CveEmpresa = result.CveEmpresa,
|
||||
CveCliente = result.CveCliente,
|
||||
TenantId = tenant.TenantId
|
||||
};
|
||||
}
|
||||
private static string EncriptaDES(string texto)
|
||||
{
|
||||
if (string.IsNullOrEmpty(texto))
|
||||
return "";
|
||||
|
||||
byte[] key = Encoding.UTF8.GetBytes("$Ap7+5&z");
|
||||
|
||||
byte[] iv = new byte[]
|
||||
{
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
0x90, 0xAB, 0xCD, 0xEF
|
||||
};
|
||||
|
||||
using var des = DES.Create();
|
||||
des.Key = key;
|
||||
des.IV = iv;
|
||||
des.Mode = CipherMode.CBC;
|
||||
des.Padding = PaddingMode.PKCS7;
|
||||
|
||||
byte[] input = Encoding.UTF8.GetBytes(texto);
|
||||
|
||||
using var ms = new MemoryStream();
|
||||
using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(input, 0, input.Length);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
return Convert.ToBase64String(ms.ToArray());
|
||||
}
|
||||
private static IDbConnection CreateConnection(string provider, string connectionString)
|
||||
{
|
||||
return provider?.ToLowerInvariant() switch
|
||||
{
|
||||
"sqlserver" => new SqlConnection(connectionString),
|
||||
"mssql" => new SqlConnection(connectionString),
|
||||
"postgres" => new NpgsqlConnection(connectionString),
|
||||
"postgresql" => new NpgsqlConnection(connectionString),
|
||||
_ => throw new NotSupportedException($"Proveedor de base de datos no soportado: {provider}")
|
||||
};
|
||||
}
|
||||
|
||||
private class UsuarioLoginDbResult
|
||||
{
|
||||
public int CveUsuario { get; set; }
|
||||
public int CveRol { get; set; }
|
||||
public int CveEmpresa { get; set; }
|
||||
public int CveCliente { get; set; }
|
||||
}
|
||||
}
|
||||
22
Apis/ADS.Host/Auth/StubAdsClientCredentialValidator.cs
Normal file
22
Apis/ADS.Host/Auth/StubAdsClientCredentialValidator.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using ADS.Helpers.Auth.Abstractions;
|
||||
|
||||
namespace ADS.Host.Auth;
|
||||
|
||||
public sealed class StubAdsClientCredentialValidator : IAdsClientCredentialValidator
|
||||
{
|
||||
public Task<AdsClientValidationResult> ValidateAsync(
|
||||
string clientId,
|
||||
string clientSecret,
|
||||
string? tenantId,
|
||||
string? scope,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
return Task.FromResult(new AdsClientValidationResult
|
||||
{
|
||||
IsValid = false,
|
||||
ClientId = clientId,
|
||||
TenantId = tenantId,
|
||||
Error = "IAdsClientCredentialValidator not configured in ADS.Host."
|
||||
});
|
||||
}
|
||||
}
|
||||
20
Apis/ADS.Host/Auth/StubAdsUserCredentialValidator.cs
Normal file
20
Apis/ADS.Host/Auth/StubAdsUserCredentialValidator.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using ADS.Helpers.Auth.Abstractions;
|
||||
|
||||
namespace ADS.Host.Auth;
|
||||
|
||||
public sealed class StubAdsUserCredentialValidator : IAdsUserCredentialValidator
|
||||
{
|
||||
public Task<AdsUserValidationResult> ValidateAsync(
|
||||
string userName,
|
||||
string password,
|
||||
string? tenantId,
|
||||
string? scope,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
return Task.FromResult(new AdsUserValidationResult
|
||||
{
|
||||
IsValid = false,
|
||||
Error = "IAdsUserCredentialValidator not configured in ADS.Host."
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user