Use typesafe options
This commit is contained in:
42
USEntryCoach.Server/Settings/APISettings.cs
Normal file
42
USEntryCoach.Server/Settings/APISettings.cs
Normal 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user