Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/source/License-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ No code change is required when using an environment variable—just register Au
Licensing is enforced via log messages at various levels:

- INFO: Valid license message
- WARNING: Missing license message
- WARNING: Missing license message, or a license term that has ended but is still within its grace period
- ERROR: Invalid/expired license message

### Renewal grace period

License keys include a 30-day grace period after the end of their term. When your subscription renews, you receive a new license key, and the previous key keeps logging only a WARNING (not an ERROR) for 30 days after its term ends. This gives you time to deploy the renewed key. Your current license key is always available on your [account page](https://luckypennysoftware.com/account).

There is no other license enforcement besides log messages. No central license server, no outbound HTTP calls, no degrading or disabling of features.

The log messages are logged using standard `Microsoft.Extensions.Logging` loggers under the category name `LuckyPennySoftware.AutoMapper.License`.
Expand Down
8 changes: 8 additions & 0 deletions src/AutoMapper/Licensing/License.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public License(ClaimsPrincipal claims)
ExpirationDate = expiredAt;
}

// The real end of the license term. Keys issued with a grace period carry it separately from
// exp (term end + grace); older keys omit it and their exp is the term end.
if (long.TryParse(claims.FindFirst("term_end")?.Value, out var termEnd))
{
TermEndDate = DateTimeOffset.FromUnixTimeSeconds(termEnd);
}

if (Enum.TryParse<Edition>(claims.FindFirst("edition")?.Value, out var edition))
{
Edition = edition;
Expand Down Expand Up @@ -58,6 +65,7 @@ public License(ClaimsPrincipal claims)
public string SubscriptionId { get; }
public DateTimeOffset? StartDate { get; }
public DateTimeOffset? ExpirationDate { get; }
public DateTimeOffset? TermEndDate { get; }
public Edition? Edition { get; }
public ProductType? ProductType { get; }
public bool IsPerpetual { get; }
Expand Down
46 changes: 39 additions & 7 deletions src/AutoMapper/Licensing/LicenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace AutoMapper.Licensing;

internal class LicenseValidator
{
// Grace period for keys issued without a term_end claim, measured from their exp (their term end).
internal const int DefaultGracePeriodDays = 30;

private readonly ILogger _logger;
private readonly DateTimeOffset? _buildDate;

Expand Down Expand Up @@ -35,23 +38,48 @@ public void Validate(License license)

_logger.LogDebug("The Lucky Penny license key details: {license}", license);

var diff = DateTime.UtcNow.Date.Subtract(license.ExpirationDate.Value.Date).TotalDays;
var today = DateTime.UtcNow.Date;
// Keys without a term_end claim predate the grace period: their exp is the term end, so the grace
// runs from there. Keys with one carry the grace in exp itself (term end + grace).
var termEnd = (license.TermEndDate ?? license.ExpirationDate.Value).Date;
var graceEnd = license.TermEndDate.HasValue
? license.ExpirationDate.Value.Date
: termEnd.AddDays(DefaultGracePeriodDays);
string warning = null;

// Past the end of the term: within the grace period, a warning (the renewed key may not be
// deployed yet); past it, an error.
void Expired(double expiredDaysAgo)
{
if (today <= graceEnd)
{
warning = $"Your license term for the Lucky Penny software AutoMapper ended {expiredDaysAgo} days ago. " +
$"Update your license key by {graceEnd:yyyy-MM-dd} ({graceEnd.Subtract(today).TotalDays} days remaining) to avoid license errors. " +
"If you have renewed, your renewed license key is available at https://luckypennysoftware.com/account.";
}
else
{
errors.Add($"Your license for the Lucky Penny software AutoMapper expired {expiredDaysAgo} days ago.");
}
}

var diff = today.Subtract(termEnd).TotalDays;
if (diff > 0)
{
// If perpetual, check if build date is before expiration
// If perpetual, check if build date is before the end of the term
if (license.IsPerpetual && _buildDate.HasValue)
{
var buildDateDiff = _buildDate.Value.Date.Subtract(license.ExpirationDate.Value.Date).TotalDays;
var buildDateDiff = _buildDate.Value.Date.Subtract(termEnd).TotalDays;
if (buildDateDiff <= 0)
{
_logger.LogInformation(
"Your license for the Lucky Penny software AutoMapper expired {expiredDaysAgo} days ago, but perpetual licensing is active because the build date ({buildDate:O}) is before the license expiration date ({licenseExpiration:O}).",
diff, _buildDate, license.ExpirationDate);
diff, _buildDate, termEnd);
// Don't add to errors - perpetual fallback applies
}
else
{
errors.Add($"Your license for the Lucky Penny software AutoMapper expired {diff} days ago.");
Expired(diff);
}
}
else
Expand All @@ -61,7 +89,7 @@ public void Validate(License license)
_logger.LogWarning(
"Your license for the Lucky Penny software AutoMapper has the perpetual flag set, but the build date could not be determined. Perpetual licensing is unavailable.");
}
errors.Add($"Your license for the Lucky Penny software AutoMapper expired {diff} days ago.");
Expired(diff);
}
}

