-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Adding perpetual licensing #4608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ae0486f
682e83b
0d027c2
03f4eb3
f7b15fb
7722027
0f8f8fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,21 @@ | |
| <Exec Condition=" '$(OS)' == 'Windows_NT' " Command="powershell -ExecutionPolicy Unrestricted -File "$(ProjectDir)ApiCompat\PreBuild.ps1" -version $(Version)" /> | ||
| <Exec Condition=" '$(OS)' != 'Windows_NT' " Command="bash "$(ProjectDir)ApiCompat/PreBuild.sh" $(Version)" /> | ||
| </Target> --> | ||
|
|
||
| <Target Name="EmbedBuildDate" BeforeTargets="CoreCompile"> | ||
| <Exec Command="git log -1 --format=%25cI" ConsoleToMSBuild="true" IgnoreExitCode="true" StandardOutputImportance="Low"> | ||
| <Output TaskParameter="ConsoleOutput" PropertyName="GitCommitDate" /> | ||
| </Exec> | ||
| <PropertyGroup> | ||
| <BuildDateUtc Condition="'$(GitCommitDate)' != ''">$(GitCommitDate)</BuildDateUtc> | ||
| <BuildDateUtc Condition="'$(BuildDateUtc)' == ''">$([System.DateTime]::UtcNow.ToString("O"))</BuildDateUtc> | ||
| </PropertyGroup> | ||
| <WriteLinesToFile File="$(IntermediateOutputPath)BuildDateGenerated.cs" Lines="[assembly: System.Reflection.AssemblyMetadata("BuildDateUtc", "$(BuildDateUtc)")]" Overwrite="true" /> | ||
| <ItemGroup> | ||
| <Compile Include="$(IntermediateOutputPath)BuildDateGenerated.cs" /> | ||
| </ItemGroup> | ||
| </Target> | ||
|
Comment on lines
+64
to
+75
|
||
|
|
||
| <ItemGroup> | ||
| <Using Include="System.Linq.Expressions.Expression" Static="true"/> | ||
| <Using Include="AutoMapper.Execution.ExpressionBuilder" Static="true"/> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System.Reflection; | ||
|
|
||
| namespace AutoMapper.Licensing; | ||
|
|
||
| internal static class BuildInfo | ||
| { | ||
| public static DateTimeOffset? BuildDate { get; } = GetBuildDate(); | ||
|
|
||
| private static DateTimeOffset? GetBuildDate() | ||
| { | ||
| var assembly = typeof(BuildInfo).Assembly; | ||
|
|
||
| var buildDateAttribute = assembly | ||
| .GetCustomAttributes<AssemblyMetadataAttribute>() | ||
| .FirstOrDefault(a => a.Key == "BuildDateUtc"); | ||
|
|
||
| if (buildDateAttribute?.Value != null && | ||
| DateTimeOffset.TryParse(buildDateAttribute.Value, out var buildDate)) | ||
|
jbogard marked this conversation as resolved.
|
||
| { | ||
| return buildDate; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,4 +110,119 @@ public void Should_return_invalid_when_expired() | |
| logMessages | ||
| .ShouldContain(log => log.Level == LogLevel.Error); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_allow_perpetual_license_when_build_date_before_expiration() | ||
| { | ||
| var factory = new LoggerFactory(); | ||
| var provider = new FakeLoggerProvider(); | ||
| factory.AddProvider(provider); | ||
|
|
||
| var buildDate = DateTimeOffset.UtcNow.AddDays(-30); | ||
| var licenseValidator = new LicenseValidator(factory, buildDate); | ||
| var license = new License( | ||
| new Claim("account_id", Guid.NewGuid().ToString()), | ||
| 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("edition", nameof(Edition.Professional)), | ||
| new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)), | ||
| new Claim("perpetual", "true")); | ||
|
|
||
| license.IsConfigured.ShouldBeTrue(); | ||
| license.IsPerpetual.ShouldBeTrue(); | ||
|
|
||
| licenseValidator.Validate(license); | ||
|
|
||
| var logMessages = provider.Collector.GetSnapshot(); | ||
| logMessages.ShouldNotContain(log => log.Level == LogLevel.Error); | ||
| logMessages.ShouldContain(log => log.Level == LogLevel.Information && | ||
| log.Message.Contains("perpetual")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_reject_perpetual_license_when_build_date_after_expiration() | ||
| { | ||
| var factory = new LoggerFactory(); | ||
| var provider = new FakeLoggerProvider(); | ||
| factory.AddProvider(provider); | ||
|
|
||
| var buildDate = DateTimeOffset.UtcNow.AddDays(30); // Build date in future (after expiration) | ||
| var licenseValidator = new LicenseValidator(factory, buildDate); | ||
| var license = new License( | ||
| new Claim("account_id", Guid.NewGuid().ToString()), | ||
| 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()), | ||
|
jbogard marked this conversation as resolved.
Outdated
|
||
| new Claim("edition", nameof(Edition.Professional)), | ||
| new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)), | ||
| new Claim("perpetual", "true")); | ||
|
|
||
| license.IsConfigured.ShouldBeTrue(); | ||
| license.IsPerpetual.ShouldBeTrue(); | ||
|
|
||
| licenseValidator.Validate(license); | ||
|
|
||
| var logMessages = provider.Collector.GetSnapshot(); | ||
| logMessages.ShouldContain(log => log.Level == LogLevel.Error && | ||
| log.Message.Contains("expired")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_warn_and_error_when_perpetual_but_build_date_is_null() | ||
| { | ||
| var factory = new LoggerFactory(); | ||
| var provider = new FakeLoggerProvider(); | ||
| factory.AddProvider(provider); | ||
|
|
||
| var licenseValidator = new LicenseValidator(factory, buildDate: null); | ||
| var license = new License( | ||
| new Claim("account_id", Guid.NewGuid().ToString()), | ||
| 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("edition", nameof(Edition.Professional)), | ||
| new Claim("type", nameof(AutoMapper.Licensing.ProductType.AutoMapper)), | ||
| new Claim("perpetual", "true")); | ||
|
|
||
| license.IsConfigured.ShouldBeTrue(); | ||
| license.IsPerpetual.ShouldBeTrue(); | ||
|
|
||
| licenseValidator.Validate(license); | ||
|
|
||
| var logMessages = provider.Collector.GetSnapshot(); | ||
| logMessages.ShouldContain(log => log.Level == LogLevel.Warning && log.Message.Contains("perpetual")); | ||
| logMessages.ShouldContain(log => log.Level == LogLevel.Error && log.Message.Contains("expired")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_handle_missing_perpetual_claim() | ||
| { | ||
| var factory = new LoggerFactory(); | ||
| var provider = new FakeLoggerProvider(); | ||
| factory.AddProvider(provider); | ||
|
|
||
| var licenseValidator = new LicenseValidator(factory); | ||
| var license = new License( | ||
| new Claim("account_id", Guid.NewGuid().ToString()), | ||
| new Claim("customer_id", Guid.NewGuid().ToString()), | ||
| new Claim("sub_id", Guid.NewGuid().ToString()), | ||
| new Claim("iat", DateTimeOffset.UtcNow.AddDays(-1).ToUnixTimeSeconds().ToString()), | ||
| new Claim("exp", DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeSeconds().ToString()), | ||
| new Claim("edition", nameof(Edition.Community)), | ||
| new Claim("type", nameof(AutoMapper.Licensing.ProductType.Bundle))); | ||
|
|
||
| license.IsConfigured.ShouldBeTrue(); | ||
| license.IsPerpetual.ShouldBeFalse(); | ||
|
|
||
| licenseValidator.Validate(license); | ||
|
|
||
| var logMessages = provider.Collector.GetSnapshot(); | ||
| logMessages.ShouldNotContain(log => log.Level == LogLevel.Error | ||
| || log.Level == LogLevel.Warning | ||
| || log.Level == LogLevel.Critical); | ||
| } | ||
|
Comment on lines
+201
to
+227
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.