| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Heimdall.WebApi.Security; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A person-token lifetime read from <c>HEIMDALL_AUTH_TOKEN_EXPIRATION_IN_SECONDS</c>: the seconds |
| | | 7 | | /// to use, and the configured text when it was present but unusable and the default applied in its |
| | | 8 | | /// place. Pulled out of <c>Startup.BuildJwtConfiguration</c> so the parse can be tested and the |
| | | 9 | | /// fallback reported. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="Seconds">The lifetime to use.</param> |
| | | 12 | | /// <param name="RejectedValue"> |
| | | 13 | | /// The configured text when it was present but unusable; <c>null</c> when the variable was absent |
| | | 14 | | /// or usable (absent is the documented default, not a fallback). |
| | | 15 | | /// </param> |
| | 2721 | 16 | | public sealed record TokenLifetimeSetting(double Seconds, string? RejectedValue) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Reads <paramref name="configured" /> as a positive, finite number of seconds, using |
| | | 20 | | /// <paramref name="defaultSeconds" /> when it is absent or unusable. |
| | | 21 | | /// </summary> |
| | | 22 | | public static TokenLifetimeSetting Parse(string? configured, double defaultSeconds) |
| | | 23 | | { |
| | 907 | 24 | | if (string.IsNullOrWhiteSpace(configured)) |
| | | 25 | | { |
| | 902 | 26 | | return new TokenLifetimeSetting(defaultSeconds, null); |
| | | 27 | | } |
| | | 28 | | |
| | 5 | 29 | | return double.TryParse(configured, NumberStyles.Float, CultureInfo.InvariantCulture, out var seconds) |
| | 5 | 30 | | && double.IsFinite(seconds) && seconds > 0 |
| | 5 | 31 | | ? new TokenLifetimeSetting(seconds, null) |
| | 5 | 32 | | : new TokenLifetimeSetting(defaultSeconds, configured); |
| | | 33 | | } |
| | | 34 | | } |