diff --git a/Contracts/Auth/IAuthService.cs b/Contracts/Auth/IAuthService.cs new file mode 100644 index 0000000..4fbd191 --- /dev/null +++ b/Contracts/Auth/IAuthService.cs @@ -0,0 +1,5 @@ +namespace CORRESPONSALBackend.Contracts.Auth{ + public interface IAuthService{ + public Task RefreshAgenteFacturacionToken(); + } +} \ No newline at end of file diff --git a/Controllers/FileManagerController.cs b/Controllers/FileManagerController.cs index fdd2358..1bf26a9 100644 --- a/Controllers/FileManagerController.cs +++ b/Controllers/FileManagerController.cs @@ -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("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 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; + } + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 2b0f12c..dc9bb6e 100644 --- a/Program.cs +++ b/Program.cs @@ -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(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Corresponsalias builder.Services.AddScoped(); diff --git a/Services/Auth/AuthService.cs b/Services/Auth/AuthService.cs new file mode 100644 index 0000000..5ecb5d8 --- /dev/null +++ b/Services/Auth/AuthService.cs @@ -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("AllFiles"); + } + public async Task RefreshAgenteFacturacionToken(){ + var claims = new List + { + 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; + } + } +} \ No newline at end of file