Browse Source

Address Copilot review on #25450

- Use UserManager.SetAuthenticationTokenAsync/GetAuthenticationTokenAsync/
  RemoveAuthenticationTokenAsync so the Tokens collection is loaded via
  IdentityUserStore.EnsureCollectionLoadedAsync instead of relying on the
  GetByIdAsync(includeDetails) convention.
- Add a RemoveLinkUserTokenAsync(manager, user, purpose) overload to invalidate
  tokens issued for purposes other than LinkUserTokenPurpose.
- Add a cross-UnitOfWork persistence test for SetLink/Get/Remove ConsentAsync.
- Drop the unused IdentityLinkUserRepository field from LinkUserTokenProvider_Tests.
pull/25450/head
maliming 2 months ago
parent
commit
8429dbaca4
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 11
      modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/IdentityUserManagerSingleActiveTokenExtensions.cs
  2. 17
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityLinkUserManager.cs
  3. 2
      modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/LinkUserTokenProvider_Tests.cs
  4. 27
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityLinkUserManager_Tests.cs

11
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/IdentityUserManagerSingleActiveTokenExtensions.cs

@ -47,7 +47,16 @@ public static class IdentityUserManagerSingleActiveTokenExtensions
/// </summary>
public static Task<IdentityResult> RemoveLinkUserTokenAsync(this IdentityUserManager manager, IdentityUser user)
{
var name = LinkUserTokenProviderConsts.LinkUserTokenProviderName + ":" + LinkUserTokenProviderConsts.LinkUserTokenPurpose;
return RemoveLinkUserTokenAsync(manager, user, LinkUserTokenProviderConsts.LinkUserTokenPurpose);
}
/// <summary>
/// Removes the stored link-user token hash for <paramref name="user"/> and the given <paramref name="purpose"/>,
/// immediately invalidating any previously issued link-user token for that purpose.
/// </summary>
public static Task<IdentityResult> RemoveLinkUserTokenAsync(this IdentityUserManager manager, IdentityUser user, string purpose)
{
var name = LinkUserTokenProviderConsts.LinkUserTokenProviderName + ":" + purpose;
return manager.RemoveAuthenticationTokenAsync(user, AbpSingleActiveTokenProvider.InternalLoginProvider, name);
}
}

17
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityLinkUserManager.cs

@ -167,11 +167,11 @@ public class IdentityLinkUserManager : DomainService
using (CurrentTenant.Change(sourceLinkUser.TenantId))
{
var user = await UserManager.GetByIdAsync(sourceLinkUser.UserId);
user.SetToken(
(await UserManager.SetAuthenticationTokenAsync(
user,
LinkUserTokenProviderConsts.LinkUserConsentLoginProvider,
LinkUserTokenProviderConsts.LinkUserConsentTokenName,
consent);
(await UserManager.UpdateAsync(user)).CheckErrors();
consent)).CheckErrors();
}
}
@ -184,9 +184,10 @@ public class IdentityLinkUserManager : DomainService
{
return null;
}
return user.FindToken(
return await UserManager.GetAuthenticationTokenAsync(
user,
LinkUserTokenProviderConsts.LinkUserConsentLoginProvider,
LinkUserTokenProviderConsts.LinkUserConsentTokenName)?.Value;
LinkUserTokenProviderConsts.LinkUserConsentTokenName);
}
}
@ -199,10 +200,10 @@ public class IdentityLinkUserManager : DomainService
{
return;
}
user.RemoveToken(
(await UserManager.RemoveAuthenticationTokenAsync(
user,
LinkUserTokenProviderConsts.LinkUserConsentLoginProvider,
LinkUserTokenProviderConsts.LinkUserConsentTokenName);
(await UserManager.UpdateAsync(user)).CheckErrors();
LinkUserTokenProviderConsts.LinkUserConsentTokenName)).CheckErrors();
}
}
}

2
modules/identity/test/Volo.Abp.Identity.AspNetCore.Tests/Volo/Abp/Identity/AspNetCore/LinkUserTokenProvider_Tests.cs

@ -9,12 +9,10 @@ namespace Volo.Abp.Identity.AspNetCore;
public class LinkUserTokenProvider_Tests : AbpSingleActiveTokenProviderTestBase
{
protected IIdentityLinkUserRepository IdentityLinkUserRepository { get; }
protected IdentityLinkUserManager IdentityLinkUserManager { get; }
public LinkUserTokenProvider_Tests()
{
IdentityLinkUserRepository = GetRequiredService<IIdentityLinkUserRepository>();
IdentityLinkUserManager = GetRequiredService<IdentityLinkUserManager>();
}

27
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityLinkUserManager_Tests.cs

@ -256,4 +256,31 @@ public class IdentityLinkUserManager_Tests : AbpIdentityDomainTestBase
{
await IdentityLinkUserManager.RemoveLinkConsentAsync(new IdentityLinkUserInfo(Guid.NewGuid(), null));
}
[Fact]
public virtual async Task SetLinkConsentAsync_Should_Persist_Across_UnitOfWork()
{
var john = await UserRepository.GetAsync(TestData.UserJohnId);
var source = new IdentityLinkUserInfo(john.Id, john.TenantId);
await UsingUowAsync(async () =>
{
await IdentityLinkUserManager.SetLinkConsentAsync(source, "consent-payload");
});
await UsingUowAsync(async () =>
{
(await IdentityLinkUserManager.GetLinkConsentAsync(source)).ShouldBe("consent-payload");
});
await UsingUowAsync(async () =>
{
await IdentityLinkUserManager.RemoveLinkConsentAsync(source);
});
await UsingUowAsync(async () =>
{
(await IdentityLinkUserManager.GetLinkConsentAsync(source)).ShouldBeNull();
});
}
}

Loading…
Cancel
Save