Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Minor changes with TwoFactorAuthenticationService

supporta2251e4a6723
diff --git a/src/Business/Grand.Business.Authentication/Services/TwoFactorAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/TwoFactorAuthenticationService.cs
index 12879e629..34863cc7c 100644
--- a/src/Business/Grand.Business.Authentication/Services/TwoFactorAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/TwoFactorAuthenticationService.cs
@@ -5,7 +5,6 @@ using Grand.Domain.Common;
using Grand.Domain.Customers;
using Grand.Domain.Localization;
using Grand.Infrastructure;
-using Microsoft.Extensions.DependencyInjection;
using Grand.Business.Core.Utilities.Authentication;
namespace Grand.Business.Authentication.Services
@@ -14,17 +13,17 @@ namespace Grand.Business.Authentication.Services
{
private readonly IWorkContext _workContext;
private readonly IUserFieldService _userFieldService;
- private readonly IServiceProvider _serviceProvider;
+ private readonly IEnumerable<ISMSVerificationService> _smsVerificationService;
private readonly TwoFactorAuthenticator _twoFactorAuthentication;
public TwoFactorAuthenticationService(
IWorkContext workContext,
IUserFieldService userFieldService,
- IServiceProvider serviceProvider)
+ IEnumerable<ISMSVerificationService> smsVerificationService)
{
_workContext = workContext;
_userFieldService = userFieldService;
- _serviceProvider = serviceProvider;
+ _smsVerificationService = smsVerificationService;
_twoFactorAuthentication = new TwoFactorAuthenticator();
}
@@ -43,8 +42,10 @@ namespace Grand.Business.Authentication.Services
return validUntil >= DateTime.UtcNow;
case TwoFactorAuthenticationType.SMSVerification:
- var smsVerificationService = _serviceProvider.GetRequiredService<ISMSVerificationService>();
- return await smsVerificationService.Authenticate(secretKey, token.Trim(), customer);
+ if (!_smsVerificationService.Any())
+ throw new Exception("ISMSVerificationService not registered in DI container");
+ var smsVerificationService = _smsVerificationService.FirstOrDefault();
+ return await smsVerificationService!.Authenticate(secretKey, token.Trim(), customer);
default:
return false;
}
@@ -70,8 +71,10 @@ namespace Grand.Business.Authentication.Services
break;
case TwoFactorAuthenticationType.SMSVerification:
- var smsVerificationService = _serviceProvider.GetRequiredService<ISMSVerificationService>();
- model = await smsVerificationService.GenerateCode(secretKey, customer, language);
+ if (!_smsVerificationService.Any())
+ throw new Exception("ISMSVerificationService not registered in DI container");
+ var smsVerificationService = _smsVerificationService.FirstOrDefault();
+ model = await smsVerificationService!.GenerateCode(secretKey, customer, language);
break;
}
return model;
diff --git a/src/Tests/Grand.Business.Authentication.Tests/Services/TwoFactorAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/TwoFactorAuthenticationServiceTests.cs
index b406d7b6c..373c9119a 100644
--- a/src/Tests/Grand.Business.Authentication.Tests/Services/TwoFactorAuthenticationServiceTests.cs
+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/TwoFactorAuthenticationServiceTests.cs
@@ -1,4 +1,5 @@
using Grand.Business.Authentication.Services;
+using Grand.Business.Core.Interfaces.Authentication;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Domain.Customers;
using Grand.Infrastructure;
@@ -12,16 +13,16 @@ namespace Grand.Business.Authentication.Tests.Services
{
private Mock<IWorkContext> _workContextMock;
private Mock<IUserFieldService> _userFieldServiceMock;
- private Mock<IServiceProvider> _serviceProviderMock;
+ private Mock<IEnumerable<ISMSVerificationService>> _sMsVerificationService;
private TwoFactorAuthenticationService _twoFactorAuthenticationService;
[TestInitialize()]
public void Init()
{
_workContextMock = new Mock<IWorkContext>();
_userFieldServiceMock = new Mock<IUserFieldService>();
- _serviceProviderMock = new Mock<IServiceProvider>();
+ _sMsVerificationService = new Mock<IEnumerable<ISMSVerificationService>>();
_twoFactorAuthenticationService = new TwoFactorAuthenticationService(_workContextMock.Object,
- _userFieldServiceMock.Object, _serviceProviderMock.Object);
+ _userFieldServiceMock.Object, _sMsVerificationService.Object);
}
[TestMethod()]