Skip to content
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
Expand Up @@ -15,7 +15,6 @@ public static IServiceCollection AddAlertClient(this IServiceCollection services
{
builder.Configure = opt => opt.BaseAddress = new Uri(alertServiceBaseAddress);
})
.AddMiddleware<EnvironmentCallerMiddleware>()
.UseAuthentication();
});
}
Expand All @@ -31,7 +30,6 @@ public static IServiceCollection AddAlertClient(this IServiceCollection services
{
builder.BaseAddress = alertServiceBaseAddressFunc.Invoke();
})
.AddMiddleware<EnvironmentCallerMiddleware>()
.UseAuthentication();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ public class AuthenticationService : IAuthenticationService
{
private readonly TokenProvider _tokenProvider;
private readonly JwtTokenValidator? _jwtTokenValidator;
private readonly IMultiEnvironmentContext _multiEnvironmentContext;

public AuthenticationService(TokenProvider tokenProvider,
JwtTokenValidator? jwtTokenValidator)
JwtTokenValidator? jwtTokenValidator,
IMultiEnvironmentContext multiEnvironmentContext)
{
_tokenProvider = tokenProvider;
_jwtTokenValidator = jwtTokenValidator;
_multiEnvironmentContext = multiEnvironmentContext;
}

public async Task ExecuteAsync(HttpRequestMessage requestMessage)
Expand All @@ -24,5 +27,8 @@ public async Task ExecuteAsync(HttpRequestMessage requestMessage)

if (!_tokenProvider.AccessToken.IsNullOrWhiteSpace())
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _tokenProvider.AccessToken);

if (!string.IsNullOrEmpty(_multiEnvironmentContext.CurrentEnvironment) && !requestMessage.Headers.Any(x=>x.Key == IsolationConsts.ENVIRONMENT))
requestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, _multiEnvironmentContext.CurrentEnvironment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\Service\Masa.BuildingBlocks.Service.Caller\Masa.BuildingBlocks.Service.Caller.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\StackSdks\Masa.BuildingBlocks.StackSdks.Isolation\Masa.BuildingBlocks.StackSdks.Isolation.csproj" />
<ProjectReference Include="..\..\Isolation\Masa.Contrib.Isolation.MultiEnvironment\Masa.Contrib.Isolation.MultiEnvironment.csproj" />
<ProjectReference Include="..\..\Service\Caller\Masa.Contrib.Service.Caller.HttpClient\Masa.Contrib.Service.Caller.HttpClient.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Masa.BuildingBlocks.Isolation;
using Masa.Contrib.Isolation.MultiEnvironment;

namespace Microsoft.Extensions.DependencyInjection;

public static class MasaCallerClientBuilderExtensions
Expand All @@ -17,10 +20,13 @@ public static IMasaCallerClientBuilder UseAuthentication(
masaCallerClientBuilder.Services.AddHttpContextAccessor();
masaCallerClientBuilder.Services.TryAddScoped<ITokenGenerater, DefaultTokenGenerater>();
masaCallerClientBuilder.Services.TryAddScoped(s => s.GetRequiredService<ITokenGenerater>().Generater());
masaCallerClientBuilder.Services.TryAddScoped<MultiEnvironmentContext>();
masaCallerClientBuilder.Services.TryAddScoped(typeof(IMultiEnvironmentContext), s => s.GetRequiredService<MultiEnvironmentContext>());
masaCallerClientBuilder.UseAuthentication(serviceProvider =>
new AuthenticationService(
serviceProvider.GetRequiredService<TokenProvider>(),
null
null,
serviceProvider.GetRequiredService<IMultiEnvironmentContext>()
));
return masaCallerClientBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ protected override void UseHttpClientPost(MasaHttpClientBuilder masaHttpClientBu
{
masaHttpClientBuilder.UseAuthentication(serviceProvider => new AuthenticationService(
serviceProvider.GetRequiredService<TokenProvider>(),
serviceProvider.GetRequiredService<JwtTokenValidator>()
serviceProvider.GetRequiredService<JwtTokenValidator>(),
serviceProvider.GetRequiredService<IMultiEnvironmentContext>()
));
base.UseHttpClientPost(masaHttpClientBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
global using IdentityModel;
global using IdentityModel.Client;
global using Masa.BuildingBlocks.Service.Caller;
global using Masa.BuildingBlocks.StackSdks.Isolation;
global using Masa.BuildingBlocks.Isolation;
global using Masa.Contrib.Service.Caller.HttpClient;
global using Masa.Contrib.StackSdks.Caller;
global using Microsoft.AspNetCore.Http;
Expand All @@ -18,4 +20,3 @@
global using System.Reflection;
global using System.Security.Claims;
global using System.Security.Cryptography;

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public EnvironmentCallerMiddleware(ILoggerFactory? loggerFactory = null)

public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDelegate next, CancellationToken cancellationToken = default)
{

try
{
if (masaHttpContext.RequestMessage.Content != null && masaHttpContext.RequestMessage.Content.Headers.ContentType?.MediaType == "application/json")
Expand All @@ -24,7 +23,7 @@ public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDele
{
PropertyNameCaseInsensitive = true
});
if (!string.IsNullOrEmpty(obj?.Environment))
if (!string.IsNullOrEmpty(obj?.Environment) && !masaHttpContext.RequestMessage.Headers.Any(x => x.Key == IsolationConsts.ENVIRONMENT))
{
masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, obj?.Environment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, s
{
builder.Configure = opt => opt.BaseAddress = new Uri(mcServiceBaseAddress);
})
.AddMiddleware<EnvironmentCallerMiddleware>()
.UseAuthentication();
});
}
Expand All @@ -33,7 +32,6 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, F
{
builder.BaseAddress = mcServiceBaseAddressFunc.Invoke();
})
.AddMiddleware<EnvironmentCallerMiddleware>()
.UseAuthentication();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static IServiceCollection AddSchedulerClient(this IServiceCollection serv
{
builder.Configure = opt => opt.BaseAddress = new Uri(schedulerServiceBaseAddress);
})
.AddMiddleware<EnvironmentCallerMiddleware>()
.UseAuthentication();
});
}
Expand Down