INIT vol 1.

This commit is contained in:
2024-12-17 19:23:15 +01:00
parent e201f8e89c
commit 8bbcbfab09
11 changed files with 206 additions and 23 deletions
+36
View File
@@ -0,0 +1,36 @@
namespace WorkFlowCheck.API
{
public class ApiKeyMiddleware
{
private readonly RequestDelegate _next;
private const string ApiKeyHeaderName = "X-Api-Key";
public ApiKeyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (!context.Request.Headers.TryGetValue(ApiKeyHeaderName, out var extractedApiKey))
{
context.Response.StatusCode = 401; // Unauthorized
await context.Response.WriteAsync("API Key hiányzik.");
return;
}
var appSettings = context.RequestServices.GetRequiredService<IConfiguration>();
var apiKey = appSettings.GetValue<string>("ApiKey");
if (!apiKey.Equals(extractedApiKey))
{
context.Response.StatusCode = 403; // Forbidden
await context.Response.WriteAsync("Helytelen API Key.");
return;
}
await _next(context);
}
}
}
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkFlowCheck.BL.DTO;
namespace WorkFlowCheck.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
[HttpGet]
public ActionResult<ResponseDTO> Get()
{
return new ResponseDTO { Message = "Success" };
}
}
}
+62 -23
View File
@@ -1,36 +1,75 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using System.Text.Json;
using System.Text.Json.Serialization;
using WorkFlowCheck.API;
using WorkFlowCheck.DL;
var builder = WebApplication.CreateSlimBuilder(args);
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// SWAGGER !!!
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WorkFlowCheck API", Version = "v1" });
// API Key Auth definíció
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
{
Description = "API Key szükséges. Használat: 'ApiKey: {kulcs}'",
Name = "X-Api-Key", // A fejlécek között keresendõ
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "ApiKey"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ApiKey"
},
Scheme = "ApiKey",
Name = "ApiKey",
In = ParameterLocation.Header,
},
Array.Empty<string>()
}
});
});
// Szolgáltatások hozzáadása
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
}); ;
var app = builder.Build();
// Swagger middleware
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WorkFlowCheck API v1");
});
}
// Routing és endpointok
app.UseHttpsRedirection();
app.UseAuthorization();
var sampleTodos = new Todo[] {
new(1, "Walk the dog"),
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
new(4, "Clean the bathroom"),
new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
};
app.MapControllers();
var todosApi = app.MapGroup("/todos");
todosApi.MapGet("/", () => sampleTodos);
todosApi.MapGet("/{id}", (int id) =>
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
? Results.Ok(todo)
: Results.NotFound());
app.UseMiddleware<ApiKeyMiddleware>();
app.Run();
public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
@@ -8,4 +8,13 @@
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\WorkFlowCheck.BL\WorkFlowCheck.BL.csproj" />
<ProjectReference Include="..\WorkFlowCheck.DL\WorkFlowCheck.DL.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
+4
View File
@@ -1,4 +1,8 @@
{
"DefaultConnection": {
"ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WorkFlowCheckDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False"
},
"ApiKey": "YOUR_SECRET_API_KEY",
"Logging": {
"LogLevel": {
"Default": "Information",
+10
View File
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;
namespace WorkFlowCheck.BL.DTO
{
[JsonSerializable(typeof(ResponseDTO))]
public class ResponseDTO
{
public string Message { get; set; } = null!;
}
}
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+19
View File
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkFlowCheck.DL
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Entities.User> Users { get; set; } = null!;
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace WorkFlowCheck.DL.Entities
{
public class User
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string Email { get; set; } = null!;
}
}
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup>
</Project>
+12
View File
@@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.NFC", "Plugin.NFC\Pl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkFlowCheck.MAUI", "WorkFlowCheck.MAUI\WorkFlowCheck.MAUI.csproj", "{0E2BD120-EE91-4A24-A78A-CE7CA87E09AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkFlowCheck.DL", "WorkFlowCheck.DL\WorkFlowCheck.DL.csproj", "{25A8C0E7-18C7-4672-ABE8-29596E7E9B72}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkFlowCheck.BL", "WorkFlowCheck.BL\WorkFlowCheck.BL.csproj", "{73049F81-7A5D-4819-ADB8-A9926672365E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -29,6 +33,14 @@ Global
{0E2BD120-EE91-4A24-A78A-CE7CA87E09AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E2BD120-EE91-4A24-A78A-CE7CA87E09AE}.Release|Any CPU.Build.0 = Release|Any CPU
{0E2BD120-EE91-4A24-A78A-CE7CA87E09AE}.Release|Any CPU.Deploy.0 = Release|Any CPU
{25A8C0E7-18C7-4672-ABE8-29596E7E9B72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25A8C0E7-18C7-4672-ABE8-29596E7E9B72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25A8C0E7-18C7-4672-ABE8-29596E7E9B72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25A8C0E7-18C7-4672-ABE8-29596E7E9B72}.Release|Any CPU.Build.0 = Release|Any CPU
{73049F81-7A5D-4819-ADB8-A9926672365E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73049F81-7A5D-4819-ADB8-A9926672365E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73049F81-7A5D-4819-ADB8-A9926672365E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73049F81-7A5D-4819-ADB8-A9926672365E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE