94 lines
3.8 KiB
C#
94 lines
3.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Microsoft.IdentityModel.Tokens;
|
||
|
|
using Net.Business.Entities;
|
||
|
|
using Net.Business.Entities.Authenticate;
|
||
|
|
using Net.Connection;
|
||
|
|
using Net.CrossCotting;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IdentityModel.Tokens.Jwt;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Net.Http;
|
||
|
|
using System.Security.Claims;
|
||
|
|
using System.Text;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace Net.Data.Authenticate
|
||
|
|
{
|
||
|
|
public class AuthenticateRepository : RepositoryBase<object>, IAuthenticateRepository
|
||
|
|
{
|
||
|
|
#region Variables Globales
|
||
|
|
private string _aplicacionName;
|
||
|
|
private string _metodoName;
|
||
|
|
private readonly Regex regex = new Regex(@"<(\w+)>.*");
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
|
||
|
|
public AuthenticateRepository(IConnectionSQL context, IHttpClientFactory clientFactory, IConfiguration configuration) : base(context)
|
||
|
|
{
|
||
|
|
_aplicacionName = this.GetType().Name;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<BE_ResultadoTransaccion<BE_AuthenticateResponse>> GetToken(BE_AuthenticateRequest value)
|
||
|
|
{
|
||
|
|
|
||
|
|
_metodoName = regex.Match(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name).Groups[1].Value.ToString();
|
||
|
|
|
||
|
|
ResultadoTransaccion<BE_AuthenticateResponse> resultadoTransaccion = new ResultadoTransaccion<BE_AuthenticateResponse>(_metodoName, _aplicacionName);
|
||
|
|
|
||
|
|
return await Task.Run(() =>
|
||
|
|
{
|
||
|
|
BE_AuthenticateResponse response = new BE_AuthenticateResponse();
|
||
|
|
response.Type = "Bearer";
|
||
|
|
|
||
|
|
//=== OBTENEMOS LISTADO DE CLIENTES
|
||
|
|
var ls_entity_client = get_client();
|
||
|
|
|
||
|
|
//=== BUSCAMOS EXISTENCIA DEL CLIENTE
|
||
|
|
var result_client = ls_entity_client.FirstOrDefault(x => x.client_id == value.client_id);
|
||
|
|
if (result_client != null)
|
||
|
|
{
|
||
|
|
string semilla = "c071$@p*$b@.m0dul3";
|
||
|
|
|
||
|
|
var claims = new[]
|
||
|
|
{
|
||
|
|
new Claim("Usuario", result_client.client_secret)
|
||
|
|
};
|
||
|
|
|
||
|
|
//firma - header
|
||
|
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(semilla));
|
||
|
|
var signCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||
|
|
|
||
|
|
//generador de JWT
|
||
|
|
var token = new JwtSecurityToken(
|
||
|
|
issuer: "ARDICORD",
|
||
|
|
audience: "CUBICO",
|
||
|
|
claims: claims,
|
||
|
|
expires: DateTime.Now.AddHours(10),
|
||
|
|
signingCredentials: signCredentials
|
||
|
|
);
|
||
|
|
|
||
|
|
string tokenGenerado = new JwtSecurityTokenHandler().WriteToken(token);
|
||
|
|
response.Token = tokenGenerado;
|
||
|
|
return resultadoTransaccion.GetInformacionResultado(0, "", "", response);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return resultadoTransaccion.GetInformacionResultado(-1, -1, "El usuario no tiene permiso");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<ModelCredential> get_client()
|
||
|
|
{
|
||
|
|
return new List<ModelCredential>()
|
||
|
|
{
|
||
|
|
new ModelCredential(){ client_secret = "YzBkZjU0ODY0NjY0NDAxN2FkOTdlMzhjMTIwNGRjMjk", client_id ="0HXoLsLmCIz05OjUwbqISz" },
|
||
|
|
new ModelCredential(){ client_secret = "YzBkZjU0ODY0NjY0NDAxN2FkOTdlMzhjMTIwNGRjMjk", client_id ="0HXoLsLmCIz05OjUwbqISx" },
|
||
|
|
new ModelCredential(){ client_secret = "XmTjrMQPeAOFwA3SoUUTKolohA9HfDVSijY66pOtSsJ", client_id ="rpDDCZBSsJRbQnejvULvn5" },
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|