From df7c5f61cb4a72df67f05132acf8b7ac7edebba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 2 Jan 2020 18:51:58 +0300 Subject: [PATCH 01/12] Added DeviceFlowCodes and related classes. --- .../Volo/Abp/IdentityServer/Clients/Client.cs | 2 +- .../IdentityServer/Devices/DeviceFlowCodes.cs | 31 +++++++++++++++++++ .../Devices/IDeviceFlowCodesRepository.cs | 10 ++++++ .../Devices/DeviceFlowCodesRepository.cs | 17 ++++++++++ ...IdentityServerEntityFrameworkCoreModule.cs | 2 ++ .../IIdentityServerDbContext.cs | 3 ++ .../IdentityServerDbContext.cs | 4 +++ ...yServerDbContextModelCreatingExtensions.cs | 19 ++++++++++++ .../AbpIdentityServerMongoDbContext.cs | 3 ++ ...pIdentityServerMongoDbContextExtensions.cs | 6 ++++ .../MongoDB/AbpIdentityServerMongoDbModule.cs | 2 ++ .../IAbpIdentityServerMongoDbContext.cs | 3 ++ .../MongoDB/MongoDeviceFlowCodesRepository.cs | 16 ++++++++++ 13 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowCodes.cs create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/Client.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/Client.cs index 031aa86bfc..b7a53ad66f 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/Client.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/Client.cs @@ -108,10 +108,10 @@ namespace Volo.Abp.IdentityServer.Clients } public Client(Guid id, [NotNull] string clientId) + : base(id) { Check.NotNull(clientId, nameof(clientId)); - Id = id; ClientId = clientId; //TODO: Replace magics with constants? diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowCodes.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowCodes.cs new file mode 100644 index 0000000000..8814519cd4 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowCodes.cs @@ -0,0 +1,31 @@ +using System; +using Volo.Abp.Domain.Entities.Auditing; + +namespace Volo.Abp.IdentityServer.Devices +{ + public class DeviceFlowCodes : CreationAuditedAggregateRoot + { + public virtual string DeviceCode { get; set; } + + public virtual string UserCode { get; set; } + + public virtual string SubjectId { get; set; } + + public virtual string ClientId { get; set; } + + public virtual DateTime? Expiration { get; set; } + + public virtual string Data { get; set; } + + private DeviceFlowCodes() + { + + } + + public DeviceFlowCodes(Guid id) + : base(id) + { + + } + } +} \ No newline at end of file diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs new file mode 100644 index 0000000000..7dbbcaff69 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs @@ -0,0 +1,10 @@ +using System; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.IdentityServer.Devices +{ + public interface IDeviceFlowCodesRepository : IBasicRepository + { + + } +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs new file mode 100644 index 0000000000..f8882d743d --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs @@ -0,0 +1,17 @@ +using System; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.IdentityServer.EntityFrameworkCore; + +namespace Volo.Abp.IdentityServer.Devices +{ + public class DeviceFlowCodesRepository : EfCoreRepository, + IDeviceFlowCodesRepository + { + public DeviceFlowCodesRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + + } + } +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs index 74a4ce754f..a932cd3351 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs @@ -2,6 +2,7 @@ using Volo.Abp.EntityFrameworkCore; 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.Modularity; @@ -34,6 +35,7 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore options.AddRepository(); options.AddRepository(); options.AddRepository(); + options.AddRepository(); }); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs index 3c7f425dc9..52ba7c8e6c 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs @@ -3,6 +3,7 @@ using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; 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; @@ -46,5 +47,7 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore DbSet ClientProperties { get; set; } DbSet PersistedGrants { get; set; } + + DbSet DeviceFlowCodes { get; set; } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContext.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContext.cs index 800aa8b8be..f63cd6bf1c 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContext.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContext.cs @@ -3,6 +3,7 @@ using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; 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; @@ -47,6 +48,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore public DbSet PersistedGrants { get; set; } + public DbSet DeviceFlowCodes { get; set; } + public IdentityServerDbContext(DbContextOptions options) : base(options) { @@ -56,6 +59,7 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); + builder.ConfigureIdentityServer(); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContextModelCreatingExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContextModelCreatingExtensions.cs index a4ca63afa7..ad74721eb4 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContextModelCreatingExtensions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IdentityServerDbContextModelCreatingExtensions.cs @@ -6,6 +6,7 @@ using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; 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; @@ -289,6 +290,24 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore apiScopeClaim.Property(x => x.Type).HasMaxLength(UserClaimConsts.TypeMaxLength).IsRequired(); apiScopeClaim.Property(x => x.Name).HasMaxLength(ApiScopeConsts.NameMaxLength).IsRequired(); }); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "DeviceFlowCodes", options.Schema); + + b.ConfigureByConvention(); + + b.Property(x => x.DeviceCode).HasMaxLength(200).IsRequired(); + b.Property(x => x.UserCode).HasMaxLength(200).IsRequired(); + b.Property(x => x.SubjectId).HasMaxLength(200); + b.Property(x => x.ClientId).HasMaxLength(200).IsRequired(); + b.Property(x => x.Expiration).IsRequired(); + b.Property(x => x.Data).HasMaxLength(50000).IsRequired(); + + b.HasIndex(x => new { x.UserCode }).IsUnique(); + b.HasIndex(x => x.DeviceCode).IsUnique(); + b.HasIndex(x => x.Expiration); + }); } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContext.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContext.cs index 16f99b9119..b4b5fe9b60 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContext.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContext.cs @@ -2,6 +2,7 @@ using Volo.Abp.Data; using Volo.Abp.IdentityServer.ApiResources; using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; using Volo.Abp.MongoDB; using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource; @@ -19,6 +20,8 @@ namespace Volo.Abp.IdentityServer.MongoDB public IMongoCollection PersistedGrants => Collection(); + public IMongoCollection DeviceFlowCodes => Collection(); + protected override void CreateModel(IMongoModelBuilder modelBuilder) { base.CreateModel(modelBuilder); diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContextExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContextExtensions.cs index 474188a0e1..b6a20d842a 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContextExtensions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbContextExtensions.cs @@ -1,6 +1,7 @@ using System; 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.MongoDB; @@ -39,6 +40,11 @@ namespace Volo.Abp.IdentityServer.MongoDB { b.CollectionName = options.CollectionPrefix + "PersistedGrants"; }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "DeviceFlowCodes"; + }); } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbModule.cs index 1076d19ecf..251378e087 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/AbpIdentityServerMongoDbModule.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; using Volo.Abp.Modularity; using Volo.Abp.MongoDB; @@ -32,6 +33,7 @@ namespace Volo.Abp.IdentityServer.MongoDB options.AddRepository(); options.AddRepository(); options.AddRepository(); + options.AddRepository(); }); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/IAbpIdentityServerMongoDbContext.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/IAbpIdentityServerMongoDbContext.cs index cda4718896..3bb21a8198 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/IAbpIdentityServerMongoDbContext.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/IAbpIdentityServerMongoDbContext.cs @@ -1,6 +1,7 @@ using MongoDB.Driver; using Volo.Abp.Data; using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; using Volo.Abp.IdentityServer.IdentityResources; using Volo.Abp.MongoDB; @@ -18,5 +19,7 @@ namespace Volo.Abp.IdentityServer.MongoDB IMongoCollection IdentityResources { get; } IMongoCollection PersistedGrants { get; } + + IMongoCollection DeviceFlowCodes { get; } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs new file mode 100644 index 0000000000..6589376e52 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs @@ -0,0 +1,16 @@ +using System; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.IdentityServer.Devices; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.IdentityServer.MongoDB +{ + public class MongoDeviceFlowCodesRepository : + MongoDbRepository, IDeviceFlowCodesRepository + { + public MongoDeviceFlowCodesRepository( + IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + } +} \ No newline at end of file From a022b003a7ba4f3fa3a36f0f33e20232292225bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 2 Jan 2020 19:23:28 +0300 Subject: [PATCH 02/12] Rename ClientAutoMapperProfile to IdentityServerAutoMapperProfile and move to the Volo.Abp.IdentityServer namespace --- .../Abp/IdentityServer/AbpIdentityServerDomainModule.cs | 2 +- ...MapperProfile.cs => IdentityServerAutoMapperProfile.cs} | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/{Clients/ClientAutoMapperProfile.cs => IdentityServerAutoMapperProfile.cs} (94%) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index c89e7e74d2..f6f811ad96 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -28,7 +28,7 @@ namespace Volo.Abp.IdentityServer Configure(options => { - options.AddProfile(validate: true); + options.AddProfile(validate: true); }); AddIdentityServer(context.Services); diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/ClientAutoMapperProfile.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerAutoMapperProfile.cs similarity index 94% rename from modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/ClientAutoMapperProfile.cs rename to modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerAutoMapperProfile.cs index 81e5841d6b..ba1f44953c 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Clients/ClientAutoMapperProfile.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerAutoMapperProfile.cs @@ -2,14 +2,15 @@ using System.Security.Claims; using AutoMapper; using Volo.Abp.IdentityServer.ApiResources; +using Volo.Abp.IdentityServer.Clients; using Volo.Abp.IdentityServer.Grants; using Volo.Abp.IdentityServer.IdentityResources; -namespace Volo.Abp.IdentityServer.Clients +namespace Volo.Abp.IdentityServer { - public class ClientAutoMapperProfile : Profile + public class IdentityServerAutoMapperProfile : Profile { - public ClientAutoMapperProfile() + public IdentityServerAutoMapperProfile() { //TODO: Reverse maps will not used probably. Remove those will not used From bbb689cd7b0479a3382f03c563c4dfef6f8271a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 2 Jan 2020 19:44:43 +0300 Subject: [PATCH 03/12] Add initial --- .../IdentityServer/Devices/DeviceFlowStore.cs | 98 +++++++++++++++++++ .../Devices/IDeviceFlowCodesRepository.cs | 7 +- .../Devices/DeviceFlowCodesRepository.cs | 12 +++ .../Grants/PersistedGrantRepository.cs | 3 +- .../MongoDB/MongoDeviceFlowCodesRepository.cs | 13 +++ 5 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs new file mode 100644 index 0000000000..bfc5e62f7c --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs @@ -0,0 +1,98 @@ +using System; +using System.IdentityModel.Tokens.Jwt; +using System.Threading.Tasks; +using IdentityModel; +using IdentityServer4.Models; +using IdentityServer4.Stores; +using IdentityServer4.Stores.Serialization; +using JetBrains.Annotations; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; + +namespace Volo.Abp.IdentityServer.Devices +{ + public class DeviceFlowStore : IDeviceFlowStore, ITransientDependency + { + protected IDeviceFlowCodesRepository DeviceFlowCodesRepository { get; } + protected IGuidGenerator GuidGenerator { get; } + protected IPersistentGrantSerializer PersistentGrantSerializer { get; } + + public DeviceFlowStore( + IDeviceFlowCodesRepository deviceFlowCodesRepository, + IGuidGenerator guidGenerator, + IPersistentGrantSerializer persistentGrantSerializer) + { + DeviceFlowCodesRepository = deviceFlowCodesRepository; + GuidGenerator = guidGenerator; + PersistentGrantSerializer = persistentGrantSerializer; + } + + public async Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, DeviceCode data) + { + Check.NotNull(deviceCode, nameof(deviceCode)); + Check.NotNull(userCode, nameof(userCode)); + Check.NotNull(data, nameof(data)); + + await DeviceFlowCodesRepository.InsertAsync( + new DeviceFlowCodes(GuidGenerator.Create()) + { + DeviceCode = deviceCode, + UserCode = userCode, + ClientId = data.ClientId, + SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value, + CreationTime = data.CreationTime, + Expiration = data.CreationTime.AddSeconds(data.Lifetime), + Data = Serialize(data) + } + ).ConfigureAwait(false); + } + + public async Task FindByUserCodeAsync(string userCode) + { + Check.NotNull(userCode, nameof(userCode)); + + var deviceCodes = await DeviceFlowCodesRepository.FindByUserCodeAsync(userCode).ConfigureAwait(false); + if (deviceCodes == null) + { + return null; + } + + return DeserializeToDeviceCode(deviceCodes.Data); + } + + public Task FindByDeviceCodeAsync(string deviceCode) + { + throw new NotImplementedException(); + } + + public Task UpdateByUserCodeAsync(string userCode, DeviceCode data) + { + throw new NotImplementedException(); + } + + public Task RemoveByDeviceCodeAsync(string deviceCode) + { + throw new NotImplementedException(); + } + + private string Serialize([CanBeNull] DeviceCode deviceCode) + { + if (deviceCode == null) + { + return null; + } + + return PersistentGrantSerializer.Serialize(deviceCode); + } + + protected virtual DeviceCode DeserializeToDeviceCode([CanBeNull] string data) + { + if (data == null) + { + return null; + } + + return PersistentGrantSerializer.Deserialize(data); + } + } +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs index 7dbbcaff69..f40f8f0353 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs @@ -1,10 +1,15 @@ using System; +using System.Threading; +using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; namespace Volo.Abp.IdentityServer.Devices { public interface IDeviceFlowCodesRepository : IBasicRepository { - + Task FindByUserCodeAsync( + string userCode, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs index f8882d743d..c722d3b99a 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs @@ -1,4 +1,7 @@ using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.IdentityServer.EntityFrameworkCore; @@ -13,5 +16,14 @@ namespace Volo.Abp.IdentityServer.Devices { } + + public async Task FindByUserCodeAsync( + string userCode, + CancellationToken cancellationToken = default) + { + return await DbSet + .FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs index e0af04c53e..ca404c84e5 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs @@ -12,7 +12,8 @@ namespace Volo.Abp.IdentityServer.Grants { public class PersistentGrantRepository : EfCoreRepository, IPersistentGrantRepository { - public PersistentGrantRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) + public PersistentGrantRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) { } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs index 6589376e52..8357c116f4 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs @@ -1,4 +1,7 @@ using System; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver.Linq; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.IdentityServer.Devices; using Volo.Abp.MongoDB; @@ -11,6 +14,16 @@ namespace Volo.Abp.IdentityServer.MongoDB public MongoDeviceFlowCodesRepository( IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) { + + } + + public async Task FindByUserCodeAsync( + string userCode, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); } } } \ No newline at end of file From 5ec0362dd15c0d526167ded7ce2616c6ad6b7a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 2 Jan 2020 20:28:47 +0300 Subject: [PATCH 04/12] Complete the DeviceFlowStore --- .../AbpIdentityServerDomainModule.cs | 14 ++-- .../IdentityServer/Devices/DeviceFlowStore.cs | 84 ++++++++++++++----- .../Devices/IDeviceFlowCodesRepository.cs | 5 ++ .../IdentityServerBuilderExtensions.cs | 2 + .../Devices/DeviceFlowCodesRepository.cs | 9 ++ .../MongoDB/MongoDeviceFlowCodesRepository.cs | 7 ++ 6 files changed, 97 insertions(+), 24 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index f6f811ad96..7c22dccf0e 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -1,11 +1,10 @@ -using System.Security.Cryptography.X509Certificates; -using IdentityServer4.Services; +using IdentityServer4.Services; using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.AutoMapper; using Volo.Abp.Caching; using Volo.Abp.Identity; -using Volo.Abp.IdentityServer.Clients; using Volo.Abp.Modularity; using Volo.Abp.Security; using Volo.Abp.Validation; @@ -33,7 +32,7 @@ namespace Volo.Abp.IdentityServer AddIdentityServer(context.Services); } - + private static void AddIdentityServer(IServiceCollection services) { var configuration = services.GetConfiguration(); @@ -58,7 +57,12 @@ namespace Volo.Abp.IdentityServer if (!services.IsAdded()) { - identityServerBuilder.AddInMemoryPersistedGrants(); + services.TryAddSingleton(); + } + + if (!services.IsAdded()) + { + services.TryAddSingleton(); } if (!services.IsAdded()) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs index bfc5e62f7c..771e3acbde 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs @@ -33,25 +33,29 @@ namespace Volo.Abp.IdentityServer.Devices Check.NotNull(userCode, nameof(userCode)); Check.NotNull(data, nameof(data)); - await DeviceFlowCodesRepository.InsertAsync( - new DeviceFlowCodes(GuidGenerator.Create()) - { - DeviceCode = deviceCode, - UserCode = userCode, - ClientId = data.ClientId, - SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value, - CreationTime = data.CreationTime, - Expiration = data.CreationTime.AddSeconds(data.Lifetime), - Data = Serialize(data) - } - ).ConfigureAwait(false); + await DeviceFlowCodesRepository + .InsertAsync( + new DeviceFlowCodes(GuidGenerator.Create()) + { + DeviceCode = deviceCode, + UserCode = userCode, + ClientId = data.ClientId, + SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value, + CreationTime = data.CreationTime, + Expiration = data.CreationTime.AddSeconds(data.Lifetime), + Data = Serialize(data) + } + ).ConfigureAwait(false); } public async Task FindByUserCodeAsync(string userCode) { Check.NotNull(userCode, nameof(userCode)); - var deviceCodes = await DeviceFlowCodesRepository.FindByUserCodeAsync(userCode).ConfigureAwait(false); + var deviceCodes = await DeviceFlowCodesRepository + .FindByUserCodeAsync(userCode) + .ConfigureAwait(false); + if (deviceCodes == null) { return null; @@ -60,19 +64,61 @@ namespace Volo.Abp.IdentityServer.Devices return DeserializeToDeviceCode(deviceCodes.Data); } - public Task FindByDeviceCodeAsync(string deviceCode) + public async Task FindByDeviceCodeAsync(string deviceCode) { - throw new NotImplementedException(); + Check.NotNull(deviceCode, nameof(deviceCode)); + + var deviceCodes = await DeviceFlowCodesRepository + .FindByDeviceCodeAsync(deviceCode) + .ConfigureAwait(false); + + if (deviceCodes == null) + { + return null; + } + + return DeserializeToDeviceCode(deviceCodes.Data); } - public Task UpdateByUserCodeAsync(string userCode, DeviceCode data) + public async Task UpdateByUserCodeAsync(string userCode, DeviceCode data) { - throw new NotImplementedException(); + Check.NotNull(userCode, nameof(userCode)); + Check.NotNull(data, nameof(data)); + + + var deviceCodes = await DeviceFlowCodesRepository + .FindByUserCodeAsync(userCode) + .ConfigureAwait(false); + + if (deviceCodes == null) + { + throw new InvalidOperationException($"Could not update device code by the given userCode: {userCode}"); + } + + deviceCodes.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value; + deviceCodes.Data = Serialize(data); + + await DeviceFlowCodesRepository + .UpdateAsync(deviceCodes, autoSave: true) + .ConfigureAwait(false); } - public Task RemoveByDeviceCodeAsync(string deviceCode) + public async Task RemoveByDeviceCodeAsync(string deviceCode) { - throw new NotImplementedException(); + Check.NotNull(deviceCode, nameof(deviceCode)); + + var deviceCodes = await DeviceFlowCodesRepository + .FindByDeviceCodeAsync(deviceCode) + .ConfigureAwait(false); + + if (deviceCodes == null) + { + return; + } + + await DeviceFlowCodesRepository + .DeleteAsync(deviceCodes, autoSave: true) + .ConfigureAwait(false); } private string Serialize([CanBeNull] DeviceCode deviceCode) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs index f40f8f0353..41c7e92574 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs @@ -11,5 +11,10 @@ namespace Volo.Abp.IdentityServer.Devices string userCode, CancellationToken cancellationToken = default ); + + Task FindByDeviceCodeAsync( + string deviceCode, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs index 2168b2067e..516d400db6 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs @@ -1,6 +1,7 @@ using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; namespace Volo.Abp.IdentityServer @@ -10,6 +11,7 @@ namespace Volo.Abp.IdentityServer public static IIdentityServerBuilder AddAbpStores(this IIdentityServerBuilder builder) { builder.Services.AddTransient(); + builder.Services.AddTransient(); return builder .AddClientStore() diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs index c722d3b99a..d5ac9ae815 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs @@ -25,5 +25,14 @@ namespace Volo.Abp.IdentityServer.Devices .FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken)) .ConfigureAwait(false); } + + public async Task FindByDeviceCodeAsync( + string deviceCode, + CancellationToken cancellationToken = default) + { + return await DbSet + .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs index 8357c116f4..3ad3ecf104 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs @@ -25,5 +25,12 @@ namespace Volo.Abp.IdentityServer.MongoDB .FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken)) .ConfigureAwait(false); } + + public async Task FindByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } \ No newline at end of file From d815b9cfeea830574e5aa777c6bdff8a7ff70210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 12:41:17 +0300 Subject: [PATCH 05/12] Refactor AbpTabLinkTagHelperService: Remove unnecessary async. --- .../TagHelpers/Tab/AbpTabLinkTagHelperService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Tab/AbpTabLinkTagHelperService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Tab/AbpTabLinkTagHelperService.cs index ba5e3e811a..54a29bf691 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Tab/AbpTabLinkTagHelperService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Tab/AbpTabLinkTagHelperService.cs @@ -7,7 +7,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Tab { public class AbpTabLinkTagHelperService : AbpTagHelperService { - public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { SetPlaceholderForNameIfNotProvided(); @@ -18,6 +18,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Tab tabHeaderItems.Add(new TabItem(tabHeader, "", false, TagHelper.Name, TagHelper.ParentDropdownName, false)); output.SuppressOutput(); + + return Task.CompletedTask; } protected virtual string GetTabHeaderItem(TagHelperContext context, TagHelperOutput output) From 401376e10951fc5a33d9111a5d9b1c20ee63e51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 12:44:50 +0300 Subject: [PATCH 06/12] Resolved #2540. Resolved #2539. * Send IServiceProvider to DoWork method of the periodic background worker #2539 * Introduce AsyncPeriodicBackgroundWorkerBase #2540 --- .../BackgroundJobs/BackgroundJobExecuter.cs | 5 +- .../BackgroundJobs/IBackgroundJobExecuter.cs | 6 +- .../Hangfire/HangfireJobExecutionAdapter.cs | 3 +- .../Abp/BackgroundJobs/RabbitMQ/JobQueue.cs | 3 +- .../Abp/BackgroundJobs/BackgroundJobWorker.cs | 93 +++++++++---------- .../AsyncPeriodicBackgroundWorkerBase.cs | 55 +++++++++++ .../BackgroundWorkerContext.cs | 14 +++ .../PeriodicBackgroundWorkerBase.cs | 22 +++-- 8 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AsyncPeriodicBackgroundWorkerBase.cs create mode 100644 framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerContext.cs diff --git a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs index ec0ac8ee3f..c9b5790003 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs @@ -4,7 +4,6 @@ using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; -using Volo.Abp.Threading; namespace Volo.Abp.BackgroundJobs { @@ -21,7 +20,7 @@ namespace Volo.Abp.BackgroundJobs Logger = NullLogger.Instance; } - public virtual void Execute(JobExecutionContext context) + public virtual async Task ExecuteAsync(JobExecutionContext context) { var job = context.ServiceProvider.GetService(context.JobType); if (job == null) @@ -41,7 +40,7 @@ namespace Volo.Abp.BackgroundJobs { if (jobExecuteMethod.Name == nameof(IAsyncBackgroundJob.ExecuteAsync)) { - AsyncHelper.RunSync(() => (Task) jobExecuteMethod.Invoke(job, new[] {context.JobArgs})); + await ((Task) jobExecuteMethod.Invoke(job, new[] {context.JobArgs})).ConfigureAwait(false); } else { diff --git a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJobExecuter.cs b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJobExecuter.cs index 2470b2945b..eaea1764f3 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJobExecuter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJobExecuter.cs @@ -1,7 +1,9 @@ -namespace Volo.Abp.BackgroundJobs +using System.Threading.Tasks; + +namespace Volo.Abp.BackgroundJobs { public interface IBackgroundJobExecuter { - void Execute(JobExecutionContext context); + Task ExecuteAsync(JobExecutionContext context); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs index 4a61aa2845..676a3eaafc 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.Threading; namespace Volo.Abp.BackgroundJobs.Hangfire { @@ -25,7 +26,7 @@ namespace Volo.Abp.BackgroundJobs.Hangfire { var jobType = Options.GetJob(typeof(TArgs)).JobType; var context = new JobExecutionContext(scope.ServiceProvider, jobType, args); - JobExecuter.Execute(context); + AsyncHelper.RunSync(() => JobExecuter.ExecuteAsync(context)); } } } diff --git a/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/JobQueue.cs b/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/JobQueue.cs index 7675a64ca0..fb18ca00f9 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/JobQueue.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/JobQueue.cs @@ -10,6 +10,7 @@ using Nito.AsyncEx; using RabbitMQ.Client; using RabbitMQ.Client.Events; using Volo.Abp.RabbitMQ; +using Volo.Abp.Threading; namespace Volo.Abp.BackgroundJobs.RabbitMQ { @@ -181,7 +182,7 @@ namespace Volo.Abp.BackgroundJobs.RabbitMQ try { - JobExecuter.Execute(context); + AsyncHelper.RunSync(() => JobExecuter.ExecuteAsync(context)); ChannelAccessor.Channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } catch (BackgroundJobExecutionException) diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs index 74a3f6128f..a16690f346 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -9,92 +10,88 @@ using Volo.Abp.Timing; namespace Volo.Abp.BackgroundJobs { - public class BackgroundJobWorker : PeriodicBackgroundWorkerBase, IBackgroundJobWorker + public class BackgroundJobWorker : AsyncPeriodicBackgroundWorkerBase, IBackgroundJobWorker { protected AbpBackgroundJobOptions JobOptions { get; } protected AbpBackgroundJobWorkerOptions WorkerOptions { get; } - protected IServiceScopeFactory ServiceScopeFactory { get; } - public BackgroundJobWorker( AbpTimer timer, IOptions jobOptions, IOptions workerOptions, IServiceScopeFactory serviceScopeFactory) - : base(timer) + : base( + timer, + serviceScopeFactory) { - ServiceScopeFactory = serviceScopeFactory; WorkerOptions = workerOptions.Value; JobOptions = jobOptions.Value; Timer.Period = WorkerOptions.JobPollPeriod; } - protected override void DoWork() + protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) { - using (var scope = ServiceScopeFactory.CreateScope()) - { - var store = scope.ServiceProvider.GetRequiredService(); + var store = workerContext.ServiceProvider.GetRequiredService(); - var waitingJobs = AsyncHelper.RunSync(() => store.GetWaitingJobsAsync(WorkerOptions.MaxJobFetchCount)); + var waitingJobs = await store.GetWaitingJobsAsync(WorkerOptions.MaxJobFetchCount).ConfigureAwait(false); - if (!waitingJobs.Any()) - { - return; - } + if (!waitingJobs.Any()) + { + return; + } + + var jobExecuter = workerContext.ServiceProvider.GetRequiredService(); + var clock = workerContext.ServiceProvider.GetRequiredService(); + var serializer = workerContext.ServiceProvider.GetRequiredService(); - var jobExecuter = scope.ServiceProvider.GetRequiredService(); - var clock = scope.ServiceProvider.GetRequiredService(); - var serializer = scope.ServiceProvider.GetRequiredService(); + foreach (var jobInfo in waitingJobs) + { + jobInfo.TryCount++; + jobInfo.LastTryTime = clock.Now; - foreach (var jobInfo in waitingJobs) + try { - jobInfo.TryCount++; - jobInfo.LastTryTime = clock.Now; + var jobConfiguration = JobOptions.GetJob(jobInfo.JobName); + var jobArgs = serializer.Deserialize(jobInfo.JobArgs, jobConfiguration.ArgsType); + var context = new JobExecutionContext(workerContext.ServiceProvider, jobConfiguration.JobType, jobArgs); try { - var jobConfiguration = JobOptions.GetJob(jobInfo.JobName); - var jobArgs = serializer.Deserialize(jobInfo.JobArgs, jobConfiguration.ArgsType); - var context = new JobExecutionContext(scope.ServiceProvider, jobConfiguration.JobType, jobArgs); + await jobExecuter.ExecuteAsync(context).ConfigureAwait(false); - try - { - jobExecuter.Execute(context); + await store.DeleteAsync(jobInfo.Id).ConfigureAwait(false); + } + catch (BackgroundJobExecutionException) + { + var nextTryTime = CalculateNextTryTime(jobInfo, clock); - AsyncHelper.RunSync(() => store.DeleteAsync(jobInfo.Id)); + if (nextTryTime.HasValue) + { + jobInfo.NextTryTime = nextTryTime.Value; } - catch (BackgroundJobExecutionException) + else { - var nextTryTime = CalculateNextTryTime(jobInfo, clock); - - if (nextTryTime.HasValue) - { - jobInfo.NextTryTime = nextTryTime.Value; - } - else - { - jobInfo.IsAbandoned = true; - } - - TryUpdate(store, jobInfo); + jobInfo.IsAbandoned = true; } + + await TryUpdateAsync(store, jobInfo).ConfigureAwait(false); } - catch (Exception ex) - { - Logger.LogException(ex); - jobInfo.IsAbandoned = true; - TryUpdate(store, jobInfo); - } + } + catch (Exception ex) + { + Logger.LogException(ex); + jobInfo.IsAbandoned = true; + await TryUpdateAsync(store, jobInfo).ConfigureAwait(false); } } } - protected virtual void TryUpdate(IBackgroundJobStore store, BackgroundJobInfo jobInfo) + protected virtual async Task TryUpdateAsync(IBackgroundJobStore store, BackgroundJobInfo jobInfo) { try { - AsyncHelper.RunSync(() => store.UpdateAsync(jobInfo)); + await store.UpdateAsync(jobInfo).ConfigureAwait(false); } catch (Exception updateEx) { diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AsyncPeriodicBackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AsyncPeriodicBackgroundWorkerBase.cs new file mode 100644 index 0000000000..cd8474e654 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AsyncPeriodicBackgroundWorkerBase.cs @@ -0,0 +1,55 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Volo.Abp.Threading; + +namespace Volo.Abp.BackgroundWorkers +{ + public abstract class AsyncPeriodicBackgroundWorkerBase : BackgroundWorkerBase + { + protected IServiceScopeFactory ServiceScopeFactory { get; } + protected AbpTimer Timer { get; } + + protected AsyncPeriodicBackgroundWorkerBase( + AbpTimer timer, + IServiceScopeFactory serviceScopeFactory) + { + ServiceScopeFactory = serviceScopeFactory; + Timer = timer; + Timer.Elapsed += Timer_Elapsed; + } + + public override async Task StartAsync(CancellationToken cancellationToken = default) + { + await base.StartAsync(cancellationToken).ConfigureAwait(false); + Timer.Start(cancellationToken); + } + + public override async Task StopAsync(CancellationToken cancellationToken = default) + { + Timer.Stop(cancellationToken); + await base.StopAsync(cancellationToken).ConfigureAwait(false); + } + + private void Timer_Elapsed(object sender, System.EventArgs e) + { + try + { + using (var scope = ServiceScopeFactory.CreateScope()) + { + AsyncHelper.RunSync( + () => DoWorkAsync(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)) + ); + } + } + catch (Exception ex) + { + Logger.LogException(ex); + } + } + + protected abstract Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerContext.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerContext.cs new file mode 100644 index 0000000000..dc5635ae88 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerContext.cs @@ -0,0 +1,14 @@ +using System; + +namespace Volo.Abp.BackgroundWorkers +{ + public class PeriodicBackgroundWorkerContext + { + public IServiceProvider ServiceProvider { get; } + + public PeriodicBackgroundWorkerContext(IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/PeriodicBackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/PeriodicBackgroundWorkerBase.cs index e2b0f5b53a..863657ab79 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/PeriodicBackgroundWorkerBase.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/PeriodicBackgroundWorkerBase.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Volo.Abp.Threading; @@ -11,14 +12,14 @@ namespace Volo.Abp.BackgroundWorkers /// public abstract class PeriodicBackgroundWorkerBase : BackgroundWorkerBase { - protected readonly AbpTimer Timer; + protected IServiceScopeFactory ServiceScopeFactory { get; } + protected AbpTimer Timer { get; } - /// - /// Initializes a new instance of the class. - /// - /// A timer. - protected PeriodicBackgroundWorkerBase(AbpTimer timer) + protected PeriodicBackgroundWorkerBase( + AbpTimer timer, + IServiceScopeFactory serviceScopeFactory) { + ServiceScopeFactory = serviceScopeFactory; Timer = timer; Timer.Elapsed += Timer_Elapsed; } @@ -34,12 +35,15 @@ namespace Volo.Abp.BackgroundWorkers Timer.Stop(cancellationToken); await base.StopAsync(cancellationToken).ConfigureAwait(false); } - + private void Timer_Elapsed(object sender, System.EventArgs e) { try { - DoWork(); + using (var scope = ServiceScopeFactory.CreateScope()) + { + DoWork(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)); + } } catch (Exception ex) { @@ -50,6 +54,6 @@ namespace Volo.Abp.BackgroundWorkers /// /// Periodic works should be done by implementing this method. /// - protected abstract void DoWork(); + protected abstract void DoWork(PeriodicBackgroundWorkerContext workerContext); } } \ No newline at end of file From 4ba43061651219e0228b25f7951ec2b3c7102d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 12:45:24 +0300 Subject: [PATCH 07/12] Remove unnecessary async usages. --- .../Volo/Abp/Cli/Commands/HelpCommand.cs | 6 ++++-- .../ProjectNugetPackageAdder.cs | 4 +++- framework/test/SimpleConsoleDemo/Program.cs | 3 +-- .../BackgroundJobExecuter_Tests.cs | 4 ++-- .../Abp/Caching/DistributedCache_Tests.cs | 20 +++++++++---------- .../Volo/Abp/Uow/UnitOfWork_Events_Tests.cs | 7 ++++++- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs index 2da6b82c8a..060bd91cc7 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs @@ -24,12 +24,12 @@ namespace Volo.Abp.Cli.Commands AbpCliOptions = cliOptions.Value; } - public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + public Task ExecuteAsync(CommandLineArgs commandLineArgs) { if (string.IsNullOrWhiteSpace(commandLineArgs.Target)) { Logger.LogInformation(GetUsageInfo()); - return; + return Task.CompletedTask; } var commandType = AbpCliOptions.Commands[commandLineArgs.Target]; @@ -39,6 +39,8 @@ namespace Volo.Abp.Cli.Commands var command = (IConsoleCommand) scope.ServiceProvider.GetRequiredService(commandType); Logger.LogInformation(command.GetUsageInfo()); } + + return Task.CompletedTask; } public string GetUsageInfo() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs index 4bfe3ef4b8..76b7584ae8 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs @@ -48,7 +48,7 @@ namespace Volo.Abp.Cli.ProjectModification .ConfigureAwait(false)).ConfigureAwait(false); } - public async Task AddAsync(string projectFile, NugetPackageInfo package) + public Task AddAsync(string projectFile, NugetPackageInfo package) { using (DirectoryHelper.ChangeCurrentDirectory(Path.GetDirectoryName(projectFile))) { @@ -71,6 +71,8 @@ namespace Volo.Abp.Cli.ProjectModification Logger.LogInformation("Successfully installed."); } + + return Task.CompletedTask; } protected virtual async Task FindNugetPackageInfoAsync(string moduleName) diff --git a/framework/test/SimpleConsoleDemo/Program.cs b/framework/test/SimpleConsoleDemo/Program.cs index 82e6d0a660..3ac0c8b23b 100644 --- a/framework/test/SimpleConsoleDemo/Program.cs +++ b/framework/test/SimpleConsoleDemo/Program.cs @@ -10,7 +10,7 @@ namespace SimpleConsoleDemo { class Program { - static async Task Main(string[] args) + static void Main(string[] args) { using (var application = AbpApplicationFactory.Create(options => { @@ -42,7 +42,6 @@ namespace SimpleConsoleDemo writer.Write(); } - Console.WriteLine(); Console.WriteLine("Press ENTER to exit!"); Console.ReadLine(); diff --git a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs index 81c76f2559..50d90d0196 100644 --- a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs +++ b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs @@ -23,7 +23,7 @@ namespace Volo.Abp.BackgroundJobs //Act - _backgroundJobExecuter.Execute( + await _backgroundJobExecuter.ExecuteAsync( new JobExecutionContext( ServiceProvider, typeof(MyJob), @@ -46,7 +46,7 @@ namespace Volo.Abp.BackgroundJobs //Act - _backgroundJobExecuter.Execute( + await _backgroundJobExecuter.ExecuteAsync( new JobExecutionContext( ServiceProvider, typeof(MyAsyncJob), diff --git a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs index 3db7fa9aaf..50a4a76763 100644 --- a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs +++ b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs @@ -38,7 +38,7 @@ namespace Volo.Abp.Caching } [Fact] - public async Task GetOrAddAsync() + public void GetOrAdd() { var personCache = GetRequiredService>(); @@ -49,12 +49,12 @@ namespace Volo.Abp.Caching bool factoryExecuted = false; - var cacheItem = await personCache.GetOrAddAsync(cacheKey, - async () => + var cacheItem = personCache.GetOrAdd(cacheKey, + () => { factoryExecuted = true; return new PersonCacheItem(personName); - }).ConfigureAwait(false); + }); factoryExecuted.ShouldBeTrue(); cacheItem.Name.ShouldBe(personName); @@ -63,12 +63,12 @@ namespace Volo.Abp.Caching factoryExecuted = false; - cacheItem = await personCache.GetOrAddAsync(cacheKey, - async () => + cacheItem = personCache.GetOrAdd(cacheKey, + () => { factoryExecuted = true; return new PersonCacheItem(personName); - }).ConfigureAwait(false); + }); factoryExecuted.ShouldBeFalse(); cacheItem.Name.ShouldBe(personName); @@ -165,12 +165,12 @@ namespace Volo.Abp.Caching bool factoryExecuted = false; - var cacheItem = await personCache.GetOrAddAsync(cacheKey, - async () => + var cacheItem = personCache.GetOrAdd(cacheKey, + () => { factoryExecuted = true; return new PersonCacheItem(personName); - }).ConfigureAwait(false); + }); factoryExecuted.ShouldBeTrue(); cacheItem.Name.ShouldBe(personName); diff --git a/framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs b/framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs index 9ee2cae2f3..7d9a176076 100644 --- a/framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs +++ b/framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs @@ -24,7 +24,12 @@ namespace Volo.Abp.Uow using (var uow = _unitOfWorkManager.Begin()) { - uow.OnCompleted(async () => completed = true); + uow.OnCompleted(() => + { + completed = true; + return Task.CompletedTask; + }); + uow.Disposed += (sender, args) => disposed = true; await uow.CompleteAsync().ConfigureAwait(false); From c1f02ba06621d381a86b3ff2a36036b7a7dfcd8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 14:55:04 +0300 Subject: [PATCH 08/12] Implemented TokenCleanupService --- .../Volo.Abp.IdentityServer.Domain.csproj | 1 + .../IdentityServer/Devices/DeviceFlowStore.cs | 1 - .../Devices/IDeviceFlowCodesRepository.cs | 7 ++ .../Grants/IPersistentGrantRepository.cs | 6 ++ .../Tokens/TokenCleanupBackgroundWorker.cs | 34 +++++++ .../Tokens/TokenCleanupOptions.cs | 34 +++++++ .../Tokens/TokenCleanupService.cs | 88 +++++++++++++++++++ .../Devices/DeviceFlowCodesRepository.cs | 13 +++ .../Grants/PersistedGrantRepository.cs | 27 ++++-- .../MongoDB/MongoDeviceFlowCodesRepository.cs | 15 ++++ .../MongoDB/MongoPersistedGrantRepository.cs | 14 ++- 11 files changed, 232 insertions(+), 8 deletions(-) create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupBackgroundWorker.cs create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo.Abp.IdentityServer.Domain.csproj b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo.Abp.IdentityServer.Domain.csproj index 70d0fbcba7..e7f4a6bc43 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo.Abp.IdentityServer.Domain.csproj +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo.Abp.IdentityServer.Domain.csproj @@ -20,6 +20,7 @@ + diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs index 771e3acbde..4bd55ab2c6 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/DeviceFlowStore.cs @@ -1,5 +1,4 @@ using System; -using System.IdentityModel.Tokens.Jwt; using System.Threading.Tasks; using IdentityModel; using IdentityServer4.Models; diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs index 41c7e92574..b6bcb69bed 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Devices/IDeviceFlowCodesRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -16,5 +17,11 @@ namespace Volo.Abp.IdentityServer.Devices string deviceCode, CancellationToken cancellationToken = default ); + + Task> GetListByExpirationAsync( + DateTime maxExpirationDate, + int maxResultCount, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs index 6e316806b6..65cfabd7e7 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs @@ -18,6 +18,12 @@ namespace Volo.Abp.IdentityServer.Grants CancellationToken cancellationToken = default ); + Task> GetListByExpirationAsync( + DateTime maxExpirationDate, + int maxResultCount, + CancellationToken cancellationToken = default + ); + Task DeleteAsync( string subjectId, string clientId, diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupBackgroundWorker.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupBackgroundWorker.cs new file mode 100644 index 0000000000..4bec8318eb --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupBackgroundWorker.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.Threading; + +namespace Volo.Abp.IdentityServer.Tokens +{ + public class TokenCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase + { + protected TokenCleanupOptions Options { get; } + + public TokenCleanupBackgroundWorker( + AbpTimer timer, + IServiceScopeFactory serviceScopeFactory, + IOptions options) + : base( + timer, + serviceScopeFactory) + { + Options = options.Value; + timer.Period = Options.CleanupPeriod; + } + + protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) + { + await workerContext + .ServiceProvider + .GetRequiredService() + .CleanAsync() + .ConfigureAwait(false); + } + } +} diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs new file mode 100644 index 0000000000..3252008506 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs @@ -0,0 +1,34 @@ +using Volo.Abp.BackgroundWorkers; + +namespace Volo.Abp.IdentityServer.Tokens +{ + public class TokenCleanupOptions + { + /// + /// Default: 3,600,000 ms. + /// + public int CleanupPeriod { get; set; } = 3_600_000; + + /// + /// Default value: 100. + /// + public int CleanupBatchSize { get; set; } = 100; + + /// + /// The number of loop if there are + /// more than tokens in the database. + /// So, if is 10 and is 100, + /// then the cleanup worker will clean 1,000 items in one at max. + /// + /// Default value: 10. + /// + public int CleanupLoopCount { get; set; } = 10; + + /// + /// Default value: true. + /// If is false, + /// this property is ignored and the cleanup worker doesn't work for this application instance. + /// + public bool EnableCleanup { get; set; } = true; + } +} \ No newline at end of file diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs new file mode 100644 index 0000000000..ac38f792c1 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs @@ -0,0 +1,88 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; +using Volo.Abp.IdentityServer.Devices; +using Volo.Abp.IdentityServer.Grants; +using Volo.Abp.Timing; +using Volo.Abp.Uow; + +namespace Volo.Abp.IdentityServer.Tokens +{ + public class TokenCleanupService : ITransientDependency + { + protected IPersistentGrantRepository PersistentGrantRepository { get; } + protected IDeviceFlowCodesRepository DeviceFlowCodesRepository { get; } + protected IClock Clock { get; } + protected TokenCleanupOptions Options { get; } + + public TokenCleanupService( + IPersistentGrantRepository persistentGrantRepository, + IDeviceFlowCodesRepository deviceFlowCodesRepository, + IClock clock, + IOptions options) + { + PersistentGrantRepository = persistentGrantRepository; + DeviceFlowCodesRepository = deviceFlowCodesRepository; + Clock = clock; + Options = options.Value; + } + + public virtual async Task CleanAsync() + { + await RemoveGrantsAsync() + .ConfigureAwait(false); + + await RemoveDeviceCodesAsync() + .ConfigureAwait(false); + } + + [UnitOfWork] + protected virtual async Task RemoveGrantsAsync() + { + for (int i = 0; i < Options.CleanupLoopCount; i++) + { + var persistentGrants = await PersistentGrantRepository + .GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize) + .ConfigureAwait(false); + + //TODO: Can be optimized if the repository implements the batch deletion + foreach (var persistentGrant in persistentGrants) + { + await PersistentGrantRepository + .DeleteAsync(persistentGrant) + .ConfigureAwait(false); + } + + //No need to continue to query if it gets more than max items. + if (persistentGrants.Count < Options.CleanupBatchSize) + { + break; + } + } + } + + protected virtual async Task RemoveDeviceCodesAsync() + { + for (int i = 0; i < Options.CleanupLoopCount; i++) + { + var deviceFlowCodeses = await DeviceFlowCodesRepository + .GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize) + .ConfigureAwait(false); + + //TODO: Can be optimized if the repository implements the batch deletion + foreach (var deviceFlowCodes in deviceFlowCodeses) + { + await DeviceFlowCodesRepository + .DeleteAsync(deviceFlowCodes) + .ConfigureAwait(false); + } + + //No need to continue to query if it gets more than max items. + if (deviceFlowCodeses.Count < Options.CleanupBatchSize) + { + break; + } + } + } + } +} \ No newline at end of file diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs index d5ac9ae815..a8d2bce3ce 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Devices/DeviceFlowCodesRepository.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -34,5 +36,16 @@ namespace Volo.Abp.IdentityServer.Devices .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) .ConfigureAwait(false); } + + public async Task> GetListByExpirationAsync(DateTime maxExpirationDate, int maxResultCount, + CancellationToken cancellationToken = default) + { + return await DbSet + .Where(x => x.Expiration != null && x.Expiration < maxExpirationDate) + .OrderBy(x => x.ClientId) + .Take(maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs index ca404c84e5..cf208c7875 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/Grants/PersistedGrantRepository.cs @@ -18,21 +18,36 @@ namespace Volo.Abp.IdentityServer.Grants } - public Task FindByKeyAsync( + public async Task FindByKeyAsync( string key, CancellationToken cancellationToken = default) { - return DbSet - .FirstOrDefaultAsync(x => x.Key == key, GetCancellationToken(cancellationToken)); + return await DbSet + .FirstOrDefaultAsync(x => x.Key == key, GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); } - public Task> GetListBySubjectIdAsync( + public async Task> GetListBySubjectIdAsync( string subjectId, CancellationToken cancellationToken = default) { - return DbSet + return await DbSet .Where(x => x.SubjectId == subjectId) - .ToListAsync(GetCancellationToken(cancellationToken)); + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } + + public async Task> GetListByExpirationAsync( + DateTime maxExpirationDate, + int maxResultCount, + CancellationToken cancellationToken = default) + { + return await DbSet + .Where(x => x.Expiration != null && x.Expiration < maxExpirationDate) + .OrderBy(x => x.ClientId) + .Take(maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); } public async Task DeleteAsync( diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs index 3ad3ecf104..86daac80a9 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoDeviceFlowCodesRepository.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; using MongoDB.Driver.Linq; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.IdentityServer.Devices; @@ -32,5 +34,18 @@ namespace Volo.Abp.IdentityServer.MongoDB .FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken)) .ConfigureAwait(false); } + + public async Task> GetListByExpirationAsync( + DateTime maxExpirationDate, + int maxResultCount, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .Where(x => x.Expiration != null && x.Expiration < maxExpirationDate) + .OrderBy(x => x.ClientId) + .Take(maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } } } \ No newline at end of file diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoPersistedGrantRepository.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoPersistedGrantRepository.cs index 3ee17df174..4b414482db 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoPersistedGrantRepository.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.MongoDB/Volo/Abp/IdentityServer/MongoDB/MongoPersistedGrantRepository.cs @@ -27,7 +27,19 @@ namespace Volo.Abp.IdentityServer.MongoDB { return await GetMongoQueryable() .Where(x => x.SubjectId == subjectId) - .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); + } + + public async Task> GetListByExpirationAsync(DateTime maxExpirationDate, int maxResultCount, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .Where(x => x.Expiration != null && x.Expiration < maxExpirationDate) + .OrderBy(x => x.ClientId) + .Take(maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)) + .ConfigureAwait(false); } public async Task DeleteAsync(string subjectId, string clientId, CancellationToken cancellationToken = default) From aea7eed317250fcd584c7a4972f9f369e80e5c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 15:13:05 +0300 Subject: [PATCH 09/12] Added test for the TokenCleanupService --- .../Tokens/TokenCleanupService_Tests.cs | 37 +++++++++++++ .../AbpIdentityServerTestDataBuilder.cs | 52 ++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Tokens/TokenCleanupService_Tests.cs 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); } From 70a2b13d43dcb88ab7cb299fe9b7c38cdd7b4d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 15:47:03 +0300 Subject: [PATCH 10/12] Added tests for the DeviceFlowStore. --- .../Devices/DeviceFlowStore_Tests.cs | 95 +++++++++++++++++++ .../AbpIdentityServerTestDataBuilder.cs | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Devices/DeviceFlowStore_Tests.cs diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Devices/DeviceFlowStore_Tests.cs b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Devices/DeviceFlowStore_Tests.cs new file mode 100644 index 0000000000..7d4c630de7 --- /dev/null +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/Devices/DeviceFlowStore_Tests.cs @@ -0,0 +1,95 @@ +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using IdentityModel; +using IdentityServer4.Models; +using IdentityServer4.Stores; +using Shouldly; +using Xunit; + +namespace Volo.Abp.IdentityServer.Devices +{ + public class DeviceFlowStore_Tests : AbpIdentityServerTestBase + { + private readonly IDeviceFlowStore _deviceFlowStore; + + public DeviceFlowStore_Tests() + { + _deviceFlowStore = GetRequiredService(); + } + + [Fact] + public async Task StoreDeviceAuthorizationAsync() + { + await _deviceFlowStore.StoreDeviceAuthorizationAsync( + "DeviceCode-Test1", + "UserCode-Test1", + new DeviceCode + { + ClientId = "ClientId1", + AuthorizedScopes = new string[] { "s1", "s2" }, + IsAuthorized = true, + Lifetime = 42, + RequestedScopes = new string[] { "rs1" }, + Subject = new ClaimsPrincipal( + new[] + { + new ClaimsIdentity(new[] + { + new Claim(JwtClaimTypes.Subject, "sid1") + }) + } + ) + } + ); + + void Check(DeviceCode deviceCode) + { + deviceCode.ClientId.ShouldBe("ClientId1"); + deviceCode.AuthorizedScopes.ShouldNotBeNull(); + deviceCode.AuthorizedScopes.Count().ShouldBe(2); + deviceCode.AuthorizedScopes.ShouldContain("s1"); + deviceCode.AuthorizedScopes.ShouldContain("s2"); + deviceCode.IsAuthorized.ShouldBeTrue(); + deviceCode.Lifetime.ShouldBe(42); + deviceCode.RequestedScopes.ShouldNotBeNull(); + deviceCode.RequestedScopes.Count().ShouldBe(1); + deviceCode.RequestedScopes.ShouldContain("rs1"); + deviceCode.Subject.ShouldNotBeNull(); + deviceCode.Subject.Claims.ShouldContain(x => x.Type == JwtClaimTypes.Subject && x.Value == "sid1"); + } + + Check(await _deviceFlowStore.FindByUserCodeAsync("UserCode-Test1")); + Check(await _deviceFlowStore.FindByDeviceCodeAsync("DeviceCode-Test1")); + } + + [Fact] + public async Task RemoveByDeviceCodeAsync() + { + (await _deviceFlowStore.FindByDeviceCodeAsync("DeviceCode1")).ShouldNotBeNull(); + + await _deviceFlowStore.RemoveByDeviceCodeAsync("DeviceCode1"); + + (await _deviceFlowStore.FindByDeviceCodeAsync("DeviceCode1")).ShouldBeNull(); + } + + [Fact] + public async Task UpdateByDeviceCodeAsync() + { + var deviceCode = await _deviceFlowStore.FindByDeviceCodeAsync("DeviceCode1"); + deviceCode.ShouldNotBeNull(); + deviceCode.Lifetime.ShouldBe(42); + + await _deviceFlowStore.UpdateByUserCodeAsync( + "DeviceFlowCodesUserCode1", + new DeviceCode + { + Lifetime = 43 + } + ); + + deviceCode = await _deviceFlowStore.FindByDeviceCodeAsync("DeviceCode1"); + deviceCode.Lifetime.ShouldBe(43); + } + } +} 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 849dd874d5..ee6c8c59b6 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 @@ -65,7 +65,7 @@ namespace Volo.Abp.IdentityServer ClientId = "c1", DeviceCode = "DeviceCode1", Expiration = _clock.Now.AddDays(1), - Data = "", + Data = "{\"Lifetime\":\"42\"}", UserCode = "DeviceFlowCodesUserCode1", SubjectId = "DeviceFlowCodesSubjectId1" } From cc9ea5983cae04be8044b00f3dbd344bb61c49e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 15:59:19 +0300 Subject: [PATCH 11/12] Add TokenCleanupBackgroundWorker to the BackgroundWorkerManager. --- .../AbpIdentityServerDomainModule.cs | 17 +++++++++++++++++ .../Tokens/TokenCleanupOptions.cs | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index 7c22dccf0e..fa1a65f556 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -2,9 +2,12 @@ using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; using Volo.Abp.AutoMapper; +using Volo.Abp.BackgroundWorkers; using Volo.Abp.Caching; using Volo.Abp.Identity; +using Volo.Abp.IdentityServer.Tokens; using Volo.Abp.Modularity; using Volo.Abp.Security; using Volo.Abp.Validation; @@ -76,5 +79,19 @@ namespace Volo.Abp.IdentityServer identityServerBuilder.AddInMemoryIdentityResources(configuration.GetSection("IdentityServer:IdentityResources")); } } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + if (options.IsCleanupEnabled) + { + context.ServiceProvider + .GetRequiredService() + .Add( + context.ServiceProvider + .GetRequiredService() + ); + } + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs index 3252008506..816ef6af15 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupOptions.cs @@ -29,6 +29,6 @@ namespace Volo.Abp.IdentityServer.Tokens /// If is false, /// this property is ignored and the cleanup worker doesn't work for this application instance. /// - public bool EnableCleanup { get; set; } = true; + public bool IsCleanupEnabled { get; set; } = true; } } \ No newline at end of file From 42f80ecdbbb6fe6248f56eb3fda8e434f3a0b97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 3 Jan 2020 16:00:34 +0300 Subject: [PATCH 12/12] Add missing AbpBackgroundWorkersModule dependency. --- .../Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index fa1a65f556..0ff798e7fd 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -20,7 +20,8 @@ namespace Volo.Abp.IdentityServer typeof(AbpIdentityDomainModule), typeof(AbpSecurityModule), typeof(AbpCachingModule), - typeof(AbpValidationModule) + typeof(AbpValidationModule), + typeof(AbpBackgroundWorkersModule) )] public class AbpIdentityServerDomainModule : AbpModule {