Skip to content

Commit bdc9d47

Browse files
committed
Add API key authentication for Elsa client
Introduced an `ApiKeyHttpMessageHandler` to use an API key as the authorization header for Elsa API client. This ensures secure interaction with the Elsa server. Also made updates to the `ElsaClientOptions` class to include the API key. An `AddElsaClient` method is added with the API key parameter in `DependencyInjectionExtensions` class for convenience.
1 parent 382abfd commit bdc9d47

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/clients/Elsa.Api.Client/Extensions/DependencyInjectionExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json.Serialization;
33
using Elsa.Api.Client.Contracts;
44
using Elsa.Api.Client.Converters;
5+
using Elsa.Api.Client.HttpMessageHandlers;
56
using Elsa.Api.Client.Options;
67
using Elsa.Api.Client.Resources.ActivityDescriptorOptions.Contracts;
78
using Elsa.Api.Client.Resources.ActivityDescriptors.Contracts;
@@ -31,6 +32,31 @@ namespace Elsa.Api.Client.Extensions;
3132
[PublicAPI]
3233
public static class DependencyInjectionExtensions
3334
{
35+
/// <summary>
36+
/// Adds the Elsa client to the service collection.
37+
/// </summary>
38+
/// <param name="services">The service collection.</param>
39+
/// <param name="baseAddress">The base address of the Elsa API.</param>
40+
/// <param name="apiKey">The API key to use for authentication.</param>
41+
/// <param name="configureOptions">An optional delegate that can be used to configure the client options.</param>
42+
/// <param name="configureBuilderOptions">An optional delegate that can be used to configure the client builder options.</param>
43+
public static IServiceCollection AddElsaClient(this IServiceCollection services, Uri baseAddress, string apiKey, Action<ElsaClientOptions>? configureOptions = default, Action<ElsaClientBuilderOptions>? configureBuilderOptions = default)
44+
{
45+
services.AddScoped<ApiKeyHttpMessageHandler>();
46+
return services.AddElsaClient(
47+
options =>
48+
{
49+
options.BaseAddress = baseAddress;
50+
options.ApiKey = apiKey;
51+
configureOptions?.Invoke(options);
52+
},
53+
configureBuilderOptions: options =>
54+
{
55+
options.ConfigureHttpClientBuilder = builder => builder.AddHttpMessageHandler<ApiKeyHttpMessageHandler>();
56+
configureBuilderOptions?.Invoke(options);
57+
});
58+
}
59+
3460
/// <summary>
3561
/// Adds the Elsa client to the service collection.
3662
/// </summary>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Net.Http.Headers;
2+
using Elsa.Api.Client.Options;
3+
using Microsoft.Extensions.Options;
4+
5+
namespace Elsa.Api.Client.HttpMessageHandlers;
6+
7+
/// <summary>
8+
/// An <see cref="HttpMessageHandler"/> that configures the outgoing HTTP request to use an API key as the authorization header.
9+
/// </summary>
10+
public class ApiKeyHttpMessageHandler : DelegatingHandler
11+
{
12+
private readonly ElsaClientOptions _options;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="ApiKeyHttpMessageHandler"/> class.
16+
/// </summary>
17+
public ApiKeyHttpMessageHandler(IOptions<ElsaClientOptions> options)
18+
{
19+
_options = options.Value;
20+
}
21+
22+
/// <inheritdoc />
23+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
24+
{
25+
var apiKey = _options.ApiKey;
26+
request.Headers.Authorization = new AuthenticationHeaderValue("ApiKey", apiKey);
27+
28+
return await base.SendAsync(request, cancellationToken);
29+
}
30+
}

src/clients/Elsa.Api.Client/Options/ElsaClientOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public class ElsaClientOptions
99
/// Gets or sets the base address of the Elsa server.
1010
/// </summary>
1111
public Uri BaseAddress { get; set; } = default!;
12-
12+
1313
/// <summary>
14-
/// Gets or sets the API key to use when authenticating with the Elsa server.
14+
/// Gets or sets the API key function to use when authenticating with the Elsa server.
1515
/// </summary>
16-
public string ApiKey { get; set; } = default!;
16+
public string? ApiKey { get; set; }
1717

1818
/// <summary>
1919
/// Gets or sets a delegate that can be used to configure the HTTP client.

0 commit comments

Comments
 (0)