You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
43 lines
1.3 KiB
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>();
|
|
}
|
|
}
|
|
}
|
|
|