Skip to content
Open
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
19 changes: 15 additions & 4 deletions src/AutoMapper/Licensing/LicenseAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,22 @@ private License Initialize()
/// Resolves the license key from, in order of precedence: the explicitly configured value,
/// the product-specific <c>AUTOMAPPER_LICENSE_KEY</c> environment variable, then the shared
/// <c>LUCKYPENNY_LICENSE_KEY</c> environment variable (usable across Lucky Penny products).
/// Whitespace is removed from each value, and empty values are treated as unset.
/// </summary>
internal static string ResolveLicenseKey(string explicitKey) =>
explicitKey
?? Environment.GetEnvironmentVariable(AutoMapperLicenseKeyEnvVariable)
?? Environment.GetEnvironmentVariable(SharedLicenseKeyEnvVariable);
NormalizeLicenseKey(explicitKey)
?? NormalizeLicenseKey(Environment.GetEnvironmentVariable(AutoMapperLicenseKeyEnvVariable))
?? NormalizeLicenseKey(Environment.GetEnvironmentVariable(SharedLicenseKeyEnvVariable));

private static string NormalizeLicenseKey(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return null;
}

return new string(key.Where(c => !char.IsWhiteSpace(c)).ToArray());
}

private Claim[] ValidateKey(string licenseKey)
{
Expand Down Expand Up @@ -105,4 +116,4 @@ private Claim[] ValidateKey(string licenseKey)
}
}

}
}
70 changes: 70 additions & 0 deletions src/UnitTests/Licensing/LicenseKeyEnvironmentVariableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,76 @@ public class LicenseKeyEnvironmentVariableTests
{
private const string AutoMapperEnvVar = LicenseAccessor.AutoMapperLicenseKeyEnvVariable;
private const string SharedEnvVar = LicenseAccessor.SharedLicenseKeyEnvVariable;
private const string CleanKey = "header.pay-load.sign_ature";

public static TheoryData<string> KeysWithWhitespace => new()
{
CleanKey + "\n",
CleanKey + "\r\n",
CleanKey + " ",
CleanKey + "\t",
" \t" + CleanKey,
"header.\r\npay-\nload.sign_\tature",
"\u00a0header.\u2003pay-load.sign_ature\u2028"
};

[Theory]
[MemberData(nameof(KeysWithWhitespace))]
public void ExplicitKey_StripsWhitespace_AndTakesPrecedence(string key)
{
WithEnvironment(autoMapper: "env-automapper-key", shared: "env-shared-key", () =>
LicenseAccessor.ResolveLicenseKey(key).ShouldBe(CleanKey));
}

[Theory]
[MemberData(nameof(KeysWithWhitespace))]
public void AutoMapperEnvironmentVariable_StripsWhitespace_AndTakesPrecedence(string key)
{
WithEnvironment(autoMapper: key, shared: "env-shared-key", () =>
LicenseAccessor.ResolveLicenseKey(null).ShouldBe(CleanKey));
}

[Theory]
[MemberData(nameof(KeysWithWhitespace))]
public void SharedEnvironmentVariable_StripsWhitespace(string key)
{
WithEnvironment(autoMapper: null, shared: key, () =>
LicenseAccessor.ResolveLicenseKey(null).ShouldBe(CleanKey));
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("\t\r\n")]
[InlineData("\u00a0\u2003\u2028")]
public void BlankExplicitKey_FallsBackToAutoMapperEnvironmentVariable(string key)
{
WithEnvironment(autoMapper: " env-automapper-key\n", shared: "env-shared-key", () =>
LicenseAccessor.ResolveLicenseKey(key).ShouldBe("env-automapper-key"));
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("\t\r\n")]
[InlineData("\u00a0\u2003\u2028")]
public void BlankHigherPrecedenceKeys_FallBackToSharedEnvironmentVariable(string key)
{
WithEnvironment(autoMapper: key, shared: " env-shared-key\n", () =>
LicenseAccessor.ResolveLicenseKey(key).ShouldBe("env-shared-key"));
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("\t\r\n")]
[InlineData("\u00a0\u2003\u2028")]
public void ReturnsNull_WhenAllKeysAreBlank(string key)
{
WithEnvironment(autoMapper: key, shared: key, () =>
LicenseAccessor.ResolveLicenseKey(key).ShouldBeNull());
}

[Fact]
public void ExplicitKey_TakesPrecedence_OverBothEnvironmentVariables()
Expand Down
Loading