From 09177fcaa090bd4b441ce6a3322bf593abe6dd14 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 28 Jul 2021 15:16:25 +0800 Subject: [PATCH] Add tenant info to IdentityClients. --- framework/Volo.Abp.sln | 7 ++++ .../IdentityModel/AbpIdentityClientOptions.cs | 34 ++++++++++++++- .../IdentityModelAuthenticationService.cs | 14 +------ .../Volo.Abp.IdentityModel.Tests.csproj | 24 +++++++++++ .../AbpIdentityClientOptions_Tests.cs | 42 +++++++++++++++++++ .../IdentityModel/AbpIdentityModelTestBase.cs | 12 ++++++ .../AbpIdentityModelTestModule.cs | 10 +++++ .../appsettings.json | 40 ++++++++++++++++++ 8 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj create mode 100644 framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityClientOptions_Tests.cs create mode 100644 framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestBase.cs create mode 100644 framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestModule.cs create mode 100644 framework/test/Volo.Abp.IdentityModel.Tests/appsettings.json diff --git a/framework/Volo.Abp.sln b/framework/Volo.Abp.sln index 0605f5c103..95ac94f7c0 100644 --- a/framework/Volo.Abp.sln +++ b/framework/Volo.Abp.sln @@ -383,6 +383,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.TextTemplating.Scr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.MongoDB.Tests.SecondContext", "test\Volo.Abp.MongoDB.Tests.SecondContext\Volo.Abp.MongoDB.Tests.SecondContext.csproj", "{90B1866A-EF99-40B9-970E-B898E5AA523F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.IdentityModel.Tests", "test\Volo.Abp.IdentityModel.Tests\Volo.Abp.IdentityModel.Tests.csproj", "{40C6740E-BFCA-4D37-8344-3D84E2044BB2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1141,6 +1143,10 @@ Global {90B1866A-EF99-40B9-970E-B898E5AA523F}.Debug|Any CPU.Build.0 = Debug|Any CPU {90B1866A-EF99-40B9-970E-B898E5AA523F}.Release|Any CPU.ActiveCfg = Release|Any CPU {90B1866A-EF99-40B9-970E-B898E5AA523F}.Release|Any CPU.Build.0 = Release|Any CPU + {40C6740E-BFCA-4D37-8344-3D84E2044BB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {40C6740E-BFCA-4D37-8344-3D84E2044BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40C6740E-BFCA-4D37-8344-3D84E2044BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {40C6740E-BFCA-4D37-8344-3D84E2044BB2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1334,6 +1340,7 @@ Global {C996F458-98FB-483D-9306-4701290E2FC1} = {447C8A77-E5F0-4538-8687-7383196D04EA} {75D8DADB-3FA9-4C1D-B23A-DBFD08133B7C} = {447C8A77-E5F0-4538-8687-7383196D04EA} {90B1866A-EF99-40B9-970E-B898E5AA523F} = {447C8A77-E5F0-4538-8687-7383196D04EA} + {40C6740E-BFCA-4D37-8344-3D84E2044BB2} = {447C8A77-E5F0-4538-8687-7383196D04EA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityClientOptions.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityClientOptions.cs index 0e877ee441..e8a8bfcd61 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityClientOptions.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityClientOptions.cs @@ -1,4 +1,9 @@ -namespace Volo.Abp.IdentityModel +using System; +using System.Collections.Generic; +using System.Linq; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.IdentityModel { public class AbpIdentityClientOptions { @@ -8,5 +13,30 @@ { IdentityClients = new IdentityClientConfigurationDictionary(); } + + public IdentityClientConfiguration GetClientConfiguration(ICurrentTenant currentTenant, string identityClientName = null) + { + if (identityClientName.IsNullOrWhiteSpace()) + { + identityClientName = IdentityClientConfigurationDictionary.DefaultName; + } + + if (currentTenant.Id.HasValue) + { + var tenantConfiguration = IdentityClients.FirstOrDefault(x => x.Key == $"{identityClientName}.{currentTenant.Id}"); + if (tenantConfiguration.Key == null && !currentTenant.Name.IsNullOrWhiteSpace()) + { + tenantConfiguration = IdentityClients.FirstOrDefault(x => x.Key == $"{identityClientName}.{currentTenant.Name}"); + } + + if (tenantConfiguration.Key != null) + { + return tenantConfiguration.Value; + } + } + + return IdentityClients.GetOrDefault(identityClientName) ?? + IdentityClients.Default; + } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs index cc0b470a1e..f3c67c7b95 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs @@ -66,7 +66,7 @@ namespace Volo.Abp.IdentityModel protected virtual async Task GetAccessTokenOrNullAsync(string identityClientName) { - var configuration = GetClientConfiguration(identityClientName); + var configuration = ClientOptions.GetClientConfiguration(CurrentTenant, identityClientName); if (configuration == null) { Logger.LogWarning($"Could not find {nameof(IdentityClientConfiguration)} for {identityClientName}. Either define a configuration for {identityClientName} or set a default configuration."); @@ -114,17 +114,6 @@ namespace Volo.Abp.IdentityModel client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } - private IdentityClientConfiguration GetClientConfiguration(string identityClientName = null) - { - if (identityClientName.IsNullOrEmpty()) - { - return ClientOptions.IdentityClients.Default; - } - - return ClientOptions.IdentityClients.GetOrDefault(identityClientName) ?? - ClientOptions.IdentityClients.Default; - } - protected virtual async Task GetTokenEndpoint(IdentityClientConfiguration configuration) { //TODO: Can use (configuration.Authority + /connect/token) directly? @@ -205,6 +194,7 @@ namespace Volo.Abp.IdentityModel UserName = configuration.UserName, Password = configuration.UserPassword }; + IdentityModelHttpRequestMessageOptions.ConfigureHttpRequestMessage?.Invoke(request); AddParametersToRequestAsync(configuration, request); diff --git a/framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj b/framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj new file mode 100644 index 0000000000..f2323aaeef --- /dev/null +++ b/framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj @@ -0,0 +1,24 @@ + + + + + + net5.0 + + + + + + + + + + + + + + Always + + + + diff --git a/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityClientOptions_Tests.cs b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityClientOptions_Tests.cs new file mode 100644 index 0000000000..ef7bd895f5 --- /dev/null +++ b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityClientOptions_Tests.cs @@ -0,0 +1,42 @@ +using System; +using Microsoft.Extensions.Options; +using Shouldly; +using Volo.Abp.MultiTenancy; +using Xunit; + +namespace Volo.Abp.IdentityModel +{ + public class AbpIdentityClientOptions_Tests : AbpIdentityModelTestBase + { + private readonly ICurrentTenant _currentTenant; + private readonly AbpIdentityClientOptions _identityClientOptions; + + public AbpIdentityClientOptions_Tests() + { + _currentTenant = GetRequiredService(); + _identityClientOptions = GetRequiredService>().Value; + } + + [Fact] + public void GetClientConfiguration_Test() + { + var hostDefaultConfiguration = _identityClientOptions.GetClientConfiguration(_currentTenant); + hostDefaultConfiguration.UserName.ShouldBe("host_default_admin"); + + var hostIdentityConfiguration = _identityClientOptions.GetClientConfiguration(_currentTenant, "Identity"); + hostIdentityConfiguration.UserName.ShouldBe("host_identity_admin"); + + using (_currentTenant.Change(Guid.Parse("f72a344f-651e-49f0-85f6-be260a10e4df"), "Test_Tenant1")) + { + var tenantDefaultConfiguration = _identityClientOptions.GetClientConfiguration(_currentTenant); + tenantDefaultConfiguration.UserName.ShouldBe("tenant_default_admin"); + } + + using (_currentTenant.Change(Guid.Parse("f72a344f-651e-49f0-85f6-be260a10e4df"))) + { + var tenantIdentityConfiguration = _identityClientOptions.GetClientConfiguration(_currentTenant, "Identity"); + tenantIdentityConfiguration.UserName.ShouldBe("tenant_identity_admin"); + } + } + } +} diff --git a/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestBase.cs b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestBase.cs new file mode 100644 index 0000000000..80ee47ae37 --- /dev/null +++ b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestBase.cs @@ -0,0 +1,12 @@ +using Volo.Abp.Testing; + +namespace Volo.Abp.IdentityModel +{ + public abstract class AbpIdentityModelTestBase : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} diff --git a/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestModule.cs b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestModule.cs new file mode 100644 index 0000000000..2a17244e42 --- /dev/null +++ b/framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestModule.cs @@ -0,0 +1,10 @@ +using Volo.Abp.Modularity; + +namespace Volo.Abp.IdentityModel +{ + [DependsOn(typeof(AbpIdentityModelModule))] + public class AbpIdentityModelTestModule : AbpModule + { + + } +} diff --git a/framework/test/Volo.Abp.IdentityModel.Tests/appsettings.json b/framework/test/Volo.Abp.IdentityModel.Tests/appsettings.json new file mode 100644 index 0000000000..f4d01ff407 --- /dev/null +++ b/framework/test/Volo.Abp.IdentityModel.Tests/appsettings.json @@ -0,0 +1,40 @@ +{ + "IdentityClients": { + "Default": { + "GrantType": "password", + "ClientId": "Test_App", + "ClientSecret": "1q2w3e*", + "UserName": "host_default_admin", + "UserPassword": "1q2w3E*", + "Authority": "https://localhost:44395", + "Scope": "Test_Scope" + }, + "Default.Test_Tenant1": { + "GrantType": "password", + "ClientId": "Test_App", + "ClientSecret": "1q2w3e*", + "UserName": "tenant_default_admin", + "UserPassword": "1q2w3E*", + "Authority": "https://localhost:44395", + "Scope": "Test_Scope" + }, + "Identity": { + "GrantType": "password", + "ClientId": "Test_App", + "ClientSecret": "1q2w3e*", + "UserName": "host_identity_admin", + "UserPassword": "1q2w3E*", + "Authority": "https://localhost:44395", + "Scope": "Test_Scope" + }, + "Identity.f72a344f-651e-49f0-85f6-be260a10e4df": { + "GrantType": "password", + "ClientId": "Test_App", + "ClientSecret": "1q2w3e*", + "UserName": "tenant_identity_admin", + "UserPassword": "1q2w3E*", + "Authority": "https://localhost:44395", + "Scope": "Test_Scope" + } + } +}