forked from sanchezvem/PyroFetes
MAJ ajout de la méthode refresh
This commit is contained in:
6
PyroFetes/DTO/Refrresh/Request/RefreshTokenDto.cs
Normal file
6
PyroFetes/DTO/Refrresh/Request/RefreshTokenDto.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PyroFetes.DTO.Refrresh.Request;
|
||||
|
||||
public class RefreshTokenDto
|
||||
{
|
||||
public string? Token { get; set; }
|
||||
}
|
||||
6
PyroFetes/DTO/Refrresh/Response/GetRefreshDto.cs
Normal file
6
PyroFetes/DTO/Refrresh/Response/GetRefreshDto.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PyroFetes.DTO.Refrresh.Response;
|
||||
|
||||
public class GetRefreshDto
|
||||
{
|
||||
public string? Token { get; set; }
|
||||
}
|
||||
74
PyroFetes/Endpoints/Refresh/RefreshTokenEndpoint.cs
Normal file
74
PyroFetes/Endpoints/Refresh/RefreshTokenEndpoint.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Security;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Refrresh.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Refresh;
|
||||
|
||||
public class RefreshTokenEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<GetRefreshDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/refresh");
|
||||
// [Authorize] est géré ici avec les rôles
|
||||
Roles("Admin", "User");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 1. 🚨 CORRECTION : Lire le claim 'Name' du jeton validé par le middleware
|
||||
// Le claim 'Name' contient le nom d'utilisateur
|
||||
string? userName = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(userName))
|
||||
{
|
||||
// Si le claim Name est manquant (ce qui ne devrait pas arriver)
|
||||
await Send.UnauthorizedAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 🚨 CORRECTION : Utiliser le Nom (Name) pour la recherche BDD
|
||||
// Assurez-vous que la propriété de l'entité est 'Name' et qu'elle est unique
|
||||
var login = await database.Users.FirstOrDefaultAsync(x => x.Name == userName, ct);
|
||||
if (login == null)
|
||||
{
|
||||
await Send.UnauthorizedAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Création du NOUVEAU jeton
|
||||
string jwtToken = JwtBearer.CreateToken(
|
||||
o =>
|
||||
{
|
||||
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
||||
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||
|
||||
// Assurez-vous que les claims sont corrects pour Angular
|
||||
if (login.Fonction != null) o.User.Roles.Add(login.Fonction);
|
||||
|
||||
// Ajouter le claim Name (Nom d'utilisateur)
|
||||
o.User.Claims.Add((ClaimTypes.Name, login.Name)!);
|
||||
|
||||
// Si votre API mettait d'autres claims, ajoutez-les ici (ex: "Username")
|
||||
// o.User.Claims.Add(("Username", login.Name)!);
|
||||
});
|
||||
|
||||
// 4. Envoi de la réponse
|
||||
GetRefreshDto responseDto = new()
|
||||
{
|
||||
Token = jwtToken
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await Send.UnauthorizedAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user