| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Heimdall.Command.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// How long a scope owner invitation stays acceptable (UC-21, FR-SC-18). Read from |
| | | 7 | | /// <c>HEIMDALL_SCOPE_INVITATION_LIFETIME_IN_SECONDS</c>; seven days by default. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Values from 3600 (an hour) to 2592000 (thirty days) are accepted. Anything else — unparsable or out |
| | | 11 | | /// of range — falls back to the default rather than to an invitation that never expires, and |
| | | 12 | | /// <see cref="RejectedLifetime" /> says so for start-up to warn. |
| | | 13 | | /// </remarks> |
| | | 14 | | public class ScopeInvitationOptions |
| | | 15 | | { |
| | | 16 | | public const string LifetimeVariable = "HEIMDALL_SCOPE_INVITATION_LIFETIME_IN_SECONDS"; |
| | | 17 | | |
| | | 18 | | /// <summary>Seven days, in seconds.</summary> |
| | | 19 | | public const double DefaultLifetimeSeconds = 604800; |
| | | 20 | | |
| | | 21 | | private const double MinLifetimeSeconds = 3600; |
| | | 22 | | |
| | | 23 | | private const double MaxLifetimeSeconds = 2592000; |
| | | 24 | | |
| | | 25 | | /// <summary>How long after its creation an invitation can still be accepted.</summary> |
| | 2022 | 26 | | public TimeSpan Lifetime { get; init; } = TimeSpan.FromSeconds(DefaultLifetimeSeconds); |
| | | 27 | | |
| | | 28 | | /// <summary>The configured text when it was present but unusable; <c>null</c> otherwise.</summary> |
| | 1968 | 29 | | public string? RejectedLifetime { get; init; } |
| | | 30 | | |
| | 973 | 31 | | public static ScopeInvitationOptions FromEnvironment() => From(Environment.GetEnvironmentVariable(LifetimeVariable)) |
| | | 32 | | |
| | | 33 | | /// <summary>Reads a configured number of seconds, falling back to the default when it is unusable.</summary> |
| | | 34 | | public static ScopeInvitationOptions From(string? configured) |
| | | 35 | | { |
| | 984 | 36 | | var usable = |
| | 984 | 37 | | double.TryParse(configured, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) |
| | 984 | 38 | | && value is >= MinLifetimeSeconds and <= MaxLifetimeSeconds; |
| | | 39 | | |
| | 984 | 40 | | return new ScopeInvitationOptions |
| | 984 | 41 | | { |
| | 984 | 42 | | Lifetime = TimeSpan.FromSeconds(usable ? value : DefaultLifetimeSeconds), |
| | 984 | 43 | | RejectedLifetime = usable || string.IsNullOrWhiteSpace(configured) ? null : configured |
| | 984 | 44 | | }; |
| | | 45 | | } |
| | | 46 | | } |