Resolved #719: Create Volo.Abp.IdentityModel package.

pull/725/head
Halil ibrahim Kalkan 7 years ago
parent b4cafbdbee
commit 8100002994

@ -218,7 +218,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Security.Tests", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Settings.Tests", "test\Volo.Abp.Settings.Tests\Volo.Abp.Settings.Tests.csproj", "{5F3A2D1E-EA89-40A7-8D2F-FB4EB2092403}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Http.Client.IdentityModel", "src\Volo.Abp.Http.Client.IdentityModel\Volo.Abp.Http.Client.IdentityModel.csproj", "{D211A446-38FA-4F97-9A95-1F004A0FFF69}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Http.Client.IdentityModel", "src\Volo.Abp.Http.Client.IdentityModel\Volo.Abp.Http.Client.IdentityModel.csproj", "{D211A446-38FA-4F97-9A95-1F004A0FFF69}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.IdentityModel", "src\Volo.Abp.IdentityModel\Volo.Abp.IdentityModel.csproj", "{64D99E19-EE25-465A-82E5-17B25F4C4E18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -622,6 +624,10 @@ Global
{D211A446-38FA-4F97-9A95-1F004A0FFF69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D211A446-38FA-4F97-9A95-1F004A0FFF69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D211A446-38FA-4F97-9A95-1F004A0FFF69}.Release|Any CPU.Build.0 = Release|Any CPU
{64D99E19-EE25-465A-82E5-17B25F4C4E18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64D99E19-EE25-465A-82E5-17B25F4C4E18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64D99E19-EE25-465A-82E5-17B25F4C4E18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64D99E19-EE25-465A-82E5-17B25F4C4E18}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -728,6 +734,7 @@ Global
{7CE07034-7E02-4C78-B981-F1039412CA5E} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{5F3A2D1E-EA89-40A7-8D2F-FB4EB2092403} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{D211A446-38FA-4F97-9A95-1F004A0FFF69} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{64D99E19-EE25-465A-82E5-17B25F4C4E18} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="IdentityModel" Version="3.10.4" />
<ProjectReference Include="..\Volo.Abp.Http.Client\Volo.Abp.Http.Client.csproj" />
<ProjectReference Include="..\Volo.Abp.IdentityModel\Volo.Abp.IdentityModel.csproj" />
</ItemGroup>
</Project>

@ -1,18 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.IdentityModel;
using Volo.Abp.Modularity;
namespace Volo.Abp.Http.Client.IdentityModel
{
[DependsOn(
typeof(AbpHttpClientModule)
typeof(AbpHttpClientModule),
typeof(AbpIdentityModelModule)
)]
public class AbpHttpClientIdentityModelModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
Configure<IdentityClientOptions>(configuration);
}
}
}

@ -0,0 +1,56 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.IdentityModel;
namespace Volo.Abp.Http.Client.IdentityModel
{
[Dependency(ReplaceServices = true)]
public class IdentityModelRemoteServiceHttpClientAuthenticator : IRemoteServiceHttpClientAuthenticator, ITransientDependency
{
public IHttpContextAccessor HttpContextAccessor { get; set; }
protected IIdentityModelHttpClientAuthenticator IdentityModelHttpClientAuthenticator { get; }
public IdentityModelRemoteServiceHttpClientAuthenticator(IIdentityModelHttpClientAuthenticator identityModelHttpClientAuthenticator)
{
IdentityModelHttpClientAuthenticator = identityModelHttpClientAuthenticator;
}
public async Task Authenticate(RemoteServiceHttpClientAuthenticateContext context)
{
var accessToken = await GetAccessTokenFromHttpContextOrNullAsync();
if (accessToken != null)
{
//TODO: "Bearer" should be configurable
context.Client.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Bearer", accessToken);
}
else
{
await IdentityModelHttpClientAuthenticator.Authenticate(
new IdentityModelHttpClientAuthenticateContext(
context.Client,
context.RemoteService.GetIdentityClient()
)
);
}
}
protected virtual async Task<string> GetAccessTokenFromHttpContextOrNullAsync()
{
//TODO: What if the access_token in the current Http Request is not usable for this client?
var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext == null)
{
return null;
}
return await httpContext.GetTokenAsync("access_token");
}
}
}

