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 login model for API - add a new validator

KrzysztofPajak9068ef62051a
diff --git a/src/API/Grand.Api/Controllers/TokenWebController.cs b/src/API/Grand.Api/Controllers/TokenWebController.cs
index 8b96e175e..88394c412 100644
--- a/src/API/Grand.Api/Controllers/TokenWebController.cs
+++ b/src/API/Grand.Api/Controllers/TokenWebController.cs
@@ -6,6 +6,7 @@ using Grand.Business.Common.Interfaces.Directory;
using Grand.Business.Customers.Interfaces;
using Grand.Domain.Customers;
using Grand.Infrastructure;
+using Grand.Infrastructure.Configuration;
using MediatR;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Authorization;
@@ -21,29 +22,29 @@ namespace Grand.Api.Controllers
public class TokenWebController : Controller
{
private readonly ICustomerService _customerService;
- private readonly ICustomerManagerService _customerManagerService;
private readonly IMediator _mediator;
private readonly IStoreHelper _storeHelper;
private readonly IRefreshTokenService _refreshTokenService;
private readonly IUserFieldService _userFieldService;
private readonly IAntiforgery _antiforgery;
+ private readonly GrandWebApiConfig _apiConfig;
public TokenWebController(
ICustomerService customerService,
- ICustomerManagerService customerManagerService,
IMediator mediator,
IStoreHelper storeHelper,
IRefreshTokenService refreshTokenService,
IUserFieldService userFieldService,
- IAntiforgery antiforgery)
+ IAntiforgery antiforgery,
+ GrandWebApiConfig apiConfig)
{
_customerService = customerService;
- _customerManagerService = customerManagerService;
_mediator = mediator;
_storeHelper = storeHelper;
_refreshTokenService = refreshTokenService;
_userFieldService = userFieldService;
_antiforgery = antiforgery;
+ _apiConfig = apiConfig;
}
[AllowAnonymous]
@@ -51,6 +52,9 @@ namespace Grand.Api.Controllers
[HttpPost]
public async Task<IActionResult> Guest()
{
+ if (!_apiConfig.Enabled)
+ return BadRequest("API is disabled");
+
var customer = await _customerService.InsertGuestCustomer(_storeHelper.StoreHost);
var claims = new Dictionary<string, string> {
{ "Guid", customer.CustomerGuid.ToString()}
@@ -65,27 +69,15 @@ namespace Grand.Api.Controllers
[HttpPost]
public async Task<IActionResult> Login([FromBody] LoginModel model)
{
+ if (!ModelState.IsValid)
+ return BadRequest(ModelState);
+
try
{
- var base64EncodedBytes = Convert.FromBase64String(model.Password);
- var password = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
-
- var result = await _customerManagerService.LoginCustomer(model.Email, password);
- if (!result.Equals(CustomerLoginResults.Successful))
- {
- return BadRequest(result.ToString());
- }
-
var customer = await _customerService.GetCustomerByEmail(model.Email);
-
var claims = new Dictionary<string, string> {
- {
- "Email", model.Email
- },
- {
- "Token",
- await _userFieldService.GetFieldsForEntity<string>(customer, SystemCustomerFieldNames.PasswordToken)
- }
+ { "Email", model.Email },
+ { "Token", await _userFieldService.GetFieldsForEntity<string>(customer, SystemCustomerFieldNames.PasswordToken) }
};
var tokenDto = await GetToken(claims, customer);
return Ok(tokenDto);
@@ -101,6 +93,9 @@ namespace Grand.Api.Controllers
[HttpPost]
public async Task<IActionResult> Refresh([FromBody] TokenDto tokenDto)
{
+ if (!_apiConfig.Enabled)
+ return BadRequest("API is disabled");
+
string email = null, guid = null;
Customer customer = null;
var claims = new Dictionary<string, string>();
@@ -145,7 +140,10 @@ namespace Grand.Api.Controllers
[AllowAnonymous]
public IActionResult Antiforgery()
{
- var token = _antiforgery.GetAndStoreTokens(this.HttpContext).RequestToken;
+ if (_apiConfig.Enabled)
+ return BadRequest("API is disabled");
+
+ var token = _antiforgery.GetAndStoreTokens(HttpContext).RequestToken;
return Ok(token);
}
diff --git a/src/API/Grand.Api/Models/Common/LoginModel.cs b/src/API/Grand.Api/Models/Common/LoginModel.cs
index 5b187d986..0e52fce6c 100644
--- a/src/API/Grand.Api/Models/Common/LoginModel.cs
+++ b/src/API/Grand.Api/Models/Common/LoginModel.cs
@@ -1,6 +1,6 @@
namespace Grand.Api.Models.Common
{
- public class LoginModel
+ public class LoginWebModel
{
public string Email { get; set; }
public string Password { get; set; }
diff --git a/src/API/Grand.Api/Models/Common/LoginWebModel.cs b/src/API/Grand.Api/Models/Common/LoginWebModel.cs
new file mode 100644
index 000000000..5b187d986
--- /dev/null
+++ b/src/API/Grand.Api/Models/Common/LoginWebModel.cs
@@ -0,0 +1,8 @@
+namespace Grand.Api.Models.Common
+{
+ public class LoginModel
+ {
+ public string Email { get; set; }
+ public string Password { get; set; }
+ }
+}
diff --git a/src/API/Grand.Api/Validators/Common/LoginValidator.cs b/src/API/Grand.Api/Validators/Common/LoginValidator.cs
index e5dc9bc58..a3289bf4e 100644
--- a/src/API/Grand.Api/Validators/Common/LoginValidator.cs
+++ b/src/API/Grand.Api/Validators/Common/LoginValidator.cs
@@ -46,9 +46,6 @@ namespace Grand.Api.Validators.Common
}
return false;
}).WithMessage("Customer not exist");
- EncryptionService = encryptionService;
}
-
- public IEncryptionService EncryptionService { get; }
}
}
diff --git a/src/API/Grand.Api/Validators/Common/LoginWebValidator.cs b/src/API/Grand.Api/Validators/Common/LoginWebValidator.cs
new file mode 100644
index 000000000..ae0aabefa
--- /dev/null
+++ b/src/API/Grand.Api/Validators/Common/LoginWebValidator.cs
@@ -0,0 +1,47 @@
+using FluentValidation;
+using Grand.Api.Models.Common;
+using Grand.Business.Customers.Interfaces;
+using Grand.Domain.Customers;
+using Grand.Infrastructure.Configuration;
+using Grand.Infrastructure.Validators;
+using System.Collections.Generic;
+
+namespace Grand.Api.Validators.Common
+{
+ public class LoginWebValidator : BaseGrandValidator<LoginModel>
+ {
+ public LoginWebValidator(
+ IEnumerable<IValidatorConsumer<LoginModel>> validators,
+ GrandWebApiConfig apiConfig,
+ ICustomerService customerService,
+ ICustomerManagerService customerManagerService)
+ : base(validators)
+ {
+ if (!apiConfig.Enabled)
+ RuleFor(x => x).Must((x) => false).WithMessage("API is disabled");
+ else
+ {
+ RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required");
+ RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required");
+ RuleFor(x => x).MustAsync(async (x, context) =>
+ {
+ if (!string.IsNullOrEmpty(x.Email))
+ {
+ var customer = await customerService.GetCustomerByEmail(x.Email.ToLowerInvariant());
+ if (customer != null && customer.Active && !customer.IsSystemAccount())
+ {
+ var base64EncodedBytes = System.Convert.FromBase64String(x.Password);
+ var password = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
+ var result = await customerManagerService.LoginCustomer(x.Email, password);
+ if (result == CustomerLoginResults.Successful)
+ return true;
+
+ return false;
+ }
+ }
+ return false;
+ }).WithMessage("Customer not exist or password is wrong");
+ }
+ }
+ }
+}