api v2.1
This commit is contained in:
@@ -1,42 +1,39 @@
|
||||
namespace API.Middlewares;
|
||||
using Application.Endpoints;
|
||||
|
||||
public class ApiKeyValidationMiddleware
|
||||
namespace API.Middlewares;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class ApiKeyMiddleware
|
||||
{
|
||||
private const string APIKEYNAME = "ApiKey";
|
||||
private readonly RequestDelegate _next;
|
||||
private const string API_KEY_HEADER_NAME = "ApiKey";
|
||||
private readonly string _apiKey;
|
||||
|
||||
public ApiKeyValidationMiddleware(RequestDelegate next)
|
||||
public ApiKeyMiddleware(RequestDelegate next, IConfiguration configuration)
|
||||
{
|
||||
_next = next;
|
||||
_apiKey = configuration.GetValue<string>("ApiKeySettings:ApiKey");
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (!context.Request.Headers.TryGetValue(APIKEYNAME, out var extractedApiKey))
|
||||
if (!context.Request.Headers.TryGetValue(API_KEY_HEADER_NAME, out var extractedApiKey))
|
||||
{
|
||||
context.Response.StatusCode = 401;
|
||||
await context.Response.WriteAsync("API Key was not provided.");
|
||||
context.Response.StatusCode = HttpStatusCodes.Unauthorized; // Unauthorized
|
||||
await context.Response.WriteAsync("API Key is missing");
|
||||
return;
|
||||
}
|
||||
|
||||
var appSettings = context.RequestServices.GetRequiredService<IConfiguration>();
|
||||
|
||||
var apiKey = appSettings.GetValue<string>("ApiKey");
|
||||
|
||||
if (string.IsNullOrEmpty(apiKey))
|
||||
if (!_apiKey.Equals(extractedApiKey))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
await context.Response.WriteAsync("Unable to retrieve API key.");
|
||||
context.Response.StatusCode = HttpStatusCodes.Forbidden; // Forbidden
|
||||
await context.Response.WriteAsync("Invalid API Key");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!apiKey.Equals(extractedApiKey))
|
||||
{
|
||||
context.Response.StatusCode = 401;
|
||||
await context.Response.WriteAsync("Unauthorized client.");
|
||||
return;
|
||||
}
|
||||
|
||||
await _next(context);
|
||||
await _next(context); // API Key is valid, proceed to the next middleware
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
// Middlewares
|
||||
app.UseMiddleware<ApiKeyValidationMiddleware>();
|
||||
app.UseMiddleware<ApiKeyMiddleware>();
|
||||
app.UseMiddleware<BodyCheckMiddleware>();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
"HealthcareManagerDatabase": "Host=surus.db.elephantsql.com;Database=newbwuyu;Username=newbwuyu;Password=0end9Ixqo9PeTE4HVslX7_FVwruEhFf-;",
|
||||
"MongoDBDatabase": "mongodb+srv://andrei_cerbu:andrei_cerbu@cluster0.v80skg6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ApiKey": "testapikey"
|
||||
"ApiKeySettings": {
|
||||
"ApiKey": "testapikey"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -9,6 +9,8 @@
|
||||
"HealthcareManagerDatabase": "Host=surus.db.elephantsql.com;Database=newbwuyu;Username=newbwuyu;Password=0end9Ixqo9PeTE4HVslX7_FVwruEhFf-;",
|
||||
"MongoDBDatabase": "mongodb+srv://andrei_cerbu:andrei_cerbu@cluster0.v80skg6.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ApiKey": "testapikey"
|
||||
"ApiKeySettings": {
|
||||
"ApiKey": "testapikey"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("API")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+2ccec617a59a712428e67340dc46bebefb152aca")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b48d5ee19e5228d9bcb979f2140f996fe7a12723")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("API")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("API")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
abd09d11442616a05d74c5cf91eb2ab3a3f4e18f9d1feb34f7d619b58a9ee139
|
||||
2a4e1c7096998582acfb9acc87d23cdf43dae534213aaa9d36839c4312b69e45
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/2ccec617a59a712428e67340dc46bebefb152aca/*"}}
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/b48d5ee19e5228d9bcb979f2140f996fe7a12723/*"}}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user