chore(API): Hinzufügen von .net 7-Unterstützung für API

This commit is contained in:
Developer 02 2025-03-13 10:12:55 +01:00
parent 10b557374d
commit fb38bc1fd4
11 changed files with 216 additions and 174 deletions

View File

@ -6,13 +6,15 @@ using WorkFlow.Application.Contracts;
using WorkFlow.Application.DTO.Config; using WorkFlow.Application.DTO.Config;
using WorkFlow.Domain.Entities; using WorkFlow.Domain.Entities;
namespace WorkFlow.API.Controllers namespace WorkFlow.API.Controllers;
{
[APIKeyAuth] [APIKeyAuth]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class ConfigController(ILogger<ConfigController> logger, IConfigService service) : CRUDControllerBaseWithErrorHandling<IConfigService, ConfigCreateDto, ConfigDto, ConfigUpdateDto, Config, int>(logger, service) public class ConfigController : CRUDControllerBaseWithErrorHandling<IConfigService, ConfigCreateDto, ConfigDto, ConfigUpdateDto, Config, int>
{
public ConfigController(ILogger<ConfigController> logger, IConfigService service) : base(logger, service)
{ {
} }
} }

View File

@ -6,13 +6,15 @@ using WorkFlow.Application.Contracts;
using WorkFlow.Application.DTO.Profile; using WorkFlow.Application.DTO.Profile;
using WorkFlow.Domain.Entities; using WorkFlow.Domain.Entities;
namespace WorkFlow.API.Controllers namespace WorkFlow.API.Controllers;
{
[APIKeyAuth] [APIKeyAuth]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class ProfileController(ILogger<ProfileController> logger, IProfileService service) : CRUDControllerBaseWithErrorHandling<IProfileService, ProfileCreateDto, ProfileDto, ProfileUpdateDto, Profile, int>(logger, service) public class ProfileController : CRUDControllerBaseWithErrorHandling<IProfileService, ProfileCreateDto, ProfileDto, ProfileUpdateDto, Profile, int>
{
public ProfileController(ILogger<ProfileController> logger, IProfileService service) : base(logger, service)
{ {
} }
} }

View File

@ -7,14 +7,21 @@ using WorkFlow.Application.Contracts;
using WorkFlow.Application.DTO.ProfileControlsTF; using WorkFlow.Application.DTO.ProfileControlsTF;
using WorkFlow.Domain.Entities; using WorkFlow.Domain.Entities;
namespace WorkFlow.API.Controllers namespace WorkFlow.API.Controllers;
{
[APIKeyAuth] [APIKeyAuth]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class ProfileControlsTFController(ILogger<ProfileControlsTFController> logger, IProfileControlsTFService service) : CRUDControllerBase<IProfileControlsTFService, ProfileControlsTFCreateDto, ProfileControlsTFDto, ProfileControlsTFUpdateDto, ProfileControlsTF, int>(logger, service) public class ProfileControlsTFController : CRUDControllerBase<IProfileControlsTFService, ProfileControlsTFCreateDto, ProfileControlsTFDto, ProfileControlsTFUpdateDto, ProfileControlsTF, int>
{ {
private readonly ILogger<ProfileControlsTFController> logger;
public ProfileControlsTFController(ILogger<ProfileControlsTFController> logger, IProfileControlsTFService service) : base(logger, service)
{
this.logger = logger;
}
[NonAction] [NonAction]
public override Task<IActionResult> GetAll() => base.GetAll(); public override Task<IActionResult> GetAll() => base.GetAll();
@ -117,4 +124,3 @@ namespace WorkFlow.API.Controllers
} }
} }
} }
}

View File

@ -13,8 +13,15 @@ namespace WorkFlow.API.Controllers
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class ProfileObjStateController(ILogger<ProfileObjStateController> logger, IProfileObjStateService service) : CRUDControllerBaseWithErrorHandling<IProfileObjStateService, ProfileObjStateCreateDto, ProfileObjStateDto, ProfileObjStateUpdateDto, ProfileObjState, int>(logger, service) public class ProfileObjStateController : CRUDControllerBaseWithErrorHandling<IProfileObjStateService, ProfileObjStateCreateDto, ProfileObjStateDto, ProfileObjStateUpdateDto, ProfileObjState, int>
{ {
private readonly ILogger<ProfileObjStateController> logger;
public ProfileObjStateController(ILogger<ProfileObjStateController> logger, IProfileObjStateService service) : base(logger, service)
{
this.logger = logger;
}
[NonAction] [NonAction]
public override Task<IActionResult> GetAll() => base.GetAll(); public override Task<IActionResult> GetAll() => base.GetAll();

View File

@ -6,13 +6,15 @@ using WorkFlow.Application.Contracts;
using WorkFlow.Application.DTO.State; using WorkFlow.Application.DTO.State;
using WorkFlow.Domain.Entities; using WorkFlow.Domain.Entities;
namespace WorkFlow.API.Controllers namespace WorkFlow.API.Controllers;
{
[APIKeyAuth] [APIKeyAuth]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class StateController(ILogger<StateController> logger, IStateService service) : CRUDControllerBaseWithErrorHandling<IStateService, StateCreateDto, StateDto, StateUpdateDto, State, int>(logger, service) public class StateController : CRUDControllerBaseWithErrorHandling<IStateService, StateCreateDto, StateDto, StateUpdateDto, State, int>
{
public StateController(ILogger<StateController> logger, IStateService service) : base(logger, service)
{ {
} }
} }

View File

@ -4,14 +4,23 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using WorkFlow.API.Attributes; using WorkFlow.API.Attributes;
namespace WorkFlow.API.Controllers namespace WorkFlow.API.Controllers;
{
[APIKeyAuth] [APIKeyAuth]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class UserController(ILogger<UserController> logger, IUserService userService) : ControllerBase public class UserController : ControllerBase
{ {
private readonly ILogger<UserController> logger;
private readonly IUserService userService;
public UserController(ILogger<UserController> logger, IUserService userService)
{
this.logger = logger;
this.userService = userService;
}
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAsync() public async Task<IActionResult> GetAsync()
{ {
@ -43,4 +52,3 @@ namespace WorkFlow.API.Controllers
} }
} }
} }
}

