|
@@ -0,0 +1,91 @@
|
|
|
+using IdentityModel.Client;
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
+using Sharing.Enterprise.Dtos;
|
|
|
+using Sharing.Enterprise.Extensions;
|
|
|
+using System.Net.Http.Headers;
|
|
|
+using XF.Domain.Cache;
|
|
|
+using XF.Domain.Exceptions;
|
|
|
+
|
|
|
+namespace Sharing.Enterprise
|
|
|
+{
|
|
|
+ public abstract class EnterpriseClient //: ISingletonDependency, ISelfDependency
|
|
|
+ {
|
|
|
+ private static readonly string KeyToken = "EnterpriseKeyToken";
|
|
|
+ private readonly IServiceScopeFactory _scopeFactory;
|
|
|
+
|
|
|
+ public EnterpriseClient(IServiceScopeFactory scopeFactory)
|
|
|
+ {
|
|
|
+ _scopeFactory = scopeFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<TResponse?> RequestAsync<TRequest, TResponse>(
|
|
|
+ TRequest request,
|
|
|
+ string baseAddress,
|
|
|
+ // ConfigurationEnterprise configEnterprise,
|
|
|
+ CancellationToken cancellationToken)
|
|
|
+ where TRequest : ISharingRequest, new()
|
|
|
+ {
|
|
|
+ using var scope = _scopeFactory.CreateScope();
|
|
|
+ var provider = scope.ServiceProvider;
|
|
|
+
|
|
|
+ var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
|
|
|
+ var cacheToken = provider.GetRequiredService<ITypedCache<TokenInfo>>();
|
|
|
+
|
|
|
+ var token = cacheToken.GetOrSet(KeyToken,
|
|
|
+ d => GetTokenAsync(cancellationToken).GetAwaiter().GetResult(),
|
|
|
+ TimeSpan.FromMinutes(58));
|
|
|
+
|
|
|
+ // request.BuildClientInfo(configProvince.ClientId, configProvince.ClientSecret);
|
|
|
+
|
|
|
+ return await httpInvoker.RequestAsync<TRequest, TResponse>(request,
|
|
|
+ d => d.SetHttpClient(baseAddress, token?.AccessToken ?? string.Empty),
|
|
|
+ cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<TokenInfo> GetTokenAsync(CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ using var scope = _scopeFactory.CreateScope();
|
|
|
+ var provider = scope.ServiceProvider;
|
|
|
+ var channelconfigManager = provider.GetRequiredService<IChannelConfigurationManager>();
|
|
|
+ var httpInvoker = provider.GetRequiredService<IHttpInvoker>();
|
|
|
+
|
|
|
+ var configEnterprise = channelconfigManager.GetConfigurationEnterprise();
|
|
|
+ var baseAddress = configEnterprise.AddressUrl;
|
|
|
+ if (!baseAddress.EndsWith('/'))
|
|
|
+ baseAddress += "/";
|
|
|
+
|
|
|
+ string authorization = configEnterprise.ClientId + ":" + configEnterprise.ClientSecret;
|
|
|
+ authorization = Sharing.Enterprise.Extensions.Encoder.Base64Code(authorization);
|
|
|
+ authorization = authorization.Replace("@", "=");
|
|
|
+
|
|
|
+ var request = new ClientCredentialsTokenRequest
|
|
|
+ {
|
|
|
+ Address = $"{baseAddress}blade-auth/oauth/getAccessToken",
|
|
|
+ Headers =
|
|
|
+ {
|
|
|
+ Accept =
|
|
|
+ {
|
|
|
+ MediaTypeWithQualityHeaderValue.Parse("application/json")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ request.Headers.Add("Authorization", "Basic " + authorization);
|
|
|
+ request.Headers.Add("Tenant-Id", configEnterprise.TenantId);
|
|
|
+ var tokenResponse = await httpInvoker.GetTokenAsync(request, cancellationToken);
|
|
|
+ if (tokenResponse is null)
|
|
|
+ throw new UserFriendlyException("获取token请求失败");
|
|
|
+ if (tokenResponse.IsError)
|
|
|
+ throw new UserFriendlyException("获取token请求失败");
|
|
|
+
|
|
|
+ var tokenInfo = System.Text.Json.JsonSerializer.Deserialize<TokenRsp>(tokenResponse.Raw);
|
|
|
+ if (tokenInfo is null || !tokenInfo.Success || tokenInfo?.TokenInfo is null)
|
|
|
+ throw new UserFriendlyException("token解析失败");
|
|
|
+ return tokenInfo.TokenInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|