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

@ -0,0 +1,42 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.Extensions.Options;
namespace USEntryCoach.Server.Settings;
public sealed class ApiSettings
{
public const string SectionName = "API";
[Required(ErrorMessage = "OpenAI API token is required!", AllowEmptyStrings = false)]
public required string OpenAiToken { get; set; }
}
public sealed class AuthenticationSettings
{
public const string SectionName = "Authentication";
private string _jwtGenerationSecret = null!;
[Required(ErrorMessage = "JWT generation secret token is required!", AllowEmptyStrings = false)]
public required string JwtGenerationSecret
{
get => _jwtGenerationSecret;
set
{
_jwtGenerationSecret = value;
JwtGenerationSecretBytes = Encoding.UTF8.GetBytes(value);
}
}
public byte[] JwtGenerationSecretBytes { get; private set; }
const string JwtExpiryTimeMin = "00:00:30";
const string JwtExpiryTimeMax = "24:00:00";
[Range(typeof(TimeSpan), JwtExpiryTimeMin, JwtExpiryTimeMax,
ErrorMessage = $"JWT expiry time must be in the range from {JwtExpiryTimeMin} to {JwtExpiryTimeMax}.")]
public TimeSpan JwtExpiryTime { get; set; } = TimeSpan.FromMinutes(5);
}