Skip to content

Commit efb8fe1

Browse files
authored
VST-2898: Update license activation logic and add logging (#2901)
1 parent 18eb4a9 commit efb8fe1

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

src/VirtoCommerce.Platform.Core/PlatformConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace VirtoCommerce.Platform.Core
99
{
1010
public static class PlatformConstants
1111
{
12+
public const string UserAgent = "VirtoCommerce/3.0 (https://virtocommerce.com)";
13+
1214
public static class Security
1315
{
1416
public static class GrantTypes

src/VirtoCommerce.Platform.Core/PlatformOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PlatformOptions
1313

1414
// The public URL for license activation
1515
[Url]
16-
public string LicenseActivationUrl { get; set; } = "https://virtocommerce.com/admin/api/licenses/activate/";
16+
public string LicenseActivationUrl { get; set; } = "https://license.virtocommerce.org/api/licenses/activate/";
1717

1818
// Local path for license file
1919
public string LicenseFilePath { get; set; } = "app_data/VirtoCommerce.lic";

src/VirtoCommerce.Platform.Web/Controllers/Api/LicensingController.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Options;
1011
using VirtoCommerce.Platform.Core;
1112
using VirtoCommerce.Platform.Core.Settings;
@@ -21,40 +22,67 @@ public class LicensingController : Controller
2122
private readonly PlatformOptions _platformOptions;
2223
private readonly ISettingsManager _settingsManager;
2324
private readonly LicenseProvider _licenseProvider;
24-
25-
public LicensingController(IOptions<PlatformOptions> platformOptions, ISettingsManager settingsManager, LicenseProvider licenseProvider)
25+
private readonly ILogger<LicensingController> _logger;
26+
private readonly IHttpClientFactory _httpClientFactory;
27+
28+
public LicensingController(
29+
IOptions<PlatformOptions> platformOptions,
30+
ISettingsManager settingsManager,
31+
LicenseProvider licenseProvider,
32+
ILogger<LicensingController> logger,
33+
IHttpClientFactory httpClientFactory)
2634
{
2735
_platformOptions = platformOptions.Value;
2836
_settingsManager = settingsManager;
2937
_licenseProvider = licenseProvider;
38+
_logger = logger;
39+
_httpClientFactory = httpClientFactory;
3040
}
3141

3242
[HttpPost]
3343
[Route("activateByCode")]
3444
public async Task<ActionResult<License>> ActivateByCode([FromBody] string activationCode)
3545
{
46+
_logger.LogInformation("Activation by code started.");
47+
3648
if (!Regex.IsMatch(activationCode, "^([a-zA-Z_0-9-])+$"))
3749
{
50+
_logger.LogWarning("Invalid activation code");
3851
return BadRequest(new { Message = $"Activation code \"{activationCode}\" is invalid" });
3952
}
4053

4154
License license = null;
4255

43-
using (var httpClient = new HttpClient())
56+
var activationUrl = new Uri(_platformOptions.LicenseActivationUrl + activationCode);
57+
try
4458
{
45-
var activationUrl = new Uri(_platformOptions.LicenseActivationUrl + activationCode);
59+
var httpClient = _httpClientFactory.CreateClient();
60+
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(PlatformConstants.UserAgent);
61+
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
62+
63+
_logger.LogInformation("Sending request to activation URL: {ActivationUrl}", _platformOptions.LicenseActivationUrl);
4664
var httpResponse = await httpClient.GetAsync(activationUrl);
4765

4866
if (httpResponse.IsSuccessStatusCode)
4967
{
5068
var rawLicense = await httpResponse.Content.ReadAsStringAsync();
69+
_logger.LogInformation("License content '{LicenseContent}' received successful.", rawLicense);
5170
license = License.Parse(rawLicense, _platformOptions.LicensePublicKeyResourceName);
71+
_logger.LogInformation("License activation successful.");
72+
}
73+
else
74+
{
75+
_logger.LogWarning("License activation failed using {ActivationUrl} by code with status code: {StatusCode}", _platformOptions.LicenseActivationUrl, httpResponse.StatusCode);
5276
}
53-
}
5477

55-
if (license != null)
78+
if (license != null)
79+
{
80+
await DisableTrial();
81+
}
82+
}
83+
catch (Exception ex)
5684
{
57-
await DisableTrial();
85+
_logger.LogError(ex, "Error occurred during license activation using {ActivationUrl} by code.", _platformOptions.LicenseActivationUrl);
5886
}
5987

6088
return Ok(license);

tests/VirtoCommerce.Platform.Web.Tests/Controllers/Api/LicensingControllerTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Threading.Tasks;
56
using AutoFixture;
67
using FluentAssertions;
78
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
810
using Microsoft.Extensions.Options;
911
using Moq;
1012
using VirtoCommerce.Platform.Core;
@@ -22,6 +24,8 @@ public class LicensingControllerTests : PlatformWebMockHelper
2224
private readonly Mock<IOptions<PlatformOptions>> _platformOptionsMock = new Mock<IOptions<PlatformOptions>>();
2325
private readonly Mock<ISettingsManager> _settingsManager = new Mock<ISettingsManager>();
2426
private readonly Mock<Func<IPlatformRepository>> _platformRepository = new Mock<Func<IPlatformRepository>>();
27+
private readonly Mock<ILogger<LicensingController>> _logger = new Mock<ILogger<LicensingController>>();
28+
private readonly Mock<IHttpClientFactory> _httpClientFactory = new Mock<IHttpClientFactory>();
2529
private readonly PlatformOptions platformOptions = new PlatformOptions();
2630
private readonly LicenseProvider _licenseProvider;
2731

@@ -33,7 +37,7 @@ public LicensingControllerTests()
3337

3438
_licenseProvider = new LicenseProvider(_platformOptionsMock.Object, _platformRepository.Object);
3539

36-
_controller = new LicensingController(_platformOptionsMock.Object, _settingsManager.Object, _licenseProvider);
40+
_controller = new LicensingController(_platformOptionsMock.Object, _settingsManager.Object, _licenseProvider, _logger.Object, _httpClientFactory.Object);
3741
}
3842

3943
[Theory]

0 commit comments

Comments
 (0)