Created IAccessTokenProvider.

pull/221/head
Halil İbrahim Kalkan 7 years ago
parent 6e306e4c13
commit f7aaf2e387

@ -13,6 +13,10 @@
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.Http\Volo.Abp.Http.csproj" />

@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Http.Client.Authentication
{
public class HttpContextAccessTokenProvider : IAccessTokenProvider, ISingletonDependency
{
public IHttpContextAccessor HttpContextAccessor { get; set; }
public async Task<string> GetOrNullAsync()
{
var httpContext = HttpContextAccessor?.HttpContext;
if (httpContext == null)
{
return null;
}
return await httpContext.GetTokenAsync("access_token");
}
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.Http.Client.Authentication
{
public interface IAccessTokenProvider //TODO: Not sure if this class should be here
{
Task<string> GetOrNullAsync();
}
}

@ -5,9 +5,12 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators;
using Volo.Abp.Json;
@ -27,6 +30,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
private readonly RemoteServiceOptions _remoteServiceOptions;
private readonly AbpHttpClientOptions _clientOptions;
private readonly IJsonSerializer _jsonSerializer;
private readonly IAccessTokenProvider _accessTokenProvider;
static DynamicHttpProxyInterceptor()
{
@ -40,11 +44,13 @@ namespace Volo.Abp.Http.Client.DynamicProxying
IOptions<AbpHttpClientOptions> clientOptions,
IOptionsSnapshot<RemoteServiceOptions> remoteServiceOptions,
IApiDescriptionFinder apiDescriptionFinder,
IJsonSerializer jsonSerializer)
IJsonSerializer jsonSerializer,
IAccessTokenProvider accessTokenProvider)
{
_httpClientFactory = httpClientFactory;
_apiDescriptionFinder = apiDescriptionFinder;
_jsonSerializer = jsonSerializer;
_accessTokenProvider = accessTokenProvider;
_clientOptions = clientOptions.Value;
_remoteServiceOptions = remoteServiceOptions.Value;
}
@ -105,7 +111,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{
using (var client = _httpClientFactory.Create())
{
var baseUrl = GetBaseUrl();
var clientConfig = _clientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
var baseUrl = GetBaseUrl(clientConfig);
var action = await _apiDescriptionFinder.FindActionAsync(baseUrl, typeof(TService), invocation.Method);
var apiVersion = GetApiVersionInfo(action);
var url = baseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion);
@ -117,6 +125,13 @@ namespace Volo.Abp.Http.Client.DynamicProxying
AddHeaders(invocation, action, requestMessage, apiVersion);
var accessToken = await _accessTokenProvider.GetOrNullAsync();
if (accessToken != null)
{
//TODO: "Bearer" should not by static.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
var response = await client.SendAsync(requestMessage);
if (!response.IsSuccessStatusCode)
@ -167,7 +182,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
}
var headers = action.Parameters.Where(p => p.BindingSourceId == ParameterBindingSources.Header).ToArray();
foreach (var headerParameter in headers)
{
var value = HttpActionParameterHelper.FindParameterValue(invocation.ArgumentsDictionary, headerParameter);
@ -178,12 +193,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying
}
}
private string GetBaseUrl()
private string GetBaseUrl(DynamicHttpClientProxyConfig config)
{
var clientConfig = _clientOptions.HttpClientProxies.GetOrDefault(typeof(TService))
?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
return _remoteServiceOptions.RemoteServices.GetOrDefault(clientConfig.RemoteServiceName)?.BaseUrl
return _remoteServiceOptions.RemoteServices.GetOrDefault(config.RemoteServiceName)?.BaseUrl
?? _remoteServiceOptions.RemoteServices.Default?.BaseUrl
?? throw new AbpException($"Could not find Base URL for {typeof(TService).FullName}.");
}

Loading…
Cancel
Save