Browse Source

Merge pull request #4173 from abpframework/liangshiwei/identityModel

IdentityModelAuthenticationService support multi-tenancy
rel-2.9
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
7c9adbf9fe
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj
  2. 6
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs
  3. 67
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

1
framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj

@ -17,6 +17,7 @@
<ItemGroup>
<PackageReference Include="IdentityModel" Version="4.1.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" />
<ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
</ItemGroup>

6
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs

@ -1,11 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
namespace Volo.Abp.IdentityModel
{
[DependsOn(
typeof(AbpThreadingModule)
typeof(AbpThreadingModule),
typeof(AbpMultiTenancyModule)
)]
public class AbpIdentityModelModule : AbpModule
{
@ -13,7 +15,7 @@ namespace Volo.Abp.IdentityModel
{
var configuration = context.Services.GetConfiguration();
context.Services.AddHttpClient();
context.Services.AddHttpClient(IdentityModelAuthenticationService.HttpClientName);
Configure<AbpIdentityClientOptions>(configuration);
}

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

@ -11,6 +11,7 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
namespace Volo.Abp.IdentityModel
@ -18,19 +19,23 @@ namespace Volo.Abp.IdentityModel
[Dependency(ReplaceServices = true)]
public class IdentityModelAuthenticationService : IIdentityModelAuthenticationService, ITransientDependency
{
public const string HttpClientName = "IdentityModelAuthenticationServiceHttpClientName";
public ILogger<IdentityModelAuthenticationService> Logger { get; set; }
protected AbpIdentityClientOptions ClientOptions { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected IHttpClientFactory HttpClientFactory { get; }
protected ICurrentTenant CurrentTenant { get; }
public IdentityModelAuthenticationService(
IOptions<AbpIdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider,
IHttpClientFactory httpClientFactory)
IHttpClientFactory httpClientFactory,
ICurrentTenant currentTenant)
{
ClientOptions = options.Value;
CancellationTokenProvider = cancellationTokenProvider;
HttpClientFactory = httpClientFactory;
CurrentTenant = currentTenant;
Logger = NullLogger<IdentityModelAuthenticationService>.Instance;
}
@ -46,7 +51,6 @@ namespace Volo.Abp.IdentityModel
SetAccessToken(client, accessToken);
return true;
}
protected virtual async Task<string> GetAccessTokenOrNullAsync(string identityClientName)
@ -106,40 +110,39 @@ namespace Volo.Abp.IdentityModel
protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse(
IdentityClientConfiguration configuration)
{
using (var httpClient = HttpClientFactory.CreateClient())
var httpClient = HttpClientFactory.CreateClient(HttpClientName);
return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
Address = configuration.Authority,
Policy =
{
Address = configuration.Authority,
Policy =
{
RequireHttps = configuration.RequireHttps
}
});
}
RequireHttps = configuration.RequireHttps
}
});
}
protected virtual async Task<TokenResponse> GetTokenResponse(
DiscoveryDocumentResponse discoveryResponse,
IdentityClientConfiguration configuration)
{
using (var httpClient = HttpClientFactory.CreateClient())
var httpClient = HttpClientFactory.CreateClient(HttpClientName);
AddHeaders(httpClient);
switch (configuration.GrantType)
{
switch (configuration.GrantType)
{
case OidcConstants.GrantTypes.ClientCredentials:
return await httpClient.RequestClientCredentialsTokenAsync(
await CreateClientCredentialsTokenRequestAsync(discoveryResponse, configuration),
CancellationTokenProvider.Token
);
case OidcConstants.GrantTypes.Password:
return await httpClient.RequestPasswordTokenAsync(
await CreatePasswordTokenRequestAsync(discoveryResponse, configuration),
CancellationTokenProvider.Token
);
default:
throw new AbpException("Grant type was not implemented: " + configuration.GrantType);
}
case OidcConstants.GrantTypes.ClientCredentials:
return await httpClient.RequestClientCredentialsTokenAsync(
await CreateClientCredentialsTokenRequestAsync(discoveryResponse, configuration),
CancellationTokenProvider.Token
);
case OidcConstants.GrantTypes.Password:
return await httpClient.RequestPasswordTokenAsync(
await CreatePasswordTokenRequestAsync(discoveryResponse, configuration),
CancellationTokenProvider.Token
);
default:
throw new AbpException("Grant type was not implemented: " + configuration.GrantType);
}
}
@ -186,5 +189,15 @@ namespace Volo.Abp.IdentityModel
return Task.CompletedTask;
}
protected virtual void AddHeaders(HttpClient client)
{
//tenantId
if (CurrentTenant.Id.HasValue)
{
//TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
client.DefaultRequestHeaders.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
}
}
}

Loading…
Cancel
Save