Use typesafe options
All checks were successful
Build Backend and Frontend / Build & Test .NET Backend (push) Successful in 33s
Build Backend and Frontend / Build Frontend (push) Successful in 12s

This commit is contained in:
Simon Lübeß
2025-05-27 12:20:23 +02:00
parent f4737f4dcf
commit dffd31cd0f
5 changed files with 73 additions and 55 deletions

View File

@ -6,12 +6,21 @@ using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.IdentityModel.Tokens;
using USEntryCoach.Server.Data;
using USEntryCoach.Server.Services;
using USEntryCoach.Server.Settings;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var apiSettingsSection = builder.Configuration.GetSection(ApiSettings.SectionName);
var authSettingsSection = builder.Configuration.GetSection(AuthenticationSettings.SectionName);
builder.Services.AddOptionsWithValidateOnStart<ApiSettings>().Bind(apiSettingsSection).ValidateDataAnnotations();
builder.Services.AddOptionsWithValidateOnStart<AuthenticationSettings>().Bind(authSettingsSection).ValidateDataAnnotations();
ApiSettings? apiSettings = apiSettingsSection.Get<ApiSettings>();
AuthenticationSettings? authSettings = authSettingsSection.Get<AuthenticationSettings>();
builder.Services.AddSingleton<TokenService>();
// Configure JWT token generation.
@ -21,28 +30,14 @@ builder.Services.AddAuthentication(config =>
config.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(config =>
{
// TODO: Use Microsoft.Extensions.Options?
// var myOptions = new MyOptions(); // Your settings class
// builder.Configuration.GetSection("MySection").Bind(myOptions);
// myOptions now has the values
//
// TODO: This is identical in TokenService
string? secretToken = builder.Configuration.GetValue<string>("Authentication:Secret");
if (secretToken == null)
{
throw new Exception("No Authentication Secret Token set! Please define a value for \"Authentication:SecretToken\" in appsettings.json.");
}
byte[] secretKey = Encoding.ASCII.GetBytes(secretToken);
// TODO: Only for debug!
config.RequireHttpsMetadata = false;
config.SaveToken = true;
config.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(secretKey),
IssuerSigningKey = new SymmetricSecurityKey(authSettings!.JwtGenerationSecretBytes),
ValidateIssuer = false,
ValidateAudience = false
};
@ -75,9 +70,9 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
HttpClient client = new();
string? apiKey = app.Configuration.GetValue<string>("API:OpenAI");
//string? apiKey = app.Configuration.GetValue<string>("API:OpenAI");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiSettings.OpenAiToken}");
app.MapPost("/login", Login);
@ -131,8 +126,8 @@ app.MapGet("/user", (ClaimsPrincipal user) =>
app.MapGet("/ephemeral_token", async () =>
{
if (apiKey == null)
throw new Exception("API key not set");
//if (apiKey == null)
// throw new Exception("API key not set");
var options = new
{