From 000b619c56e099454ab24232e99f3c97e2b268cc Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 24 Jan 2019 14:32:03 +0300 Subject: [PATCH] Resolved #757: IdentityServer module should work with inmemory stores if data access layer is not added --- .../AbpIdentityServerBuilderExtensions.cs | 19 +++++---- ....cs => AbpIdentityServerBuilderOptions.cs} | 11 ++++- .../AbpIdentityServerDomainModule.cs | 41 ++++++++++++++----- .../AspNetIdentity/AbpProfileService.cs | 1 + .../Grants/PersistedGrantStore.cs | 3 +- .../IdentityServerBuilderExtensions.cs | 19 +++++++++ ...IdentityServerEntityFrameworkCoreModule.cs | 16 +++++++- .../MongoDB/AbpIdentityServerMongoDbModule.cs | 10 +++++ .../AbpIdentityServerDomainTestModule.cs | 7 +--- .../Volo.Abp.IdentityServer.TestBase.csproj | 1 + .../AbpIdentityServerTestBaseModule.cs | 4 -- 11 files changed, 100 insertions(+), 32 deletions(-) rename modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/{AbpIdentityServerOptions.cs => AbpIdentityServerBuilderOptions.cs} (65%) create mode 100644 modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderExtensions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderExtensions.cs index 585b022b34..6719192fdd 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderExtensions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderExtensions.cs @@ -13,16 +13,21 @@ namespace Volo.Abp.IdentityServer public static class AbpIdentityServerBuilderExtensions { public static IIdentityServerBuilder AddAbpIdentityServer( - this IIdentityServerBuilder builder, - Action optionsAction = null) + this IIdentityServerBuilder builder, + AbpIdentityServerBuilderOptions options = null) { - var options = new AbpIdentityServerOptions(); - optionsAction?.Invoke(options); + if (options == null) + { + options = new AbpIdentityServerBuilderOptions(); + } //TODO: AspNet Identity integration lines. Can be extracted to a extension method - builder.AddAspNetIdentity(); - builder.AddProfileService(); - builder.AddResourceOwnerValidator(); + if (options.IntegrateToAspNetIdentity) + { + builder.AddAspNetIdentity(); + builder.AddProfileService(); + builder.AddResourceOwnerValidator(); + } builder.Services.Replace(ServiceDescriptor.Transient()); diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerOptions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs similarity index 65% rename from modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerOptions.cs rename to modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs index 2b66f7a0fb..1b7eb8f4d3 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerOptions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs @@ -1,6 +1,9 @@ +using System.IdentityModel.Tokens.Jwt; +using Volo.Abp.Security.Claims; + namespace Volo.Abp.IdentityServer { - public class AbpIdentityServerOptions + public class AbpIdentityServerBuilderOptions { /// /// Updates to be compatible with identity server claims. @@ -13,5 +16,11 @@ namespace Volo.Abp.IdentityServer /// Default: true. /// public bool UpdateAbpClaimTypes { get; set; } = true; + + /// + /// Integrate to AspNet Identity. + /// Default: true. + /// + public bool IntegrateToAspNetIdentity { get; set; } = true; } } \ No newline at end of file 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 e47f16a8b3..68fd77e354 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,4 +1,6 @@ -using Microsoft.Extensions.DependencyInjection; +using IdentityServer4.Services; +using IdentityServer4.Stores; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; using Volo.Abp.Domain; using Volo.Abp.Identity; @@ -8,11 +10,13 @@ using Volo.Abp.Security; namespace Volo.Abp.IdentityServer { - [DependsOn(typeof(AbpIdentityServerDomainSharedModule))] - [DependsOn(typeof(AbpDddDomainModule))] - [DependsOn(typeof(AbpAutoMapperModule))] - [DependsOn(typeof(AbpIdentityDomainModule))] - [DependsOn(typeof(AbpSecurityModule))] + [DependsOn( + typeof(AbpIdentityServerDomainSharedModule), + typeof(AbpDddDomainModule), + typeof(AbpAutoMapperModule), + typeof(AbpIdentityDomainModule), + typeof(AbpSecurityModule) + )] public class AbpIdentityServerDomainModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) @@ -24,9 +28,12 @@ namespace Volo.Abp.IdentityServer AddIdentityServer(context.Services); } - + private static void AddIdentityServer(IServiceCollection services) { + var configuration = services.GetConfiguration(); + var builderOptions = services.ExecutePreConfiguredActions(); + var identityServerBuilder = services.AddIdentityServer(options => { options.Events.RaiseErrorEvents = true; @@ -37,11 +44,25 @@ namespace Volo.Abp.IdentityServer identityServerBuilder .AddDeveloperSigningCredential() //TODO: Should be able to change this! - .AddClientStore() - .AddResourceStore() - .AddAbpIdentityServer(); + .AddAbpIdentityServer(builderOptions); services.ExecutePreConfiguredActions(identityServerBuilder); + + if (!services.IsAdded()) + { + identityServerBuilder.AddInMemoryPersistedGrants(); + } + + if (!services.IsAdded()) + { + identityServerBuilder.AddInMemoryClients(configuration.GetSection("IdentityServer:Clients")); + } + + if (!services.IsAdded()) + { + identityServerBuilder.AddInMemoryApiResources(configuration.GetSection("IdentityServer:ApiResources")); + identityServerBuilder.AddInMemoryIdentityResources(configuration.GetSection("IdentityServer:IdentityResources")); + } } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpProfileService.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpProfileService.cs index 7b411221df..63cbad5a87 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpProfileService.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpProfileService.cs @@ -12,6 +12,7 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity public class AbpProfileService : ProfileService { private readonly ICurrentTenant _currentTenant; + public AbpProfileService( IdentityUserManager userManager, IUserClaimsPrincipalFactory claimsFactory, diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs index 0d40241ee5..eb14ebc2ba 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs @@ -2,13 +2,12 @@ using System.Linq; using System.Threading.Tasks; using IdentityServer4.Stores; -using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; using Volo.Abp.ObjectMapping; namespace Volo.Abp.IdentityServer.Grants { - public class PersistedGrantStore : IPersistedGrantStore, ITransientDependency + public class PersistedGrantStore : IPersistedGrantStore { private readonly IPersistentGrantRepository _persistentGrantRepository; private readonly IObjectMapper _objectMapper; 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 new file mode 100644 index 0000000000..04ef77e652 --- /dev/null +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityServerBuilderExtensions.cs @@ -0,0 +1,19 @@ +using IdentityServer4.Stores; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Grants; + +namespace Volo.Abp.IdentityServer +{ + public static class IdentityServerBuilderExtensions + { + public static IIdentityServerBuilder AddAbpStores(this IIdentityServerBuilder builder) + { + builder.Services.AddTransient(); + + return builder + .AddClientStore() + .AddResourceStore(); + } + } +} \ No newline at end of file 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 3ef521b474..74a4ce754f 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 @@ -8,10 +8,22 @@ using Volo.Abp.Modularity; namespace Volo.Abp.IdentityServer.EntityFrameworkCore { - [DependsOn(typeof(AbpIdentityServerDomainModule))] - [DependsOn(typeof(AbpEntityFrameworkCoreModule))] + [DependsOn( + typeof(AbpIdentityServerDomainModule), + typeof(AbpEntityFrameworkCoreModule) + )] public class AbpIdentityServerEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.PreConfigure( + builder => + { + builder.AddAbpStores(); + } + ); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => 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 3e96a2f0bc..7aa5bdecb8 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 @@ -14,6 +14,16 @@ namespace Volo.Abp.IdentityServer.MongoDB )] public class AbpIdentityServerMongoDbModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.PreConfigure( + builder => + { + builder.AddAbpStores(); + } + ); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddMongoDbContext(options => diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/AbpIdentityServerDomainTestModule.cs b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/AbpIdentityServerDomainTestModule.cs index d3f50e3276..fd7736f9cf 100644 --- a/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/AbpIdentityServerDomainTestModule.cs +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.Domain.Tests/Volo/Abp/IdentityServer/AbpIdentityServerDomainTestModule.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Authorization.Permissions; -using Volo.Abp.Modularity; +using Volo.Abp.Modularity; namespace Volo.Abp.IdentityServer { diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo.Abp.IdentityServer.TestBase.csproj b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo.Abp.IdentityServer.TestBase.csproj index 27602d93ac..d0cb84ddba 100644 --- a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo.Abp.IdentityServer.TestBase.csproj +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo.Abp.IdentityServer.TestBase.csproj @@ -13,6 +13,7 @@ + diff --git a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestBaseModule.cs b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestBaseModule.cs index d823634160..b80cfedc20 100644 --- a/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestBaseModule.cs +++ b/modules/identityserver/test/Volo.Abp.IdentityServer.TestBase/Volo/Abp/IdentityServer/AbpIdentityServerTestBaseModule.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace Volo.Abp.IdentityServer { @@ -26,9 +25,6 @@ namespace Volo.Abp.IdentityServer { using (var scope = context.ServiceProvider.CreateScope()) { - //var dataSeeder = scope.ServiceProvider.GetRequiredService(); - //AsyncHelper.RunSync(() => dataSeeder.SeedAsync("1q2w3E*")); - scope.ServiceProvider .GetRequiredService() .Build();