|
| 1 | +using Code311.Licensing.Diagnostics; |
| 2 | +using Code311.Licensing.Models; |
| 3 | +using Code311.Licensing.Sources; |
| 4 | + |
| 5 | +namespace Code311.Licensing.Validation; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Validates license payload semantics. |
| 9 | +/// </summary> |
| 10 | +public interface ILicenseValidator |
| 11 | +{ |
| 12 | + LicenseValidationResult Validate(Code311License? license, DateTimeOffset nowUtc, LicensingOptions options); |
| 13 | +} |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Provides explicit startup validation flow. |
| 17 | +/// </summary> |
| 18 | +public interface IStartupLicenseValidator |
| 19 | +{ |
| 20 | + Task<LicenseValidationResult> ValidateAtStartupAsync(CancellationToken cancellationToken = default); |
| 21 | +} |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// Provides bounded runtime feature-level checks for integration points. |
| 25 | +/// </summary> |
| 26 | +public interface ILicenseFeatureGate |
| 27 | +{ |
| 28 | + Task<LicenseFeatureCheckResult> CheckFeatureAsync(string feature, CancellationToken cancellationToken = default); |
| 29 | +} |
| 30 | + |
| 31 | +public sealed class DefaultLicenseValidator : ILicenseValidator |
| 32 | +{ |
| 33 | + public LicenseValidationResult Validate(Code311License? license, DateTimeOffset nowUtc, LicensingOptions options) |
| 34 | + { |
| 35 | + var items = new List<LicenseStatusItem>(); |
| 36 | + |
| 37 | + if (license is null) |
| 38 | + { |
| 39 | + items.Add(new LicenseStatusItem(LicenseStatusLevel.Error, "license.missing", "No license payload could be resolved.")); |
| 40 | + return new LicenseValidationResult(false, LicenseStatusLevel.Error, items, null); |
| 41 | + } |
| 42 | + |
| 43 | + if (license.NotBeforeUtc.HasValue && nowUtc < license.NotBeforeUtc.Value) |
| 44 | + { |
| 45 | + items.Add(new LicenseStatusItem(LicenseStatusLevel.Error, "license.not_before", "License is not active yet.")); |
| 46 | + } |
| 47 | + |
| 48 | + if (license.ExpiresUtc.HasValue) |
| 49 | + { |
| 50 | + if (nowUtc >= license.ExpiresUtc.Value) |
| 51 | + { |
| 52 | + items.Add(new LicenseStatusItem(LicenseStatusLevel.Error, "license.expired", "License has expired.")); |
| 53 | + } |
| 54 | + else if (nowUtc >= license.ExpiresUtc.Value.AddDays(-Math.Abs(options.ExpiryWarningWindowDays))) |
| 55 | + { |
| 56 | + items.Add(new LicenseStatusItem(LicenseStatusLevel.Warning, "license.expiring_soon", "License is approaching expiry.")); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (items.All(x => x.Level != LicenseStatusLevel.Error)) |
| 61 | + { |
| 62 | + items.Add(new LicenseStatusItem(LicenseStatusLevel.Valid, "license.valid", "License is valid.")); |
| 63 | + } |
| 64 | + |
| 65 | + var overall = items.Any(x => x.Level == LicenseStatusLevel.Error) |
| 66 | + ? LicenseStatusLevel.Error |
| 67 | + : items.Any(x => x.Level == LicenseStatusLevel.Warning) |
| 68 | + ? LicenseStatusLevel.Warning |
| 69 | + : LicenseStatusLevel.Valid; |
| 70 | + |
| 71 | + return new LicenseValidationResult(overall != LicenseStatusLevel.Error, overall, items, license); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public sealed class StartupLicenseValidator( |
| 76 | + ILicenseSource source, |
| 77 | + ILicenseValidator validator, |
| 78 | + ILicensingStatusReporter reporter, |
| 79 | + LicensingOptions options) : IStartupLicenseValidator |
| 80 | +{ |
| 81 | + public async Task<LicenseValidationResult> ValidateAtStartupAsync(CancellationToken cancellationToken = default) |
| 82 | + { |
| 83 | + var license = await source.GetLicenseAsync(cancellationToken).ConfigureAwait(false); |
| 84 | + var result = validator.Validate(license, DateTimeOffset.UtcNow, options); |
| 85 | + |
| 86 | + var top = result.Items.FirstOrDefault() ?? new LicenseStatusItem(result.OverallLevel, "license.status", "License status generated."); |
| 87 | + reporter.Report(new LicenseRuntimeStatus(DateTimeOffset.UtcNow, LicenseCheckStage.Startup, result.OverallLevel, top.Code, top.Message)); |
| 88 | + |
| 89 | + if (options.RequireValidLicenseAtStartup && !result.IsValid) |
| 90 | + { |
| 91 | + throw new InvalidOperationException("Code311 startup license validation failed."); |
| 92 | + } |
| 93 | + |
| 94 | + return result; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +public sealed class LicenseFeatureGate( |
| 99 | + ILicenseSource source, |
| 100 | + ILicenseValidator validator, |
| 101 | + ILicensingStatusReporter reporter, |
| 102 | + LicensingOptions options) : ILicenseFeatureGate |
| 103 | +{ |
| 104 | + public async Task<LicenseFeatureCheckResult> CheckFeatureAsync(string feature, CancellationToken cancellationToken = default) |
| 105 | + { |
| 106 | + ArgumentException.ThrowIfNullOrWhiteSpace(feature); |
| 107 | + |
| 108 | + var license = await source.GetLicenseAsync(cancellationToken).ConfigureAwait(false); |
| 109 | + var validation = validator.Validate(license, DateTimeOffset.UtcNow, options); |
| 110 | + |
| 111 | + if (!validation.IsValid || validation.License is null) |
| 112 | + { |
| 113 | + var denied = new LicenseFeatureCheckResult(false, feature, LicenseStatusLevel.Error, "License invalid for feature check.", license); |
| 114 | + reporter.Report(new LicenseRuntimeStatus(DateTimeOffset.UtcNow, LicenseCheckStage.RuntimeFeature, denied.Level, "feature.denied.invalid_license", denied.Reason)); |
| 115 | + return denied; |
| 116 | + } |
| 117 | + |
| 118 | + if (!validation.License.Features.Contains(feature)) |
| 119 | + { |
| 120 | + var denied = new LicenseFeatureCheckResult(false, feature, LicenseStatusLevel.Warning, "Feature not covered by current license.", validation.License); |
| 121 | + reporter.Report(new LicenseRuntimeStatus(DateTimeOffset.UtcNow, LicenseCheckStage.RuntimeFeature, denied.Level, "feature.denied.not_licensed", denied.Reason)); |
| 122 | + return denied; |
| 123 | + } |
| 124 | + |
| 125 | + var allowed = new LicenseFeatureCheckResult(true, feature, validation.OverallLevel, "Feature is licensed.", validation.License); |
| 126 | + reporter.Report(new LicenseRuntimeStatus(DateTimeOffset.UtcNow, LicenseCheckStage.RuntimeFeature, allowed.Level, "feature.allowed", allowed.Reason)); |
| 127 | + return allowed; |
| 128 | + } |
| 129 | +} |
0 commit comments