diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs new file mode 100644 index 0000000000..8de564cc23 --- /dev/null +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.IdentityServer.Devices; +using Volo.Abp.IdentityServer.Grants; +using Xunit; + +namespace Volo.Abp.IdentityServer.Tokens +{ + public class TokenCleanupService_Tests : AbpIdentityServerTestBase + { + private readonly IPersistentGrantRepository _persistentGrantRepository; + private readonly IDeviceFlowCodesRepository _deviceFlowCodesRepository; + private readonly TokenCleanupService _tokenCleanupService; + + public TokenCleanupService_Tests() + { + _persistentGrantRepository = GetRequiredService(); + _deviceFlowCodesRepository = GetRequiredService(); + _tokenCleanupService = GetRequiredService(); + } + + [Fact] + public async Task Should_Clear_Expired_Tokens() + { + var persistentGrantCount = await _persistentGrantRepository.GetCountAsync(); + var deviceFlowCodesCount = await _deviceFlowCodesRepository.GetCountAsync(); + + await _tokenCleanupService.CleanAsync(); + + (await _persistentGrantRepository.GetCountAsync()) + .ShouldBe(persistentGrantCount - 1); + + (await _deviceFlowCodesRepository.GetCountAsync()) + .ShouldBe(deviceFlowCodesCount - 1); + } + } +} diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs index e1676f2408..849dd874d5 100644 --- a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs @@ -6,8 +6,10 @@ using Volo.Abp.Guids; using Volo.Abp.Identity; using Volo.Abp.IdentityServer.ApiResources; using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; using Volo.Abp.IdentityServer.IdentityResources; +using Volo.Abp.Timing; namespace Volo.Abp.IdentityServer { @@ -19,7 +21,9 @@ namespace Volo.Abp.IdentityServer private readonly IIdentityResourceRepository _identityResourceRepository; private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository; private readonly IPersistentGrantRepository _persistentGrantRepository; + private readonly IDeviceFlowCodesRepository _deviceFlowCodesRepository; private readonly AbpIdentityServerTestData _testData; + private readonly IClock _clock; public AbpIdentityServerTestDataBuilder( IGuidGenerator guidGenerator, @@ -28,7 +32,9 @@ namespace Volo.Abp.IdentityServer IIdentityResourceRepository identityResourceRepository, IIdentityClaimTypeRepository identityClaimTypeRepository, AbpIdentityServerTestData testData, - IPersistentGrantRepository persistentGrantRepository) + IPersistentGrantRepository persistentGrantRepository, + IDeviceFlowCodesRepository deviceFlowCodesRepository, + IClock clock) { _testData = testData; _guidGenerator = guidGenerator; @@ -37,10 +43,13 @@ namespace Volo.Abp.IdentityServer _identityResourceRepository = identityResourceRepository; _identityClaimTypeRepository = identityClaimTypeRepository; _persistentGrantRepository = persistentGrantRepository; + _clock = clock; + _deviceFlowCodesRepository = deviceFlowCodesRepository; } public async Task BuildAsync() { + await AddDeviceFlowCodes().ConfigureAwait(false); await AddPersistedGrants().ConfigureAwait(false); await AddIdentityResources().ConfigureAwait(false); await AddApiResources().ConfigureAwait(false); @@ -48,6 +57,34 @@ namespace Volo.Abp.IdentityServer await AddClaimTypes().ConfigureAwait(false); } + private async Task AddDeviceFlowCodes() + { + await _deviceFlowCodesRepository.InsertAsync( + new DeviceFlowCodes(_guidGenerator.Create()) + { + ClientId = "c1", + DeviceCode = "DeviceCode1", + Expiration = _clock.Now.AddDays(1), + Data = "", + UserCode = "DeviceFlowCodesUserCode1", + SubjectId = "DeviceFlowCodesSubjectId1" + } + ); + + await _deviceFlowCodesRepository.InsertAsync( + new DeviceFlowCodes(_guidGenerator.Create()) + { + ClientId = "c1", + DeviceCode = "DeviceCode2", + Expiration = _clock.Now.AddDays(-1), + Data = "", + UserCode = "DeviceFlowCodesUserCode2", + SubjectId = "DeviceFlowCodesSubjectId2" + } + ); + + } + private async Task AddPersistedGrants() { await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) @@ -74,7 +111,18 @@ namespace Volo.Abp.IdentityServer SubjectId = "PersistedGrantSubjectId3", ClientId = "c1", Type = "c1type", - Data = "" + Data = "", + Expiration = _clock.Now.AddDays(1), + }).ConfigureAwait(false); + + await _persistentGrantRepository.InsertAsync(new PersistedGrant(_guidGenerator.Create()) + { + Key = "PersistedGrantKey_Expired1", + SubjectId = "PersistedGrantSubjectId_Expired1", + ClientId = "c1", + Type = "c1type", + Data = "", + Expiration = _clock.Now.AddDays(-1) }).ConfigureAwait(false); }