Expand All @@ -81,12 +109,16 @@ public void Validate(License license)
_logger.LogError(
"Please visit https://luckypennysoftware.com to obtain a valid license for the Lucky Penny software AutoMapper.");
}
else if (warning != null)
{
_logger.LogWarning(warning);
}
else
{
_logger.LogInformation("You have a valid license key for the Lucky Penny software {type} {edition} edition. The license expires on {licenseExpiration}.",
license.ProductType,
license.Edition,
license.ExpirationDate);
termEnd);
}
}
}
103 changes: 100 additions & 3 deletions src/UnitTests/Licensing/LicenseValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void Should_return_invalid_when_expired()
new Claim("customer_id", Guid.NewGuid().ToString()),
new Claim("sub_id", Guid.NewGuid().ToString()),
new Claim("iat", DateTimeOffset.UtcNow.AddYears(-1).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-1).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-31).ToUnixTimeSeconds().ToString()),
new Claim("edition", nameof(Edition.Professional)),
new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)));

Expand Down Expand Up @@ -155,7 +155,7 @@ public void Should_reject_perpetual_license_when_build_date_after_expiration()
new Claim("customer_id", Guid.NewGuid().ToString()),
new Claim("sub_id", Guid.NewGuid().ToString()),
new Claim("iat", DateTimeOffset.UtcNow.AddYears(-1).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-31).ToUnixTimeSeconds().ToString()),
new Claim("edition", nameof(Edition.Professional)),
new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)),
new Claim("perpetual", "true"));
Expand Down Expand Up @@ -183,7 +183,7 @@ public void Should_warn_and_error_when_perpetual_but_build_date_is_null()
new Claim("customer_id", Guid.NewGuid().ToString()),
new Claim("sub_id", Guid.NewGuid().ToString()),
new Claim("iat", DateTimeOffset.UtcNow.AddYears(-1).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-10).ToUnixTimeSeconds().ToString()),
new Claim("exp", DateTimeOffset.UtcNow.AddDays(-40).ToUnixTimeSeconds().ToString()),
new Claim("edition", nameof(Edition.Professional)),
new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)),
new Claim("perpetual", "true"));
Expand Down Expand Up @@ -225,4 +225,101 @@ public void Should_handle_missing_perpetual_claim()
|| log.Level == LogLevel.Warning
|| log.Level == LogLevel.Critical);
}

