-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
382abfd
commit bdc9d47
Showing
3 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
src/clients/Elsa.Api.Client/HttpMessageHandlers/ApiKeyHttpMessageHandler.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,30 @@ | ||
using System.Net.Http.Headers; | ||
using Elsa.Api.Client.Options; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Elsa.Api.Client.HttpMessageHandlers; | ||
|
||
/// <summary> | ||
/// An <see cref="HttpMessageHandler"/> that configures the outgoing HTTP request to use an API key as the authorization header. | ||
/// </summary> | ||
public class ApiKeyHttpMessageHandler : DelegatingHandler | ||
{ | ||
private readonly ElsaClientOptions _options; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ApiKeyHttpMessageHandler"/> class. | ||
/// </summary> | ||
public ApiKeyHttpMessageHandler(IOptions<ElsaClientOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var apiKey = _options.ApiKey; | ||
request.Headers.Authorization = new AuthenticationHeaderValue("ApiKey", apiKey); | ||
|
||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
} |
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