-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14015 from abpframework/dapr-improvements
Dapr improvements & documentation
- Loading branch information
Showing
17 changed files
with
752 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprAppApiTokenValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Volo.Abp.Authorization; | ||
using Volo.Abp.Dapr; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace Volo.Abp.AspNetCore.Mvc.Dapr; | ||
|
||
public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDependency | ||
{ | ||
protected IHttpContextAccessor HttpContextAccessor { get; } | ||
protected HttpContext HttpContext => GetHttpContext(); | ||
|
||
public DaprAppApiTokenValidator(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
HttpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public virtual void CheckDaprAppApiToken() | ||
{ | ||
var expectedAppApiToken = GetConfiguredAppApiTokenOrNull(); | ||
if (expectedAppApiToken.IsNullOrWhiteSpace()) | ||
{ | ||
return; | ||
} | ||
|
||
var headerAppApiToken = GetDaprAppApiTokenOrNull(); | ||
if (headerAppApiToken.IsNullOrWhiteSpace()) | ||
{ | ||
throw new AbpAuthorizationException("Expected Dapr App API Token is not provided! Dapr should set the 'dapr-api-token' HTTP header."); | ||
} | ||
|
||
if (expectedAppApiToken != headerAppApiToken) | ||
{ | ||
throw new AbpAuthorizationException("The Dapr App API Token (provided in the 'dapr-api-token' HTTP header) doesn't match the expected value!"); | ||
} | ||
} | ||
|
||
public virtual bool IsValidDaprAppApiToken() | ||
{ | ||
var expectedAppApiToken = GetConfiguredAppApiTokenOrNull(); | ||
if (expectedAppApiToken.IsNullOrWhiteSpace()) | ||
{ | ||
return true; | ||
} | ||
|
||
var headerAppApiToken = GetDaprAppApiTokenOrNull(); | ||
return expectedAppApiToken == headerAppApiToken; | ||
} | ||
|
||
public virtual string? GetDaprAppApiTokenOrNull() | ||
{ | ||
string apiTokenHeader = HttpContext.Request.Headers["dapr-api-token"]; | ||
if (string.IsNullOrEmpty(apiTokenHeader) || apiTokenHeader.Length < 1) | ||
{ | ||
return null; | ||
} | ||
|
||
return apiTokenHeader; | ||
} | ||
|
||
protected virtual string? GetConfiguredAppApiTokenOrNull() | ||
{ | ||
return HttpContext | ||
.RequestServices | ||
.GetRequiredService<IDaprApiTokenProvider>() | ||
.GetAppApiToken(); | ||
} | ||
|
||
protected virtual HttpContext GetHttpContext() | ||
{ | ||
return HttpContextAccessor.HttpContext ?? throw new AbpException("HttpContext is not available!"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rc/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprHttpContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Volo.Abp.AspNetCore.Mvc.Dapr; | ||
|
||
public static class DaprHttpContextExtensions | ||
{ | ||
public static void ValidateDaprAppApiToken(this HttpContext httpContext) | ||
{ | ||
httpContext | ||
.RequestServices | ||
.GetRequiredService<IDaprAppApiTokenValidator>() | ||
.CheckDaprAppApiToken(); | ||
} | ||
|
||
public static bool IsValidDaprAppApiToken(this HttpContext httpContext) | ||
{ | ||
return httpContext | ||
.RequestServices | ||
.GetRequiredService<IDaprAppApiTokenValidator>() | ||
.IsValidDaprAppApiToken(); | ||
} | ||
|
||
public static string? GetDaprAppApiTokenOrNull(HttpContext httpContext) | ||
{ | ||
return httpContext | ||
.RequestServices | ||
.GetRequiredService<IDaprAppApiTokenValidator>() | ||
.GetDaprAppApiTokenOrNull(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...rc/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/IDaprAppApiTokenValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Volo.Abp.AspNetCore.Mvc.Dapr; | ||
|
||
public interface IDaprAppApiTokenValidator | ||
{ | ||
void CheckDaprAppApiToken(); | ||
bool IsValidDaprAppApiToken(); | ||
string? GetDaprAppApiTokenOrNull(); | ||
} |
63 changes: 44 additions & 19 deletions
63
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,73 @@ | ||
using System.Collections.Concurrent; | ||
using System.Text.Json; | ||
using System.Text.Json; | ||
using Dapr.Client; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Json.SystemTextJson; | ||
|
||
namespace Volo.Abp.Dapr; | ||
|
||
public class AbpDaprClientFactory : ITransientDependency | ||
public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency | ||
{ | ||
protected AbpDaprOptions Options { get; } | ||
protected AbpSystemTextJsonSerializerOptions SystemTextJsonSerializerOptions { get; } | ||
protected AbpDaprOptions DaprOptions { get; } | ||
protected JsonSerializerOptions JsonSerializerOptions { get; } | ||
protected IDaprApiTokenProvider DaprApiTokenProvider { get; } | ||
|
||
public AbpDaprClientFactory( | ||
IOptions<AbpDaprOptions> options, | ||
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions) | ||
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions, | ||
IDaprApiTokenProvider daprApiTokenProvider) | ||
{ | ||
Options = options.Value; | ||
SystemTextJsonSerializerOptions = systemTextJsonSerializerOptions.Value; | ||
DaprApiTokenProvider = daprApiTokenProvider; | ||
DaprOptions = options.Value; | ||
JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); | ||
} | ||
|
||
public virtual async Task<DaprClient> CreateAsync() | ||
public virtual DaprClient Create(Action<DaprClientBuilder>? builderAction = null) | ||
{ | ||
var builder = new DaprClientBuilder() | ||
.UseJsonSerializationOptions(await CreateJsonSerializerOptions()); | ||
.UseJsonSerializationOptions(JsonSerializerOptions); | ||
|
||
if (!Options.HttpEndpoint.IsNullOrWhiteSpace()) | ||
if (!DaprOptions.HttpEndpoint.IsNullOrWhiteSpace()) | ||
{ | ||
builder.UseHttpEndpoint(Options.HttpEndpoint); | ||
builder.UseHttpEndpoint(DaprOptions.HttpEndpoint); | ||
} | ||
|
||
if (!Options.GrpcEndpoint.IsNullOrWhiteSpace()) | ||
if (!DaprOptions.GrpcEndpoint.IsNullOrWhiteSpace()) | ||
{ | ||
builder.UseGrpcEndpoint(Options.GrpcEndpoint); | ||
builder.UseGrpcEndpoint(DaprOptions.GrpcEndpoint); | ||
} | ||
|
||
var apiToken = DaprApiTokenProvider.GetDaprApiToken(); | ||
if (!apiToken.IsNullOrWhiteSpace()) | ||
{ | ||
builder.UseDaprApiToken(apiToken); | ||
} | ||
|
||
builderAction?.Invoke(builder); | ||
|
||
return builder.Build(); | ||
} | ||
|
||
private readonly static ConcurrentDictionary<string, JsonSerializerOptions> JsonSerializerOptionsCache = new ConcurrentDictionary<string, JsonSerializerOptions>(); | ||
public virtual HttpClient CreateHttpClient( | ||
string? appId = null, | ||
string? daprEndpoint = null, | ||
string? daprApiToken = null) | ||
{ | ||
if(daprEndpoint.IsNullOrWhiteSpace() && | ||
!DaprOptions.HttpEndpoint.IsNullOrWhiteSpace()) | ||
{ | ||
daprEndpoint = DaprOptions.HttpEndpoint; | ||
} | ||
|
||
protected virtual Task<JsonSerializerOptions> CreateJsonSerializerOptions() | ||
return DaprClient.CreateInvokeHttpClient( | ||
appId, | ||
daprEndpoint, | ||
daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken() | ||
); | ||
} | ||
|
||
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions) | ||
{ | ||
return Task.FromResult(JsonSerializerOptionsCache.GetOrAdd(nameof(AbpDaprClientFactory), | ||
_ => new JsonSerializerOptions(SystemTextJsonSerializerOptions.JsonSerializerOptions))); | ||
return new JsonSerializerOptions(systemTextJsonSerializerOptions.JsonSerializerOptions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/DaprApiTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace Volo.Abp.Dapr; | ||
|
||
public class DaprApiTokenProvider : IDaprApiTokenProvider, ISingletonDependency | ||
{ | ||
protected AbpDaprOptions Options { get; } | ||
|
||
public DaprApiTokenProvider(IOptions<AbpDaprOptions> options) | ||
{ | ||
Options = options.Value; | ||
} | ||
|
||
public virtual string? GetDaprApiToken() | ||
{ | ||
return Options.DaprApiToken; | ||
} | ||
|
||
public virtual string? GetAppApiToken() | ||
{ | ||
return Options.AppApiToken; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Dapr.Client; | ||
|
||
namespace Volo.Abp.Dapr; | ||
|
||
public interface IAbpDaprClientFactory | ||
{ | ||
DaprClient Create(Action<DaprClientBuilder>? builderAction = null); | ||
|
||
HttpClient CreateHttpClient( | ||
string? appId = null, | ||
string? daprEndpoint = null, | ||
string? daprApiToken = null | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprApiTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Volo.Abp.Dapr; | ||
|
||
public interface IDaprApiTokenProvider | ||
{ | ||
string? GetDaprApiToken(); | ||
|
||
string? GetAppApiToken(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.