| | | 1 | | using Anichron.API.Settings; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace Anichron.API.Infrastructure; |
| | | 6 | | |
| | | 7 | | internal sealed class JwtSettingsValidator : IValidateOptions<JwtSettings> |
| | | 8 | | { |
| | | 9 | | private const int minSecretBytes = 32; |
| | | 10 | | |
| | | 11 | | public ValidateOptionsResult Validate(string? name, JwtSettings options) |
| | 77 | 12 | | { |
| | 77 | 13 | | List<string> failures = []; |
| | | 14 | | |
| | 77 | 15 | | if (string.IsNullOrEmpty(options.Secret)) |
| | 3 | 16 | | failures.Add("Jwt:Secret configuration is missing."); |
| | 74 | 17 | | else if (Encoding.UTF8.GetByteCount(options.Secret) < minSecretBytes) |
| | 1 | 18 | | failures.Add("Jwt:Secret must be at least 32 bytes."); |
| | 77 | 19 | | if (string.IsNullOrEmpty(options.Issuer)) |
| | 3 | 20 | | failures.Add("Jwt:Issuer is missing."); |
| | 77 | 21 | | if (string.IsNullOrEmpty(options.Audience)) |
| | 3 | 22 | | failures.Add("Jwt:Audience is missing."); |
| | | 23 | | |
| | 77 | 24 | | return failures.Count == 0 ? ValidateOptionsResult.Success : ValidateOptionsResult.Fail(failures); |
| | 77 | 25 | | } |
| | | 26 | | } |