Implement US-13 patient authentication and persistent ownership
This commit is contained in:
1 parent
ebf50827d6
commit
0ce9b2b168
36 files changed
+1203
-28
No files matched your search
@@ -1,3 +1,10 @@
|
||||
using ApiGateway.Security;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.BearerToken;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||
using Microsoft.AspNetCore.DataProtection.Repositories;
|
||||
namespace ApiGateway;
|
||||
|
||||
public class Program
|
||||
@@ -7,23 +14,73 @@ public class Program
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddProblemDetails();
|
||||
builder.Services.AddSingleton<PrototypeAccounts>();
|
||||
builder.Services.AddAuthentication("ClientOrService")
|
||||
.AddPolicyScheme("ClientOrService", null, options => options.ForwardDefaultSelector = context =>
|
||||
context.Request.Headers.ContainsKey("X-Service-Key") ? "InternalService" : "Bearer")
|
||||
.AddBearerToken("Bearer", options =>
|
||||
{
|
||||
options.BearerTokenExpiration = TimeSpan.FromMinutes(builder.Configuration.GetValue("Authentication:TokenLifetimeMinutes", 30));
|
||||
})
|
||||
.AddScheme<AuthenticationSchemeOptions, InternalServiceAuthenticationHandler>("InternalService", null);
|
||||
builder.Services.AddAuthorization(options => { options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build(); options.AddPolicy("Patient", policy => policy.RequireRole("Patient").RequireClaim(System.Security.Claims.ClaimTypes.NameIdentifier)); });
|
||||
builder.Services.AddDataProtection().SetApplicationName("HealthApp.ApiGateway");
|
||||
builder.Services.AddOptions<KeyManagementOptions>().Configure<IConfiguration, ILoggerFactory>((options, configuration, logger) =>
|
||||
{
|
||||
var keyDirectory = configuration["Authentication:KeyDirectory"];
|
||||
if (string.IsNullOrWhiteSpace(keyDirectory)) throw new InvalidOperationException("Authentication:KeyDirectory is required.");
|
||||
options.XmlRepository = new FileSystemXmlRepository(new DirectoryInfo(keyDirectory), logger);
|
||||
});
|
||||
builder.Services.AddHttpClient("PatientProfile", (services, client) =>
|
||||
client.BaseAddress = GetBaseUri(services.GetRequiredService<IConfiguration>(), "PatientProfile"))
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetBaseUri(configuration, "PatientProfile");
|
||||
client.DefaultRequestHeaders.Add("X-Gateway-Key", configuration["Services:PatientProfile:Key"]);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
|
||||
builder.Services.AddHttpClient("Observation", (services, client) =>
|
||||
client.BaseAddress = GetBaseUri(services.GetRequiredService<IConfiguration>(), "Observation"))
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetBaseUri(configuration, "Observation");
|
||||
client.DefaultRequestHeaders.Add("X-Gateway-Key", configuration["Services:Observation:Key"]);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
|
||||
builder.Services.AddHttpClient("Insights", (services, client) =>
|
||||
client.BaseAddress = GetBaseUri(services.GetRequiredService<IConfiguration>(), "Insights"))
|
||||
{
|
||||
var configuration = services.GetRequiredService<IConfiguration>();
|
||||
client.BaseAddress = GetBaseUri(configuration, "Insights");
|
||||
client.DefaultRequestHeaders.Add("X-Gateway-Key", configuration["Services:Insights:Key"]);
|
||||
})
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
|
||||
var app = builder.Build();
|
||||
_ = GetBaseUri(app.Configuration, "PatientProfile");
|
||||
_ = GetBaseUri(app.Configuration, "Observation");
|
||||
_ = GetBaseUri(app.Configuration, "Insights");
|
||||
foreach (var key in new[] { "Services:PatientProfile:Key", "Services:Observation:Key", "Services:Insights:Key", "InternalAccess:Observation:Key", "InternalAccess:Insights:Key" })
|
||||
if (string.IsNullOrWhiteSpace(app.Configuration[key]) || app.Configuration[key]!.Length < 32)
|
||||
throw new InvalidOperationException($"{key} must contain at least 32 characters.");
|
||||
if (string.IsNullOrWhiteSpace(app.Configuration["Authentication:KeyDirectory"]))
|
||||
throw new InvalidOperationException("Authentication:KeyDirectory is required.");
|
||||
var lifetime = app.Configuration.GetValue("Authentication:TokenLifetimeMinutes", 30);
|
||||
if (lifetime <= 0 || lifetime > 1440) throw new InvalidOperationException("Token lifetime must be between 1 and 1440 minutes.");
|
||||
if (!app.Environment.IsDevelopment())
|
||||
foreach (var service in new[] { "PatientProfile", "Observation", "Insights" })
|
||||
if (GetBaseUri(app.Configuration, service).Scheme != "https")
|
||||
throw new InvalidOperationException("Production service endpoints must use HTTPS.");
|
||||
_ = app.Services.GetRequiredService<PrototypeAccounts>();
|
||||
app.UseExceptionHandler();
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (!app.Environment.IsDevelopment() && !context.Request.IsHttps) { context.Response.StatusCode = 400; return; }
|
||||
await next(context);
|
||||
});
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseMiddleware<PatientAccessMiddleware>();
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user