From 8901f921c7e9dd586e6ab534f556e53badeee59b Mon Sep 17 00:00:00 2001 From: BuzzLeclair Date: Sun, 2 Mar 2025 16:50:53 +0100 Subject: [PATCH] =?UTF-8?q?-=20Ajout=20d'automapper=20pour=20convertir=20l?= =?UTF-8?q?es=20dto=20en=20entit=C3=A9=20et=20inversement=20-=20Ajout=20de?= =?UTF-8?q?s=20m=C3=A9thodes=20n=C3=A9cessaire=20pour=20la=20gestion=20de?= =?UTF-8?q?=20JWT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ldap-cesi/Configurations/Conf.cs | 126 +++++++++++++++++- ldap-cesi/Context/PgContext.cs | 8 +- ldap-cesi/Controllers/HashController.cs | 22 +++ ldap-cesi/Controllers/JwtController.cs | 28 ++++ ldap-cesi/Controllers/SalarieController.cs | 39 ++++++ ldap-cesi/Controllers/ServicesController.cs | 55 ++++++++ .../Controllers/UtilisateurController.cs | 47 +++++++ .../DTOs/Inputs/Salarie/SalarieCreateDto.cs | 12 ++ .../DTOs/Inputs/Salarie/SalarieUpdateDto.cs | 20 +++ .../DTOs/Inputs/Service/ServiceCreateDto.cs | 6 + .../DTOs/Inputs/Service/ServiceUpdateDto.cs | 8 ++ ldap-cesi/DTOs/Inputs/Site/SiteCreateDto.cs | 6 + ldap-cesi/DTOs/Inputs/Site/SiteUpdateDto.cs | 9 ++ .../Inputs/Utilisateur/UtilisateurLoginDto.cs | 7 + .../Utilisateur/UtilisateurOutputDto.cs | 9 ++ ldap-cesi/Entities/Site.cs | 2 +- ldap-cesi/Entities/Utilisateur.cs | 11 +- ldap-cesi/Mapper/AutoMapperProfile.cs | 16 +++ ldap-cesi/Models/IResponseDataModel.cs | 7 + ldap-cesi/Models/IResponseModel.cs | 8 ++ ldap-cesi/Models/ResponseDataModel.cs | 7 + ldap-cesi/Models/ResponseModel.cs | 10 ++ ldap-cesi/Program.cs | 21 ++- .../Interfaces/IRepositoryBase.cs | 0 .../Interfaces/IRepositoryRole.cs | 0 .../Interfaces/IRepositorySalarie.cs | 0 .../Interfaces/IRepositoryService.cs | 0 .../Interfaces/IRepositorySite.cs | 0 .../Interfaces/IRepositoryUtilisateur.cs | 2 +- .../RepositoryBase.cs | 0 .../RoleRepository.cs | 0 .../SalarieRepository.cs | 0 .../ServiceRepository.cs | 0 .../SiteRepository.cs | 0 .../UtilisateurRepository.cs | 7 + ldap-cesi/Services/Interfaces/IJwtService.cs | 10 ++ ldap-cesi/Services/Interfaces/IRoleService.cs | 6 + .../Services/Interfaces/IRsaKeyService.cs | 8 ++ .../Services/Interfaces/ISalarieService.cs | 10 ++ .../Services/Interfaces/IServiceService.cs | 12 ++ ldap-cesi/Services/Interfaces/ISiteService.cs | 6 + .../Interfaces/IUtilisateurService.cs | 13 ++ ldap-cesi/Services/JwtService.cs | 126 ++++++++++++++++++ ldap-cesi/Services/RoleService.cs | 15 +++ ldap-cesi/Services/RsaKeyService.cs | 36 +++++ ldap-cesi/Services/SalarieService.cs | 38 ++++++ ldap-cesi/Services/ServiceService.cs | 54 ++++++++ ldap-cesi/Services/UtilisateurService.cs | 80 +++++++++++ ldap-cesi/ldap-cesi.csproj | 16 ++- ldap-cesi/ldap-cesi.http | 6 - 50 files changed, 900 insertions(+), 29 deletions(-) create mode 100644 ldap-cesi/Controllers/HashController.cs create mode 100644 ldap-cesi/Controllers/JwtController.cs create mode 100644 ldap-cesi/Controllers/SalarieController.cs create mode 100644 ldap-cesi/Controllers/ServicesController.cs create mode 100644 ldap-cesi/Controllers/UtilisateurController.cs create mode 100644 ldap-cesi/DTOs/Inputs/Salarie/SalarieCreateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Salarie/SalarieUpdateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Service/ServiceCreateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Service/ServiceUpdateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Site/SiteCreateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Site/SiteUpdateDto.cs create mode 100644 ldap-cesi/DTOs/Inputs/Utilisateur/UtilisateurLoginDto.cs create mode 100644 ldap-cesi/DTOs/Outputs/Utilisateur/UtilisateurOutputDto.cs create mode 100644 ldap-cesi/Mapper/AutoMapperProfile.cs create mode 100644 ldap-cesi/Models/IResponseDataModel.cs create mode 100644 ldap-cesi/Models/IResponseModel.cs create mode 100644 ldap-cesi/Models/ResponseDataModel.cs create mode 100644 ldap-cesi/Models/ResponseModel.cs rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositoryBase.cs (100%) rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositoryRole.cs (100%) rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositorySalarie.cs (100%) rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositoryService.cs (100%) rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositorySite.cs (100%) rename ldap-cesi/{Repository => Repositories}/Interfaces/IRepositoryUtilisateur.cs (72%) rename ldap-cesi/{Repository => Repositories}/RepositoryBase.cs (100%) rename ldap-cesi/{Repository => Repositories}/RoleRepository.cs (100%) rename ldap-cesi/{Repository => Repositories}/SalarieRepository.cs (100%) rename ldap-cesi/{Repository => Repositories}/ServiceRepository.cs (100%) rename ldap-cesi/{Repository => Repositories}/SiteRepository.cs (100%) rename ldap-cesi/{Repository => Repositories}/UtilisateurRepository.cs (54%) create mode 100644 ldap-cesi/Services/Interfaces/IJwtService.cs create mode 100644 ldap-cesi/Services/Interfaces/IRoleService.cs create mode 100644 ldap-cesi/Services/Interfaces/IRsaKeyService.cs create mode 100644 ldap-cesi/Services/Interfaces/ISalarieService.cs create mode 100644 ldap-cesi/Services/Interfaces/IServiceService.cs create mode 100644 ldap-cesi/Services/Interfaces/ISiteService.cs create mode 100644 ldap-cesi/Services/Interfaces/IUtilisateurService.cs create mode 100644 ldap-cesi/Services/JwtService.cs create mode 100644 ldap-cesi/Services/RoleService.cs create mode 100644 ldap-cesi/Services/RsaKeyService.cs create mode 100644 ldap-cesi/Services/SalarieService.cs create mode 100644 ldap-cesi/Services/ServiceService.cs create mode 100644 ldap-cesi/Services/UtilisateurService.cs delete mode 100644 ldap-cesi/ldap-cesi.http diff --git a/ldap-cesi/Configurations/Conf.cs b/ldap-cesi/Configurations/Conf.cs index d1dbb77..e4f5b10 100644 --- a/ldap-cesi/Configurations/Conf.cs +++ b/ldap-cesi/Configurations/Conf.cs @@ -1,6 +1,16 @@ +using System.Reflection; +using System.Security.Cryptography; +using System.Text; using ldap_cesi.Context; -using ldap_cesi.Entities; +using ldap_cesi.Repository; +using ldap_cesi.Repository.Services; +using ldap_cesi.Services; +using ldap_cesi.Services.Interfaces; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; +using Serilog; namespace ldap_cesi.Configurations; @@ -8,12 +18,126 @@ public static class Conf { public static void BuildConf(this WebApplicationBuilder builder) { + builder.AddRepositories(); + builder.AddServices(); + builder.Services.AddControllers(); builder.AddEFCoreConfiguration(); + builder.CorseConfiguration(); + builder.AddSwagger(); + builder.AddSerilog(); + builder.AddJwt(); + builder.Services.AddAutoMapper(typeof(Program)); + } + + public static void AddRepositories(this WebApplicationBuilder builder) + { + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); } + public static void AddServices(this WebApplicationBuilder builder) + { + builder.Services.AddScoped(); + // builder.Services.AddScoped(); + // builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddSingleton(); + } public static void AddEFCoreConfiguration(this WebApplicationBuilder builder) { string connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); } + public static void CorseConfiguration(this WebApplicationBuilder builder) + { + builder.Services.AddCors(options => + { + options.AddPolicy("AllowAll", + builder => builder + .AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader()); + }); + } + + public static void AddSerilog(this WebApplicationBuilder builder) + { + var loggerConfiguration = new LoggerConfiguration() + .WriteTo.Console() + .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Hour); + var logger = loggerConfiguration.CreateLogger(); + builder.Logging.AddSerilog(logger); + builder.Services.AddLogging(); + } + + public static void AddJwt(this WebApplicationBuilder builder) + { + var rsaKeyService = builder.Services.BuildServiceProvider().GetRequiredService(); + var rsaKey = rsaKeyService.GetRsaKey(); + + builder.Services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(options => + { + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuer = true, + ValidateAudience = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + ValidIssuer = builder.Configuration["Jwt:Issuer"], + ValidAudience = builder.Configuration["Jwt:Audience"], + IssuerSigningKey = new RsaSecurityKey(rsaKey) + }; + }); + } + + public static void AddSwagger(this WebApplicationBuilder builder) + { + // Add services to the container. + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddAuthorization(); + builder.Configuration.AddEnvironmentVariables(); + builder.Services.AddSwaggerGen(c => + { + c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey, + Scheme = "Bearer", + Description = "JWT Token. Use \"Bearer {token}\"" + }); + c.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Id = "Bearer", + Type = ReferenceType.SecurityScheme + } + }, + new List() + } + }); + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "LDAP - Backend", + Version = "v1" + }); + var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename)); + }); + } } \ No newline at end of file diff --git a/ldap-cesi/Context/PgContext.cs b/ldap-cesi/Context/PgContext.cs index 778e575..fc8c4c3 100644 --- a/ldap-cesi/Context/PgContext.cs +++ b/ldap-cesi/Context/PgContext.cs @@ -26,10 +26,6 @@ public partial class PgContext : DbContext public virtual DbSet Utilisateurs { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263. - => optionsBuilder.UseNpgsql("Host=192.168.1.196;Database=ldap;Username=postgres;Password=pimer0-buzz"); - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => @@ -118,9 +114,13 @@ public partial class PgContext : DbContext entity.Property(e => e.Nom) .HasMaxLength(50) .HasColumnName("nom"); + entity.Property(e => e.Email) + .HasMaxLength(50) + .HasColumnName("email"); entity.Property(e => e.Prenom) .HasMaxLength(50) .HasColumnName("prenom"); + entity.Ignore(e => e.AccessToken); entity.HasOne(d => d.IdRoleNavigation).WithMany(p => p.Utilisateurs) .HasForeignKey(d => d.IdRole) diff --git a/ldap-cesi/Controllers/HashController.cs b/ldap-cesi/Controllers/HashController.cs new file mode 100644 index 0000000..35c7594 --- /dev/null +++ b/ldap-cesi/Controllers/HashController.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Mvc; + +namespace ldap_cesi.Controllers; + + +[ApiController] +[Route("api/[controller]")] +public class HashController : ControllerBase +{ + [HttpPost("hash")] + public IActionResult HashString([FromBody] string StringToHash) + { + if (string.IsNullOrEmpty(StringToHash)) + { + return BadRequest("Vous devez fournir une chaine de caractere pour la hasher."); + } + + string hashedString = BCrypt.Net.BCrypt.HashPassword(StringToHash); + + return Ok(new { HashedString = hashedString }); + } +} \ No newline at end of file diff --git a/ldap-cesi/Controllers/JwtController.cs b/ldap-cesi/Controllers/JwtController.cs new file mode 100644 index 0000000..55fad8d --- /dev/null +++ b/ldap-cesi/Controllers/JwtController.cs @@ -0,0 +1,28 @@ +using ldap_cesi.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace ldap_cesi.Controllers; + +[ApiController] +[Route("/api/jwt")] +public class JwtController : ControllerBase +{ + private readonly IJwtService _jwtService; + + public JwtController(IJwtService jwtService) + { + _jwtService = jwtService; + } + + [HttpGet("public-key")] + public IActionResult GetPublicKey() + { + var publicKey = _jwtService.GetPublicKey(); + if (string.IsNullOrEmpty(publicKey)) + { + return BadRequest("Impossible de récupérer la clé publique"); + } + + return Ok(publicKey); + } +} \ No newline at end of file diff --git a/ldap-cesi/Controllers/SalarieController.cs b/ldap-cesi/Controllers/SalarieController.cs new file mode 100644 index 0000000..fdf1cf7 --- /dev/null +++ b/ldap-cesi/Controllers/SalarieController.cs @@ -0,0 +1,39 @@ +using ldap_cesi.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace ldap_cesi.Controllers; +[ApiController] +[Route("api/salarie")] +public class SalarieController : ControllerBase +{ + private ISalarieService _salarieService; + + public SalarieController(ISalarieService salarieService) + { + _salarieService = salarieService; + } + + // GET: api/salaries + /// + /// Endpoint qui retournes tous les salaries + /// + /// List + [HttpGet] + public async Task GetUtilisateurs() + { + var result = await _salarieService.GetAll(); + return result.Success ? Ok(result) : BadRequest(result); + } + + // GET: api/salaries + /// + /// Endpoint retourne le salarie correspondant à l'id en param + /// + /// Salarie + [HttpGet("{id}")] + public async Task GetUtilisateurById(int id) + { + var result = await _salarieService.GetById(id); + return result.Success ? Ok(result) : BadRequest(result); + } +} \ No newline at end of file diff --git a/ldap-cesi/Controllers/ServicesController.cs b/ldap-cesi/Controllers/ServicesController.cs new file mode 100644 index 0000000..0d0e987 --- /dev/null +++ b/ldap-cesi/Controllers/ServicesController.cs @@ -0,0 +1,55 @@ +using ldap_cesi.DTOs.Inputs.Service; +using ldap_cesi.Services.Interfaces; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace ldap_cesi.Controllers; + +[ApiController] +[Route("api/service")] +public class ServicesController : ControllerBase +{ + private IServiceService _serviceService; + + public ServicesController(IServiceService serviceService) + { + _serviceService = serviceService; + } + + // GET: api/services + /// + /// Endpoint qui retournes tous les services + /// + /// Retourne tous les services + [HttpGet] + // [Authorize(Roles = "admin")] + public async Task GetServices() + { + var result = await _serviceService.GetAll(); + return result.Success ? Ok(result) : BadRequest(result); + } + + // GET: api/services + /// + /// Endpoint retourne le service correspondant à l'id en param + /// + /// Service + [HttpGet("{id}")] + public async Task GetServices(int id) + { + var result = await _serviceService.GetById(id); + return result.Success ? Ok(result) : BadRequest(result); + } + + // {POST}: api/services + /// + /// Endpoint qui créé un service + /// + /// Retourne tous les services + [HttpPost] + public async Task CreateService([FromBody] ServiceCreateDto serviceInputDto) + { + var result = await _serviceService.CreateService(serviceInputDto); + return result.Success ? Ok(result) : BadRequest(result); + } +} \ No newline at end of file diff --git a/ldap-cesi/Controllers/UtilisateurController.cs b/ldap-cesi/Controllers/UtilisateurController.cs new file mode 100644 index 0000000..f9b5a51 --- /dev/null +++ b/ldap-cesi/Controllers/UtilisateurController.cs @@ -0,0 +1,47 @@ +using ldap_cesi.DTOs.Inputs; +using ldap_cesi.Services.Interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace ldap_cesi.Controllers; +[ApiController] +[Route("api/utilisateur")] +public class UtilisateurController : ControllerBase +{ + private IUtilisateurService _utilisateurService; + + public UtilisateurController(IUtilisateurService utilisateurService) + { + _utilisateurService = utilisateurService; + } + + [HttpPost("login")] + public async Task Login([FromBody] UtilisateurLoginDto utilisateurInput) + { + var response = await _utilisateurService.Login(utilisateurInput); + return StatusCode(response.StatusCode, response); + } + + // GET: api/utilisateurs + /// + /// Endpoint qui retournes tous les utilisateurs + /// + /// List + [HttpGet] + public async Task GetUtilisateurs() + { + var result = await _utilisateurService.GetAll(); + return result.Success ? Ok(result) : BadRequest(result); + } + + // GET: api/utilisateur + /// + /// Endpoint retourne l'utilisateur correspondant à l'id en param + /// + /// Utilisateur + [HttpGet("{id}")] + public async Task GetUtilisateurById(int id) + { + var result = await _utilisateurService.GetById(id); + return result.Success ? Ok(result) : BadRequest(result); + } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Salarie/SalarieCreateDto.cs b/ldap-cesi/DTOs/Inputs/Salarie/SalarieCreateDto.cs new file mode 100644 index 0000000..7804ce4 --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Salarie/SalarieCreateDto.cs @@ -0,0 +1,12 @@ +namespace ldap_cesi.DTOs.Inputs.Salarie; + +public class SalarieCreateDto +{ + public string Nom { get; set; } + public string Prenom { get; set; } + public string Telephone_fix { get; set; } + public string Telephone_portable { get; set; } + public string Email { get; set; } + public int IdSite { get; set; } + public int IdService { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Salarie/SalarieUpdateDto.cs b/ldap-cesi/DTOs/Inputs/Salarie/SalarieUpdateDto.cs new file mode 100644 index 0000000..3245bfd --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Salarie/SalarieUpdateDto.cs @@ -0,0 +1,20 @@ +namespace ldap_cesi.DTOs.Inputs.Service; + +public class SalarieUpdateDto +{ + public int Id { get; set; } + + public string Nom { get; set; } = null!; + + public string Prenom { get; set; } = null!; + + public string TelephoneFixe { get; set; } = null!; + + public string TelephonePortable { get; set; } = null!; + + public string Email { get; set; } = null!; + + public int IdSite { get; set; } + + public int IdService { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Service/ServiceCreateDto.cs b/ldap-cesi/DTOs/Inputs/Service/ServiceCreateDto.cs new file mode 100644 index 0000000..11d3052 --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Service/ServiceCreateDto.cs @@ -0,0 +1,6 @@ +namespace ldap_cesi.DTOs.Inputs.Service; + +public class ServiceCreateDto +{ + public string Nom { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Service/ServiceUpdateDto.cs b/ldap-cesi/DTOs/Inputs/Service/ServiceUpdateDto.cs new file mode 100644 index 0000000..37e44b5 --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Service/ServiceUpdateDto.cs @@ -0,0 +1,8 @@ +namespace ldap_cesi.DTOs.Inputs.Service; + +public class ServiceUpdateDto +{ + public int Id { get; set; } + + public string Nom { get; set; } = null!; +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Site/SiteCreateDto.cs b/ldap-cesi/DTOs/Inputs/Site/SiteCreateDto.cs new file mode 100644 index 0000000..706affd --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Site/SiteCreateDto.cs @@ -0,0 +1,6 @@ +namespace ldap_cesi.DTOs.Inputs.Site; + +public class SiteCreateDto +{ + public string Ville { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Site/SiteUpdateDto.cs b/ldap-cesi/DTOs/Inputs/Site/SiteUpdateDto.cs new file mode 100644 index 0000000..bcd5fb7 --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Site/SiteUpdateDto.cs @@ -0,0 +1,9 @@ +namespace ldap_cesi.DTOs.Inputs.Site; + +public class SiteUpdateDto +{ + public int Id { get; set; } + + public string Ville { get; set; } + +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Inputs/Utilisateur/UtilisateurLoginDto.cs b/ldap-cesi/DTOs/Inputs/Utilisateur/UtilisateurLoginDto.cs new file mode 100644 index 0000000..2ce64f1 --- /dev/null +++ b/ldap-cesi/DTOs/Inputs/Utilisateur/UtilisateurLoginDto.cs @@ -0,0 +1,7 @@ +namespace ldap_cesi.DTOs.Inputs; + +public class UtilisateurLoginDto +{ + public string Email { get; set; } + public string MotDePasse { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/DTOs/Outputs/Utilisateur/UtilisateurOutputDto.cs b/ldap-cesi/DTOs/Outputs/Utilisateur/UtilisateurOutputDto.cs new file mode 100644 index 0000000..b508c8d --- /dev/null +++ b/ldap-cesi/DTOs/Outputs/Utilisateur/UtilisateurOutputDto.cs @@ -0,0 +1,9 @@ +namespace ldap_cesi.DTOs.Outputs.Utilisateur; + +public class UtilisateurOutputDto +{ + public int Id { get; set; } + public string Email { get; set; } + public string Nom { get; set; } + public string RoleNom { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/Entities/Site.cs b/ldap-cesi/Entities/Site.cs index 40c7f26..1ed7f2a 100644 --- a/ldap-cesi/Entities/Site.cs +++ b/ldap-cesi/Entities/Site.cs @@ -7,7 +7,7 @@ public partial class Site { public int Id { get; set; } - public string? Ville { get; set; } + public string Ville { get; set; } public virtual ICollection Salaries { get; set; } = new List(); } diff --git a/ldap-cesi/Entities/Utilisateur.cs b/ldap-cesi/Entities/Utilisateur.cs index 8f2223b..9037a5c 100644 --- a/ldap-cesi/Entities/Utilisateur.cs +++ b/ldap-cesi/Entities/Utilisateur.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; namespace ldap_cesi.Entities; @@ -7,13 +8,15 @@ public partial class Utilisateur { public int Id { get; set; } - public string? Nom { get; set; } + public string Nom { get; set; } - public string? Prenom { get; set; } + public string Prenom { get; set; } - public string? MotDePasse { get; set; } + public string MotDePasse { get; set; } + public string Email { get; set; } public int IdRole { get; set; } - + [NotMapped] + public string AccessToken { get; set; } public virtual Role IdRoleNavigation { get; set; } = null!; } diff --git a/ldap-cesi/Mapper/AutoMapperProfile.cs b/ldap-cesi/Mapper/AutoMapperProfile.cs new file mode 100644 index 0000000..15fc504 --- /dev/null +++ b/ldap-cesi/Mapper/AutoMapperProfile.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using ldap_cesi.DTOs.Inputs.Service; +using ldap_cesi.DTOs.Outputs.Utilisateur; +using ldap_cesi.Entities; + +namespace ldap_cesi.Mapper; + +public class AutoMapperProfile : Profile +{ + public AutoMapperProfile() + { + CreateMap() + .ForMember(dest => dest.RoleNom, opt => opt.MapFrom(src => src.IdRoleNavigation.Nom)); + CreateMap(); + } +} \ No newline at end of file diff --git a/ldap-cesi/Models/IResponseDataModel.cs b/ldap-cesi/Models/IResponseDataModel.cs new file mode 100644 index 0000000..1eb89dc --- /dev/null +++ b/ldap-cesi/Models/IResponseDataModel.cs @@ -0,0 +1,7 @@ +namespace ldap_cesi.Models; + +public interface IResponseDataModel : IResponseModel +{ + public T Data { get; set; } + string Token { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/Models/IResponseModel.cs b/ldap-cesi/Models/IResponseModel.cs new file mode 100644 index 0000000..1cb04de --- /dev/null +++ b/ldap-cesi/Models/IResponseModel.cs @@ -0,0 +1,8 @@ +namespace ldap_cesi.Models; + +public interface IResponseModel +{ + public int StatusCode { get; set; } + public string? Message { get; set; } + public bool Success { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/Models/ResponseDataModel.cs b/ldap-cesi/Models/ResponseDataModel.cs new file mode 100644 index 0000000..eb66c0f --- /dev/null +++ b/ldap-cesi/Models/ResponseDataModel.cs @@ -0,0 +1,7 @@ +namespace ldap_cesi.Models; + +public class ResponseDataModel : ResponseModel, IResponseDataModel where T : class +{ + public T Data { get; set; } = null!; + public string Token { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/Models/ResponseModel.cs b/ldap-cesi/Models/ResponseModel.cs new file mode 100644 index 0000000..57393de --- /dev/null +++ b/ldap-cesi/Models/ResponseModel.cs @@ -0,0 +1,10 @@ +namespace ldap_cesi.Models; + +public class ResponseModel : IResponseModel +{ + public bool Success { get; set; } + public string? Message { get; set; } + public string? TokenJWT { get; set; } + + public int StatusCode { get; set; } +} \ No newline at end of file diff --git a/ldap-cesi/Program.cs b/ldap-cesi/Program.cs index 5881c4b..e5ec0b7 100644 --- a/ldap-cesi/Program.cs +++ b/ldap-cesi/Program.cs @@ -1,18 +1,29 @@ using ldap_cesi.Configurations; var builder = WebApplication.CreateBuilder(args); - builder.BuildConf(); -builder.Services.AddOpenApi(); - var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.MapOpenApi(); + app.UseSwagger(); + app.UseSwaggerUI(); + + app.Use(async (context, next) => + { + if (context.Request.Path == "/") + { + context.Response.Redirect("/swagger"); + return; + } + await next(); + }); } app.UseHttpsRedirection(); - +app.UseRouting(); +app.UseAuthentication(); +app.UseAuthorization(); +app.MapControllers(); app.Run(); diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryBase.cs b/ldap-cesi/Repositories/Interfaces/IRepositoryBase.cs similarity index 100% rename from ldap-cesi/Repository/Interfaces/IRepositoryBase.cs rename to ldap-cesi/Repositories/Interfaces/IRepositoryBase.cs diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryRole.cs b/ldap-cesi/Repositories/Interfaces/IRepositoryRole.cs similarity index 100% rename from ldap-cesi/Repository/Interfaces/IRepositoryRole.cs rename to ldap-cesi/Repositories/Interfaces/IRepositoryRole.cs diff --git a/ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs b/ldap-cesi/Repositories/Interfaces/IRepositorySalarie.cs similarity index 100% rename from ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs rename to ldap-cesi/Repositories/Interfaces/IRepositorySalarie.cs diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryService.cs b/ldap-cesi/Repositories/Interfaces/IRepositoryService.cs similarity index 100% rename from ldap-cesi/Repository/Interfaces/IRepositoryService.cs rename to ldap-cesi/Repositories/Interfaces/IRepositoryService.cs diff --git a/ldap-cesi/Repository/Interfaces/IRepositorySite.cs b/ldap-cesi/Repositories/Interfaces/IRepositorySite.cs similarity index 100% rename from ldap-cesi/Repository/Interfaces/IRepositorySite.cs rename to ldap-cesi/Repositories/Interfaces/IRepositorySite.cs diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs b/ldap-cesi/Repositories/Interfaces/IRepositoryUtilisateur.cs similarity index 72% rename from ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs rename to ldap-cesi/Repositories/Interfaces/IRepositoryUtilisateur.cs index 4235770..e05950f 100644 --- a/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs +++ b/ldap-cesi/Repositories/Interfaces/IRepositoryUtilisateur.cs @@ -4,5 +4,5 @@ namespace ldap_cesi.Repository.Services; public interface IRepositoryUtilisateur : IRepositoryBase { - + Task GetByEmailAsync(string email); } \ No newline at end of file diff --git a/ldap-cesi/Repository/RepositoryBase.cs b/ldap-cesi/Repositories/RepositoryBase.cs similarity index 100% rename from ldap-cesi/Repository/RepositoryBase.cs rename to ldap-cesi/Repositories/RepositoryBase.cs diff --git a/ldap-cesi/Repository/RoleRepository.cs b/ldap-cesi/Repositories/RoleRepository.cs similarity index 100% rename from ldap-cesi/Repository/RoleRepository.cs rename to ldap-cesi/Repositories/RoleRepository.cs diff --git a/ldap-cesi/Repository/SalarieRepository.cs b/ldap-cesi/Repositories/SalarieRepository.cs similarity index 100% rename from ldap-cesi/Repository/SalarieRepository.cs rename to ldap-cesi/Repositories/SalarieRepository.cs diff --git a/ldap-cesi/Repository/ServiceRepository.cs b/ldap-cesi/Repositories/ServiceRepository.cs similarity index 100% rename from ldap-cesi/Repository/ServiceRepository.cs rename to ldap-cesi/Repositories/ServiceRepository.cs diff --git a/ldap-cesi/Repository/SiteRepository.cs b/ldap-cesi/Repositories/SiteRepository.cs similarity index 100% rename from ldap-cesi/Repository/SiteRepository.cs rename to ldap-cesi/Repositories/SiteRepository.cs diff --git a/ldap-cesi/Repository/UtilisateurRepository.cs b/ldap-cesi/Repositories/UtilisateurRepository.cs similarity index 54% rename from ldap-cesi/Repository/UtilisateurRepository.cs rename to ldap-cesi/Repositories/UtilisateurRepository.cs index 57cf96c..0cf84cf 100644 --- a/ldap-cesi/Repository/UtilisateurRepository.cs +++ b/ldap-cesi/Repositories/UtilisateurRepository.cs @@ -1,6 +1,7 @@ using ldap_cesi.Context; using ldap_cesi.Entities; using ldap_cesi.Repository.Services; +using Microsoft.EntityFrameworkCore; namespace ldap_cesi.Repository; @@ -10,4 +11,10 @@ public class UtilisateurRepository : RepositoryBase, IRepositoryUti { } + + public async Task GetByEmailAsync(string email) + { + return await _context.Utilisateurs.Include(u => u.IdRoleNavigation) + .FirstOrDefaultAsync(u => u.Email == email); + } } \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IJwtService.cs b/ldap-cesi/Services/Interfaces/IJwtService.cs new file mode 100644 index 0000000..1ba65ee --- /dev/null +++ b/ldap-cesi/Services/Interfaces/IJwtService.cs @@ -0,0 +1,10 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Services.Interfaces; + +public interface IJwtService +{ + string GenerateToken(Utilisateur utilisateur); + string GetPublicKey(); + Task ValidateToken(string token, int userId); +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IRoleService.cs b/ldap-cesi/Services/Interfaces/IRoleService.cs new file mode 100644 index 0000000..b558e3b --- /dev/null +++ b/ldap-cesi/Services/Interfaces/IRoleService.cs @@ -0,0 +1,6 @@ +namespace ldap_cesi.Services.Interfaces; + +public interface IRoleService +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IRsaKeyService.cs b/ldap-cesi/Services/Interfaces/IRsaKeyService.cs new file mode 100644 index 0000000..a08d475 --- /dev/null +++ b/ldap-cesi/Services/Interfaces/IRsaKeyService.cs @@ -0,0 +1,8 @@ +using System.Security.Cryptography; + +namespace ldap_cesi.Services.Interfaces; + +public interface IRsaKeyService +{ + RSA GetRsaKey(); +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/ISalarieService.cs b/ldap-cesi/Services/Interfaces/ISalarieService.cs new file mode 100644 index 0000000..c6bce16 --- /dev/null +++ b/ldap-cesi/Services/Interfaces/ISalarieService.cs @@ -0,0 +1,10 @@ +using ldap_cesi.Entities; +using ldap_cesi.Models; + +namespace ldap_cesi.Services.Interfaces; + +public interface ISalarieService +{ + Task>> GetAll(); + Task> GetById(int id); +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IServiceService.cs b/ldap-cesi/Services/Interfaces/IServiceService.cs new file mode 100644 index 0000000..f44a387 --- /dev/null +++ b/ldap-cesi/Services/Interfaces/IServiceService.cs @@ -0,0 +1,12 @@ +using ldap_cesi.DTOs.Inputs.Service; +using ldap_cesi.Entities; +using ldap_cesi.Models; + +namespace ldap_cesi.Services.Interfaces; + +public interface IServiceService +{ + Task>> GetAll(); + Task> GetById(int id); + Task> CreateService(ServiceCreateDto serviceCreateDto); +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/ISiteService.cs b/ldap-cesi/Services/Interfaces/ISiteService.cs new file mode 100644 index 0000000..2b37660 --- /dev/null +++ b/ldap-cesi/Services/Interfaces/ISiteService.cs @@ -0,0 +1,6 @@ +namespace ldap_cesi.Services.Interfaces; + +public interface ISiteService +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IUtilisateurService.cs b/ldap-cesi/Services/Interfaces/IUtilisateurService.cs new file mode 100644 index 0000000..fb93caf --- /dev/null +++ b/ldap-cesi/Services/Interfaces/IUtilisateurService.cs @@ -0,0 +1,13 @@ +using ldap_cesi.DTOs.Inputs; +using ldap_cesi.DTOs.Outputs.Utilisateur; +using ldap_cesi.Entities; +using ldap_cesi.Models; + +namespace ldap_cesi.Services.Interfaces; + +public interface IUtilisateurService +{ + Task>> GetAll(); + Task> GetById(int id); + Task> Login(UtilisateurLoginDto utilisateurInput); +} \ No newline at end of file diff --git a/ldap-cesi/Services/JwtService.cs b/ldap-cesi/Services/JwtService.cs new file mode 100644 index 0000000..085d41e --- /dev/null +++ b/ldap-cesi/Services/JwtService.cs @@ -0,0 +1,126 @@ +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Security.Cryptography; +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Services.Interfaces; +using Microsoft.IdentityModel.Tokens; + +namespace ldap_cesi.Services; + +public class JwtService : IJwtService +{ + private readonly IRsaKeyService _rsaKeyService; + private readonly IConfiguration _configuration; + private readonly PgContext _context; + private readonly ILogger _logger; + + public JwtService( + IConfiguration configuration, + PgContext context, + IRsaKeyService rsaKeyService, + ILogger logger) + { + _configuration = configuration; + _context = context; + _rsaKeyService = rsaKeyService; + _logger = logger; + } + + public string GenerateToken(Utilisateur utilisateur) + { + try + { + var claims = new[] + { + new Claim(JwtRegisteredClaimNames.Sub, utilisateur.Email), + new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), + new Claim(ClaimTypes.Name, utilisateur.Nom), + new Claim(ClaimTypes.Role, utilisateur.IdRoleNavigation.Nom), + new Claim(ClaimTypes.NameIdentifier, utilisateur.Id.ToString()) + }; + + var key = new RsaSecurityKey(_rsaKeyService.GetRsaKey()); + var creds = new SigningCredentials(key, SecurityAlgorithms.RsaSha256); + + var tokenExpiryMinutes = _configuration.GetValue("Jwt:TokenExpiryMinutes", 30); + var token = new JwtSecurityToken( + issuer: _configuration["Jwt:Issuer"], + audience: _configuration["Jwt:Audience"], + claims: claims, + expires: DateTime.UtcNow.AddMinutes(tokenExpiryMinutes), + signingCredentials: creds); + + var tokenString = new JwtSecurityTokenHandler().WriteToken(token); + utilisateur.AccessToken = tokenString; + _context.Utilisateurs.Update(utilisateur); + _context.SaveChanges(); + + return tokenString; + } + catch (Exception ex) + { + _logger.LogError(ex, "An error occurred while generating the JWT token."); + throw; + } + } + + public string GetPublicKey() + { + try + { + var publicKey = _rsaKeyService.GetRsaKey().ExportSubjectPublicKeyInfo(); + return Convert.ToBase64String(publicKey); + } + catch (Exception ex) + { + _logger.LogError(ex, "An error occurred while retrieving the public key."); + throw; + } + } + + public async Task ValidateToken(string token, int userId) + { + try + { + var tokenHandler = new JwtSecurityTokenHandler(); + var validationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new RsaSecurityKey(_rsaKeyService.GetRsaKey()), + ValidateIssuer = true, + ValidateAudience = true, + ValidIssuer = _configuration["Jwt:Issuer"], + ValidAudience = _configuration["Jwt:Audience"], + ClockSkew = TimeSpan.Zero, + ValidateLifetime = true + }; + + var principal = tokenHandler.ValidateToken(token, validationParameters, out var validatedToken); + var nameIdClaim = principal.FindFirst(ClaimTypes.NameIdentifier); + if (nameIdClaim == null || !int.TryParse(nameIdClaim.Value, out var tokenUserId) || tokenUserId != userId) + { + _logger.LogWarning("Token validation failed: Invalid user ID."); + return false; + } + + var utilisateur = await _context.Utilisateurs.FindAsync(userId); + if (utilisateur == null) + { + _logger.LogWarning("Token validation failed: User not found."); + return false; + } + + utilisateur.AccessToken = token; + _context.Utilisateurs.Update(utilisateur); + await _context.SaveChangesAsync(); + + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "An error occurred while validating the JWT token."); + return false; + } + } +} \ No newline at end of file diff --git a/ldap-cesi/Services/RoleService.cs b/ldap-cesi/Services/RoleService.cs new file mode 100644 index 0000000..b388bae --- /dev/null +++ b/ldap-cesi/Services/RoleService.cs @@ -0,0 +1,15 @@ +using ldap_cesi.Repository.Services; +using ldap_cesi.Services.Interfaces; + +namespace ldap_cesi.Services; + +public class RoleService : IRoleService +{ + private IRepositoryRole _repositoryRole; + + public RoleService(IRepositoryRole repositoryRole) + { + _repositoryRole = repositoryRole; + } + +} \ No newline at end of file diff --git a/ldap-cesi/Services/RsaKeyService.cs b/ldap-cesi/Services/RsaKeyService.cs new file mode 100644 index 0000000..a71da2f --- /dev/null +++ b/ldap-cesi/Services/RsaKeyService.cs @@ -0,0 +1,36 @@ +using System.Security.Cryptography; +using ldap_cesi.Services.Interfaces; + +namespace ldap_cesi.Services; + +public class RsaKeyService : IRsaKeyService +{ + private readonly RSA _rsa; + private readonly string _keyPath; + + public RsaKeyService(IConfiguration configuration) + { + _rsa = RSA.Create(); + _keyPath = configuration["Jwt:KeyPath"] ?? "cle.bin"; + LoadOrCreateRsaKey(); + } + + private void LoadOrCreateRsaKey() + { + if (File.Exists(_keyPath)) + { + var keyBytes = File.ReadAllBytes(_keyPath); + _rsa.ImportRSAPrivateKey(keyBytes, out _); + } + else + { + var keyBytes = _rsa.ExportRSAPrivateKey(); + File.WriteAllBytes(_keyPath, keyBytes); + } + } + + public RSA GetRsaKey() + { + return _rsa; + } +} \ No newline at end of file diff --git a/ldap-cesi/Services/SalarieService.cs b/ldap-cesi/Services/SalarieService.cs new file mode 100644 index 0000000..be4410a --- /dev/null +++ b/ldap-cesi/Services/SalarieService.cs @@ -0,0 +1,38 @@ +using ldap_cesi.Entities; +using ldap_cesi.Models; +using ldap_cesi.Repository.Services; +using ldap_cesi.Services.Interfaces; + +namespace ldap_cesi.Services; + +public class SalarieService : ISalarieService +{ + private IRepositorySalarie _repositorySalarie; + + public SalarieService(IRepositorySalarie repositorySalarie) + { + _repositorySalarie = repositorySalarie; + } + + public async Task>> GetAll() + { + var salaries = await _repositorySalarie.GetAllAsync(); + return new ResponseDataModel> + { + Success = true, + Data = salaries, + StatusCode = 200, + }; + } + + public async Task> GetById(int id) + { + var service = await _repositorySalarie.GetByIdAsync(id); + return new ResponseDataModel + { + Success = true, + Data = service, + StatusCode = 200, + }; + } +} \ No newline at end of file diff --git a/ldap-cesi/Services/ServiceService.cs b/ldap-cesi/Services/ServiceService.cs new file mode 100644 index 0000000..c29cb22 --- /dev/null +++ b/ldap-cesi/Services/ServiceService.cs @@ -0,0 +1,54 @@ +using AutoMapper; +using ldap_cesi.DTOs.Inputs.Service; +using ldap_cesi.Entities; +using ldap_cesi.Models; +using ldap_cesi.Repository.Services; +using ldap_cesi.Services.Interfaces; + +namespace ldap_cesi.Services; + +public class ServiceService : IServiceService +{ + private readonly IRepositoryService _repositoryService; + private readonly IMapper _mapper; + + public ServiceService(IRepositoryService repositoryService, IMapper mapper) + { + _repositoryService = repositoryService; + _mapper = mapper; + } + + + public async Task>> GetAll() + { + var services = await _repositoryService.GetAllAsync(); + return new ResponseDataModel> + { + Success = true, + Data = services, + StatusCode = 200, + }; + } + + public async Task> GetById(int id) + { + var service = await _repositoryService.GetByIdAsync(id); + return new ResponseDataModel + { + Success = true, + Data = service, + StatusCode = 200, + }; + } + + public async Task> CreateService(ServiceCreateDto serviceCreateDto) + { + var service = _mapper.Map(serviceCreateDto); + var response = await _repositoryService.AddAsync(service); + return new ResponseDataModel + { + Success = true, + Data = response.Id.ToString(), + }; + } +} \ No newline at end of file diff --git a/ldap-cesi/Services/UtilisateurService.cs b/ldap-cesi/Services/UtilisateurService.cs new file mode 100644 index 0000000..bc7a00f --- /dev/null +++ b/ldap-cesi/Services/UtilisateurService.cs @@ -0,0 +1,80 @@ +using AutoMapper; +using ldap_cesi.DTOs.Inputs; +using ldap_cesi.DTOs.Outputs.Utilisateur; +using ldap_cesi.Entities; +using ldap_cesi.Models; +using ldap_cesi.Repository.Services; +using ldap_cesi.Services.Interfaces; + +namespace ldap_cesi.Services; + +public class UtilisateurService : IUtilisateurService +{ + private readonly IRepositoryUtilisateur _repositoryUtilisateur; + private readonly IJwtService _jwtService; + private readonly IMapper _mapper; + public UtilisateurService(IRepositoryUtilisateur repositoryUtilisateur, IJwtService jwtService, IMapper mapper) + { + _repositoryUtilisateur = repositoryUtilisateur; + _jwtService = jwtService; + _mapper = mapper; + } + + public async Task>> GetAll() + { + var utilisateurs = await _repositoryUtilisateur.GetAllAsync(); + return new ResponseDataModel> + { + Success = true, + StatusCode = 200, + Data = utilisateurs, + }; + } + + public async Task> GetById(int id) + { + var utililisateur = await _repositoryUtilisateur.GetByIdAsync(id); + return new ResponseDataModel + { + Success = true, + StatusCode = 200, + Data = utililisateur, + }; + } + + public async Task> Login(UtilisateurLoginDto utilisateurInput) + { + var utilisateur = await _repositoryUtilisateur.GetByEmailAsync(utilisateurInput.Email); + + if (utilisateur == null) + { + return new ResponseDataModel + { + Success = false, + StatusCode = 404, + Message = "Utilisateur non trouvé." + }; + } + + if (!BCrypt.Net.BCrypt.Verify(utilisateurInput.MotDePasse, utilisateur.MotDePasse)) + { + return new ResponseDataModel + { + Success = false, + StatusCode = 401, + Message = "Mot de passe incorrect." + }; + } + var token = _jwtService.GenerateToken(utilisateur); + var utilisateurOutputDto = _mapper.Map(utilisateur); + + return new ResponseDataModel + { + Success = true, + StatusCode = 200, + Data = utilisateurOutputDto, + Token = token, + Message = "Connexion réussie." + }; + } +} \ No newline at end of file diff --git a/ldap-cesi/ldap-cesi.csproj b/ldap-cesi/ldap-cesi.csproj index 99d7a59..0d8adcc 100644 --- a/ldap-cesi/ldap-cesi.csproj +++ b/ldap-cesi/ldap-cesi.csproj @@ -5,30 +5,32 @@ enable enable ldap_cesi + true - + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + - - - + diff --git a/ldap-cesi/ldap-cesi.http b/ldap-cesi/ldap-cesi.http deleted file mode 100644 index 352c4f9..0000000 --- a/ldap-cesi/ldap-cesi.http +++ /dev/null @@ -1,6 +0,0 @@ -@ldap_cesi_HostAddress = http://localhost:5080 - -GET {{ldap_cesi_HostAddress}}/weatherforecast/ -Accept: application/json - -###