Skip to content

Commit 02b9878

Browse files
VCST-2464: Fix refresh token after impersonation (#2869)
1 parent 2c5dab1 commit 02b9878

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ public async Task<ActionResult> Exchange()
212212
// Create a new authentication ticket, but reuse the properties stored in the
213213
// authorization code/refresh token, including the scopes originally granted.
214214
var ticket = await CreateTicketAsync(user, context);
215-
ticket.Principal.SetAuthenticationMethod(info.Principal.GetAuthenticationMethod(), [Destinations.AccessToken]);
215+
216+
var destinations = new[] { Destinations.AccessToken };
217+
CopyClaim(info.Principal, ticket.Principal, ClaimTypes.AuthenticationMethod, destinations);
218+
CopyClaim(info.Principal, ticket.Principal, PlatformConstants.Security.Claims.OperatorUserId, destinations);
219+
CopyClaim(info.Principal, ticket.Principal, PlatformConstants.Security.Claims.OperatorUserName, destinations);
216220

217221
return SignIn(ticket.Principal, ticket.AuthenticationScheme);
218222
}
@@ -288,10 +292,8 @@ public async Task<ActionResult> Exchange()
288292
}
289293

290294
// Resolve Impersonator from claims or from current user
291-
var operatorUserId = string.IsNullOrEmpty(User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserId)) ?
292-
user.Id : User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserId);
293-
var operatorUserName = string.IsNullOrEmpty(User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserName)) ?
294-
user.UserName : User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserName);
295+
var operatorUserId = User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserId)?.EmptyToNull() ?? user.Id;
296+
var operatorUserName = User.FindFirstValue(PlatformConstants.Security.Claims.OperatorUserName)?.EmptyToNull() ?? user.UserName;
295297

296298
var userId = openIdConnectRequest.GetParameter("user_id")?.Value?.ToString();
297299
ApplicationUser impersonatedUser;
@@ -567,6 +569,12 @@ public async Task<IActionResult> Logout()
567569
}
568570

569571

572+
private static void CopyClaim(ClaimsPrincipal source, ClaimsPrincipal target, string claimType, IList<string> destinations)
573+
{
574+
var value = source.FindFirstValue(claimType);
575+
target.SetClaimWithDestinations(claimType, value, destinations);
576+
}
577+
570578
private static bool RequestHasExpired(OpenIddictRequest request, AuthenticateResult result)
571579
{
572580
return request.MaxAge != null &&

0 commit comments

Comments
 (0)