barhen-pfw
1 year ago
11 changed files with 251 additions and 81 deletions
@ -0,0 +1,32 @@ |
|||
using Cloud_Manager.Models.ClientContractModels; |
|||
using Cloud_Manager.Models.CustomModels; |
|||
using Cloud_Manager.Models.Interfaces; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace Cloud_Manager.Controllers |
|||
{ |
|||
[Route("api/[controller]")]
|
|||
[ApiController] |
|||
public class KeyGeneratorController : ControllerBase |
|||
{ |
|||
private readonly IKeyService m_keyService; |
|||
public KeyGeneratorController(IKeyService a_keyService) |
|||
{ |
|||
m_keyService = a_keyService; |
|||
} |
|||
|
|||
[HttpPost, Route("generate-key")] |
|||
public async Task<IActionResult> GenerateKeyAsync(Contract a_contract) |
|||
{ |
|||
if (await m_keyService.GenerateKey(a_contract)) |
|||
{ |
|||
return Ok("Key generated"); |
|||
} |
|||
else |
|||
{ |
|||
return BadRequest(); |
|||
} |
|||
} |
|||
} |
|||
} |
@ -1,33 +0,0 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace Cloud_Manager.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; |
|||
|
|||
public WeatherForecastController(ILogger<WeatherForecastController> logger) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
[HttpGet(Name = "GetWeatherForecast")] |
|||
public IEnumerable<WeatherForecast> Get() |
|||
{ |
|||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast |
|||
{ |
|||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
|||
TemperatureC = Random.Shared.Next(-20, 55), |
|||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] |
|||
}) |
|||
.ToArray(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,43 @@ |
|||
using Cloud_Manager.Models.Enums; |
|||
using Cloud_Manager.Models.Interfaces; |
|||
using Cloud_Manager.Models.ServiceRepo; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Cloud_Manager.Middleware |
|||
{ |
|||
public class KeyValidationMiddleware |
|||
{ |
|||
private readonly RequestDelegate m_next; |
|||
|
|||
public KeyValidationMiddleware(RequestDelegate next) |
|||
{ |
|||
m_next = next; |
|||
} |
|||
|
|||
public async Task InvokeAsync(HttpContext a_httpContext, IKeyService a_keyService) |
|||
{ |
|||
string apiKey = a_httpContext.Request.Headers["BISK-API-KEY"]!; |
|||
AuthEnums status = a_keyService.ValidateKey(apiKey); |
|||
if (AuthEnums.Valid != status && a_httpContext.Request.Path != "/api/KeyGenerator/generate-key") |
|||
{ |
|||
a_httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized; |
|||
await a_httpContext.Response.WriteAsync("API Key status : " + status); |
|||
|
|||
return; |
|||
} |
|||
|
|||
await m_next.Invoke(a_httpContext); |
|||
} |
|||
} |
|||
|
|||
public static class KeyValidationMiddlewareExtensions |
|||
{ |
|||
public static IApplicationBuilder UseKeyValidation( |
|||
this IApplicationBuilder builder) |
|||
{ |
|||
return builder.UseMiddleware<KeyValidationMiddleware>(); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue