Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure IdentityModel request message & configure all http clients. #4353

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Volo.Abp.Castle;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
Expand All @@ -21,5 +23,24 @@ public override void ConfigureServices(ServiceConfigurationContext context)
var configuration = context.Services.GetConfiguration();
Configure<AbpRemoteServiceOptions>(configuration);
}

public override void PostConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpHttpClientOptions>(options =>
{
if (options.HttpClientActions.Any())
{
var httpClientNames = options.HttpClientProxies.Select(x => x.Value.RemoteServiceName);
foreach (var httpClientName in httpClientNames)
{
foreach (var httpClientAction in options.HttpClientActions)
{
context.Services.Configure<HttpClientFactoryOptions>(httpClientName,
x => x.HttpClientActions.Add(httpClientAction.Invoke(httpClientName)));
}
}
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using Volo.Abp.Http.Client.DynamicProxying;

namespace Volo.Abp.Http.Client
Expand All @@ -8,9 +9,12 @@ public class AbpHttpClientOptions
{
public Dictionary<Type, DynamicHttpClientProxyConfig> HttpClientProxies { get; set; }

public List<Func<string, Action<HttpClient>>> HttpClientActions { get; }

public AbpHttpClientOptions()
{
HttpClientProxies = new Dictionary<Type, DynamicHttpClientProxyConfig>();
HttpClientActions = new List<Func<string, Action<HttpClient>>>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ public class IdentityModelAuthenticationService : IIdentityModelAuthenticationSe
protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected IHttpClientFactory HttpClientFactory { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IdentityModelHttpRequestMessageOptions IdentityModelHttpRequestMessageOptions { get; }

public IdentityModelAuthenticationService(
IOptions<AbpIdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider,
IHttpClientFactory httpClientFactory,
ICurrentTenant currentTenant)
ICurrentTenant currentTenant,
IOptions<IdentityModelHttpRequestMessageOptions> identityModelHttpRequestMessageOptions)
{
ClientOptions = options.Value;
CancellationTokenProvider = cancellationTokenProvider;
HttpClientFactory = httpClientFactory;
CurrentTenant = currentTenant;
IdentityModelHttpRequestMessageOptions = identityModelHttpRequestMessageOptions.Value;
Logger = NullLogger<IdentityModelAuthenticationService>.Instance;
}

Expand Down Expand Up @@ -74,7 +77,7 @@ public virtual async Task<string> GetAccessTokenAsync(IdentityClientConfiguratio
}

var tokenResponse = await GetTokenResponse(discoveryResponse, configuration);

if (tokenResponse.IsError)
{
if (tokenResponse.ErrorDescription != null)
Expand Down Expand Up @@ -112,14 +115,16 @@ protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse(
{
using (var httpClient = HttpClientFactory.CreateClient(HttpClientName))
{
return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
var request = new DiscoveryDocumentRequest
{
Address = configuration.Authority,
Policy =
{
RequireHttps = configuration.RequireHttps
}
});
};
IdentityModelHttpRequestMessageOptions.ConfigureHttpRequestMessage(request);
return await httpClient.GetDiscoveryDocumentAsync(request);
}
}

Expand Down Expand Up @@ -160,6 +165,7 @@ protected virtual Task<PasswordTokenRequest> CreatePasswordTokenRequestAsync(Dis
UserName = configuration.UserName,
Password = configuration.UserPassword
};
IdentityModelHttpRequestMessageOptions.ConfigureHttpRequestMessage(request);

AddParametersToRequestAsync(configuration, request);

Expand All @@ -177,6 +183,7 @@ protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTok
ClientId = configuration.ClientId,
ClientSecret = configuration.ClientSecret
};
IdentityModelHttpRequestMessageOptions.ConfigureHttpRequestMessage(request);

AddParametersToRequestAsync(configuration, request);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Net.Http;

namespace Volo.Abp.IdentityModel
{
public class IdentityModelHttpRequestMessageOptions
{
public Action<HttpRequestMessage> ConfigureHttpRequestMessage { get; set; }
}
}