private static License TermLicense(int termEndDaysFromNow, int? expDaysFromNow, bool perpetual = false)
{
var claims = new List<Claim>
{
new("account_id", Guid.NewGuid().ToString()),
new("customer_id", Guid.NewGuid().ToString()),
new("sub_id", Guid.NewGuid().ToString()),
new("iat", DateTimeOffset.UtcNow.AddYears(-1).ToUnixTimeSeconds().ToString()),
new("edition", nameof(Edition.Professional)),
new("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)),
};
if (expDaysFromNow is { } exp)
{
// A key issued with a grace period: exp is the term end + grace, term_end the real term end.
claims.Add(new("exp", DateTimeOffset.UtcNow.AddDays(exp).ToUnixTimeSeconds().ToString()));
claims.Add(new("term_end", DateTimeOffset.UtcNow.AddDays(termEndDaysFromNow).ToUnixTimeSeconds().ToString()));
}
else
{
// A key issued before the grace period: exp is the term end, no term_end claim.
claims.Add(new("exp", DateTimeOffset.UtcNow.AddDays(termEndDaysFromNow).ToUnixTimeSeconds().ToString()));
}
if (perpetual)
claims.Add(new("perpetual", "true"));
return new License(claims.ToArray());
}

private static IReadOnlyList<FakeLogRecord> Validate(License license, DateTimeOffset? buildDate = null)
{
var factory = new LoggerFactory();
var provider = new FakeLoggerProvider();
factory.AddProvider(provider);

license.IsConfigured.ShouldBeTrue();
new LicenseValidator(factory, buildDate).Validate(license);

return provider.Collector.GetSnapshot();
}

[Fact]
public void Should_warn_within_grace_period_after_term_end()
{
var logMessages = Validate(TermLicense(termEndDaysFromNow: -5, expDaysFromNow: 25));

logMessages.ShouldNotContain(log => log.Level == LogLevel.Error);
logMessages.ShouldContain(log => log.Level == LogLevel.Warning && log.Message.Contains("ended 5 days ago"));
}

[Fact]
public void Should_error_after_grace_period()
{
var logMessages = Validate(TermLicense(termEndDaysFromNow: -31, expDaysFromNow: -1));

logMessages.ShouldContain(log => log.Level == LogLevel.Error && log.Message.Contains("expired 31 days ago"));
}

[Fact]
public void Should_be_valid_before_term_end()
{
var logMessages = Validate(TermLicense(termEndDaysFromNow: 10, expDaysFromNow: 40));

logMessages.ShouldNotContain(log => log.Level == LogLevel.Error || log.Level == LogLevel.Warning);
logMessages.ShouldContain(log => log.Level == LogLevel.Information);
}

[Fact]
public void Should_warn_within_default_grace_period_for_keys_without_term_end()
{
var logMessages = Validate(TermLicense(termEndDaysFromNow: -10, expDaysFromNow: null));

logMessages.ShouldNotContain(log => log.Level == LogLevel.Error);
logMessages.ShouldContain(log => log.Level == LogLevel.Warning && log.Message.Contains("ended 10 days ago"));
}

[Fact]
public void Should_use_term_end_for_perpetual_cutoff()
{
// Built during the grace period: after the term end, so perpetual fallback does not apply.
var logMessages = Validate(
TermLicense(termEndDaysFromNow: -40, expDaysFromNow: -10, perpetual: true),
buildDate: DateTimeOffset.UtcNow.AddDays(-20));

logMessages.ShouldContain(log => log.Level == LogLevel.Error && log.Message.Contains("expired"));
logMessages.ShouldNotContain(log => log.Message.Contains("perpetual licensing is active"));
}

[Fact]
public void Should_allow_perpetual_when_built_before_term_end()
{
var logMessages = Validate(
TermLicense(termEndDaysFromNow: -40, expDaysFromNow: -10, perpetual: true),
buildDate: DateTimeOffset.UtcNow.AddDays(-50));

logMessages.ShouldNotContain(log => log.Level == LogLevel.Error || log.Level == LogLevel.Warning);
logMessages.ShouldContain(log => log.Level == LogLevel.Information && log.Message.Contains("perpetual"));
}
}
Loading