@ -5,17 +5,19 @@ namespace Volo.Abp.Http.Client
{
public static class RemoteServiceConfigurationExtensions
{
public const string IdentityClient = "IdentityClient";
[CanBeNull]
public static string GetIdentityClient([NotNull] this RemoteServiceConfiguration configuration)
{
Check.NotNullOrEmpty(configuration, nameof(configuration));
return configuration.GetOrDefault("IdentityClient");
return configuration.GetOrDefault(IdentityClient);
}
public static RemoteServiceConfiguration SetIdentityClient([NotNull] this RemoteServiceConfiguration configuration, [CanBeNull] string value)
{
configuration["IdentityClient"] = value;
configuration[IdentityClient] = value;
return configuration;
}
}

@ -1,9 +0,0 @@
using System.Threading.Tasks;
namespace Volo.Abp.Http.Client.Authentication
{
public interface IHttpClientAuthenticator
{
Task Authenticate(HttpClientAuthenticateContext context);
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.Http.Client.Authentication
{
public interface IRemoteServiceHttpClientAuthenticator
{
Task Authenticate(RemoteServiceHttpClientAuthenticateContext context);
}
}

@ -4,9 +4,9 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Http.Client.Authentication
{
[Dependency(TryRegister = true)]
public class NullHttpClientAuthenticator : IHttpClientAuthenticator, ISingletonDependency
public class NullRemoteServiceHttpClientAuthenticator : IRemoteServiceHttpClientAuthenticator, ISingletonDependency
{
public Task Authenticate(HttpClientAuthenticateContext context)
public Task Authenticate(RemoteServiceHttpClientAuthenticateContext context)
{
return Task.CompletedTask;
}

@ -2,7 +2,7 @@
namespace Volo.Abp.Http.Client.Authentication
{
public class HttpClientAuthenticateContext
public class RemoteServiceHttpClientAuthenticateContext
{
public HttpClient Client { get; }
@ -10,7 +10,7 @@ namespace Volo.Abp.Http.Client.Authentication
public RemoteServiceConfiguration RemoteService { get; }
public HttpClientAuthenticateContext(
public RemoteServiceHttpClientAuthenticateContext(
HttpClient client,
HttpRequestMessage request,
RemoteServiceConfiguration remoteService)

@ -27,7 +27,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
private readonly RemoteServiceOptions _remoteServiceOptions;
private readonly AbpHttpClientOptions _clientOptions;
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClientAuthenticator _clientAuthenticator;
private readonly IRemoteServiceHttpClientAuthenticator _clientAuthenticator;
static DynamicHttpProxyInterceptor()
{
@ -42,7 +42,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
IOptionsSnapshot<RemoteServiceOptions> remoteServiceOptions,
IApiDescriptionFinder apiDescriptionFinder,
IJsonSerializer jsonSerializer,
IHttpClientAuthenticator clientAuthenticator)
IRemoteServiceHttpClientAuthenticator clientAuthenticator)
{
_httpClientFactory = httpClientFactory;
_apiDescriptionFinder = apiDescriptionFinder;
@ -123,7 +123,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
AddHeaders(invocation, action, requestMessage, apiVersion);
await _clientAuthenticator.Authenticate(
new HttpClientAuthenticateContext(
new RemoteServiceHttpClientAuthenticateContext(
client,
requestMessage,
remoteServiceConfig

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Volo.Abp.IdentityModel</AssemblyName>
<PackageId>Volo.Abp.IdentityModel</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityModel" Version="3.10.4" />
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace Volo.Abp.IdentityModel
{
public class AbpIdentityModelModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
Configure<IdentityClientOptions>(configuration);
}
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.IdentityModel
{
public interface IIdentityModelHttpClientAuthenticator
{
Task Authenticate(IdentityModelHttpClientAuthenticateContext context);
}
}

@ -1,7 +1,7 @@
using System.Collections.Generic;
using IdentityModel;
namespace Volo.Abp.Http.Client.IdentityModel
namespace Volo.Abp.IdentityModel
{
public class IdentityClientConfiguration : Dictionary<string, string>
{

@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Volo.Abp.Http.Client.IdentityModel
namespace Volo.Abp.IdentityModel
{
public class IdentityClientConfigurationDictionary : Dictionary<string, IdentityClientConfiguration>
{

@ -0,0 +1,27 @@
using System.Net.Http;
namespace Volo.Abp.IdentityModel
{
public class IdentityModelHttpClientAuthenticateContext
{
public HttpClient Client { get; }
/// <summary>
/// The identity client name configured with the <see cref="IdentityClientOptions"/>.
/// </summary>
public string IdentityClientName { get; }
/// <summary>
///
/// </summary>
/// <param name="client"><see cref="HttpClient"/> object to be authorized</param>
/// <param name="identityClientName">The identity client name configured with the <see cref="IdentityClientOptions"/>.</param>
public IdentityModelHttpClientAuthenticateContext(
HttpClient client,
string identityClientName = null)
{
Client = client;
IdentityClientName = identityClientName;
}
}
}

@ -4,21 +4,14 @@ using System.Net.Http.Headers;
using System.Threading.Tasks;
using IdentityModel;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client.Authentication;
namespace Volo.Abp.Http.Client.IdentityModel
namespace Volo.Abp.IdentityModel
{
//TODO: This class should be optimized and improved:
[Dependency(ReplaceServices = true)]
public class IdentityModelHttpClientAuthenticator : IHttpClientAuthenticator, ITransientDependency
public class IdentityModelHttpClientAuthenticator : IIdentityModelHttpClientAuthenticator, ITransientDependency
{
public IHttpContextAccessor HttpContextAccessor { get; set; }
protected IdentityClientOptions ClientOptions { get; }
public IdentityModelHttpClientAuthenticator(
@ -27,10 +20,9 @@ namespace Volo.Abp.Http.Client.IdentityModel
ClientOptions = options.Value;
}
public async Task Authenticate(HttpClientAuthenticateContext context)
public async Task Authenticate(IdentityModelHttpClientAuthenticateContext context)
{
var accessToken = await GetAccessTokenFromHttpContextOrNullAsync() ??
await GetAccessTokenFromServerOrNullAsync(context);
var accessToken = await GetAccessTokenFromServerOrNullAsync(context);
if (accessToken != null)
{
@ -40,18 +32,7 @@ namespace Volo.Abp.Http.Client.IdentityModel
}
}
protected virtual async Task<string> GetAccessTokenFromHttpContextOrNullAsync()
{
var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext == null)
{
return null;
}
return await httpContext.GetTokenAsync("access_token");
}
protected virtual async Task<string> GetAccessTokenFromServerOrNullAsync(HttpClientAuthenticateContext context)
protected virtual async Task<string> GetAccessTokenFromServerOrNullAsync(IdentityModelHttpClientAuthenticateContext context)
{
var configuration = GetClientConfiguration(context);
@ -75,15 +56,14 @@ namespace Volo.Abp.Http.Client.IdentityModel
return tokenResponse.AccessToken;
}
private IdentityClientConfiguration GetClientConfiguration(HttpClientAuthenticateContext context)
private IdentityClientConfiguration GetClientConfiguration(IdentityModelHttpClientAuthenticateContext context)
{
var identityClientName = context.RemoteService.GetIdentityClient();
if (identityClientName.IsNullOrEmpty())
if (context.IdentityClientName.IsNullOrEmpty())
{
return ClientOptions.IdentityClients.Default;
}
return ClientOptions.IdentityClients.GetOrDefault(identityClientName) ??
return ClientOptions.IdentityClients.GetOrDefault(context.IdentityClientName) ??
ClientOptions.IdentityClients.Default;
}

@ -64,6 +64,7 @@ $projects = (
"framework/src/Volo.Abp.Http.Abstractions",
"framework/src/Volo.Abp.Http.Client",
"framework/src/Volo.Abp.Http.Client.IdentityModel",
"framework/src/Volo.Abp.IdentityModel",
"framework/src/Volo.Abp.Json",
"framework/src/Volo.Abp.Localization",
"framework/src/Volo.Abp.Localization.Abstractions",

Loading…
Cancel
Save