From 810000299421679a40ea734a8232e015b400a30a Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 7 Jan 2019 22:34:51 +0300 Subject: [PATCH] Resolved #719: Create Volo.Abp.IdentityModel package. --- framework/Volo.Abp.sln | 9 ++- .../Volo.Abp.Http.Client.IdentityModel.csproj | 1 + .../AbpHttpClientIdentityModelModule.cs | 10 +--- ...delRemoteServiceHttpClientAuthenticator.cs | 56 +++++++++++++++++++ .../RemoteServiceConfigurationExtensions.cs | 6 +- .../IHttpClientAuthenticator.cs | 9 --- .../IRemoteServiceHttpClientAuthenticator.cs | 9 +++ ...llRemoteServiceHttpClientAuthenticator.cs} | 4 +- ...teServiceHttpClientAuthenticateContext.cs} | 4 +- .../DynamicHttpProxyInterceptor.cs | 6 +- .../Volo.Abp.IdentityModel.csproj | 21 +++++++ .../IdentityModel/AbpIdentityModelModule.cs | 15 +++++ .../IIdentityModelHttpClientAuthenticator.cs | 9 +++ .../IdentityClientConfiguration.cs | 2 +- .../IdentityClientConfigurationDictionary.cs | 2 +- .../IdentityModel/IdentityClientOptions.cs | 2 +- ...ntityModelHttpClientAuthenticateContext.cs | 27 +++++++++ ...elRemoteServiceHttpClientAuthenticator.cs} | 36 +++--------- nupkg/common.ps1 | 1 + 19 files changed, 172 insertions(+), 57 deletions(-) create mode 100644 framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs delete mode 100644 framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IHttpClientAuthenticator.cs create mode 100644 framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IRemoteServiceHttpClientAuthenticator.cs rename framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/{NullHttpClientAuthenticator.cs => NullRemoteServiceHttpClientAuthenticator.cs} (52%) rename framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/{HttpClientAuthenticateContext.cs => RemoteServiceHttpClientAuthenticateContext.cs} (80%) create mode 100644 framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj create mode 100644 framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs create mode 100644 framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IIdentityModelHttpClientAuthenticator.cs rename framework/src/{Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client => Volo.Abp.IdentityModel/Volo/Abp}/IdentityModel/IdentityClientConfiguration.cs (98%) rename framework/src/{Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client => Volo.Abp.IdentityModel/Volo/Abp}/IdentityModel/IdentityClientConfigurationDictionary.cs (89%) rename framework/src/{Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client => Volo.Abp.IdentityModel/Volo/Abp}/IdentityModel/IdentityClientOptions.cs (84%) create mode 100644 framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelHttpClientAuthenticateContext.cs rename framework/src/{Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelHttpClientAuthenticator.cs => Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs} (69%) diff --git a/framework/Volo.Abp.sln b/framework/Volo.Abp.sln index 36aef8acce..3b18df4e23 100644 --- a/framework/Volo.Abp.sln +++ b/framework/Volo.Abp.sln @@ -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} diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo.Abp.Http.Client.IdentityModel.csproj b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo.Abp.Http.Client.IdentityModel.csproj index c19bd29eb4..b96d471322 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo.Abp.Http.Client.IdentityModel.csproj +++ b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo.Abp.Http.Client.IdentityModel.csproj @@ -16,6 +16,7 @@ + \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/AbpHttpClientIdentityModelModule.cs b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/AbpHttpClientIdentityModelModule.cs index 9946083fb8..356acfe16f 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/AbpHttpClientIdentityModelModule.cs +++ b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/AbpHttpClientIdentityModelModule.cs @@ -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(configuration); - } } } diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs new file mode 100644 index 0000000000..a5ef33e83b --- /dev/null +++ b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs @@ -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 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"); + } + } +} diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/RemoteServiceConfigurationExtensions.cs b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/RemoteServiceConfigurationExtensions.cs index b42920442f..9d1c8a71e9 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/RemoteServiceConfigurationExtensions.cs +++ b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/RemoteServiceConfigurationExtensions.cs @@ -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; } } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IHttpClientAuthenticator.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IHttpClientAuthenticator.cs deleted file mode 100644 index 83d6f9dd31..0000000000 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IHttpClientAuthenticator.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; - -namespace Volo.Abp.Http.Client.Authentication -{ - public interface IHttpClientAuthenticator - { - Task Authenticate(HttpClientAuthenticateContext context); - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IRemoteServiceHttpClientAuthenticator.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IRemoteServiceHttpClientAuthenticator.cs new file mode 100644 index 0000000000..920d38f2e6 --- /dev/null +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/IRemoteServiceHttpClientAuthenticator.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Http.Client.Authentication +{ + public interface IRemoteServiceHttpClientAuthenticator + { + Task Authenticate(RemoteServiceHttpClientAuthenticateContext context); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullHttpClientAuthenticator.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullRemoteServiceHttpClientAuthenticator.cs similarity index 52% rename from framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullHttpClientAuthenticator.cs rename to framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullRemoteServiceHttpClientAuthenticator.cs index a22ebd3674..6928f10dae 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullHttpClientAuthenticator.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/NullRemoteServiceHttpClientAuthenticator.cs @@ -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; } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/HttpClientAuthenticateContext.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/RemoteServiceHttpClientAuthenticateContext.cs similarity index 80% rename from framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/HttpClientAuthenticateContext.cs rename to framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/RemoteServiceHttpClientAuthenticateContext.cs index 7374b79694..fff00d5bf8 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/HttpClientAuthenticateContext.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/Authentication/RemoteServiceHttpClientAuthenticateContext.cs @@ -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) diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index f3f6e8eae6..d367c7b09e 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -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, 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 diff --git a/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj b/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj new file mode 100644 index 0000000000..ac757528c7 --- /dev/null +++ b/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj @@ -0,0 +1,21 @@ + + + + + + netstandard2.0 + Volo.Abp.IdentityModel + Volo.Abp.IdentityModel + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + \ No newline at end of file diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs new file mode 100644 index 0000000000..cadeb794a7 --- /dev/null +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs @@ -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(configuration); + } + } +} diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IIdentityModelHttpClientAuthenticator.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IIdentityModelHttpClientAuthenticator.cs new file mode 100644 index 0000000000..80215263c8 --- /dev/null +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IIdentityModelHttpClientAuthenticator.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.IdentityModel +{ + public interface IIdentityModelHttpClientAuthenticator + { + Task Authenticate(IdentityModelHttpClientAuthenticateContext context); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfiguration.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs similarity index 98% rename from framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfiguration.cs rename to framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs index 9512328222..520195e7ce 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfiguration.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using IdentityModel; -namespace Volo.Abp.Http.Client.IdentityModel +namespace Volo.Abp.IdentityModel { public class IdentityClientConfiguration : Dictionary { diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfigurationDictionary.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfigurationDictionary.cs similarity index 89% rename from framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfigurationDictionary.cs rename to framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfigurationDictionary.cs index 7f79df0b37..62c9359a74 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientConfigurationDictionary.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfigurationDictionary.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Volo.Abp.Http.Client.IdentityModel +namespace Volo.Abp.IdentityModel { public class IdentityClientConfigurationDictionary : Dictionary { diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientOptions.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientOptions.cs similarity index 84% rename from framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientOptions.cs rename to framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientOptions.cs index e9b1175bcb..f05d5c8511 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityClientOptions.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientOptions.cs @@ -1,4 +1,4 @@ -namespace Volo.Abp.Http.Client.IdentityModel +namespace Volo.Abp.IdentityModel { public class IdentityClientOptions { diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelHttpClientAuthenticateContext.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelHttpClientAuthenticateContext.cs new file mode 100644 index 0000000000..849ae7196d --- /dev/null +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelHttpClientAuthenticateContext.cs @@ -0,0 +1,27 @@ +using System.Net.Http; + +namespace Volo.Abp.IdentityModel +{ + public class IdentityModelHttpClientAuthenticateContext + { + public HttpClient Client { get; } + + /// + /// The identity client name configured with the . + /// + public string IdentityClientName { get; } + + /// + /// + /// + /// object to be authorized + /// The identity client name configured with the . + public IdentityModelHttpClientAuthenticateContext( + HttpClient client, + string identityClientName = null) + { + Client = client; + IdentityClientName = identityClientName; + } + } +} diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelHttpClientAuthenticator.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs similarity index 69% rename from framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelHttpClientAuthenticator.cs rename to framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs index c3aaf4d93c..4a0daea58d 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelHttpClientAuthenticator.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs @@ -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 GetAccessTokenFromHttpContextOrNullAsync() - { - var httpContext = HttpContextAccessor?.HttpContext; - if (httpContext == null) - { - return null; - } - - return await httpContext.GetTokenAsync("access_token"); - } - - protected virtual async Task GetAccessTokenFromServerOrNullAsync(HttpClientAuthenticateContext context) + protected virtual async Task 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; } diff --git a/nupkg/common.ps1 b/nupkg/common.ps1 index ba084c1175..e29ba634b9 100644 --- a/nupkg/common.ps1 +++ b/nupkg/common.ps1 @@ -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",