Added refresh token endpoint
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
namespace PyroFetes.DTO.Refresh.Request;
|
||||||
|
|
||||||
|
public class RefreshTokenDto
|
||||||
|
{
|
||||||
|
public string? Token { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace PyroFetes.DTO.Refresh.Response;
|
||||||
|
|
||||||
|
public class GetRefreshTokenDto
|
||||||
|
{
|
||||||
|
public string? Token { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using FastEndpoints;
|
||||||
|
using FastEndpoints.Security;
|
||||||
|
using PyroFetes.DTO.Refresh.Request;
|
||||||
|
using PyroFetes.DTO.Refresh.Response;
|
||||||
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Refresh;
|
||||||
|
|
||||||
|
public class RefreshTokenEndpoint(UsersRepository usersRepository) : Endpoint<RefreshTokenDto, GetRefreshTokenDto>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Post("/refresh");
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(RefreshTokenDto req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JwtSecurityTokenHandler handler = new();
|
||||||
|
JwtSecurityToken? token = handler.ReadJwtToken(req.Token);
|
||||||
|
string? username = token.Claims.FirstOrDefault(c => c.Type == "Name")?.Value;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(username))
|
||||||
|
{
|
||||||
|
await Send.UnauthorizedAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
User? login = await usersRepository.SingleOrDefaultAsync(new GetUserByNameSpec(username), ct);
|
||||||
|
if (login == null)
|
||||||
|
{
|
||||||
|
await Send.UnauthorizedAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string jwtToken = JwtBearer.CreateToken(o =>
|
||||||
|
{
|
||||||
|
o.SigningKey = "v9!Qx7#Lk2@pZ8$wR6!tN5%uF3&cD9^mH1*eY4";
|
||||||
|
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||||
|
if (login.Fonction is not null) o.User.Roles.Add(login.Fonction);
|
||||||
|
o.User.Claims.Add(("Name", login.Name)!);
|
||||||
|
o.User.Claims.Add(("Id", login.Id.ToString())!);
|
||||||
|
});
|
||||||
|
|
||||||
|
GetRefreshTokenDto responseDto = new()
|
||||||
|
{
|
||||||
|
Token = jwtToken
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await Send.UnauthorizedAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ public class ConnectUserEndpoint(UsersRepository usersRepository) : Endpoint<Con
|
|||||||
{
|
{
|
||||||
string jwtToken = JwtBearer.CreateToken(o =>
|
string jwtToken = JwtBearer.CreateToken(o =>
|
||||||
{
|
{
|
||||||
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
o.SigningKey = "v9!Qx7#Lk2@pZ8$wR6!tN5%uF3&cD9^mH1*eY4";
|
||||||
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||||
if (user.Fonction is not null) o.User.Roles.Add(user.Fonction);
|
if (user.Fonction is not null) o.User.Roles.Add(user.Fonction);
|
||||||
o.User.Claims.Add(("Name", user.Name)!);
|
o.User.Claims.Add(("Name", user.Name)!);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ QuestPDF.Settings.License = LicenseType.Community;
|
|||||||
|
|
||||||
// On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet
|
// On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet
|
||||||
builder.Services
|
builder.Services
|
||||||
.AddAuthenticationJwtBearer(s => s.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong")
|
.AddAuthenticationJwtBearer(s => s.SigningKey = "v9!Qx7#Lk2@pZ8$wR6!tN5%uF3&cD9^mH1*eY4")
|
||||||
.AddAuthorization()
|
.AddAuthorization()
|
||||||
.AddFastEndpoints()
|
.AddFastEndpoints()
|
||||||
.SwaggerDocument(options => { options.ShortSchemaNames = true; })
|
.SwaggerDocument(options => { options.ShortSchemaNames = true; })
|
||||||
|
|||||||
Reference in New Issue
Block a user