forked from sanchezvem/PyroFetes
MAJ avec l'authentifiation
This commit is contained in:
7
PyroFetes/DTO/Login/Request/ConnectLoginDto.cs
Normal file
7
PyroFetes/DTO/Login/Request/ConnectLoginDto.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace PyroFetes.DTO.Login.Request;
|
||||||
|
|
||||||
|
public class ConnectLoginDto
|
||||||
|
{
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public string? Password { get; set; }
|
||||||
|
}
|
||||||
24
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal file
24
PyroFetes/DTO/Login/Request/CreateLoginDto.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
// Nécessaire pour les validations
|
||||||
|
|
||||||
|
namespace PyroFetes.DTO.Login.Request;
|
||||||
|
|
||||||
|
public class CreateLoginDto
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "Le nom est requis.")]
|
||||||
|
[StringLength(50, MinimumLength = 3, ErrorMessage = "L'identifiant doit faire entre 3 et 50 caractères.")]
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "L'emil est requis.")]
|
||||||
|
[StringLength(50, MinimumLength = 3)]
|
||||||
|
public string Email { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "Le mot de passe est requis.")]
|
||||||
|
[MinLength(6, ErrorMessage = "Le mot de passe doit contenir au moins 6 caractères.")]
|
||||||
|
public string Password { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
// Ajout du champ Rôle (Optionnel, par défaut "User")
|
||||||
|
// Cela te permet d'envoyer "Admin" via Swagger
|
||||||
|
public string Fonction { get; set; } = "User";
|
||||||
|
}
|
||||||
8
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal file
8
PyroFetes/DTO/Login/Request/UpdateLoginDto.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace PyroFetes.DTO.Login.Request;
|
||||||
|
|
||||||
|
public class UpdateLoginDto
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public string? Password { get; set; }
|
||||||
|
}
|
||||||
6
PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs
Normal file
6
PyroFetes/DTO/Login/Response/GetLoginConnectDto.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
public class GetLoginConnectDto
|
||||||
|
{
|
||||||
|
public string? Token { get; set; }
|
||||||
|
}
|
||||||
10
PyroFetes/DTO/Login/Response/GetLoginDto.cs
Normal file
10
PyroFetes/DTO/Login/Response/GetLoginDto.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
public class GetLoginDto
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string? Name { get; set; } = string.Empty;
|
||||||
|
public string? Email { get; set; } = string.Empty;
|
||||||
|
public string? Password { get; set; } = string.Empty;
|
||||||
|
public string? Fonction { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PasswordGenerator;
|
||||||
|
using PyroFetes.DTO.Login.Request;
|
||||||
|
using PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class CreateLoginEndpoint(PyroFetesDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Post("/logins");
|
||||||
|
//Roles("Admin");
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
bool exists = await database.Users.AnyAsync(x => x.Name == req.Name, ct);
|
||||||
|
if (exists)
|
||||||
|
{
|
||||||
|
AddError("Ce nom d'utilisateur est déjà utilisé.");
|
||||||
|
await Send.ErrorsAsync(400, ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||||
|
|
||||||
|
Models.User login = new Models.User()
|
||||||
|
{
|
||||||
|
Name = req.Name,
|
||||||
|
Email = req.Email,
|
||||||
|
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
|
||||||
|
Salt = salt,
|
||||||
|
|
||||||
|
Fonction = string.IsNullOrEmpty(req.Fonction) ? "User" : req.Fonction
|
||||||
|
};
|
||||||
|
|
||||||
|
database.Users.Add(login);
|
||||||
|
await database.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
GetLoginDto responseDto = new()
|
||||||
|
{
|
||||||
|
Id = login.Id,
|
||||||
|
Name = login.Name,
|
||||||
|
Email = login.Email,
|
||||||
|
Fonction = login.Fonction
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal file
34
PyroFetes/Endpoints/Login/DeleteLoginEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class DeleteLoginRequest
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DeleteLoginEndpoint(PyroFetesDbContext database) : Endpoint<DeleteLoginRequest>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Delete("/logins/{@Id}", x => new {x.Id});
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(DeleteLoginRequest req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||||
|
|
||||||
|
if (login == null)
|
||||||
|
{
|
||||||
|
await Send.NotFoundAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
database.Users.Remove(login);
|
||||||
|
await database.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
await Send.NoContentAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal file
29
PyroFetes/Endpoints/Login/GetAllLoginEndpoint.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class GetAllLoginEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetLoginDto>>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Get("/logins");
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(CancellationToken ct)
|
||||||
|
{
|
||||||
|
List<GetLoginDto> logins = await database.Users
|
||||||
|
.Select(login => new GetLoginDto()
|
||||||
|
{
|
||||||
|
Id = login.Id,
|
||||||
|
Name = login.Name,
|
||||||
|
Password = login.Password,
|
||||||
|
Fonction = login.Fonction
|
||||||
|
})
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
await Send.OkAsync(logins, ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
40
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal file
40
PyroFetes/Endpoints/Login/GetLoginEndpoint.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class GetLoginRequest
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GetLoginEndpoint(PyroFetesDbContext database) : Endpoint<GetLoginRequest, GetLoginDto>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Get("/logins/{@Id}", x => new {x.Id});
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(GetLoginRequest req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
Models.User? login = await database.Users
|
||||||
|
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||||
|
|
||||||
|
if (login == null)
|
||||||
|
{
|
||||||
|
await Send.NotFoundAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GetLoginDto responseDto = new()
|
||||||
|
{
|
||||||
|
Id = login.Id,
|
||||||
|
Name = login.Name,
|
||||||
|
Fonction = login.Fonction
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal file
43
PyroFetes/Endpoints/Login/UpdateLoginEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PasswordGenerator;
|
||||||
|
using PyroFetes.DTO.Login.Request;
|
||||||
|
using PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class UpdateLoginEndpoint(PyroFetesDbContext database) : Endpoint<UpdateLoginDto, GetLoginDto>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Put("/logins/{@Id}", x => new {x.Id});
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(UpdateLoginDto req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||||
|
|
||||||
|
if (login == null)
|
||||||
|
{
|
||||||
|
await Send.NotFoundAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||||
|
|
||||||
|
login.Name = req.Name;
|
||||||
|
login.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt);
|
||||||
|
login.Salt = salt;
|
||||||
|
await database.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
GetLoginDto responseDto = new()
|
||||||
|
{
|
||||||
|
Id = login.Id,
|
||||||
|
Name = login.Name,
|
||||||
|
Fonction = login.Fonction
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
48
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal file
48
PyroFetes/Endpoints/Login/UserLoginEndpoint.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using FastEndpoints.Security;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using PyroFetes.DTO.Login.Request;
|
||||||
|
using PyroFetes.DTO.Login.Response;
|
||||||
|
|
||||||
|
namespace PyroFetes.Endpoints.Login;
|
||||||
|
|
||||||
|
public class UserLoginEndpoint(PyroFetesDbContext database) : Endpoint<ConnectLoginDto, GetLoginConnectDto>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Post("/login");
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(ConnectLoginDto req, CancellationToken ct)
|
||||||
|
{
|
||||||
|
Models.User? login = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
|
||||||
|
|
||||||
|
if (login == null)
|
||||||
|
{
|
||||||
|
await Send.UnauthorizedAsync(ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BCrypt.Net.BCrypt.Verify(req.Password + login.Salt, login.Password))
|
||||||
|
{
|
||||||
|
string jwtToken = JwtBearer.CreateToken(
|
||||||
|
o =>
|
||||||
|
{
|
||||||
|
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
||||||
|
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||||
|
if (login.Fonction != null) o.User.Roles.Add(login.Fonction);
|
||||||
|
o.User.Claims.Add(("Username", login.Name)!);
|
||||||
|
o.User["UserId"] = "001";
|
||||||
|
});
|
||||||
|
|
||||||
|
GetLoginConnectDto responseDto = new()
|
||||||
|
{
|
||||||
|
Token = jwtToken
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send.OkAsync(responseDto, ct);
|
||||||
|
}
|
||||||
|
else await Send.UnauthorizedAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
package-lock.json
generated
Normal file
18
package-lock.json
generated
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "PyroFete",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"dependencies": {
|
||||||
|
"zone.js": "^0.16.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/zone.js": {
|
||||||
|
"version": "0.16.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.16.0.tgz",
|
||||||
|
"integrity": "sha512-LqLPpIQANebrlxY6jKcYKdgN5DTXyyHAKnnWWjE5pPfEQ4n7j5zn7mOEEpwNZVKGqx3kKKmvplEmoBrvpgROTA==",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
package.json
Normal file
5
package.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"zone.js": "^0.16.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user