Adding security

This commit is contained in:
Zeeshaun
2026-03-24 09:49:29 -05:00
parent 69648a3b8f
commit a3b74e62a9
59 changed files with 2480 additions and 0 deletions
@@ -0,0 +1,72 @@
using Flurl;
using Flurl.Http;
using LCU.Library.Internal.ApiSecurity.Constants;
using LCU.Library.Internal.ApiSecurity.Models;
using Microsoft.AspNetCore.Mvc;
namespace IntegrationTestApiOne.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly ApiSecurityConfiguration _apiSecurityConfiguration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, ApiSecurityConfiguration apiSecurityConfiguration)
{
_logger = logger;
_apiSecurityConfiguration = apiSecurityConfiguration;
}
[HttpGet("GetWeatherForecast", Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
[HttpGet("RunTestApiKeyAuthenticatedRequest", Name = "RunTestApiKeyAuthenticatedRequest")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public async Task<IActionResult> RunTestApiKeyAuthenticatedRequest()
{
var client = "TestTenantOne";
var apiKey = _apiSecurityConfiguration.InternalApiKeySet[client];
var result = await "https://localhost:32003"
.AppendPathSegment("WeatherForecast")
.AppendPathSegment("GetWeatherForecast")
.WithHeader(HeaderKeys.ApiKey, apiKey)
.WithHeader(HeaderKeys.Client, client)
.GetAsync();
return Ok(result.ResponseMessage.Content.ReadAsStringAsync().Result);
}
[HttpGet("RunTestApiKeyAuthenticatedRequestUsingMadeUpCreds", Name = "RunTestApiKeyAuthenticatedRequestUsingMadeUpCreds")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public async Task<IActionResult> RunTestApiKeyAuthenticatedRequestUsingMadeUpCreds()
{
var result = await "https://localhost:32003"
.AppendPathSegment("WeatherForecast")
.AppendPathSegment("GetWeatherForecast")
.WithHeader(HeaderKeys.ApiKey, "MadeUpApiKey")
.WithHeader(HeaderKeys.Client, "MadeUpClient")
.GetAsync();
return Ok(result.ResponseMessage.Content.ReadAsStringAsync().Result);
}
}
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LCU.Library.Internal.ApiSecurity\LCU.Library.Internal.ApiSecurity.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,47 @@
using LCU.Library.Internal.ApiSecurity;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "IntegrationTestApiOne",
Version = "v1"
});
});
builder.Services.AddApiKeySecurity(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "IntegrationTestApiOne"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
// app.UseMiddleware<ApiKeyMiddleware>();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.Run();
@@ -0,0 +1,12 @@
namespace IntegrationTestApiOne;
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
@@ -0,0 +1,18 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApiSecurityConfiguration": {
"ApiKeyAuthenticationFailureBehavior": "Error",
"ExternalApiKeySet": {
"TestTenantTwo": "TestKeyTwo"
},
"InternalApiKeySet": {
"TestTenantOne": "TestKeyOne"
}
}
}