-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9682a6
commit 91482f9
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace OpenAdm.Api.Attributes; | ||
|
||
public class TryAutenticaAttribute : Attribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.IdentityModel.Tokens; | ||
using OpenAdm.Api.Attributes; | ||
using OpenAdm.Application.Models.Tokens; | ||
using OpenAdm.Domain.Exceptions; | ||
using OpenAdm.Domain.Interfaces; | ||
|
||
namespace OpenAdm.Api.Middlewares; | ||
|
||
public class TryAutenticaMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
public TryAutenticaMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke( | ||
HttpContext httpContext, | ||
IUsuarioAutenticado usuarioAutenticado) | ||
{ | ||
if (usuarioAutenticado.Id != Guid.Empty) | ||
{ | ||
await _next(httpContext); | ||
return; | ||
} | ||
|
||
var autenticar = httpContext.Features.Get<IEndpointFeature>()?.Endpoint?.Metadata | ||
.FirstOrDefault(m => m is TryAutenticaAttribute) is TryAutenticaAttribute atributoAutorizacao; | ||
|
||
if (!autenticar) | ||
{ | ||
await _next(httpContext); | ||
return; | ||
} | ||
|
||
var token = httpContext.Request.Headers.Authorization.ToString().Split(" ").Last().Replace("Bearer", "")?.Trim(); | ||
|
||
if (string.IsNullOrWhiteSpace(token)) | ||
{ | ||
await _next(httpContext); | ||
return; | ||
} | ||
|
||
var keyJwt = VariaveisDeAmbiente.GetVariavel("JWT_KEY"); | ||
|
||
try | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
tokenHandler.ValidateToken(token, new TokenValidationParameters | ||
{ | ||
ValidateIssuer = true, | ||
ValidateAudience = true, | ||
ValidateLifetime = false, | ||
ValidateIssuerSigningKey = true, | ||
ValidIssuer = ConfiguracaoDeToken.Issue, | ||
ValidAudience = ConfiguracaoDeToken.Audience, | ||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfiguracaoDeToken.Key)) | ||
}, out SecurityToken validatedToken); | ||
|
||
var jwtToken = (JwtSecurityToken)validatedToken; | ||
|
||
var id = jwtToken.Claims.FirstOrDefault(c => c.Type == "Id")?.Value | ||
?? throw new ExceptionUnauthorize("Token inválido"); | ||
var isFuncionario = jwtToken.Claims.FirstOrDefault(c => c.Type == "IsFuncionario")?.Value; | ||
|
||
if (!Guid.TryParse(id, out Guid idParse)) | ||
{ | ||
throw new ExceptionUnauthorize("Por favor, efetue o login novamente"); | ||
} | ||
|
||
usuarioAutenticado.Id = idParse; | ||
usuarioAutenticado.IsFuncionario = !string.IsNullOrWhiteSpace(isFuncionario) && isFuncionario == "TRUE"; | ||
|
||
} | ||
catch (SecurityTokenExpiredException) | ||
{ | ||
throw new ExceptionUnauthorize("Sessão expirada, efetue o login novamente!"); | ||
} | ||
catch (Exception) | ||
{ | ||
throw new ExceptionUnauthorize("Efetue o login novamente!"); | ||
} | ||
|
||
await _next(httpContext); | ||
} | ||
} |