feature, preparar el endpoint para extraer el zip de archivos sin procesar, agregando el funcionamiento de refresh tokens

feature/Reporte_Relacion_Facturas_Alen
Felix Morales 1 year ago
parent b33303c85d
commit c1f22a2199
  1. 5
      Contracts/Auth/IAuthService.cs
  2. 28
      Controllers/FileManagerController.cs
  3. 3
      Program.cs
  4. 47
      Services/Auth/AuthService.cs

@ -0,0 +1,5 @@
namespace CORRESPONSALBackend.Contracts.Auth{
public interface IAuthService{
public Task<string> RefreshAgenteFacturacionToken();
}
}

@ -8,6 +8,7 @@ using CORRESPONSALBackend.Models.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using CORRESPONSALBackend.Contracts.Auth;
namespace CORRESPONSALBackend.Controllers
{
@ -21,6 +22,7 @@ namespace CORRESPONSALBackend.Controllers
private readonly ICasaCuervoRepository _RepoCasaCuervo;
private readonly IFilePaths4ProcessRepository _RepoRelativePath;
private readonly IConfiguration _config;
private readonly IAuthService _authService;
private readonly string RootPathCorresponsales;
@ -28,7 +30,8 @@ namespace CORRESPONSALBackend.Controllers
IFilePaths4ProcessRepository RepoRelativePath,
IConfiguration config,
IUsuariosRepository RepoUsuarios,
ICasaCuervoRepository RepoCasaCuervo)
ICasaCuervoRepository RepoCasaCuervo,
IAuthService authService)
{
_config = config;
_Repo = Repo;
@ -36,6 +39,7 @@ namespace CORRESPONSALBackend.Controllers
_RepoCasaCuervo = RepoCasaCuervo;
_RepoRelativePath = RepoRelativePath;
RootPathCorresponsales = _config.GetValue<string>("AllFiles");
_authService = authService;
}
[Route("GetFileInfoByProcess")]
@ -242,6 +246,8 @@ namespace CORRESPONSALBackend.Controllers
try{
var unprocessedFiles = await _Repo.GetFilesByProcessId(31);//Se obtiene el listado de archivos que no se han procesado
if(unprocessedFiles.Count() > 0){
var newToken = await _authService.RefreshAgenteFacturacionToken();
var tokenRefresed = await CreateNewTokenTextFile(newToken);
FilePaths4Process unprocessedFilesRelativePath = await _RepoRelativePath.getPaths4ProcessById(31);//Se obtiene la ruta relativa de los archivos sin procesar
FilePaths4Process processedFilesRelativePath = await _RepoRelativePath.getPaths4ProcessById(35);//Se obtiene la ruta relativa de los archivos procesados
var unprocessedFilesPath = RootPathCorresponsales + unprocessedFilesRelativePath.Path;//Se genera la ruta completa de los archivos sin procesar
@ -284,5 +290,25 @@ namespace CORRESPONSALBackend.Controllers
return new BadRequestObjectResult(new {respuesta = ex.Message});
}
}
private async Task<bool> CreateNewTokenTextFile(string token){
try {
FilePaths4Process RelativePath = await _RepoRelativePath.getPaths4ProcessById(31);//Se obtiene la ruta de los archivos sin procesar para agregar el archivo del token.
string refreshTokenFilePath = RootPathCorresponsales + RelativePath.Path + "Token.txt";
if(System.IO.File.Exists(refreshTokenFilePath)){
System.IO.File.Delete(refreshTokenFilePath);
}
var fileStream = System.IO.File.Create(refreshTokenFilePath);
fileStream.Close();
var writer = new StreamWriter(refreshTokenFilePath);
await writer.WriteLineAsync(token);
writer.Close();
return true;
}catch(Exception ex){
return false;
}
}
}
}

@ -30,6 +30,8 @@ using Microsoft.OpenApi.Models;
using CORRESPONSALBackend.Repository.Reportes;
using CORRESPONSALBackend.Contracts.Reportes.Newell.Mensual;
using CORRESPONSALBackend.Services.Reportes.Newell.Mensual;
using CORRESPONSALBackend.Contracts.Auth;
using CORRESPONSALBackend.Services.Auth;
var builder = WebApplication.CreateBuilder(args);
@ -43,6 +45,7 @@ builder.Services.AddScoped<IClientesRepository, ClientesRepository>();
builder.Services.AddScoped<IBatteryRepository, BatteryRepository>();
builder.Services.AddScoped<IReporteMensualRepository, ReporteMensualRepository>();
builder.Services.AddScoped<IReporteMensualService, ReporteMensualService>();
builder.Services.AddScoped<IAuthService, AuthService>();
// Corresponsalias
builder.Services.AddScoped<IContabilidadCorresponsaliasRepository, ContabilidadCorresponsaliasRepository>();

@ -0,0 +1,47 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using CORRESPONSALBackend.Contracts.Auth;
using CORRESPONSALBackend.Contracts.Utils;
using CORRESPONSALBackend.DTO;
using CORRESPONSALBackend.Models.Utils;
using Microsoft.IdentityModel.Tokens;
namespace CORRESPONSALBackend.Services.Auth{
public class AuthService : IAuthService
{
private readonly IFileManagerRepository _Repo;
private readonly IFilePaths4ProcessRepository _RepoRelativePath;
private readonly IConfiguration _config;
private readonly string RootPathCorresponsales;
public AuthService(IFileManagerRepository Repo, IFilePaths4ProcessRepository RepoRelativePath, IConfiguration config){
_Repo = Repo;
_RepoRelativePath = RepoRelativePath;
_config = config;
RootPathCorresponsales = _config.GetValue<string>("AllFiles");
}
public async Task<string> RefreshAgenteFacturacionToken(){
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Sub, _config["Jwt:Subject"]),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString())
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
_config["Jwt:Issuer"],
_config["Jwt:Audience"],
claims,
expires: DateTime.UtcNow.AddHours(25),
//expires: DateTime.UtcNow.AddMinutes(5),
signingCredentials: signIn);
var _token = new JwtSecurityTokenHandler().WriteToken(token);
return _token;
}
}
}
Loading…
Cancel
Save