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.

Add refresh id

dkucia5ab04754cc53
diff --git a/src/API/Grand.Api/Controllers/TokenWebController.cs b/src/API/Grand.Api/Controllers/TokenWebController.cs
index b6cfdd70f..4208ead00 100644
--- a/src/API/Grand.Api/Controllers/TokenWebController.cs
+++ b/src/API/Grand.Api/Controllers/TokenWebController.cs
@@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Security.Claims;
using System.Threading.Tasks;
namespace Grand.Api.Controllers
@@ -93,8 +94,16 @@ namespace Grand.Api.Controllers
string email = null, guid = null;
Customer customer = null;
var claims = new Dictionary<string, string>();
- var principal = _refreshTokenService.GetPrincipalFromToken(tokenDto.AccessToken);
- email = principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
+ ClaimsPrincipal principal = null;
+ try
+ {
+ principal = _refreshTokenService.GetPrincipalFromToken(tokenDto.AccessToken);
+ email = principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
+ }
+ catch(Exception)
+ {
+ return BadRequest("Invalid access token");
+ }
if (!string.IsNullOrEmpty(email))
{
customer = await _customerService.GetCustomerByEmail(email);
@@ -127,12 +136,13 @@ namespace Grand.Api.Controllers
private async Task<TokenDto> GetToken(Dictionary<string, string> claims, Customer customer)
{
+ var refreshTokenValue = _refreshTokenService.GenerateRefreshToken();
+ var refreshToken=await _refreshTokenService.SaveRefreshTokenToCustomer(customer, refreshTokenValue);
+ claims.Add("RefreshId", refreshToken.RefreshId);
var token = await _mediator.Send(new GenerateTokenWebCommand() { Claims = claims });
- var refreshToken = _refreshTokenService.GenerateRefreshToken();
- await _refreshTokenService.SaveRefreshTokenToCustomer(customer, refreshToken);
return new TokenDto() {
AccessToken = token,
- RefreshToken = refreshToken
+ RefreshToken = refreshTokenValue
};
}
}
diff --git a/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs b/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
index e5416e8f7..65592ea1a 100644
--- a/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
+++ b/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
@@ -79,7 +79,8 @@ namespace Grand.Api.Infrastructure
ValidateIssuerSigningKey = true,
ValidIssuer = config.ValidIssuer,
ValidAudience = config.ValidAudience,
- IssuerSigningKey = JwtSecurityKey.Create(config.SecretKey)
+ IssuerSigningKey = JwtSecurityKey.Create(config.SecretKey),
+ ClockSkew = TimeSpan.Zero
};
options.Events = new JwtBearerEvents {
diff --git a/src/Business/Grand.Business.Authentication/Interfaces/IRefreshTokenService.cs b/src/Business/Grand.Business.Authentication/Interfaces/IRefreshTokenService.cs
index e2aaf8dbc..8f1c4ce2a 100644
--- a/src/Business/Grand.Business.Authentication/Interfaces/IRefreshTokenService.cs
+++ b/src/Business/Grand.Business.Authentication/Interfaces/IRefreshTokenService.cs
@@ -12,7 +12,7 @@ namespace Grand.Business.Authentication.Interfaces
public interface IRefreshTokenService
{
string GenerateRefreshToken();
- Task SaveRefreshTokenToCustomer(Customer customer, string refreshToken);
+ Task<RefreshToken> SaveRefreshTokenToCustomer(Customer customer, string refreshToken);
Task<RefreshToken> GetCustomerRefreshToken(Customer customer);
ClaimsPrincipal GetPrincipalFromToken(string token);
}
diff --git a/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
index fad5821e7..6efd91c2c 100644
--- a/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
@@ -4,6 +4,7 @@ using Grand.Business.Common.Interfaces.Security;
using Grand.Business.Common.Services.Security;
using Grand.Business.Customers.Interfaces;
using Grand.Domain.Customers;
+using Grand.Domain.Security;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System;
using System.Collections.Generic;
@@ -19,21 +20,24 @@ namespace Grand.Business.Authentication.Services
private readonly IPermissionService _permissionService;
private readonly IUserFieldService _userFieldService;
private readonly IGroupService _groupService;
+ private readonly IRefreshTokenService _refreshTokenService;
private string _errorMessage;
public JwtBearerCustomerAuthenticationService(ICustomerService customerService,IPermissionService permissionService
- ,IUserFieldService userFieldService,IGroupService groupService)
+ ,IUserFieldService userFieldService,IGroupService groupService,IRefreshTokenService refreshTokenService)
{
_customerService = customerService;
_permissionService = permissionService;
_userFieldService = userFieldService;
_groupService = groupService;
+ _refreshTokenService = refreshTokenService;
}
public async Task<bool> Valid(TokenValidatedContext context)
{
var email = context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
var passwordToken = context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Token")?.Value;
+ var refreshId = context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "RefreshId")?.Value;
Customer customer = null;
if(email is null)
{
@@ -57,7 +61,14 @@ namespace Grand.Business.Authentication.Services
return false;
}
- if(!(await _permissionService.Authorize(StandardPermission.AllowUseApi,customer)))
+ var refreshToken = await _refreshTokenService.GetCustomerRefreshToken(customer);
+ if(refreshToken is null || string.IsNullOrEmpty(refreshId) || !refreshId.Equals(refreshToken?.RefreshId))
+ {
+ _errorMessage = "Invalid token or cancel by refresh token";
+ return false;
+ }
+
+ if (!(await _permissionService.Authorize(StandardPermission.AllowUseApi,customer)))
{
_errorMessage = "Customer not has permission";
return false;
diff --git a/src/Business/Grand.Business.Authentication/Services/RefreshTokenService.cs b/src/Business/Grand.Business.Authentication/Services/RefreshTokenService.cs
index 34f8e1b16..55fdccfdf 100644
--- a/src/Business/Grand.Business.Authentication/Services/RefreshTokenService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/RefreshTokenService.cs
@@ -15,7 +15,6 @@ namespace Grand.Business.Authentication.Services
{
public class RefreshTokenService : IRefreshTokenService
{
- private const string _userFieldRefreshToken = "RefreshToken";
private readonly IUserFieldService _userFieldService;
private readonly GrandWebApiConfig _grandWebApiConfig;
@@ -35,18 +34,21 @@ namespace Grand.Business.Authentication.Services
}
}
- public async Task SaveRefreshTokenToCustomer(Customer customer, string refreshToken)
+ public async Task<RefreshToken> SaveRefreshTokenToCustomer(Customer customer, string refreshToken)
{
- await _userFieldService.SaveField(customer, _userFieldRefreshToken, new RefreshToken() {
+ var token = new RefreshToken() {
+ RefreshId=Guid.NewGuid().ToString(),
Token = refreshToken,
IsActive = true,
ValidTo = DateTime.UtcNow.AddMinutes(_grandWebApiConfig.RefreshTokenExpiryInMinutes)
- });
+ };
+ await _userFieldService.SaveField(customer, SystemCustomerFieldNames.RefreshToken, token);
+ return token;
}
public async Task<RefreshToken> GetCustomerRefreshToken(Customer customer)
{
- return await _userFieldService.GetFieldsForEntity<RefreshToken>(customer, _userFieldRefreshToken);
+ return await _userFieldService.GetFieldsForEntity<RefreshToken>(customer, SystemCustomerFieldNames.RefreshToken);
}
public ClaimsPrincipal GetPrincipalFromToken(string token)
diff --git a/src/Core/Grand.Domain/Customers/SystemCustomerFieldNames.cs b/src/Core/Grand.Domain/Customers/SystemCustomerFieldNames.cs
index 0d7d691bf..a63bf55fc 100644
--- a/src/Core/Grand.Domain/Customers/SystemCustomerFieldNames.cs
+++ b/src/Core/Grand.Domain/Customers/SystemCustomerFieldNames.cs
@@ -55,5 +55,6 @@ namespace Grand.Domain.Customers
public static string UseLoyaltyPointsDuringCheckout { get { return "UseLoyaltyPointsDuringCheckout"; } }
public static string CookieAccepted { get { return "Cookie.Accepted"; } }
public static string ConsentCookies { get { return "ConsentCookies"; } }
+ public static string RefreshToken { get { return "RefreshToken"; } }
}
}
\ No newline at end of file
diff --git a/src/Core/Grand.Domain/Security/RefreshToken.cs b/src/Core/Grand.Domain/Security/RefreshToken.cs
index b28aeece0..66c0f3f5a 100644
--- a/src/Core/Grand.Domain/Security/RefreshToken.cs
+++ b/src/Core/Grand.Domain/Security/RefreshToken.cs
@@ -8,6 +8,7 @@ namespace Grand.Domain.Security
{
public class RefreshToken
{
+ public string RefreshId { get; set; }
public string Token { get; set; }
public DateTime ValidTo { get; set; }
public bool IsActive { get; set; }