View File

@ -4,11 +4,18 @@ using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using WorkFlow.API.Models; using WorkFlow.API.Models;
namespace WorkFlow.API.Filters namespace WorkFlow.API.Filters;
public class APIKeyAuthHeaderOpFilter : IOperationFilter
{ {
public class APIKeyAuthHeaderOpFilter(IOptions<APIKeyAuthOptions> options, IWebHostEnvironment environment) : IOperationFilter private readonly APIKeyAuthOptions apiKeyAuthOptions;
private readonly IWebHostEnvironment environment;
public APIKeyAuthHeaderOpFilter(IOptions<APIKeyAuthOptions> options, IWebHostEnvironment environment)
{ {
private readonly APIKeyAuthOptions apiKeyAuthOptions = options.Value; this.environment = environment;
apiKeyAuthOptions = options.Value;
}
public void Apply(OpenApiOperation operation, OperationFilterContext context) public void Apply(OpenApiOperation operation, OperationFilterContext context)
{ {
@ -33,4 +40,3 @@ namespace WorkFlow.API.Filters
operation.Parameters.Add(param); operation.Parameters.Add(param);
} }
} }
}

View File

@ -1,14 +1,22 @@
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace WorkFlow.API.Filters namespace WorkFlow.API.Filters;
public class APIKeyAuthFilter : IAuthorizationFilter
{ {
public class APIKeyAuthFilter(Func<string?, bool> isValidKey, string headerName = "X-API-Key") : IAuthorizationFilter private readonly Func<string?, bool> isValidKey;
private readonly string headerName;
public APIKeyAuthFilter(Func<string?, bool> isValidKey, string headerName = "X-API-Key")
{ {
this.isValidKey = isValidKey;
this.headerName = headerName;
}
public void OnAuthorization(AuthorizationFilterContext context) public void OnAuthorization(AuthorizationFilterContext context)
{ {
if (!isValidKey(context.HttpContext.Request.Headers[headerName])) if (!isValidKey(context.HttpContext.Request.Headers[headerName]))
context.Result = new UnauthorizedResult(); context.Result = new UnauthorizedResult();
} }
} }
}

View File

@ -5,13 +5,14 @@ namespace WorkFlow.API.Models
{ {
public static class ModelExtensions public static class ModelExtensions
{ {
public static List<Claim> ToClaimList(this UserReadDto user) => [ public static List<Claim> ToClaimList(this UserReadDto user) => new()
{
new (ClaimTypes.NameIdentifier, user.Id.ToString()), new (ClaimTypes.NameIdentifier, user.Id.ToString()),
new (ClaimTypes.Name, user.Username), new (ClaimTypes.Name, user.Username),
new (ClaimTypes.Surname, user.Name ?? ""), new (ClaimTypes.Surname, user.Name ?? ""),
new (ClaimTypes.GivenName, user.Prename ?? ""), new (ClaimTypes.GivenName, user.Prename ?? ""),
new (ClaimTypes.Email, user.Email ?? "") new (ClaimTypes.Email, user.Email ?? "")
]; };
public static Dictionary<string, object> ToClaimDictionary(this UserReadDto user) => user.ToClaimList().ToDictionary(claim => claim.Type, claim => (object) claim.Value); public static Dictionary<string, object> ToClaimDictionary(this UserReadDto user) => user.ToClaimList().ToDictionary(claim => claim.Type, claim => (object) claim.Value);
} }

View File

@ -72,7 +72,7 @@ try
{ {
var clientParams = lazyProvider.GetRequiredService<IOptions<ClientParams>>()?.Value; var clientParams = lazyProvider.GetRequiredService<IOptions<ClientParams>>()?.Value;
var publicKey = clientParams!.PublicKeys.Get(authTokenKeys.Issuer, authTokenKeys.Audience); var publicKey = clientParams!.PublicKeys.Get(authTokenKeys.Issuer, authTokenKeys.Audience);
return [publicKey.SecurityKey]; return new List<SecurityKey>() { publicKey.SecurityKey };
}, },
ValidateIssuer = true, ValidateIssuer = true,
ValidIssuer = authTokenKeys.Issuer, ValidIssuer = authTokenKeys.Issuer,

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<PackageId>WorkFlow.API</PackageId> <PackageId>WorkFlow.API</PackageId>
@ -20,7 +20,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DigitalData.Auth.Client" Version="1.3.3" /> <PackageReference Include="DigitalData.Auth.Client" Version="1.3.3" />
<PackageReference Include="DigitalData.Core.API" Version="2.1.1" /> <PackageReference Include="DigitalData.Core.API" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.13" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.20" />
<PackageReference Include="NLog" Version="5.3.4" /> <PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.14" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.3.14" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />