Browse Source

Add tenant info to IdentityClients.

pull/9675/head
maliming 5 years ago
parent
commit
09177fcaa0
  1. 7
      framework/Volo.Abp.sln
  2. 34
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityClientOptions.cs
  3. 14
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs
  4. 24
      framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj
  5. 42
      framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityClientOptions_Tests.cs
  6. 12
      framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestBase.cs
  7. 10
      framework/test/Volo.Abp.IdentityModel.Tests/Volo/Abp/IdentityModel/AbpIdentityModelTestModule.cs
  8. 40
      framework/test/Volo.Abp.IdentityModel.Tests/appsettings.json

7
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}

34
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;
}
}
}
}

14
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

@ -66,7 +66,7 @@ namespace Volo.Abp.IdentityModel
protected virtual async Task<string> 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<string> 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);

24
framework/test/Volo.Abp.IdentityModel.Tests/Volo.Abp.IdentityModel.Tests.csproj

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.test.props" />
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\src\Volo.Abp.IdentityModel\Volo.Abp.IdentityModel.csproj" />
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

42
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<ICurrentTenant>();
_identityClientOptions = GetRequiredService<IOptions<AbpIdentityClientOptions>>().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");
}
}
}
}

12
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<AbpIdentityModelTestModule>
{
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{
options.UseAutofac();
}
}
}

10
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
{
}
}

40
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"
}
}
}
Loading…
Cancel
Save