diff --git a/src/WorkFlowCheck.API/Controllers/SyncController.cs b/src/WorkFlowCheck.API/Controllers/SyncController.cs index 8bdedbb..11f0fbc 100644 --- a/src/WorkFlowCheck.API/Controllers/SyncController.cs +++ b/src/WorkFlowCheck.API/Controllers/SyncController.cs @@ -41,7 +41,7 @@ namespace WorkFlowCheck.API.Controllers } [HttpGet("GetCheckPoints/{id}")] - public async Task> GetAllCheckpointsAsync(int id) + public async Task> GetCheckpointsAsync(int id) { var retVal = new ApiResponseDTO() { @@ -136,7 +136,7 @@ namespace WorkFlowCheck.API.Controllers return retVal; } - [HttpPost("authenticate")] + [HttpPost("UpdateCheckPoint")] public async Task> UpdateCheckPoint([FromBody] CheckPointDTO checkPointDTO) { var retVal = new ApiResponseDTO() diff --git a/src/WorkFlowCheck.API/ApiKeyMiddleware.cs b/src/WorkFlowCheck.API/Middleware/ApiKeyMiddleware.cs similarity index 97% rename from src/WorkFlowCheck.API/ApiKeyMiddleware.cs rename to src/WorkFlowCheck.API/Middleware/ApiKeyMiddleware.cs index 59acd94..8c8fa22 100644 --- a/src/WorkFlowCheck.API/ApiKeyMiddleware.cs +++ b/src/WorkFlowCheck.API/Middleware/ApiKeyMiddleware.cs @@ -1,4 +1,4 @@ -namespace WorkFlowCheck.API +namespace WorkFlowCheck.API.Middleware { public class ApiKeyMiddleware { diff --git a/src/WorkFlowCheck.API/Middleware/JwtMiddleware.cs b/src/WorkFlowCheck.API/Middleware/JwtMiddleware.cs new file mode 100644 index 0000000..dbca14f --- /dev/null +++ b/src/WorkFlowCheck.API/Middleware/JwtMiddleware.cs @@ -0,0 +1,55 @@ +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; + +namespace WorkFlowCheck.API.Middleware +{ + public class JwtMiddleware + { + private readonly RequestDelegate _next; + public readonly IConfiguration _configuration; + public JwtMiddleware(RequestDelegate next, IConfiguration configuration) + { + _next = next; + _configuration = configuration; + } + public async Task Invoke(HttpContext context) + { + var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); + if (!string.IsNullOrEmpty(token) && ValidateToken(token, out var claims)) + { + context.User = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt")); + } + await _next(context); + } + private bool ValidateToken(string token, out Claim[] claims) + { + var jwtSettings = _configuration.GetSection("Jwt"); + var secretKey = jwtSettings["SecretKey"]; + + claims = null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.UTF8.GetBytes(secretKey); + + try + { + var principal = tokenHandler.ValidateToken(token, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + ClockSkew = TimeSpan.Zero + }, out var validatedToken); + + claims = principal.Claims.ToArray(); + return true; + } + catch + { + return false; + } + } + } +} diff --git a/src/WorkFlowCheck.API/Program.cs b/src/WorkFlowCheck.API/Program.cs index 2cf3beb..adad151 100644 --- a/src/WorkFlowCheck.API/Program.cs +++ b/src/WorkFlowCheck.API/Program.cs @@ -2,7 +2,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using System.Text.Json; using System.Text.Json.Serialization; -using WorkFlowCheck.API; using WorkFlowCheck.DL; using Microsoft.AspNetCore.Mvc; using Serilog; @@ -11,6 +10,7 @@ using WorkFlowCheck.BL.Infra; using WorkFlowCheck.DL.Seed; using WorkFlowCheck.Common.RequestContext; using WorkFlowCheck.API.RequestContext; +using WorkFlowCheck.API.Middleware; var builder = WebApplication.CreateBuilder(args); @@ -85,6 +85,8 @@ builder.Services.AddCors(options => var app = builder.Build(); + + using (var scope = app.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); @@ -104,7 +106,10 @@ if (app.Environment.IsDevelopment()) // Routing és endpointok //app.UseHttpsRedirection(); app.UseRouting(); + app.UseMiddleware(); +app.UseMiddleware(); + app.MapControllers(); app.UseAuthorization(); app.UseCors(); diff --git a/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs b/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs index a76caca..e6332d7 100644 --- a/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs +++ b/src/WorkFlowCheck.BL/Mappings/MapperProfile.cs @@ -24,6 +24,7 @@ namespace WorkFlowCheck.BL.Mappings .ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows)); CreateMap(); CreateMap().ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows)); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/WorkFlowCheck.DL/AppDbContext.cs b/src/WorkFlowCheck.DL/AppDbContext.cs index c02e6d5..69bf86c 100644 --- a/src/WorkFlowCheck.DL/AppDbContext.cs +++ b/src/WorkFlowCheck.DL/AppDbContext.cs @@ -16,17 +16,14 @@ namespace WorkFlowCheck.DL { public class AppDbContext : DbContext { - - -#if !DEBUG -private readonly IRequestContext _requestContext; -public AppDbContext(DbContextOptions options, IRequestContext requestContext) - : base(options) + private readonly IRequestContext _requestContext; + public AppDbContext(DbContextOptions options, IRequestContext requestContext) + : base(options) { _requestContext = requestContext; } -#endif -#if DEBUG + +#if !DEBUG private dynamic _requestContext; public AppDbContext(DbContextOptions options) : base(options) @@ -102,8 +99,8 @@ public AppDbContext(DbContextOptions options, IRequestContext requ { ((IAuditableEntity)entry.Entity).CreatedAt = utcNow; ((IAuditableEntity)entry.Entity).LastModAt = utcNow; - ((IAuditableEntity)entry.Entity).CreatedBy = _requestContext.CurrentUsername; - ((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername; + ((IAuditableEntity)entry.Entity).CreatedBy = _requestContext.CurrentUsername ?? "Admin"; + ((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername ?? "Admin"; } } private void UpdateModifiedEntires(IEnumerable modifiedEntries, DateTime utcNow) @@ -111,7 +108,7 @@ public AppDbContext(DbContextOptions options, IRequestContext requ foreach (var entry in modifiedEntries) { ((IAuditableEntity)entry.Entity).LastModAt = utcNow; - ((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername; + ((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername ?? "Admin" ; } } private void UpdateDeletedEntries(IEnumerable deletedEntries)